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
!7
Update: KPI generator for Project
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Update: KPI generator for Project
Issue/2184-kpiGeneratorProject
into
dev
Overview
0
Commits
7
Pipelines
8
Changes
3
Merged
Sirieam Marie Hunke
requested to merge
Issue/2184-kpiGeneratorProject
into
dev
2 years ago
Overview
0
Commits
7
Pipelines
8
Changes
3
Expand
coscine/issues#2184
0
0
Merge request reports
Compare
dev
version 6
8b4386a8
2 years ago
version 5
0fc01c1f
2 years ago
version 4
5949488d
2 years ago
version 3
7e6f14b3
2 years ago
version 2
1ea1a12f
2 years ago
version 1
256b0db1
2 years ago
dev (base)
and
latest version
latest version
3254ea48
7 commits,
2 years ago
version 6
8b4386a8
6 commits,
2 years ago
version 5
0fc01c1f
5 commits,
2 years ago
version 4
5949488d
4 commits,
2 years ago
version 3
7e6f14b3
3 commits,
2 years ago
version 2
1ea1a12f
2 commits,
2 years ago
version 1
256b0db1
1 commit,
2 years ago
3 files
+
227
−
11
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
3
Search (e.g. *.vue) (Ctrl+P)
src/KPI Generator/Reportings/Project/ProjectReporting.cs
+
180
−
8
Options
using
KPIGenerator.Utils
;
using
Coscine.Database.DataModel
;
using
Coscine.Database.Models
;
using
Coscine.Database.ReturnObjects
;
using
Coscine.ResourceTypes
;
using
KPIGenerator.Utils
;
using
Newtonsoft.Json
;
using
static
KPIGenerator
.
Utils
.
CommandLineOptions
;
namespace
KPIGenerator.Reportings.Project
;
public
class
ProjectReporting
:
Reporting
<
ProjectReportingOptions
>
{
private
readonly
ProjectModel
_projectModel
;
private
readonly
ResourceModel
_resourceModel
;
private
readonly
ProjectRoleModel
_projectRoleModel
;
private
readonly
ProjectQuotaModel
_projectQuotaModel
;
private
readonly
ResourceTypeModel
_resourceTypeModel
;
public
ProjectReporting
(
ProjectReportingOptions
options
)
:
base
(
options
)
{
ReportingFileName
=
"projects.json"
;
_projectModel
=
new
ProjectModel
();
_projectRoleModel
=
new
ProjectRoleModel
();
_projectQuotaModel
=
new
ProjectQuotaModel
();
_resourceModel
=
new
ResourceModel
();
_resourceTypeModel
=
new
ResourceTypeModel
();
}
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
projects
=
_projectModel
.
GetAllWhere
(
r
=>
r
.
Deleted
.
Equals
(
true
)
||
r
.
Deleted
.
Equals
(
false
));
var
reportingFiles
=
new
List
<
ReportingFileObject
>();
var
returnObjects
=
Generate
(
projects
);
//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
.
Project
>
projects
)
{
var
returnObjects
=
new
List
<
ReturnObject
>();
foreach
(
var
project
in
projects
)
{
var
projectReturnObject
=
_projectModel
.
CreateReturnObjectFromDatabaseObject
(
project
);
var
projectReportEntry
=
new
ReturnObject
{
Id
=
projectReturnObject
.
Id
,
DateCreated
=
projectReturnObject
.
DateCreated
,
Organizations
=
GetOrganizations
(
projectReturnObject
),
Disciplines
=
projectReturnObject
.
Disciplines
.
ToList
(),
Deleted
=
projectReturnObject
.
Deleted
,
ProjectVisibilityId
=
projectReturnObject
.
Visibility
.
Id
,
GrantId
=
projectReturnObject
.
GrantId
,
Users
=
_projectRoleModel
.
GetAllWhere
(
x
=>
x
.
ProjectId
==
projectReturnObject
.
Id
).
Count
(),
ResourceTypeQuota
=
GetResourceTypeQuota
(
projectReturnObject
.
Id
)
};
returnObjects
.
Add
(
projectReportEntry
);
}
return
returnObjects
;
}
private
IEnumerable
<
ReportingFileObject
>
GeneratePerOrganization
(
List
<
ReturnObject
>
returnObjects
)
{
var
reportingFilesPerOrganization
=
new
List
<
ReportingFileObject
>();
var
organizationsFromProjects
=
returnObjects
.
SelectMany
(
ro
=>
ro
.
Organizations
).
DistinctBy
(
o
=>
o
.
RorUrl
);
foreach
(
var
entry
in
organizationsFromProjects
)
{
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
List
<
ResourceTypeQuotaReturnObject
>
GetResourceTypeQuota
(
Guid
projectId
)
{
var
result
=
new
List
<
ResourceTypeQuotaReturnObject
>();
var
types
=
ResourceTypeFactory
.
Instance
.
GetResourceTypes
();
var
resourceTypes
=
_resourceTypeModel
.
GetAllWhere
(
x
=>
types
.
Any
(
t
=>
x
.
SpecificType
.
Equals
(
t
)));
var
quotas
=
_projectQuotaModel
.
GetAllWhere
(
x
=>
x
.
ProjectId
==
projectId
);
var
resourceQuotas
=
quotas
.
Where
(
x
=>
resourceTypes
.
Any
(
e
=>
x
.
ResourceTypeId
.
Equals
(
e
.
Id
)));
result
.
AddRange
(
resourceQuotas
.
Select
(
x
=>
CreateQuotaReturnObject
(
x
,
projectId
)).
ToList
());
return
result
;
}
private
ResourceTypeQuotaReturnObject
CreateQuotaReturnObject
(
ProjectQuota
projectQuota
,
Guid
projectId
)
{
return
new
ResourceTypeQuotaReturnObject
{
ResourceType
=
_resourceTypeModel
.
GetById
(
projectQuota
.
ResourceTypeId
).
SpecificType
,
Allocated
=
new
QuotaDimObject
()
{
Value
=
projectQuota
.
Quota
,
Unit
=
QuotaUnit
.
GibiBYTE
,
},
Maximum
=
new
QuotaDimObject
()
{
Value
=
projectQuota
.
MaxQuota
,
Unit
=
QuotaUnit
.
GibiBYTE
,
},
TotalReserved
=
new
QuotaDimObject
()
{
Value
=
CalculateAllocatedForAll
(
_resourceTypeModel
.
GetById
(
projectQuota
.
ResourceTypeId
),
projectId
),
Unit
=
QuotaUnit
.
GibiBYTE
,
},
TotalUsed
=
new
QuotaDimObject
()
{
Value
=
CalculateUsedForAll
(
_resourceTypeModel
.
GetById
(
projectQuota
.
ResourceTypeId
),
projectId
),
Unit
=
QuotaUnit
.
BYTE
,
},
};
}
private
int
CalculateUsedForAll
(
ResourceType
resourceType
,
Guid
projectId
)
{
var
resources
=
_resourceModel
.
GetAllWhere
((
resource
)
=>
(
from
projectResource
in
resource
.
ProjectResources
where
projectResource
.
ProjectId
==
projectId
select
projectResource
).
Any
()
&&
resource
.
TypeId
==
resourceType
.
Id
);
var
used
=
resources
.
Sum
(
resource
=>
{
// Linked has no quota.
var
rt
=
ResourceTypeFactory
.
Instance
.
GetResourceType
(
resource
);
if
(
rt
.
GetResourceTypeInformation
().
Result
.
IsQuotaAvailable
)
{
return
rt
.
GetResourceQuotaUsed
(
resource
.
Id
.
ToString
(),
_resourceModel
.
GetResourceTypeOptions
(
resource
.
Id
)).
Result
;
}
else
{
return
0
;
}
}
);
return
(
int
)
used
;
}
private
int
CalculateAllocatedForAll
(
ResourceType
resourceType
,
Guid
projectId
)
{
var
resources
=
_resourceModel
.
GetAllWhere
((
resource
)
=>
(
from
projectResource
in
resource
.
ProjectResources
where
projectResource
.
ProjectId
==
projectId
select
projectResource
).
Any
()
&&
resource
.
TypeId
==
resourceType
.
Id
);
var
allocated
=
resources
.
Sum
(
resource
=>
{
// Linked has no quota.
var
rt
=
ResourceTypeFactory
.
Instance
.
GetResourceType
(
resource
);
if
(
rt
.
GetResourceTypeInformation
().
Result
.
IsQuotaAvailable
)
{
return
rt
.
GetResourceQuotaAvailable
(
resource
.
Id
.
ToString
(),
_resourceModel
.
GetResourceTypeOptions
(
resource
.
Id
)).
Result
;
}
else
{
return
0
;
}
});
return
(
int
)
allocated
;
}
private
List
<
Organization
>
GetOrganizations
(
ProjectObject
project
)
{
var
result
=
new
List
<
Organization
>();
foreach
(
var
entry
in
project
.
Organizations
)
{
result
.
Add
(
FetchOrganizationByRor
(
entry
.
Url
));
}
return
result
;
}
}
}
\ No newline at end of file
Loading