Skip to content
Snippets Groups Projects
Commit 256b0db1 authored by Sirieam Marie Hunke's avatar Sirieam Marie Hunke
Browse files

WIP

parent c12c7d25
No related branches found
No related tags found
1 merge request!7Update: KPI generator for Project
Pipeline #818324 failed
......@@ -20,8 +20,9 @@
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Coscine.ApiCommons" Version="2.*-*" />
<PackageReference Include="Coscine.Database" Version="2.*-*" />
<PackageReference Include="Coscine.Database" Version="2.15.0-issue-2183-kpige0001" />
<PackageReference Include="Coscine.Metadata" Version="2.*-*" />
<PackageReference Include="Coscine.ResourceTypes" Version="1.*-*" />
<PackageReference Include="GitLabApiClient" Version="1.8.1-beta.5" />
</ItemGroup>
</Project>
......@@ -24,12 +24,20 @@ public abstract class Reporting<O> where O : class
private static string InstanceName { get; set; } = null!;
public virtual string ReportingFileName { get; init; } = null!;
public readonly Organization _otherOrganization = new()
{
Name = "Other",
RorUrl = "https://ror.org/_other",
};
/// <summary>
/// Reporting Database GitLab Project URL
/// </summary>
/// <remarks>https://git.rwth-aachen.de/coscine/reporting/reporting-database</remarks>
private static readonly int ReportingDatabaseProjectId = 75304;
public Reporting(O options)
{
InstanceName = this.GetType().Name;
......@@ -38,7 +46,7 @@ public abstract class Reporting<O> where O : class
RdfStoreConnector = new RdfStoreConnector(Configuration.GetStringAndWait("coscine/local/virtuoso/additional/url"));
QueryEndpoint = new SparqlRemoteEndpoint(new Uri(Configuration.GetStringAndWait("coscine/local/virtuoso/additional/url")));
GitLabClient = new GitLabClient(HostUrl, Configuration.GetStringAndWait("coscine/global/gitlabtoken"));
Organizations = FetchOrganizations();
Organizations = new List<Organization>() { _otherOrganization };
}
public abstract IEnumerable<ReportingFileObject> GenerateReporting();
......@@ -132,49 +140,42 @@ public abstract class Reporting<O> where O : class
}
}
private List<Organization> FetchOrganizations()
public Organization FetchOrganizationByRor(string rorUrl)
{
var organizations = new List<Organization>();
var organizationsToFind = JsonConvert.DeserializeObject<IEnumerable<Uri>>(
Configuration.GetStringAndWait("coscine/local/organizations/list",
"['https://ror.org/', 'https://ror.org/04xfq0f34']")
);
var resultSet = new List<Triple>();
foreach (var orgGraph in organizationsToFind)
{
resultSet.AddRange(RdfStoreConnector.GetTriples(orgGraph, null));
}
var organizationTriples = resultSet.Where(r => !r.Subject.ToString().Contains('#')).Distinct().ToList();
foreach (var triple in organizationTriples)
var result = new Organization();
var organizationFound = Organizations.Find(o => o.RorUrl.Equals(rorUrl));
if (organizationFound is not null)
{
organizations.Add(new Organization
result = new Organization
{
Name = triple.Object.ToString(),
Ror = triple.Subject.ToString(),
});
Name = organizationFound.Name,
RorUrl = organizationFound.RorUrl,
};
}
// Organization "Other"
var organizationOther = organizations.Find(o => o.Name.Equals("Other"));
var organizationOtherRor = "https://ror.org/_other";
if (organizationOther is null)
else
{
organizations.Add(new Organization
var organizationTriples = RdfStoreConnector.GetLabelForSubject(new Uri(Uri.UnescapeDataString(rorUrl))).ToList();
if (organizationTriples.Any())
{
Name = "Other",
Ror = organizationOtherRor
});
result = new Organization
{
// Only one entry possible per organization, take 0th element
Name = organizationTriples[0].Object.ToString(),
RorUrl = organizationTriples[0].Subject.ToString(),
};
Organizations.Add(result); // Cache the fetched organization
}
else
{
var index = organizations.IndexOf(organizationOther);
organizations[index].Ror = organizationOtherRor;
result = _otherOrganization;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"WARNING!: Organization with ROR \"{rorUrl}\" could not be correctly identified. Will use \"{result.RorUrl}\" and \"{result.Name}\".");
Console.ResetColor();
}
return organizations;
}
return result;
}
public static string GetReportingPathGeneral(string fileName)
{
return string.Format("General/{0}", fileName);
......
using KPIGenerator.Utils;
using Coscine.Database.Models;
using Coscine.Database.ReturnObjects;
using Newtonsoft.Json;
using KPIGenerator.Utils;
using static KPIGenerator.Utils.CommandLineOptions;
using Coscine.Database.DataModel;
namespace KPIGenerator.Reportings.Project;
public class ProjectReporting : Reporting<ProjectReportingOptions>
{
private readonly ProjectModel _projectModel;
private readonly ResourceModel _resourceModel;
private readonly ProjectRoleModel _projectRoleModel;
private readonly ProjectInstituteModel _projectInstituteModel;
public ProjectReporting(ProjectReportingOptions options) : base(options)
{
ReportingFileName = "projects.json";
_projectModel = new ProjectModel();
_projectRoleModel = new ProjectRoleModel();
_resourceModel = new ResourceModel();
_projectInstituteModel = new ProjectInstituteModel();
}
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.
*/
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.Id),
Disciplines = projectReturnObject.Disciplines.ToList(),
Deleted = projectReturnObject.Deleted,
ProjectVisibilityId = projectReturnObject.Visibility.Id,
GrantId = projectReturnObject.GrantId,
Members = _projectRoleModel.GetAllWhere(x => x.ProjectId == projectReturnObject.Id).Count(),
ResourceQuota = GetResourceQuota(project)
};
}
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 ResourceQuotaReturnObject GetResourceQuota(object resource)
{
throw new NotImplementedException();
}
private List<Organization> GetOrganizations(Guid projectID)
{
var result = new List<Organization>();
var projectInstituteModel = new ProjectInstituteModel();
var organizations = from projectInstitute in projectInstituteModel.GetAllWhere((ProjectInstitute projectInstitute) => projectInstitute.ProjectId == projectID)
select projectInstitute.OrganizationUrl;
foreach (var entry in organizations)
{
result.Add(FetchOrganizationByRor(entry));
}
return result;
}
}
\ No newline at end of file
namespace KPIGenerator.Reportings.Project;
using Coscine.Database.ReturnObjects;
using KPIGenerator.Utils;
namespace KPIGenerator.Reportings.Project;
/// <summary>
/// Object containing the JSON structure for the reporting
/// </summary>
public class ReturnObject
{
public Guid Id { get; set; }
public DateTime? DateCreated { get; set; } = null;
public List<Organization>? Organizations { get; set; } = new();
public List<DisciplineObject> Disciplines { get; set; } = new();
public bool Deleted { get; set; }
public Guid ProjectVisibilityId { get; set; }
public string? GrantId { get; set; }
public int? Members { get; set; } = null;
public ResourceQuotaReturnObject? ResourceQuota { get; set; } = null!;
}
......@@ -11,7 +11,7 @@ public class Organization
/// Organizaiton ROR from GitLab project's title
/// </summary>
/// <example>04xfq0f34</example>
public string Ror { get; set; } = null!;
public string RorUrl { get; set; } = null!;
/// <summary>
/// Organizaiton ROR URL from GitLab project's title
/// </summary>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment