Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
KPI Reporting Generator
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Coscine
backend
scripts
KPI Reporting Generator
Merge requests
!6
New: KPI Reporting for Resources
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
New: KPI Reporting for Resources
Issue/2183-kpiGeneratorResource
into
dev
Overview
0
Commits
10
Pipelines
11
Changes
7
Merged
Hanna Führ
requested to merge
Issue/2183-kpiGeneratorResource
into
dev
2 years ago
Overview
0
Commits
10
Pipelines
11
Changes
7
Expand
coscine/issues#2183
Edited
2 years ago
by
Petar Hristov
0
0
Merge request reports
Compare
dev
version 9
922acf02
2 years ago
version 8
209d0dc8
2 years ago
version 7
8b7ec5d6
2 years ago
version 6
a2e671e3
2 years ago
version 5
2fc9acc6
2 years ago
version 4
8ef4d92b
2 years ago
version 3
eba13959
2 years ago
version 2
b986f879
2 years ago
version 1
ce370529
2 years ago
dev (base)
and
latest version
latest version
8cd51f4a
10 commits,
2 years ago
version 9
922acf02
9 commits,
2 years ago
version 8
209d0dc8
8 commits,
2 years ago
version 7
8b7ec5d6
7 commits,
2 years ago
version 6
a2e671e3
6 commits,
2 years ago
version 5
2fc9acc6
5 commits,
2 years ago
version 4
8ef4d92b
4 commits,
2 years ago
version 3
eba13959
3 commits,
2 years ago
version 2
b986f879
2 commits,
2 years ago
version 1
ce370529
1 commit,
2 years ago
7 files
+
197
−
55
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
7
Search (e.g. *.vue) (Ctrl+P)
src/KPI Generator/Reportings/Resource/ResourceReporting.cs
+
140
−
8
Options
using
KPIGenerator.Utils
;
using
Coscine.Database.Models
;
using
Coscine.Database.ReturnObjects
;
using
Coscine.ResourceTypes
;
using
Coscine.ResourceTypes.Base
;
using
KPIGenerator.Utils
;
using
Newtonsoft.Json
;
using
static
KPIGenerator
.
Utils
.
CommandLineOptions
;
namespace
KPIGenerator.Reportings.Resource
;
public
class
ResourceReporting
:
Reporting
<
ResourceReportingOptions
>
{
private
readonly
ResourceModel
_resourceModel
;
private
readonly
ProjectModel
_projectModel
;
private
readonly
ProjectResourceModel
_projectResourceModel
;
public
ResourceReporting
(
ResourceReportingOptions
options
)
:
base
(
options
)
{
ReportingFileName
=
"resources.json"
;
_resourceModel
=
new
ResourceModel
();
_projectModel
=
new
ProjectModel
();
_projectResourceModel
=
new
ProjectResourceModel
();
}
public
override
IEnumerable
<
ReportingFileObject
>
GenerateReporting
()
{
/*
* 1. Collect the reporting for the whole database -- General/{ReportingReportingFileName}
* 2. Append to the list the same information per organization as folders -- Organizations/{OrgRorId}/{ReportingReportingFileName}
* --> See envisioned folder structure.
*/
throw
new
NotImplementedException
();
var
resources
=
_resourceModel
.
GetAllWhere
(
r
=>
r
.
Deleted
.
Equals
(
true
)
||
r
.
Deleted
.
Equals
(
false
));
var
reportingFiles
=
new
List
<
ReportingFileObject
>();
var
returnObjects
=
Generate
(
resources
);
// General File
reportingFiles
.
Add
(
new
ReportingFileObject
{
Path
=
GetReportingPathGeneral
(
ReportingFileName
),
Content
=
ConvertStringContentsToStream
(
JsonConvert
.
SerializeObject
(
returnObjects
,
Formatting
.
Indented
))
});
// Per Organization
reportingFiles
.
AddRange
(
GeneratePerOrganization
(
returnObjects
));
return
reportingFiles
;
}
private
List
<
ReturnObject
>
Generate
(
IEnumerable
<
Coscine
.
Database
.
DataModel
.
Resource
>
resources
)
{
var
returnObjects
=
new
List
<
ReturnObject
>();
foreach
(
var
resource
in
resources
)
{
var
resourceReturnObject
=
_resourceModel
.
CreateReturnObjectFromDatabaseObject
(
resource
);
var
resourceReportEntry
=
new
ReturnObject
{
Id
=
resourceReturnObject
.
Id
,
ResourceType
=
resourceReturnObject
.
Type
.
DisplayName
,
DateCreated
=
resourceReturnObject
.
DateCreated
,
Archived
=
resourceReturnObject
.
Archived
,
Deleted
=
resourceReturnObject
.
Deleted
,
MetadataVisibilityId
=
resourceReturnObject
.
Visibility
.
Id
,
RelatedProjectId
=
GetRelatedProject
(
resource
.
Id
),
Organizations
=
GetOrganizations
(
resourceReturnObject
.
Id
),
Disciplines
=
resourceReturnObject
.
Disciplines
.
ToList
(),
License
=
resourceReturnObject
.
License
is
not
null
?
resourceReturnObject
.
License
.
DisplayName
:
null
,
ApplicationProfile
=
resourceReturnObject
.
ApplicationProfile
,
ResourceQuota
=
GetResourceQuota
(
resource
)
};
returnObjects
.
Add
(
resourceReportEntry
);
}
return
returnObjects
;
}
private
IEnumerable
<
ReportingFileObject
>
GeneratePerOrganization
(
List
<
ReturnObject
>
returnObjects
)
{
var
reportingFilesPerOrganization
=
new
List
<
ReportingFileObject
>();
var
organizationsFromResources
=
returnObjects
.
SelectMany
(
ro
=>
ro
.
Organizations
).
DistinctBy
(
o
=>
o
.
RorUrl
);
foreach
(
var
entry
in
organizationsFromResources
)
{
var
organization
=
Organizations
.
Find
(
o
=>
o
.
Equals
(
entry
));
if
(
organization
is
null
)
{
organization
=
_otherOrganization
;
Console
.
WriteLine
(
$"WARNING!: Organization \"
{
entry
.
RorUrl
}
\" could not be correctly identified. Will use \"
{
_otherOrganization
.
RorUrl
}
\"."
);
}
var
returnObjectsForOrganization
=
returnObjects
.
Where
(
ro
=>
ro
.
Organizations
.
Select
(
o
=>
o
.
RorUrl
).
Any
(
e
=>
e
.
Equals
(
entry
.
RorUrl
)));
reportingFilesPerOrganization
.
Add
(
new
ReportingFileObject
{
Path
=
GetReportingPathOrganization
(
organization
.
RorUrl
.
Replace
(
"https://ror.org/"
,
""
).
ToLower
(),
ReportingFileName
),
Content
=
ConvertStringContentsToStream
(
JsonConvert
.
SerializeObject
(
returnObjectsForOrganization
,
Formatting
.
Indented
))
});
}
return
reportingFilesPerOrganization
;
}
private
Guid
?
GetRelatedProject
(
Guid
resourceId
)
{
try
{
return
_projectResourceModel
.
GetProjectForResource
(
resourceId
);
}
catch
{
Console
.
WriteLine
(
$"There is no project related to resource with ID \"
{
resourceId
}
\"."
);
return
null
;
}
}
private
static
ResourceQuotaReturnObject
?
GetResourceQuota
(
Coscine
.
Database
.
DataModel
.
Resource
resource
)
{
BaseResourceType
?
resourceTypeDefinition
;
try
{
resourceTypeDefinition
=
ResourceTypeFactory
.
Instance
.
GetResourceType
(
resource
);
}
catch
{
Console
.
WriteLine
(
$"No resource type definition found for resource with ID \"
{
resource
.
Id
}
\"."
);
resourceTypeDefinition
=
null
;
}
if
(
resourceTypeDefinition
is
not
null
&&
resourceTypeDefinition
.
GetResourceTypeInformation
().
Result
.
IsQuotaAdjustable
)
{
return
Helpers
.
CreateResourceQuotaReturnObject
(
resource
,
resourceTypeDefinition
);
}
else
{
return
null
;
}
}
private
List
<
Organization
>
GetOrganizations
(
Guid
resourceId
)
{
var
result
=
new
List
<
Organization
>();
Guid
?
relatedProjectId
;
try
{
relatedProjectId
=
_projectResourceModel
.
GetProjectForResource
(
resourceId
);
}
catch
{
relatedProjectId
=
null
;
}
if
(
relatedProjectId
is
not
null
)
{
var
parentProject
=
_projectModel
.
GetByIdIncludingDeleted
(
relatedProjectId
.
Value
);
var
organizations
=
_projectModel
.
CreateReturnObjectFromDatabaseObject
(
parentProject
).
Organizations
.
ToList
();
foreach
(
var
entry
in
organizations
)
{
result
.
Add
(
FetchOrganizationByRor
(
entry
.
Url
));
}
}
return
result
;
}
}
}
\ No newline at end of file
Loading