Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
Loading items

Target

Select target project
  • coscine/backend/scripts/kpi-generator
1 result
Select Git revision
Loading items
Show changes
Commits on Source (3)
Showing
with 246 additions and 88 deletions
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
<AssemblyName>Coscine.KpiGenerator</AssemblyName> <AssemblyName>Coscine.KpiGenerator</AssemblyName>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<Version>0.1.8</Version> <Version>0.1.9</Version>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
...@@ -19,10 +19,18 @@ ...@@ -19,10 +19,18 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" /> <PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Coscine.ApiCommons" Version="2.*-*" /> <PackageReference Include="Coscine.ApiCommons" Version="*-*" />
<PackageReference Include="Coscine.Database" Version="2.*-*" /> <PackageReference Include="Coscine.Database" Version="*-*" />
<PackageReference Include="Coscine.Metadata" Version="2.*-*" /> <PackageReference Include="Coscine.Metadata" Version="*-*" />
<PackageReference Include="Coscine.ResourceTypes" Version="1.*-*" /> <PackageReference Include="Coscine.ResourceTypes" Version="*-*" />
<PackageReference Include="GitLabApiClient" Version="1.8.1-beta.5" /> <PackageReference Include="GitLabApiClient" Version="1.8.1-beta.5" />
<PackageReference Include="NLog" Version="5.1.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.0" />
</ItemGroup>
<ItemGroup>
<None Update="nlog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup> </ItemGroup>
</Project> </Project>
using NLog;
using NLog.LayoutRenderers;
using System.Text;
namespace Coscine.KpiGenerator.Logging
{
[LayoutRenderer("assembly-name")]
public class AssemblyNameLayoutRenderer : LayoutRenderer
{
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
var assembly = typeof(Program).Assembly.GetName();
if (assembly is not null)
{
builder.Append(assembly.Name);
}
else
{
builder.Append(new Guid().ToString().Take(8));
}
}
}
}
using NLog;
using NLog.LayoutRenderers;
using System.Text;
namespace Coscine.KpiGenerator.Logging
{
[LayoutRenderer("assembly-version")]
public class AssemblyVersionLayoutRenderer : LayoutRenderer
{
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
var assembly = typeof(Program).Assembly.GetName();
if (assembly is not null && assembly.Version is not null)
{
builder.Append(assembly.Version.ToString(3));
}
else
{
builder.Append(new Version().ToString(3));
}
}
}
}
using CommandLine; using CommandLine;
using KPIGenerator.Reportings.ApplicationProfile; using Coscine.KpiGenerator.Logging;
using KPIGenerator.Reportings.Complete; using Coscine.KpiGenerator.Reportings.ApplicationProfile;
using KPIGenerator.Reportings.Project; using Coscine.KpiGenerator.Reportings.Complete;
using KPIGenerator.Reportings.Resource; using Coscine.KpiGenerator.Reportings.Project;
using KPIGenerator.Reportings.System; using Coscine.KpiGenerator.Reportings.Resource;
using KPIGenerator.Reportings.User; using Coscine.KpiGenerator.Reportings.System;
using Coscine.KpiGenerator.Reportings.User;
using Microsoft.Extensions.Logging;
using NLog.Config;
using NLog.Extensions.Logging;
using static KPIGenerator.Utils.CommandLineOptions; using static KPIGenerator.Utils.CommandLineOptions;
namespace KPIGenerator; namespace Coscine.KpiGenerator;
public static class Program public class Program
{ {
static int Main(string[] args) private static ILogger _logger = null!;
public static int Main(string[] args)
{ {
ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("assembly-name", typeof(AssemblyNameLayoutRenderer));
ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition("assembly-version", typeof(AssemblyVersionLayoutRenderer));
_logger = LoggerFactory.Create(builder => builder.AddNLog()).CreateLogger<Program>();
try try
{ {
bool result = Parser.Default.ParseArguments< bool result = Parser.Default.ParseArguments<
...@@ -24,27 +34,30 @@ public static class Program ...@@ -24,27 +34,30 @@ public static class Program
SystemReportingOptions SystemReportingOptions
>(args) >(args)
.MapResult( .MapResult(
(CompleteReportingOptions options) => new CompleteReporting(SanitizeOptions(options)).Run(), (CompleteReportingOptions options) => new CompleteReporting(SanitizeOptions(options), _logger).Run(),
(ProjectReportingOptions options) => new ProjectReporting(SanitizeOptions(options)).Run(), (ProjectReportingOptions options) => new ProjectReporting(SanitizeOptions(options), _logger).Run(),
(ResourceReportingOptions options) => new ResourceReporting(SanitizeOptions(options)).Run(), (ResourceReportingOptions options) => new ResourceReporting(SanitizeOptions(options), _logger).Run(),
(UserReportingOptions options) => new UserReporting(SanitizeOptions(options)).Run(), (UserReportingOptions options) => new UserReporting(SanitizeOptions(options), _logger).Run(),
(ApplicationProfileReportingOptions options) => new ApplicationProfileReporting(SanitizeOptions(options)).Run(), (ApplicationProfileReportingOptions options) => new ApplicationProfileReporting(SanitizeOptions(options), _logger).Run(),
(SystemReportingOptions options) => new SystemReporting(SanitizeOptions(options)).Run(), (SystemReportingOptions options) => new SystemReporting(SanitizeOptions(options), _logger).Run(),
_ => false); _ => false);
if (result) if (result)
{ {
Console.WriteLine("\nFinished.\n"); Console.WriteLine("\nFinished.\n");
_logger.LogInformation("Finished.");
return 0; // Exit Code 0 for Success return 0; // Exit Code 0 for Success
} }
else else
{ {
Console.WriteLine("Program execution was interrupted.\n"); Console.WriteLine("Program execution was interrupted.\n");
_logger.LogInformation("Program execution was interrupted.");
return -1; // Exit Code -1 for Failure return -1; // Exit Code -1 for Failure
} }
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e.Message); Console.WriteLine(e.Message);
_logger.LogError(e, e.Message);
return -1; // Exit Code -1 for Failure return -1; // Exit Code -1 for Failure
} }
} }
...@@ -112,4 +125,12 @@ public static class Program ...@@ -112,4 +125,12 @@ public static class Program
} }
return unsanitizedOptions; return unsanitizedOptions;
} }
private static void LogInnerException(Exception ex)
{
if (ex.InnerException is not null)
{
_logger.LogError(ex.InnerException, "InnerException: {innerException}", ex.InnerException.Message);
LogInnerException(ex.InnerException);
}
}
} }
\ No newline at end of file
using Coscine.Configuration; using Coscine.Configuration;
using Coscine.KpiGenerator.Utils;
using Coscine.Metadata; using Coscine.Metadata;
using GitLabApiClient; using GitLabApiClient;
using GitLabApiClient.Models.Branches.Requests; using GitLabApiClient.Models.Branches.Requests;
using GitLabApiClient.Models.Commits.Requests.CreateCommitRequest; using GitLabApiClient.Models.Commits.Requests.CreateCommitRequest;
using KPIGenerator.Utils; using Microsoft.Extensions.Logging;
using System.Text; using System.Text;
using System.Web; using System.Web;
using VDS.RDF.Query; using VDS.RDF.Query;
using static KPIGenerator.Utils.CommandLineOptions; using static KPIGenerator.Utils.CommandLineOptions;
namespace KPIGenerator; namespace Coscine.KpiGenerator;
public abstract class Reporting<O> where O : class public abstract class Reporting<O> where O : class
{ {
...@@ -29,6 +30,7 @@ public abstract class Reporting<O> where O : class ...@@ -29,6 +30,7 @@ public abstract class Reporting<O> where O : class
private string ReportingBranch { get; init; } private string ReportingBranch { get; init; }
public string RwthRor { get; init; } public string RwthRor { get; init; }
public readonly ILogger _logger = null!;
public readonly Organization _otherOrganization = new() public readonly Organization _otherOrganization = new()
{ {
...@@ -36,9 +38,9 @@ public abstract class Reporting<O> where O : class ...@@ -36,9 +38,9 @@ public abstract class Reporting<O> where O : class
RorUrl = "https://ror.org/_other", RorUrl = "https://ror.org/_other",
}; };
public Reporting(O options) public Reporting(O options, ILogger logger)
{ {
InstanceName = this.GetType().Name; InstanceName = GetType().Name;
Options = options; Options = options;
Configuration = new ConsulConfiguration(); Configuration = new ConsulConfiguration();
RdfStoreConnector = new RdfStoreConnector(Configuration.GetStringAndWait("coscine/local/virtuoso/additional/url")); RdfStoreConnector = new RdfStoreConnector(Configuration.GetStringAndWait("coscine/local/virtuoso/additional/url"));
...@@ -52,6 +54,7 @@ public abstract class Reporting<O> where O : class ...@@ -52,6 +54,7 @@ public abstract class Reporting<O> where O : class
ReportingBranch = Configuration.GetStringAndWait("coscine/local/reporting/branch"); ReportingBranch = Configuration.GetStringAndWait("coscine/local/reporting/branch");
RwthRor = Configuration.GetStringAndWait("coscine/global/organizations/rwth/ror_url"); RwthRor = Configuration.GetStringAndWait("coscine/global/organizations/rwth/ror_url");
_logger = logger;
} }
public abstract IEnumerable<ReportingFileObject> GenerateReporting(); public abstract IEnumerable<ReportingFileObject> GenerateReporting();
...@@ -64,6 +67,11 @@ public abstract class Reporting<O> where O : class ...@@ -64,6 +67,11 @@ public abstract class Reporting<O> where O : class
if (baseOptions is not null && baseOptions.DummyMode) if (baseOptions is not null && baseOptions.DummyMode)
{ {
Console.Write(" : DUMMY MODE"); Console.Write(" : DUMMY MODE");
_logger.LogInformation("Initialized on {Domain} | {InstanceName} | DUMMY MODE {DummyMode}", Domain, InstanceName, baseOptions.DummyMode);
}
else
{
_logger.LogInformation("Initialized on {Domain} | {InstanceName}", Domain, InstanceName);
} }
Console.WriteLine($"\n{new string('-', 80)}"); Console.WriteLine($"\n{new string('-', 80)}");
EnsureGitLabInformationIsSetAndCorrect(); EnsureGitLabInformationIsSetAndCorrect();
...@@ -71,9 +79,11 @@ public abstract class Reporting<O> where O : class ...@@ -71,9 +79,11 @@ public abstract class Reporting<O> where O : class
var reportingFiles = GenerateReporting(); var reportingFiles = GenerateReporting();
Console.WriteLine($"\n{new string('=', 80)}"); Console.WriteLine($"\n{new string('=', 80)}");
Console.WriteLine(" - Reporting generated successfully. Publishing..."); Console.WriteLine(" - Reporting generated successfully. Publishing...");
_logger.LogInformation("Reporting generated successfully. Publishing {reportingFiles}", reportingFiles);
// Publish Report // Publish Report
var success = PublishAsync(reportingFiles).Result; var success = PublishAsync(reportingFiles).Result;
Console.WriteLine(success ? " - Published successfully." : " - Publishing FAILED!"); Console.WriteLine(success ? " - Published successfully." : " - Publishing FAILED!");
_logger.LogInformation("Publishing successful: {success}", success);
return success; return success;
} }
...@@ -87,6 +97,7 @@ public abstract class Reporting<O> where O : class ...@@ -87,6 +97,7 @@ public abstract class Reporting<O> where O : class
var commitMessage = $"{InstanceName} Generated - {DateTime.Now:dd.MM.yyyy HH:mm}"; // CompleteReporting Generated - 31.08.2022 10:25 var commitMessage = $"{InstanceName} Generated - {DateTime.Now:dd.MM.yyyy HH:mm}"; // CompleteReporting Generated - 31.08.2022 10:25
Console.WriteLine($" - Commit: \"{commitMessage}\""); Console.WriteLine($" - Commit: \"{commitMessage}\"");
_logger.LogInformation("Commit: {commitMessage}", commitMessage);
var projectTree = await GitLabClient.Trees.GetAsync(reportingDatabaseProject, o => var projectTree = await GitLabClient.Trees.GetAsync(reportingDatabaseProject, o =>
{ {
...@@ -143,6 +154,7 @@ public abstract class Reporting<O> where O : class ...@@ -143,6 +154,7 @@ public abstract class Reporting<O> where O : class
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e.Message); Console.WriteLine(e.Message);
_logger.LogError(e, e.Message);
return false; return false;
} }
} }
...@@ -178,6 +190,7 @@ public abstract class Reporting<O> where O : class ...@@ -178,6 +190,7 @@ public abstract class Reporting<O> where O : class
result.RorUrl = rorUrl; // Don't overwrite the RoR of the entry found, just use "Other" as name. result.RorUrl = rorUrl; // Don't overwrite the RoR of the entry found, just use "Other" as name.
Console.ForegroundColor = ConsoleColor.Yellow; Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($" WARNING!: Organization with ROR \"{rorUrl}\" could not be correctly identified. Will use \"{result.RorUrl}\" as RoR and \"{result.Name}\" as name."); Console.WriteLine($" WARNING!: Organization with ROR \"{rorUrl}\" could not be correctly identified. Will use \"{result.RorUrl}\" as RoR and \"{result.Name}\" as name.");
_logger.LogWarning("Organization with ROR {rorUrl} could not be correctly identified.", rorUrl);
Console.ResetColor(); Console.ResetColor();
} }
} }
...@@ -232,6 +245,7 @@ public abstract class Reporting<O> where O : class ...@@ -232,6 +245,7 @@ public abstract class Reporting<O> where O : class
} }
var project = GitLabClient.Projects.GetAsync(ReportingDatabaseProjectId).Result; var project = GitLabClient.Projects.GetAsync(ReportingDatabaseProjectId).Result;
Console.WriteLine($" - Report Generation to be uploaded to GitLab Project \"{project.Name}\" on branch \"{ReportingBranch}\""); Console.WriteLine($" - Report Generation to be uploaded to GitLab Project \"{project.Name}\" on branch \"{ReportingBranch}\"");
_logger.LogInformation("Report Generation to be uploaded to GitLab Project {projectName} on branch {ReportingBranch}", project.Name, ReportingBranch);
var branch = GitLabClient.Branches.GetAsync(project.Id, o => o.Search = ReportingBranch).Result; var branch = GitLabClient.Branches.GetAsync(project.Id, o => o.Search = ReportingBranch).Result;
if (!branch.Any(b => b.Name.Equals(ReportingBranch)) && Domain.Equals("DEVLEF") && !project.DefaultBranch.Equals(ReportingBranch)) if (!branch.Any(b => b.Name.Equals(ReportingBranch)) && Domain.Equals("DEVLEF") && !project.DefaultBranch.Equals(ReportingBranch))
...@@ -239,6 +253,7 @@ public abstract class Reporting<O> where O : class ...@@ -239,6 +253,7 @@ public abstract class Reporting<O> where O : class
Console.WriteLine($" - Branch \"{ReportingBranch}\" does not exist. Working on Domain {Domain}. Creating branch..."); Console.WriteLine($" - Branch \"{ReportingBranch}\" does not exist. Working on Domain {Domain}. Creating branch...");
GitLabClient.Branches.CreateAsync(ReportingDatabaseProjectId, new CreateBranchRequest(ReportingBranch, project.DefaultBranch)).Wait(); GitLabClient.Branches.CreateAsync(ReportingDatabaseProjectId, new CreateBranchRequest(ReportingBranch, project.DefaultBranch)).Wait();
Console.WriteLine($" - Branch \"{ReportingBranch}\" successfully created"); Console.WriteLine($" - Branch \"{ReportingBranch}\" successfully created");
_logger.LogInformation("Branch {ReportingBranch} successfully created", ReportingBranch);
} }
else if (!branch.Any(b => b.Name.Equals(ReportingBranch))) else if (!branch.Any(b => b.Name.Equals(ReportingBranch)))
{ {
......
using KPIGenerator.Utils; using Coscine.KpiGenerator.Utils;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using VDS.RDF.Query; using VDS.RDF.Query;
using static KPIGenerator.Utils.CommandLineOptions; using static KPIGenerator.Utils.CommandLineOptions;
namespace KPIGenerator.Reportings.ApplicationProfile; namespace Coscine.KpiGenerator.Reportings.ApplicationProfile;
public class ApplicationProfileReporting : Reporting<ApplicationProfileReportingOptions> public class ApplicationProfileReporting : Reporting<ApplicationProfileReportingOptions>
{ {
public ApplicationProfileReporting(ApplicationProfileReportingOptions options) : base(options) public ApplicationProfileReporting(ApplicationProfileReportingOptions options, ILogger _logger) : base(options, _logger)
{ {
ReportingFileName = "application_profiles.json"; ReportingFileName = "application_profiles.json";
} }
...@@ -15,6 +16,7 @@ public class ApplicationProfileReporting : Reporting<ApplicationProfileReporting ...@@ -15,6 +16,7 @@ public class ApplicationProfileReporting : Reporting<ApplicationProfileReporting
public override IEnumerable<ReportingFileObject> GenerateReporting() public override IEnumerable<ReportingFileObject> GenerateReporting()
{ {
Console.WriteLine($" - {GetType().Name}: Begin reporting generation"); Console.WriteLine($" - {GetType().Name}: Begin reporting generation");
_logger.LogInformation("{Name}: Begin reporting generation", GetType().Name);
var reportingFiles = new List<ReportingFileObject>(); var reportingFiles = new List<ReportingFileObject>();
var returnObjects = GetApplicationProfiles(); var returnObjects = GetApplicationProfiles();
...@@ -25,6 +27,7 @@ public class ApplicationProfileReporting : Reporting<ApplicationProfileReporting ...@@ -25,6 +27,7 @@ public class ApplicationProfileReporting : Reporting<ApplicationProfileReporting
Content = ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObjects, Formatting.Indented)) Content = ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObjects, Formatting.Indented))
}); });
Console.WriteLine($" - {GetType().Name}: \"{GetReportingPathGeneral(ReportingFileName)}\" generated successfully"); Console.WriteLine($" - {GetType().Name}: \"{GetReportingPathGeneral(ReportingFileName)}\" generated successfully");
_logger.LogInformation("{Name}: Generated successfully. {reportingFiles}", GetType().Name, reportingFiles);
Console.WriteLine(); Console.WriteLine();
return reportingFiles; return reportingFiles;
......
namespace KPIGenerator.Reportings.ApplicationProfile; namespace Coscine.KpiGenerator.Reportings.ApplicationProfile;
/// <summary> /// <summary>
/// Object containing the JSON structure for the reporting /// Object containing the JSON structure for the reporting
......
using KPIGenerator.Reportings.ApplicationProfile; using Coscine.KpiGenerator.Reportings.ApplicationProfile;
using KPIGenerator.Reportings.Project; using Coscine.KpiGenerator.Reportings.Project;
using KPIGenerator.Reportings.Resource; using Coscine.KpiGenerator.Reportings.Resource;
using KPIGenerator.Reportings.System; using Coscine.KpiGenerator.Reportings.System;
using KPIGenerator.Reportings.User; using Coscine.KpiGenerator.Reportings.User;
using KPIGenerator.Utils; using Coscine.KpiGenerator.Utils;
using Microsoft.Extensions.Logging;
using static KPIGenerator.Utils.CommandLineOptions; using static KPIGenerator.Utils.CommandLineOptions;
namespace KPIGenerator.Reportings.Complete; namespace Coscine.KpiGenerator.Reportings.Complete;
public class CompleteReporting : Reporting<CompleteReportingOptions> public class CompleteReporting : Reporting<CompleteReportingOptions>
{ {
public CompleteReporting(CompleteReportingOptions options) : base(options) public CompleteReporting(CompleteReportingOptions options, ILogger _logger) : base(options, _logger)
{ } { }
public override IEnumerable<ReportingFileObject> GenerateReporting() public override IEnumerable<ReportingFileObject> GenerateReporting()
...@@ -23,11 +24,12 @@ public class CompleteReporting : Reporting<CompleteReportingOptions> ...@@ -23,11 +24,12 @@ public class CompleteReporting : Reporting<CompleteReportingOptions>
result.AddRange(new ProjectReporting(new ProjectReportingOptions result.AddRange(new ProjectReporting(new ProjectReportingOptions
{ {
DummyMode = Options.DummyMode DummyMode = Options.DummyMode
}).GenerateReporting()); }, _logger).GenerateReporting());
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine($"!! Skipping ProjectReporting: {e.Message} \n"); Console.WriteLine($"!! Skipping ProjectReporting: {e.Message} \n");
_logger.LogWarning(e, "Skipping ProjectReporting: {message}", e.Message);
} }
// Resource Reporting // Resource Reporting
...@@ -36,11 +38,12 @@ public class CompleteReporting : Reporting<CompleteReportingOptions> ...@@ -36,11 +38,12 @@ public class CompleteReporting : Reporting<CompleteReportingOptions>
result.AddRange(new ResourceReporting(new ResourceReportingOptions result.AddRange(new ResourceReporting(new ResourceReportingOptions
{ {
DummyMode = Options.DummyMode DummyMode = Options.DummyMode
}).GenerateReporting()); }, _logger).GenerateReporting());
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine($"!! Skipping ResourceReporting: {e.Message} \n"); Console.WriteLine($"!! Skipping ResourceReporting: {e.Message} \n");
_logger.LogWarning(e, "Skipping ResourceReporting: {message}", e.Message);
} }
// User Reporting // User Reporting
...@@ -49,11 +52,12 @@ public class CompleteReporting : Reporting<CompleteReportingOptions> ...@@ -49,11 +52,12 @@ public class CompleteReporting : Reporting<CompleteReportingOptions>
result.AddRange(new UserReporting(new UserReportingOptions result.AddRange(new UserReporting(new UserReportingOptions
{ {
DummyMode = Options.DummyMode DummyMode = Options.DummyMode
}).GenerateReporting()); }, _logger).GenerateReporting());
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine($"!! Skipping UserReporting: {e.Message} \n"); Console.WriteLine($"!! Skipping UserReporting: {e.Message} \n");
_logger.LogWarning(e, "Skipping UserReporting: {message}", e.Message);
} }
// Application Profile Reporting // Application Profile Reporting
...@@ -62,11 +66,12 @@ public class CompleteReporting : Reporting<CompleteReportingOptions> ...@@ -62,11 +66,12 @@ public class CompleteReporting : Reporting<CompleteReportingOptions>
result.AddRange(new ApplicationProfileReporting(new ApplicationProfileReportingOptions result.AddRange(new ApplicationProfileReporting(new ApplicationProfileReportingOptions
{ {
DummyMode = Options.DummyMode DummyMode = Options.DummyMode
}).GenerateReporting()); }, _logger).GenerateReporting());
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine($"!! Skipping ApplicationProfileReporting: {e.Message} \n"); Console.WriteLine($"!! Skipping ApplicationProfileReporting: {e.Message} \n");
_logger.LogWarning(e, "Skipping ApplicationProfileReporting: {message}", e.Message);
} }
// System Status Reporting // System Status Reporting
...@@ -75,11 +80,12 @@ public class CompleteReporting : Reporting<CompleteReportingOptions> ...@@ -75,11 +80,12 @@ public class CompleteReporting : Reporting<CompleteReportingOptions>
result.AddRange(new SystemReporting(new SystemReportingOptions result.AddRange(new SystemReporting(new SystemReportingOptions
{ {
DummyMode = Options.DummyMode DummyMode = Options.DummyMode
}).GenerateReporting()); }, _logger).GenerateReporting());
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine($"!! Skipping SystemReporting: {e.Message} \n"); Console.WriteLine($"!! Skipping SystemReporting: {e.Message} \n");
_logger.LogWarning(e, "Skipping SystemReporting: {message}", e.Message);
} }
return result; return result;
......
namespace KPIGenerator.Reportings.Complete; namespace Coscine.KpiGenerator.Reportings.Complete;
/// <summary> /// <summary>
/// Object containing the JSON structure for the reporting /// Object containing the JSON structure for the reporting
......
using Coscine.Database.DataModel; using Coscine.Database.DataModel;
using Coscine.Database.Models; using Coscine.Database.Models;
using Coscine.Database.ReturnObjects; using Coscine.Database.ReturnObjects;
using Coscine.KpiGenerator.Utils;
using Coscine.ResourceTypes; using Coscine.ResourceTypes;
using KPIGenerator.Utils; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using static KPIGenerator.Utils.CommandLineOptions; using static KPIGenerator.Utils.CommandLineOptions;
namespace KPIGenerator.Reportings.Project; namespace Coscine.KpiGenerator.Reportings.Project;
public class ProjectReporting : Reporting<ProjectReportingOptions> public class ProjectReporting : Reporting<ProjectReportingOptions>
{ {
...@@ -16,7 +17,7 @@ public class ProjectReporting : Reporting<ProjectReportingOptions> ...@@ -16,7 +17,7 @@ public class ProjectReporting : Reporting<ProjectReportingOptions>
private readonly ProjectQuotaModel _projectQuotaModel; private readonly ProjectQuotaModel _projectQuotaModel;
private readonly ResourceTypeModel _resourceTypeModel; private readonly ResourceTypeModel _resourceTypeModel;
public ProjectReporting(ProjectReportingOptions options) : base(options) public ProjectReporting(ProjectReportingOptions options, ILogger _logger) : base(options, _logger)
{ {
ReportingFileName = "projects.json"; ReportingFileName = "projects.json";
_projectModel = new ProjectModel(); _projectModel = new ProjectModel();
...@@ -29,6 +30,7 @@ public class ProjectReporting : Reporting<ProjectReportingOptions> ...@@ -29,6 +30,7 @@ public class ProjectReporting : Reporting<ProjectReportingOptions>
public override IEnumerable<ReportingFileObject> GenerateReporting() public override IEnumerable<ReportingFileObject> GenerateReporting()
{ {
Console.WriteLine($" - {GetType().Name}: Begin reporting generation"); Console.WriteLine($" - {GetType().Name}: Begin reporting generation");
_logger.LogInformation("{Name}: Begin reporting generation", GetType().Name);
var projects = _projectModel.GetAllWhere(r => r.Deleted.Equals(true) || r.Deleted.Equals(false)); var projects = _projectModel.GetAllWhere(r => r.Deleted.Equals(true) || r.Deleted.Equals(false));
var reportingFiles = new List<ReportingFileObject>(); var reportingFiles = new List<ReportingFileObject>();
var returnObjects = Generate(projects); var returnObjects = Generate(projects);
...@@ -40,6 +42,7 @@ public class ProjectReporting : Reporting<ProjectReportingOptions> ...@@ -40,6 +42,7 @@ public class ProjectReporting : Reporting<ProjectReportingOptions>
Content = ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObjects, Formatting.Indented)), Content = ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObjects, Formatting.Indented)),
}); });
Console.WriteLine($" - {GetType().Name}: \"{GetReportingPathGeneral(ReportingFileName)}\" generated successfully"); Console.WriteLine($" - {GetType().Name}: \"{GetReportingPathGeneral(ReportingFileName)}\" generated successfully");
_logger.LogInformation("{Name}: Generated successfully. {reportingFiles}", GetType().Name, reportingFiles);
// Per Organization // Per Organization
reportingFiles.AddRange(GeneratePerOrganization(returnObjects)); reportingFiles.AddRange(GeneratePerOrganization(returnObjects));
...@@ -48,7 +51,7 @@ public class ProjectReporting : Reporting<ProjectReportingOptions> ...@@ -48,7 +51,7 @@ public class ProjectReporting : Reporting<ProjectReportingOptions>
return reportingFiles; return reportingFiles;
} }
private List<ReturnObject> Generate(IEnumerable<Coscine.Database.DataModel.Project> projects) private List<ReturnObject> Generate(IEnumerable<Database.DataModel.Project> projects)
{ {
var returnObjects = new List<ReturnObject>(); var returnObjects = new List<ReturnObject>();
foreach (var project in projects) foreach (var project in projects)
...@@ -83,16 +86,19 @@ public class ProjectReporting : Reporting<ProjectReportingOptions> ...@@ -83,16 +86,19 @@ public class ProjectReporting : Reporting<ProjectReportingOptions>
{ {
organization = _otherOrganization; organization = _otherOrganization;
Console.WriteLine($" WARNING!: Organization \"{entry.RorUrl}\" could not be correctly identified. Will use \"{_otherOrganization.RorUrl}\"."); Console.WriteLine($" WARNING!: Organization \"{entry.RorUrl}\" could not be correctly identified. Will use \"{_otherOrganization.RorUrl}\".");
_logger.LogWarning("Organization {ror} could not be correctly identified.", entry.RorUrl);
} }
var returnObjectsForOrganization = returnObjects.Where(ro => ro.Organizations.Select(o => o.RorUrl).Any(e => e.Contains(entry.RorUrl))).ToList(); var returnObjectsForOrganization = returnObjects.Where(ro => ro.Organizations.Select(o => o.RorUrl).Any(e => e.Contains(entry.RorUrl))).ToList();
reportingFilesPerOrganization.Add(new ReportingFileObject var reportingFile = new ReportingFileObject
{ {
Path = GetReportingPathOrganization(organization.RorUrl, ReportingFileName), Path = GetReportingPathOrganization(organization.RorUrl, ReportingFileName),
Content = ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObjectsForOrganization, Formatting.Indented)) Content = ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObjectsForOrganization, Formatting.Indented))
}); };
reportingFilesPerOrganization.Add(reportingFile);
Console.WriteLine($" - {GetType().Name}: \"{GetReportingPathOrganization(organization.RorUrl, ReportingFileName)}\" generated successfully"); Console.WriteLine($" - {GetType().Name}: \"{GetReportingPathOrganization(organization.RorUrl, ReportingFileName)}\" generated successfully");
_logger.LogInformation("{Name}: Generated successfully {ReportingFile}.", GetType().Name, reportingFile);
} }
return reportingFilesPerOrganization; return reportingFilesPerOrganization;
...@@ -131,13 +137,13 @@ public class ProjectReporting : Reporting<ProjectReportingOptions> ...@@ -131,13 +137,13 @@ public class ProjectReporting : Reporting<ProjectReportingOptions>
}, },
TotalUsed = new QuotaDimObject() TotalUsed = new QuotaDimObject()
{ {
Value = CalculateUsedForAll(_resourceTypeModel.GetById(projectQuota.ResourceTypeId), projectId), Value = CalculateUsedForAll(_resourceTypeModel.GetById(projectQuota.ResourceTypeId), projectId, QuotaUnit.GibiBYTE),
Unit = QuotaUnit.BYTE, Unit = QuotaUnit.GibiBYTE,
}, },
}; };
} }
private int CalculateUsedForAll(ResourceType resourceType, Guid projectId) private float CalculateUsedForAll(ResourceType resourceType, Guid projectId, QuotaUnit outputInThisUnit)
{ {
var resources = _resourceModel.GetAllWhere((resource) => var resources = _resourceModel.GetAllWhere((resource) =>
(from projectResource in resource.ProjectResources (from projectResource in resource.ProjectResources
...@@ -151,19 +157,28 @@ public class ProjectReporting : Reporting<ProjectReportingOptions> ...@@ -151,19 +157,28 @@ public class ProjectReporting : Reporting<ProjectReportingOptions>
var rt = ResourceTypeFactory.Instance.GetResourceType(resource); var rt = ResourceTypeFactory.Instance.GetResourceType(resource);
if (rt.GetResourceTypeInformation().Result.IsQuotaAvailable) if (rt.GetResourceTypeInformation().Result.IsQuotaAvailable)
{ {
return rt.GetResourceQuotaUsed(resource.Id.ToString(), _resourceModel.GetResourceTypeOptions(resource.Id)).Result; try
{
var usedBytes = rt.GetResourceQuotaUsed(resource.Id.ToString(), _resourceModel.GetResourceTypeOptions(resource.Id)).Result;
return Helpers.ConvertCapacityUnits(new QuotaDimObject() { Value = usedBytes, Unit = QuotaUnit.BYTE }, outputInThisUnit);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return 0f;
}
} }
else else
{ {
return 0; return 0f;
} }
} }
); );
return (int)used; return used;
} }
private int CalculateAllocatedForAll(ResourceType resourceType, Guid projectId) private float CalculateAllocatedForAll(ResourceType resourceType, Guid projectId)
{ {
var resources = _resourceModel.GetAllWhere((resource) => var resources = _resourceModel.GetAllWhere((resource) =>
(from projectResource in resource.ProjectResources (from projectResource in resource.ProjectResources
...@@ -176,16 +191,24 @@ public class ProjectReporting : Reporting<ProjectReportingOptions> ...@@ -176,16 +191,24 @@ public class ProjectReporting : Reporting<ProjectReportingOptions>
// Linked has no quota. // Linked has no quota.
var rt = ResourceTypeFactory.Instance.GetResourceType(resource); var rt = ResourceTypeFactory.Instance.GetResourceType(resource);
if (rt.GetResourceTypeInformation().Result.IsQuotaAvailable) if (rt.GetResourceTypeInformation().Result.IsQuotaAvailable)
{
try
{ {
return rt.GetResourceQuotaAvailable(resource.Id.ToString(), _resourceModel.GetResourceTypeOptions(resource.Id)).Result; return rt.GetResourceQuotaAvailable(resource.Id.ToString(), _resourceModel.GetResourceTypeOptions(resource.Id)).Result;
} }
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return 0f;
}
}
else else
{ {
return 0; return 0f;
} }
}); });
return (int)allocated; return allocated;
} }
private List<Organization> GetOrganizations(ProjectObject project) private List<Organization> GetOrganizations(ProjectObject project)
......
using Coscine.Database.ReturnObjects; using Coscine.Database.ReturnObjects;
namespace KPIGenerator.Reportings.Project; namespace Coscine.KpiGenerator.Reportings.Project;
public class ResourceTypeQuotaReturnObject public class ResourceTypeQuotaReturnObject
{ {
......
using Coscine.Database.ReturnObjects; using Coscine.Database.ReturnObjects;
using KPIGenerator.Utils; using Coscine.KpiGenerator.Utils;
namespace KPIGenerator.Reportings.Project; namespace Coscine.KpiGenerator.Reportings.Project;
/// <summary> /// <summary>
/// Object containing the JSON structure for the reporting /// Object containing the JSON structure for the reporting
......
using Coscine.Database.Models; using Coscine.Database.Models;
using Coscine.Database.ReturnObjects; using Coscine.Database.ReturnObjects;
using Coscine.KpiGenerator.Utils;
using Coscine.ResourceTypes; using Coscine.ResourceTypes;
using Coscine.ResourceTypes.Base; using Coscine.ResourceTypes.Base;
using KPIGenerator.Utils; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using static KPIGenerator.Utils.CommandLineOptions; using static KPIGenerator.Utils.CommandLineOptions;
namespace KPIGenerator.Reportings.Resource; namespace Coscine.KpiGenerator.Reportings.Resource;
public class ResourceReporting : Reporting<ResourceReportingOptions> public class ResourceReporting : Reporting<ResourceReportingOptions>
{ {
...@@ -14,7 +15,7 @@ public class ResourceReporting : Reporting<ResourceReportingOptions> ...@@ -14,7 +15,7 @@ public class ResourceReporting : Reporting<ResourceReportingOptions>
private readonly ProjectModel _projectModel; private readonly ProjectModel _projectModel;
private readonly ProjectResourceModel _projectResourceModel; private readonly ProjectResourceModel _projectResourceModel;
public ResourceReporting(ResourceReportingOptions options) : base(options) public ResourceReporting(ResourceReportingOptions options, ILogger _logger) : base(options, _logger)
{ {
ReportingFileName = "resources.json"; ReportingFileName = "resources.json";
_resourceModel = new ResourceModel(); _resourceModel = new ResourceModel();
...@@ -25,6 +26,7 @@ public class ResourceReporting : Reporting<ResourceReportingOptions> ...@@ -25,6 +26,7 @@ public class ResourceReporting : Reporting<ResourceReportingOptions>
public override IEnumerable<ReportingFileObject> GenerateReporting() public override IEnumerable<ReportingFileObject> GenerateReporting()
{ {
Console.WriteLine($" - {GetType().Name}: Begin reporting generation"); Console.WriteLine($" - {GetType().Name}: Begin reporting generation");
_logger.LogInformation("{Name}: Begin reporting generation", GetType().Name);
var resources = _resourceModel.GetAllWhere(r => r.Deleted.Equals(true) || r.Deleted.Equals(false)); var resources = _resourceModel.GetAllWhere(r => r.Deleted.Equals(true) || r.Deleted.Equals(false));
var reportingFiles = new List<ReportingFileObject>(); var reportingFiles = new List<ReportingFileObject>();
var returnObjects = Generate(resources); var returnObjects = Generate(resources);
...@@ -36,6 +38,7 @@ public class ResourceReporting : Reporting<ResourceReportingOptions> ...@@ -36,6 +38,7 @@ public class ResourceReporting : Reporting<ResourceReportingOptions>
Content = ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObjects, Formatting.Indented)) Content = ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObjects, Formatting.Indented))
}); });
Console.WriteLine($" - {GetType().Name}: \"{GetReportingPathGeneral(ReportingFileName)}\" generated successfully"); Console.WriteLine($" - {GetType().Name}: \"{GetReportingPathGeneral(ReportingFileName)}\" generated successfully");
_logger.LogInformation("{Name}: Generated successfully. {reportingFiles}", GetType().Name, reportingFiles);
// Per Organization // Per Organization
reportingFiles.AddRange(GeneratePerOrganization(returnObjects)); reportingFiles.AddRange(GeneratePerOrganization(returnObjects));
...@@ -44,7 +47,7 @@ public class ResourceReporting : Reporting<ResourceReportingOptions> ...@@ -44,7 +47,7 @@ public class ResourceReporting : Reporting<ResourceReportingOptions>
return reportingFiles; return reportingFiles;
} }
private List<ReturnObject> Generate(IEnumerable<Coscine.Database.DataModel.Resource> resources) private List<ReturnObject> Generate(IEnumerable<Database.DataModel.Resource> resources)
{ {
var returnObjects = new List<ReturnObject>(); var returnObjects = new List<ReturnObject>();
foreach (var resource in resources) foreach (var resource in resources)
...@@ -82,15 +85,18 @@ public class ResourceReporting : Reporting<ResourceReportingOptions> ...@@ -82,15 +85,18 @@ public class ResourceReporting : Reporting<ResourceReportingOptions>
{ {
organization = _otherOrganization; organization = _otherOrganization;
Console.WriteLine($" WARNING!: Organization \"{entry.RorUrl}\" could not be correctly identified. Will use \"{_otherOrganization.RorUrl}\"."); Console.WriteLine($" WARNING!: Organization \"{entry.RorUrl}\" could not be correctly identified. Will use \"{_otherOrganization.RorUrl}\".");
_logger.LogWarning("Organization {ror} could not be correctly identified.", entry.RorUrl);
} }
var returnObjectsForOrganization = returnObjects.Where(ro => ro.Organizations.Select(o => o.RorUrl).Any(e => e.Equals(entry.RorUrl))).ToList(); var returnObjectsForOrganization = returnObjects.Where(ro => ro.Organizations.Select(o => o.RorUrl).Any(e => e.Equals(entry.RorUrl))).ToList();
reportingFilesPerOrganization.Add(new ReportingFileObject var reportingFile = new ReportingFileObject
{ {
Path = GetReportingPathOrganization(organization.RorUrl, ReportingFileName), Path = GetReportingPathOrganization(organization.RorUrl, ReportingFileName),
Content = ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObjectsForOrganization, Formatting.Indented)) Content = ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObjectsForOrganization, Formatting.Indented))
}); };
reportingFilesPerOrganization.Add(reportingFile);
Console.WriteLine($" - {GetType().Name}: \"{GetReportingPathOrganization(organization.RorUrl, ReportingFileName)}\" generated successfully"); Console.WriteLine($" - {GetType().Name}: \"{GetReportingPathOrganization(organization.RorUrl, ReportingFileName)}\" generated successfully");
_logger.LogInformation("{Name}: Generated successfully {ReportingFile}.", GetType().Name, reportingFile);
} }
return reportingFilesPerOrganization; return reportingFilesPerOrganization;
} }
...@@ -108,7 +114,7 @@ public class ResourceReporting : Reporting<ResourceReportingOptions> ...@@ -108,7 +114,7 @@ public class ResourceReporting : Reporting<ResourceReportingOptions>
} }
} }
private static ResourceQuotaReturnObject? GetResourceQuota(Coscine.Database.DataModel.Resource resource) private ResourceQuotaReturnObject? GetResourceQuota(Database.DataModel.Resource resource)
{ {
BaseResourceType? resourceTypeDefinition; BaseResourceType? resourceTypeDefinition;
try try
...@@ -122,9 +128,17 @@ public class ResourceReporting : Reporting<ResourceReportingOptions> ...@@ -122,9 +128,17 @@ public class ResourceReporting : Reporting<ResourceReportingOptions>
} }
if (resourceTypeDefinition is not null && resourceTypeDefinition.GetResourceTypeInformation().Result.IsQuotaAdjustable) if (resourceTypeDefinition is not null && resourceTypeDefinition.GetResourceTypeInformation().Result.IsQuotaAdjustable)
{
try
{ {
return Helpers.CreateResourceQuotaReturnObject(resource, resourceTypeDefinition); return Helpers.CreateResourceQuotaReturnObject(resource, resourceTypeDefinition);
} }
catch (Exception ex)
{
_logger.LogError(ex, "Error: {message} for {@resource}", ex.Message, resource);
return null;
}
}
else else
{ {
return null; return null;
......
using Coscine.Database.ReturnObjects; using Coscine.Database.ReturnObjects;
using KPIGenerator.Utils; using Coscine.KpiGenerator.Utils;
namespace KPIGenerator.Reportings.Resource; namespace Coscine.KpiGenerator.Reportings.Resource;
/// <summary> /// <summary>
/// Object containing the JSON structure for the reporting /// Object containing the JSON structure for the reporting
......
using Newtonsoft.Json; using Newtonsoft.Json;
namespace KPIGenerator.Reportings.System; namespace Coscine.KpiGenerator.Reportings.System;
public class MaintenanceBannerObject public class MaintenanceBannerObject
{ {
......
namespace KPIGenerator.Reportings.System; namespace Coscine.KpiGenerator.Reportings.System;
/// <summary> /// <summary>
/// Object containing the JSON structure for the reporting /// Object containing the JSON structure for the reporting
......
using Coscine.Configuration; using Coscine.Configuration;
using KPIGenerator.Utils; using Coscine.KpiGenerator.Utils;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Text; using System.Text;
using static KPIGenerator.Utils.CommandLineOptions; using static KPIGenerator.Utils.CommandLineOptions;
namespace KPIGenerator.Reportings.System; namespace Coscine.KpiGenerator.Reportings.System;
public class SystemReporting : Reporting<SystemReportingOptions> public class SystemReporting : Reporting<SystemReportingOptions>
{ {
private readonly ConsulConfiguration _configuration; private readonly ConsulConfiguration _configuration;
public SystemReporting(SystemReportingOptions options) : base(options) public SystemReporting(SystemReportingOptions options, ILogger _logger) : base(options, _logger)
{ {
ReportingFileName = "system_status.json"; ReportingFileName = "system_status.json";
_configuration = new ConsulConfiguration(); _configuration = new ConsulConfiguration();
...@@ -20,6 +21,7 @@ public class SystemReporting : Reporting<SystemReportingOptions> ...@@ -20,6 +21,7 @@ public class SystemReporting : Reporting<SystemReportingOptions>
public override IEnumerable<ReportingFileObject> GenerateReporting() public override IEnumerable<ReportingFileObject> GenerateReporting()
{ {
Console.WriteLine($" - {GetType().Name}: Begin reporting generation"); Console.WriteLine($" - {GetType().Name}: Begin reporting generation");
_logger.LogInformation("{Name}: Begin reporting generation", GetType().Name);
var reportingFiles = new List<ReportingFileObject>(); var reportingFiles = new List<ReportingFileObject>();
var returnObject = GetSystemStatus(); var returnObject = GetSystemStatus();
...@@ -30,6 +32,7 @@ public class SystemReporting : Reporting<SystemReportingOptions> ...@@ -30,6 +32,7 @@ public class SystemReporting : Reporting<SystemReportingOptions>
Content = ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObject, Formatting.Indented)) Content = ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObject, Formatting.Indented))
}); });
Console.WriteLine($" - {GetType().Name}: \"{GetReportingPathGeneral(ReportingFileName)}\" generated successfully"); Console.WriteLine($" - {GetType().Name}: \"{GetReportingPathGeneral(ReportingFileName)}\" generated successfully");
_logger.LogInformation("{Name}: Generated successfully. {reportingFiles}", GetType().Name, reportingFiles);
Console.WriteLine(); Console.WriteLine();
return reportingFiles; return reportingFiles;
......
using Coscine.Database.ReturnObjects; using Coscine.Database.ReturnObjects;
using KPIGenerator.Utils; using Coscine.KpiGenerator.Utils;
namespace KPIGenerator.Reportings.User; namespace Coscine.KpiGenerator.Reportings.User;
/// <summary> /// <summary>
/// Object containing the JSON structure for the reporting /// Object containing the JSON structure for the reporting
......
using Coscine.Database.DataModel; using Coscine.Database.DataModel;
using Coscine.Database.Models; using Coscine.Database.Models;
using Coscine.KpiGenerator.Utils;
using Coscine.Metadata; using Coscine.Metadata;
using KPIGenerator.Utils; using Microsoft.Extensions.Logging;
using Newtonsoft.Json; using Newtonsoft.Json;
using VDS.RDF.Query; using VDS.RDF.Query;
using static KPIGenerator.Utils.CommandLineOptions; using static KPIGenerator.Utils.CommandLineOptions;
namespace KPIGenerator.Reportings.User; namespace Coscine.KpiGenerator.Reportings.User;
public class UserReporting : Reporting<UserReportingOptions> public class UserReporting : Reporting<UserReportingOptions>
{ {
...@@ -19,7 +20,7 @@ public class UserReporting : Reporting<UserReportingOptions> ...@@ -19,7 +20,7 @@ public class UserReporting : Reporting<UserReportingOptions>
private readonly LogModel _logModel; private readonly LogModel _logModel;
private readonly IEnumerable<ExternalAuthenticator> _loginProviders; private readonly IEnumerable<ExternalAuthenticator> _loginProviders;
public UserReporting(UserReportingOptions options) : base(options) public UserReporting(UserReportingOptions options, ILogger _logger) : base(options, _logger)
{ {
ReportingFileName = "users.json"; ReportingFileName = "users.json";
_externalAuthenticatorModel = new ExternalAuthenticatorModel(); _externalAuthenticatorModel = new ExternalAuthenticatorModel();
...@@ -36,6 +37,7 @@ public class UserReporting : Reporting<UserReportingOptions> ...@@ -36,6 +37,7 @@ public class UserReporting : Reporting<UserReportingOptions>
public override IEnumerable<ReportingFileObject> GenerateReporting() public override IEnumerable<ReportingFileObject> GenerateReporting()
{ {
Console.WriteLine($" - {GetType().Name}: Begin reporting generation"); Console.WriteLine($" - {GetType().Name}: Begin reporting generation");
_logger.LogInformation("{Name}: Begin reporting generation", GetType().Name);
var users = _userModel.GetAllWhere((user) => user.Tosaccepteds.Any()); var users = _userModel.GetAllWhere((user) => user.Tosaccepteds.Any());
var reportingFiles = new List<ReportingFileObject>(); var reportingFiles = new List<ReportingFileObject>();
var returnObjects = Generate(users); var returnObjects = Generate(users);
...@@ -47,6 +49,7 @@ public class UserReporting : Reporting<UserReportingOptions> ...@@ -47,6 +49,7 @@ public class UserReporting : Reporting<UserReportingOptions>
Content = ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObjects, Formatting.Indented)) Content = ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObjects, Formatting.Indented))
}); });
Console.WriteLine($" - {GetType().Name}: \"{GetReportingPathGeneral(ReportingFileName)}\" generated successfully"); Console.WriteLine($" - {GetType().Name}: \"{GetReportingPathGeneral(ReportingFileName)}\" generated successfully");
_logger.LogInformation("{Name}: Generated successfully. {reportingFiles}", GetType().Name, reportingFiles);
// Per Organization // Per Organization
reportingFiles.AddRange(GeneratePerOrganization(returnObjects)); reportingFiles.AddRange(GeneratePerOrganization(returnObjects));
...@@ -55,7 +58,7 @@ public class UserReporting : Reporting<UserReportingOptions> ...@@ -55,7 +58,7 @@ public class UserReporting : Reporting<UserReportingOptions>
return reportingFiles; return reportingFiles;
} }
private List<ReturnObject> Generate(IEnumerable<Coscine.Database.DataModel.User> users) private List<ReturnObject> Generate(IEnumerable<Database.DataModel.User> users)
{ {
var returnObjects = new List<ReturnObject>(); var returnObjects = new List<ReturnObject>();
foreach (var user in users) foreach (var user in users)
...@@ -86,16 +89,19 @@ public class UserReporting : Reporting<UserReportingOptions> ...@@ -86,16 +89,19 @@ public class UserReporting : Reporting<UserReportingOptions>
{ {
organization = _otherOrganization; organization = _otherOrganization;
Console.WriteLine($" WARNING!: Organization \"{entry.RorUrl}\" could not be correctly identified. Will use \"{_otherOrganization.RorUrl}\"."); Console.WriteLine($" WARNING!: Organization \"{entry.RorUrl}\" could not be correctly identified. Will use \"{_otherOrganization.RorUrl}\".");
_logger.LogWarning("Organization {ror} could not be correctly identified.", entry.RorUrl);
} }
var returnObjectsForOrganization = returnObjects.Where(ro => ro.Organizations.Select(o => o.RorUrl).Any(e => e.Contains(entry.RorUrl))).ToList(); var returnObjectsForOrganization = returnObjects.Where(ro => ro.Organizations.Select(o => o.RorUrl).Any(e => e.Contains(entry.RorUrl))).ToList();
reportingFilesPerOrganization.Add(new ReportingFileObject var reportingFile = new ReportingFileObject
{ {
Path = GetReportingPathOrganization(organization.RorUrl, ReportingFileName), Path = GetReportingPathOrganization(organization.RorUrl, ReportingFileName),
Content = ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObjectsForOrganization, Formatting.Indented)) Content = ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObjectsForOrganization, Formatting.Indented))
}); };
reportingFilesPerOrganization.Add(reportingFile);
Console.WriteLine($" - {GetType().Name}: \"{GetReportingPathOrganization(organization.RorUrl, ReportingFileName)}\" generated successfully"); Console.WriteLine($" - {GetType().Name}: \"{GetReportingPathOrganization(organization.RorUrl, ReportingFileName)}\" generated successfully");
_logger.LogInformation("{Name}: Generated successfully {ReportingFile}.", GetType().Name, reportingFile);
} }
return reportingFilesPerOrganization; return reportingFilesPerOrganization;
} }
...@@ -158,8 +164,8 @@ public class UserReporting : Reporting<UserReportingOptions> ...@@ -158,8 +164,8 @@ public class UserReporting : Reporting<UserReportingOptions>
result.Add(orgOrcid); result.Add(orgOrcid);
break; break;
case "shibboleth": case "shibboleth":
// Find the RoR first based on the Organization Entity Id, then try based on the user's External Id. // Find the RoR first based on the user's External Id, then try based on the Organization Entity Id.
var orgShibboleth = GetOrganizationByExternalOrEntityId(externalId.Organization) ?? GetOrganizationByExternalOrEntityId(externalId.ExternalId1); var orgShibboleth = GetOrganizationByExternalOrEntityId(externalId.ExternalId1) ?? GetOrganizationByExternalOrEntityId(externalId.Organization);
if (orgShibboleth is null) if (orgShibboleth is null)
{ {
Console.WriteLine($" No {searchedEntityType} found for user with ID \"{id}\" and login provider {loginProvider.DisplayName}"); Console.WriteLine($" No {searchedEntityType} found for user with ID \"{id}\" and login provider {loginProvider.DisplayName}");
...@@ -195,6 +201,19 @@ public class UserReporting : Reporting<UserReportingOptions> ...@@ -195,6 +201,19 @@ public class UserReporting : Reporting<UserReportingOptions>
} }
} }
// Special case until `https://ror.org/%20https://ror.org/` is moved to `https://ror.org/_other`
if (result.Any(o => o.RorUrl.Equals("https://ror.org/%20https://ror.org/")))
{
foreach (var entry in result)
{
if (entry.RorUrl.Equals("https://ror.org/%20https://ror.org/"))
{
entry.Name = _otherOrganization.Name;
entry.RorUrl = _otherOrganization.RorUrl;
}
}
}
return result.DistinctBy(e => e.RorUrl).ToList(); return result.DistinctBy(e => e.RorUrl).ToList();
} }
......
namespace KPIGenerator.Utils; namespace Coscine.KpiGenerator.Utils;
public class Organization public class Organization
{ {
......