Skip to content
Snippets Groups Projects

New: KPI Reporting for Resources

Merged Hanna Führ requested to merge Issue/2183-kpiGeneratorResource into dev
Files
7
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