Skip to content
Snippets Groups Projects
Commit e6be285e authored by Petar Hristov's avatar Petar Hristov :speech_balloon:
Browse files

New: System reporting and application profile reporting

parent 913974e0
No related branches found
No related tags found
1 merge request!31BREAKING: Migrated KPI Generator to the APIv2 infrastructure
Pipeline #1168263 failed
Showing
with 324 additions and 488 deletions
...@@ -21,9 +21,11 @@ ...@@ -21,9 +21,11 @@
<PackageReference Include="AutoMapper" Version="12.0.1" /> <PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" /> <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="CommandLineParser" Version="2.9.1" /> <PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Coscine.ApiClient" Version="1.2.1" /> <PackageReference Include="Coscine.ApiClient" Version="1.3.0-issue-2666-admin0005" />
<PackageReference Include="dotNetRdf.Core" Version="3.1.1" />
<PackageReference Include="GitLabApiClient" Version="1.8.1-beta.5" /> <PackageReference Include="GitLabApiClient" Version="1.8.1-beta.5" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="NLog" Version="5.1.0" /> <PackageReference Include="NLog" Version="5.1.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.0" /> <PackageReference Include="NLog.Extensions.Logging" Version="5.2.0" />
<PackageReference Include="Polly.Extensions.Http" Version="3.0.0" /> <PackageReference Include="Polly.Extensions.Http" Version="3.0.0" />
......
namespace Coscine.KpiGenerator.Reportings.ApplicationProfile; namespace Coscine.KpiGenerator.Models;
/// <summary> /// <summary>
/// Object containing the JSON structure for the reporting /// Object containing the JSON structure for the reporting
/// </summary> /// </summary>
public class ReturnObject public class ApplicationProfileReport
{ {
public List<string?> Titles { get; set; } = new(); public List<string> Titles { get; set; } = new();
public string Uri { get; set; } = null!; public string Uri { get; set; } = null!;
public string? Publisher { get; set; } = null!; public string? Publisher { get; set; } = null!;
public string? Rights { get; set; } = null!; public string? Rights { get; set; } = null!;
......
namespace Coscine.KpiGenerator.Models.ConfigurationModels;
/// <summary>
/// Represents the configuration settings for JSON Web Tokens (JWT) used in the application.
/// </summary>
public class KpiConfiguration
{
/// <summary>
/// The section name in the configuration file.
/// </summary>
public static readonly string Section = "KpiConfiguration";
/// <summary>
/// Indicates whether the issuer should be validated during token validation.
/// </summary>
public ApplicationProfileKpi ApplicationProfileKpi { get; set; } = null!;
/// <summary>
/// Indicates whether the audience should be validated during token validation.
/// </summary>
public ProjectKpi ProjectKpi { get; set; } = null!;
/// <summary>
/// Indicates whether the lifetime should be validated during token validation.
/// </summary>
public ResourceKpi ResourceKpi { get; set; } = null!;
/// <summary>
/// Indicates whether the issuer signing key should be validated during token validation.
/// </summary>
public SystemKpi SystemKpi { get; set; } = null!;
/// <summary>
/// A list of valid issuers for the JWT.
/// </summary>
public UserKpi UserKpi { get; set; } = null!;
}
public record ApplicationProfileKpi(string FileName);
public record Maintenance
{
public string Url { get; init; } = null!;
public string Username { get; init; } = null!;
public string Password { get; init; } = null!;
public string BasicAuthToken { get; init; } = null!;
}
public record ProjectKpi(string FileName);
public record ResourceKpi(string FileName);
public record SystemKpi
{
public string FileName { get; set; } = null!;
public Maintenance Maintenance { get; set; } = null!;
}
public record UserKpi(string FileName);
using System.Text.Json.Serialization;
namespace Coscine.KpiGenerator.Models;
public record MaintenanceReport
{
[JsonPropertyName("id")]
public int Id { get; init; }
[JsonPropertyName("title")]
public string Title { get; init; } = null!;
[JsonPropertyName("short_message")]
public string ShortMessage { get; init; } = null!;
[JsonPropertyName("message")]
public string Message { get; init; } = null!;
[JsonPropertyName("status")]
public string Status { get; init; } = null!;
[JsonPropertyName("start_at")]
public DateTime? StartAt { get; init; } = null!;
[JsonPropertyName("end_at")]
public DateTime? EndAt { get; init; } = null!;
}
namespace Coscine.KpiGenerator.Reportings.System; namespace Coscine.KpiGenerator.Models;
/// <summary> /// <summary>
/// Object containing the JSON structure for the reporting /// Object containing the JSON structure for the reporting
/// </summary> /// </summary>
public class ReturnObject public record SystemReport
{ {
public List<MaintenanceBannerObject>? Banners { get; set; } = new(); public IReadOnlyList<MaintenanceReport>? Banners { get; init; } = new List<MaintenanceReport>();
} }
\ No newline at end of file
using CommandLine; using CommandLine;
using Coscine.KpiGenerator.Logging; using Coscine.KpiGenerator.Logging;
using Coscine.KpiGenerator.Models.ConfigurationModels;
using Coscine.KpiGenerator.Reportings.Project; using Coscine.KpiGenerator.Reportings.Project;
using Coscine.KpiGenerator.Reportings.Resource; using Coscine.KpiGenerator.Reportings.Resource;
using GitLabApiClient; using GitLabApiClient;
...@@ -39,8 +40,8 @@ public class Program ...@@ -39,8 +40,8 @@ public class Program
(ProjectReportingOptions opts) => _serviceProvider.GetRequiredService<ProjectReporting>().RunAsync(opts).Result, (ProjectReportingOptions opts) => _serviceProvider.GetRequiredService<ProjectReporting>().RunAsync(opts).Result,
(ResourceReportingOptions opts) => _serviceProvider.GetRequiredService<ResourceReporting>().RunAsync(opts).Result, (ResourceReportingOptions opts) => _serviceProvider.GetRequiredService<ResourceReporting>().RunAsync(opts).Result,
(UserReportingOptions opts) => _serviceProvider.GetRequiredService<UserReporting>().RunAsync(opts).Result, (UserReportingOptions opts) => _serviceProvider.GetRequiredService<UserReporting>().RunAsync(opts).Result,
//(ApplicationProfileReportingOptions opts) => _serviceProvider.GetRequiredService<ApplicationProfileReporting>().Run(opts), (SystemReportingOptions opts) => _serviceProvider.GetRequiredService<SystemReporting>().RunAsync(opts).Result,
//(SystemReportingOptions opts) => _serviceProvider.GetRequiredService<SystemReporting>().Run(opts), (ApplicationProfileReportingOptions opts) => _serviceProvider.GetRequiredService<ApplicationProfileReporting>().RunAsync(opts).Result,
HandleParseError HandleParseError
); );
...@@ -82,9 +83,9 @@ public class Program ...@@ -82,9 +83,9 @@ public class Program
configBuilder.Sources.Clear(); configBuilder.Sources.Clear();
// Add Consul as a configuration source // Add Consul as a configuration source
configBuilder var configuration = configBuilder
.AddConsul( .AddConsul(
"coscine/Coscine.Api/appsettings", "coscine/Coscine.Infrastructure/KpiGenerator/appsettings",
options => options =>
{ {
options.ConsulConfigurationOptions = options.ConsulConfigurationOptions =
...@@ -95,14 +96,18 @@ public class Program ...@@ -95,14 +96,18 @@ public class Program
options.OnLoadException = exceptionContext => exceptionContext.Ignore = true; options.OnLoadException = exceptionContext => exceptionContext.Ignore = true;
} }
) )
.AddEnvironmentVariables(); .AddEnvironmentVariables()
.Build();
// Build the configuration
var configuration = configBuilder.Build();
var services = new ServiceCollection() var services = new ServiceCollection()
.AddSingleton<IConfiguration>(configuration); .AddSingleton<IConfiguration>(configuration);
// Add the configuration to the service collection
services.Configure<KpiConfiguration>(settings =>
{
configuration.GetSection(KpiConfiguration.Section).Bind(settings);
});
// Add logging // Add logging
services.AddLogging(builder => services.AddLogging(builder =>
{ {
...@@ -122,8 +127,8 @@ public class Program ...@@ -122,8 +127,8 @@ public class Program
services.AddTransient<ProjectReporting>(); services.AddTransient<ProjectReporting>();
services.AddTransient<ResourceReporting>(); services.AddTransient<ResourceReporting>();
services.AddTransient<UserReporting>(); services.AddTransient<UserReporting>();
//services.AddTransient<ApplicationProfileReporting>(); services.AddTransient<SystemReporting>();
//services.AddTransient<SystemReporting>(); services.AddTransient<ApplicationProfileReporting>();
_serviceProvider = services.BuildServiceProvider(); _serviceProvider = services.BuildServiceProvider();
} }
......
//using Coscine.KpiGenerator.Utils; using AutoMapper;
//using Microsoft.Extensions.Logging; using Coscine.ApiClient;
//using Newtonsoft.Json; using Coscine.ApiClient.Core.Api;
//using NLog.Extensions.Logging; using Coscine.ApiClient.Core.Client;
//using VDS.RDF.Query; using Coscine.ApiClient.Core.Model;
//using static KPIGenerator.Utils.CommandLineOptions; using Coscine.KpiGenerator.Models;
using Coscine.KpiGenerator.Models.ConfigurationModels;
//namespace Coscine.KpiGenerator.Reportings.ApplicationProfile; using Coscine.KpiGenerator.Utils;
using Microsoft.Extensions.Options;
//public class ApplicationProfileReporting : Reporting<ApplicationProfileReportingOptions> using Newtonsoft.Json;
//{ using VDS.RDF;
// private readonly ILogger<ApplicationProfileReporting> _logger; using VDS.RDF.Nodes;
using VDS.RDF.Parsing;
// public ApplicationProfileReporting(ApplicationProfileReportingOptions options) : base(options) using static KPIGenerator.Utils.CommandLineOptions;
// {
// ReportingFileName = "application_profiles.json"; namespace Coscine.KpiGenerator.Reportings.Resource;
// _logger = LoggerFactory.Create(builder => builder.AddNLog()).CreateLogger<ApplicationProfileReporting>();
// } public class ApplicationProfileReporting
{
// public override IEnumerable<ReportingFileObject> GenerateReportingAsync() private readonly IMapper _mapper;
// { private readonly KpiConfiguration _kpiConfiguration;
// Console.WriteLine($" - {GetType().Name}: Begin reporting generation");
// _logger.LogInformation("{Name}: Begin reporting generation", GetType().Name); public static string AdminToken { get; set; } = null!;
// var reportingFiles = new List<ReportingFileObject>(); public AdminApi AdminApi { get; init; }
// var returnObjects = GetApplicationProfiles(); public ProjectApi ProjectApi { get; init; }
public ApplicationProfileApi ApplicationProfileApi { get; }
// // General File public ApplicationProfileReportingOptions Options { get; private set; } = null!;
// reportingFiles.Add(new ReportingFileObject public string ReportingFileName { get; }
// {
// Path = GetReportingPathGeneral(ReportingFileName), public ApplicationProfileReporting(IMapper mapper, IOptionsMonitor<KpiConfiguration> kpiConfiguration)
// Content = ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObjects, Formatting.Indented)) {
// }); _mapper = mapper;
// Console.WriteLine($" - {GetType().Name}: \"{GetReportingPathGeneral(ReportingFileName)}\" generated successfully"); _kpiConfiguration = kpiConfiguration.CurrentValue;
// _logger.LogInformation("{Name}: Generated successfully. {reportingFiles}", GetType().Name, reportingFiles);
ReportingFileName = _kpiConfiguration.ApplicationProfileKpi.FileName;
// Console.WriteLine();
// return reportingFiles; var configuration = new Configuration()
// } {
BasePath = "http://localhost:7206/coscine",
// private List<ReturnObject> GetApplicationProfiles() ApiKeyPrefix = { { "Authorization", "Bearer" } },
// { ApiKey = { { "Authorization", ApiConfigurationUtil.GenerateAdminToken(ApiConfigurationUtil.RetrieveJwtConfiguration()) } },
// var _applicationProfile = "applicationProfile"; };
// var _title = "title";
// var _publisher = "publisher"; AdminApi = new AdminApi(configuration);
// var _rights = "rights"; ProjectApi = new ProjectApi(configuration);
// var _license = "license"; ApplicationProfileApi = new ApplicationProfileApi(configuration);
}
// var returnObjects = new List<ReturnObject>();
// var queryString = new SparqlParameterizedString public async Task<bool> RunAsync(ApplicationProfileReportingOptions reportingOptions)
// { {
// CommandText = $@"PREFIX dcterms: <http://purl.org/dc/terms/> Options = reportingOptions;
// SELECT DISTINCT * WHERE {{ var reportingFiles = await GenerateReportingAsync();
// ?{_applicationProfile} a <http://www.w3.org/ns/shacl#NodeShape> . return await GitLabUtils.PublishAsync(reportingFiles);
// OPTIONAL {{ ?{_applicationProfile} dcterms:{_title} ?{_title}. }} }
// OPTIONAL {{ ?{_applicationProfile} dcterms:{_publisher} ?{_publisher} . }}
// OPTIONAL {{ ?{_applicationProfile} dcterms:{_rights} ?{_rights} . }} public async Task<IEnumerable<ReportingFileObject>> GenerateReportingAsync()
// OPTIONAL {{ ?{_applicationProfile} dcterms:{_license} ?{_license} . }} {
// }}" var reportingFiles = new List<ReportingFileObject>();
// }; var returnObjects = new List<ApplicationProfileReport>();
// using var result = Helpers.WrapRequest(() => RdfStoreConnector.QueryEndpoint.QueryWithResultSet(queryString.ToString()));
var applicationProfiles = await RequestUtil.WrapPagedRequest<ApplicationProfileDtoPagedResponse, ApplicationProfileDto>((pageNumber) => ApplicationProfileApi.GetApplicationProfilesAsync(pageNumber: pageNumber, pageSize: 500));
// var grouped = result.GroupBy(ap => new foreach(var ap in applicationProfiles)
// { {
// Uri = ap.Value(_applicationProfile).ToString(), var applicationProfile = await ApplicationProfileApi.GetApplicationProfileAsync(ap.Uri, RdfFormat.TextTurtle);
// Publisher = ap.HasValue(_publisher) && ap.Value(_publisher) is not null ? ap.Value(_publisher).ToString() : null,
// Rights = ap.HasValue(_rights) && ap.Value(_rights) is not null ? ap.Value(_rights).ToString() : null, var g = new Graph();
// License = ap.HasValue(_license) && ap.Value(_license) is not null ? ap.Value(_license).ToString() : null StringParser.Parse(g, applicationProfile.Data.Definition.Content);
// },
// t => t.HasValue(_title) && t.Value(_title) is not null ? t.Value(_title).ToString() : null); var titleUri = new Uri("http://purl.org/dc/terms/title");
// foreach (var ap in grouped) var titles = g.GetTriplesWithPredicate(g.CreateUriNode(titleUri))
// { .Select(triple => triple.Object.AsValuedNode().AsString())
// returnObjects.Add(new ReturnObject .ToList();
// {
// Uri = ap.Key.Uri, var publisherUri = new Uri("http://purl.org/dc/terms/publisher");
// Publisher = ap.Key.Publisher, var publisher = g.GetTriplesWithPredicate(g.CreateUriNode(publisherUri))
// Rights = ap.Key.Rights, .FirstOrDefault()?.Object.AsValuedNode().AsString();
// License = ap.Key.License,
// Titles = ap.Select(t => var rightsUri = new Uri("http://purl.org/dc/terms/rights");
// { var rights = g.GetTriplesWithPredicate(g.CreateUriNode(rightsUri))
// if (t is not null) .FirstOrDefault()?.Object.AsValuedNode().AsString();
// {
// return t[..t.IndexOf('@')]; var licenseUri = new Uri("http://purl.org/dc/terms/license");
// } var license = g.GetTriplesWithPredicate(g.CreateUriNode(licenseUri))
// return t; .FirstOrDefault()?.Object.AsValuedNode().AsString();
// }).ToList()
// }); returnObjects.Add(new ApplicationProfileReport
// } {
// return returnObjects; Uri = ap.Uri,
// } Titles = titles ?? new List<string>(),
//} Publisher = publisher,
Rights = rights,
License = license
});
}
// General File
reportingFiles.Add(new ReportingFileObject
{
Path = Helpers.GetReportingPathGeneral(ReportingFileName),
Content = Helpers.ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObjects, Formatting.Indented))
});
// No per organization file
return reportingFiles;
}
}
\ No newline at end of file
...@@ -4,7 +4,9 @@ using Coscine.ApiClient.Core.Api; ...@@ -4,7 +4,9 @@ using Coscine.ApiClient.Core.Api;
using Coscine.ApiClient.Core.Client; using Coscine.ApiClient.Core.Client;
using Coscine.ApiClient.Core.Model; using Coscine.ApiClient.Core.Model;
using Coscine.KpiGenerator.Models; using Coscine.KpiGenerator.Models;
using Coscine.KpiGenerator.Models.ConfigurationModels;
using Coscine.KpiGenerator.Utils; using Coscine.KpiGenerator.Utils;
using Microsoft.Extensions.Options;
using Newtonsoft.Json; using Newtonsoft.Json;
using static KPIGenerator.Utils.CommandLineOptions; using static KPIGenerator.Utils.CommandLineOptions;
...@@ -13,6 +15,7 @@ namespace Coscine.KpiGenerator.Reportings.Project; ...@@ -13,6 +15,7 @@ namespace Coscine.KpiGenerator.Reportings.Project;
public class ProjectReporting public class ProjectReporting
{ {
private readonly IMapper _mapper; private readonly IMapper _mapper;
private readonly KpiConfiguration _kpiConfiguration;
public static string AdminToken { get; set; } = null!; public static string AdminToken { get; set; } = null!;
public AdminApi AdminApi { get; init; } public AdminApi AdminApi { get; init; }
...@@ -20,17 +23,17 @@ public class ProjectReporting ...@@ -20,17 +23,17 @@ public class ProjectReporting
public ProjectReportingOptions Options { get; private set; } = null!; public ProjectReportingOptions Options { get; private set; } = null!;
public string ReportingFileName { get; } public string ReportingFileName { get; }
public ProjectReporting(IMapper mapper) public ProjectReporting(IMapper mapper, IOptionsMonitor<KpiConfiguration> kpiConfiguration)
{ {
_mapper = mapper; _mapper = mapper;
_kpiConfiguration = kpiConfiguration.CurrentValue;
ReportingFileName = "projects.json"; ReportingFileName = _kpiConfiguration.ProjectKpi.FileName;
var configuration = new Configuration() var configuration = new Configuration()
{ {
BasePath = "http://localhost:7206/coscine", BasePath = "http://localhost:7206/coscine",
ApiKeyPrefix = { { "Authorization", "Bearer" } }, ApiKeyPrefix = { { "Authorization", "Bearer" } },
ApiKey = { { "Authorization", JwtUtil.GenerateAdminToken(JwtUtil.RetrieveJwtConfiguration()) } }, ApiKey = { { "Authorization", ApiConfigurationUtil.GenerateAdminToken(ApiConfigurationUtil.RetrieveJwtConfiguration()) } },
}; };
AdminApi = new AdminApi(configuration); AdminApi = new AdminApi(configuration);
......
...@@ -4,7 +4,9 @@ using Coscine.ApiClient.Core.Api; ...@@ -4,7 +4,9 @@ using Coscine.ApiClient.Core.Api;
using Coscine.ApiClient.Core.Client; using Coscine.ApiClient.Core.Client;
using Coscine.ApiClient.Core.Model; using Coscine.ApiClient.Core.Model;
using Coscine.KpiGenerator.Models; using Coscine.KpiGenerator.Models;
using Coscine.KpiGenerator.Models.ConfigurationModels;
using Coscine.KpiGenerator.Utils; using Coscine.KpiGenerator.Utils;
using Microsoft.Extensions.Options;
using Newtonsoft.Json; using Newtonsoft.Json;
using static KPIGenerator.Utils.CommandLineOptions; using static KPIGenerator.Utils.CommandLineOptions;
...@@ -13,6 +15,7 @@ namespace Coscine.KpiGenerator.Reportings.Resource; ...@@ -13,6 +15,7 @@ namespace Coscine.KpiGenerator.Reportings.Resource;
public class ResourceReporting public class ResourceReporting
{ {
private readonly IMapper _mapper; private readonly IMapper _mapper;
private readonly KpiConfiguration _kpiConfiguration;
public static string AdminToken { get; set; } = null!; public static string AdminToken { get; set; } = null!;
public AdminApi AdminApi { get; init; } public AdminApi AdminApi { get; init; }
...@@ -21,17 +24,17 @@ public class ResourceReporting ...@@ -21,17 +24,17 @@ public class ResourceReporting
public ResourceReportingOptions Options { get; private set; } = null!; public ResourceReportingOptions Options { get; private set; } = null!;
public string ReportingFileName { get; } public string ReportingFileName { get; }
public ResourceReporting(IMapper mapper) public ResourceReporting(IMapper mapper, IOptionsMonitor<KpiConfiguration> kpiConfiguration)
{ {
_mapper = mapper; _mapper = mapper;
_kpiConfiguration = kpiConfiguration.CurrentValue;
ReportingFileName = "resources.json"; ReportingFileName = _kpiConfiguration.ResourceKpi.FileName;
var configuration = new Configuration() var configuration = new Configuration()
{ {
BasePath = "http://localhost:7206/coscine", BasePath = "http://localhost:7206/coscine",
ApiKeyPrefix = { { "Authorization", "Bearer" } }, ApiKeyPrefix = { { "Authorization", "Bearer" } },
ApiKey = { { "Authorization", JwtUtil.GenerateAdminToken(JwtUtil.RetrieveJwtConfiguration()) } }, ApiKey = { { "Authorization", ApiConfigurationUtil.GenerateAdminToken(ApiConfigurationUtil.RetrieveJwtConfiguration()) } },
}; };
AdminApi = new AdminApi(configuration); AdminApi = new AdminApi(configuration);
......
using Newtonsoft.Json;
namespace Coscine.KpiGenerator.Reportings.System;
public class MaintenanceBannerObject
{
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("title")]
public string Title { get; set; } = null!;
[JsonProperty("short_message")]
public string ShortMessage { get; set; } = null!;
[JsonProperty("message")]
public string Message { get; set; } = null!;
[JsonProperty("status")]
public string Status { get; set; } = null!;
[JsonProperty("start_at")]
public DateTime? StartAt { get; set; } = null!;
[JsonProperty("end_at")]
public DateTime? EndAt { get; set; } = null!;
}
//using Coscine.Configuration; using AutoMapper;
//using Coscine.KpiGenerator.Utils; using Coscine.ApiClient;
//using Microsoft.Extensions.Logging; using Coscine.ApiClient.Core.Api;
//using Newtonsoft.Json; using Coscine.ApiClient.Core.Client;
//using NLog.Extensions.Logging; using Coscine.KpiGenerator.Models;
//using System.Net.Http.Headers; using Coscine.KpiGenerator.Models.ConfigurationModels;
//using System.Text; using Coscine.KpiGenerator.Utils;
//using static KPIGenerator.Utils.CommandLineOptions; using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System.Net.Http.Headers;
using System.Text;
using static KPIGenerator.Utils.CommandLineOptions;
//namespace Coscine.KpiGenerator.Reportings.System; namespace Coscine.KpiGenerator.Reportings.Resource;
//public class SystemReporting : Reporting<SystemReportingOptions> public class SystemReporting
//{ {
// private readonly ILogger<SystemReporting> _logger; private readonly IMapper _mapper;
private readonly KpiConfiguration _kpiConfiguration;
private readonly HttpClient _httpClient;
// private readonly ConsulConfiguration _configuration; public static string AdminToken { get; set; } = null!;
public AdminApi AdminApi { get; init; }
public ProjectApi ProjectApi { get; init; }
public SystemReportingOptions Options { get; private set; } = null!;
public string ReportingFileName { get; }
// public SystemReporting(SystemReportingOptions options) : base(options) public SystemReporting(IMapper mapper, IHttpClientFactory httpClientFactory, IOptionsMonitor<KpiConfiguration> kpiConfiguration)
// { {
// ReportingFileName = "system_status.json"; _mapper = mapper;
// _configuration = new ConsulConfiguration(); _kpiConfiguration = kpiConfiguration.CurrentValue;
// _logger = LoggerFactory.Create(builder => builder.AddNLog()).CreateLogger<SystemReporting>(); _httpClient = httpClientFactory.CreateClient("MaintenanceClient");
// }
// public override IEnumerable<ReportingFileObject> GenerateReportingAsync() ReportingFileName = _kpiConfiguration.SystemKpi.FileName;
// {
// Console.WriteLine($" - {GetType().Name}: Begin reporting generation");
// _logger.LogInformation("{Name}: Begin reporting generation", GetType().Name);
// var reportingFiles = new List<ReportingFileObject>();
// var returnObject = GetSystemStatus();
// // General File var configuration = new Configuration()
// reportingFiles.Add(new ReportingFileObject {
// { BasePath = "http://localhost:7206/coscine",
// Path = GetReportingPathGeneral(ReportingFileName), ApiKeyPrefix = { { "Authorization", "Bearer" } },
// Content = ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObject, Formatting.Indented)) ApiKey = { { "Authorization", ApiConfigurationUtil.GenerateAdminToken(ApiConfigurationUtil.RetrieveJwtConfiguration()) } },
// }); };
// Console.WriteLine($" - {GetType().Name}: \"{GetReportingPathGeneral(ReportingFileName)}\" generated successfully");
// _logger.LogInformation("{Name}: Generated successfully. {reportingFiles}", GetType().Name, reportingFiles);
// Console.WriteLine(); AdminApi = new AdminApi(configuration);
// return reportingFiles; ProjectApi = new ProjectApi(configuration);
// } }
// private ReturnObject GetSystemStatus() public async Task<bool> RunAsync(SystemReportingOptions reportingOptions)
// { {
// return new ReturnObject() Options = reportingOptions;
// { var reportingFiles = await GenerateReportingAsync();
// Banners = GetMaintenanceBannersAsync().Result return await GitLabUtils.PublishAsync(reportingFiles);
// // Extend here with other specific props in the future }
// };
// }
// private async Task<List<MaintenanceBannerObject>?> GetMaintenanceBannersAsync() public async Task<IEnumerable<ReportingFileObject>> GenerateReportingAsync()
// { {
// var httpClient = new HttpClient(); var reportingFiles = new List<ReportingFileObject>();
// var requestMessage = new HttpRequestMessage() var returnObject = new SystemReport()
// { {
// RequestUri = new Uri("https://noc-portal.rz.rwth-aachen.de/ticket/api/coscine/tickets"), Banners = await GetMaintenanceBannersAsync(),
// Method = HttpMethod.Get };
// };
// // Add Basic Authentication Headers // General File
// var authenticationString = $"{_configuration.GetStringAndWait("coscine/global/maintenance/user")}:{_configuration.GetStringAndWait("coscine/global/maintenance/password")}"; reportingFiles.Add(new ReportingFileObject
// var base64EncodedAuthenticationString = Convert.ToBase64String(Encoding.ASCII.GetBytes(authenticationString)); {
// requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", base64EncodedAuthenticationString); Path = Helpers.GetReportingPathGeneral(ReportingFileName),
Content = Helpers.ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObject, Formatting.Indented)),
});
// var result = await httpClient.SendAsync(requestMessage); // No per organization file
// var responseBody = JsonConvert.DeserializeObject<List<MaintenanceBannerObject>>(result.Content.ReadAsStringAsync().Result);
// if (responseBody is null)
// {
// return null;
// }
// return responseBody.OrderBy(e => e.StartAt).ToList();
// }
//}
return reportingFiles;
}
private async Task<List<MaintenanceReport>?> GetMaintenanceBannersAsync()
{
var requestMessage = new HttpRequestMessage()
{
RequestUri = new Uri(_kpiConfiguration.SystemKpi.Maintenance.Url),
Method = System.Net.Http.HttpMethod.Get
};
// Add Basic Authentication Headers
var authenticationString = $"{_kpiConfiguration.SystemKpi.Maintenance.Username}:{_kpiConfiguration.SystemKpi.Maintenance.Password}";
var base64EncodedAuthenticationString = Convert.ToBase64String(Encoding.ASCII.GetBytes(authenticationString));
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Basic", base64EncodedAuthenticationString);
var result = await _httpClient.SendAsync(requestMessage);
var responseBody = JsonConvert.DeserializeObject<List<MaintenanceReport>>(result.Content.ReadAsStringAsync().Result);
if (responseBody is null)
{
return null;
}
return responseBody.OrderBy(e => e.StartAt).ToList();
}
}
\ No newline at end of file
//using Coscine.Database.ReturnObjects;
//using Coscine.KpiGenerator.Utils;
//namespace Coscine.KpiGenerator.Reportings.User;
///// <summary>
///// Object containing the JSON structure for the reporting
///// </summary>
//public class ReturnObject
//{
// public List<RelatedProject> RelatedProjects { get; set; } = new();
// public List<Organization> Organizations { get; set; } = new();
// public List<Organization> Institutes { get; set; } = new();
// public List<DisciplineObject> Disciplines { get; set; } = new();
// public List<ExternalAuthenticatorsObject> LoginProviders { get; set; } = new();
// public DateTime? LatestActivity { get; set; } = null;
// public class RelatedProject
// {
// public Guid ProjectId { get; set; }
// public string Role { get; set; } = null!;
// }
//}
...@@ -4,7 +4,9 @@ using Coscine.ApiClient.Core.Api; ...@@ -4,7 +4,9 @@ using Coscine.ApiClient.Core.Api;
using Coscine.ApiClient.Core.Client; using Coscine.ApiClient.Core.Client;
using Coscine.ApiClient.Core.Model; using Coscine.ApiClient.Core.Model;
using Coscine.KpiGenerator.Models; using Coscine.KpiGenerator.Models;
using Coscine.KpiGenerator.Models.ConfigurationModels;
using Coscine.KpiGenerator.Utils; using Coscine.KpiGenerator.Utils;
using Microsoft.Extensions.Options;
using Newtonsoft.Json; using Newtonsoft.Json;
using static KPIGenerator.Utils.CommandLineOptions; using static KPIGenerator.Utils.CommandLineOptions;
...@@ -13,24 +15,26 @@ namespace Coscine.KpiGenerator.Reportings.Resource; ...@@ -13,24 +15,26 @@ namespace Coscine.KpiGenerator.Reportings.Resource;
public class UserReporting public class UserReporting
{ {
private readonly IMapper _mapper; private readonly IMapper _mapper;
private readonly KpiConfiguration _kpiConfiguration;
public static string AdminToken { get; set; } = null!; public static string AdminToken { get; set; } = null!;
public AdminApi AdminApi { get; init; } public AdminApi AdminApi { get; init; }
public ProjectApi ProjectApi { get; init; } public ProjectApi ProjectApi { get; init; }
public UserReportingOptions Options { get; private set; } = null!; public UserReportingOptions Options { get; private set; } = null!;
public KpiConfiguration KpiConfiguration { get; } = new();
public string ReportingFileName { get; } public string ReportingFileName { get; }
public UserReporting(IMapper mapper) public UserReporting(IMapper mapper, IOptionsMonitor<KpiConfiguration> kpiConfiguration)
{ {
_mapper = mapper; _mapper = mapper;
_kpiConfiguration = kpiConfiguration.CurrentValue;
ReportingFileName = "users.json"; ReportingFileName = _kpiConfiguration.UserKpi.FileName;
var configuration = new Configuration() var configuration = new Configuration()
{ {
BasePath = "http://localhost:7206/coscine", BasePath = "http://localhost:7206/coscine",
ApiKeyPrefix = { { "Authorization", "Bearer" } }, ApiKeyPrefix = { { "Authorization", "Bearer" } },
ApiKey = { { "Authorization", JwtUtil.GenerateAdminToken(JwtUtil.RetrieveJwtConfiguration()) } }, ApiKey = { { "Authorization", ApiConfigurationUtil.GenerateAdminToken(ApiConfigurationUtil.RetrieveJwtConfiguration()) } },
}; };
AdminApi = new AdminApi(configuration); AdminApi = new AdminApi(configuration);
......
//using Coscine.Database.Models;
//using Coscine.Database.ReturnObjects;
//using Coscine.KpiGenerator.Utils;
//using Coscine.Metadata;
//using Microsoft.Extensions.Logging;
//using Newtonsoft.Json;
//using NLog.Extensions.Logging;
//using VDS.RDF.Query;
//using static KPIGenerator.Utils.CommandLineOptions;
//namespace Coscine.KpiGenerator.Reportings.User;
//public class UserReporting : Reporting<UserReportingOptions>
//{
// private readonly ILogger<UserReporting> _logger;
// private readonly ExternalIdModel _externalIdModel;
// private readonly ProjectRoleModel _projectRoleModel;
// private readonly ProjectModel _projectModel;
// private readonly RoleModel _roleModel;
// private readonly UserModel _userModel;
// private readonly LogModel _logModel;
// public UserReporting(UserReportingOptions options) : base(options)
// {
// ReportingFileName = "users.json";
// _externalIdModel = new ExternalIdModel();
// _projectRoleModel = new ProjectRoleModel();
// _projectModel = new ProjectModel();
// _roleModel = new RoleModel();
// _userModel = new UserModel();
// _logModel = new LogModel();
// _logger = LoggerFactory.Create(builder => builder.AddNLog()).CreateLogger<UserReporting>();
// }
// public override IEnumerable<ReportingFileObject> GenerateReportingAsync()
// {
// Console.WriteLine($" - {GetType().Name}: Begin reporting generation");
// _logger.LogInformation("{Name}: Begin reporting generation", GetType().Name);
// var users = _userModel.GetAllWhere((user) => user.Tosaccepteds.Any());
// var reportingFiles = new List<ReportingFileObject>();
// var returnObjects = Generate(users);
// // General File
// reportingFiles.Add(new ReportingFileObject
// {
// Path = GetReportingPathGeneral(ReportingFileName),
// Content = ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObjects, Formatting.Indented))
// });
// Console.WriteLine($" - {GetType().Name}: \"{GetReportingPathGeneral(ReportingFileName)}\" generated successfully");
// _logger.LogInformation("{Name}: Generated successfully. {reportingFiles}", GetType().Name, reportingFiles);
// // Per Organization
// reportingFiles.AddRange(GeneratePerOrganization(returnObjects));
// Console.WriteLine();
// return reportingFiles;
// }
// private List<ReturnObject> Generate(IEnumerable<Database.DataModel.User> users)
// {
// var returnObjects = new List<ReturnObject>();
// foreach (var user in users)
// {
// var userReturnObject = _userModel.CreateReturnObjectFromDatabaseObject(user);
// var (organizations, institutes) = GetUserAffiliation(userReturnObject);
// var userReportEntry = new ReturnObject
// {
// RelatedProjects = GetRelatedProjects(user.Id),
// Organizations = organizations,
// Institutes = institutes,
// Disciplines = userReturnObject.Disciplines.ToList(),
// LoginProviders = userReturnObject.ExternalAuthenticators.ToList(),
// LatestActivity = GetLatestActivity(user.Id)
// };
// returnObjects.Add(userReportEntry);
// }
// return returnObjects;
// }
// private IEnumerable<ReportingFileObject> GeneratePerOrganization(List<ReturnObject> returnObjects)
// {
// var reportingFilesPerOrganization = new List<ReportingFileObject>();
// var organizationsFromUsers = GetTopLevelOrganizationsFromEntries(returnObjects.SelectMany(ro => ro.Organizations));
// foreach (var entry in organizationsFromUsers)
// {
// List<ReturnObject> returnObjectsForOrganization;
// // Handling of "Other" organization
// if (entry.RorUrl.Equals("https://ror.org/") || entry.RorUrl.Equals("http://ror.org/"))
// {
// returnObjectsForOrganization = returnObjects.Where(ro => ro.Organizations.Select(o => o.RorUrl).Any(e => e.Equals("https://ror.org/") || e.Equals("http://ror.org/"))).ToList();
// entry.RorUrl = _otherOrganization.RorUrl;
// }
// else
// {
// returnObjectsForOrganization = returnObjects.Where(ro => ro.Organizations.Select(o => o.RorUrl).Any(e => e.Contains(entry.RorUrl))).ToList();
// }
// var reportingFile = new ReportingFileObject
// {
// Path = GetReportingPathOrganization(entry.RorUrl, ReportingFileName),
// Content = ConvertStringContentsToStream(JsonConvert.SerializeObject(returnObjectsForOrganization, Formatting.Indented))
// };
// reportingFilesPerOrganization.Add(reportingFile);
// Console.WriteLine($" - {GetType().Name}: \"{GetReportingPathOrganization(entry.RorUrl, ReportingFileName)}\" generated successfully");
// _logger.LogInformation("{Name}: Generated successfully {ReportingFile}.", GetType().Name, reportingFile);
// }
// return reportingFilesPerOrganization;
// }
// private List<ReturnObject.RelatedProject> GetRelatedProjects(Guid id)
// {
// var result = new List<ReturnObject.RelatedProject>();
// var projectRoles = _projectRoleModel.GetAllWhere(role => role.UserId.Equals(id));
// if (projectRoles.Any())
// {
// foreach (var projectRole in projectRoles)
// {
// if (_projectModel.GetById(projectRole.ProjectId) is not null) // null if project has been deleted
// {
// result.Add(new ReturnObject.RelatedProject
// {
// ProjectId = projectRole.ProjectId,
// Role = _roleModel.GetById(projectRole.RoleId).DisplayName
// });
// }
// }
// }
// return result;
// }
// private (List<Organization> organizations, List<Organization> institutes) GetUserAffiliation(UserObject user)
// {
// var affiliations = new List<Organization>();
// // Bellow code taken from Organizations API
// var externalIds = _externalIdModel.GetAllWhere((externalId) => externalId.UserId == user.Id);
// var externalIdList = new List<string>();
// foreach (var externalId in externalIds)
// {
// externalIdList.Add(externalId.ExternalId1);
// }
// var externalOrganizations = externalIds.Select((externalId) => externalId.Organization);
// var triples = Helpers.WrapRequest(() => RdfStoreConnector.GetTriples(null, null, null, 1, externalIdList).ToList());
// foreach (var externalOrganization in externalOrganizations)
// {
// triples.AddRange(Helpers.WrapRequest(() => RdfStoreConnector.GetOrganizationByEntityId(externalOrganization)));
// }
// triples = triples.Distinct().ToList();
// foreach (var triple in triples)
// {
// affiliations.Add(new()
// {
// Name = triple.Object.ToString(),
// RorUrl = triple.Subject.ToString()
// });
// }
// // Clean up
// affiliations = CleanUpOther(affiliations);
// // Split affiliations to "organizations" and "institutes"
// var organizations = affiliations.Where(a => !a.RorUrl.Contains('#')).ToList();
// var institutes = affiliations.Where(a => a.RorUrl.Contains('#')).ToList();
// // Find the RoR of the organization based on its "rdfs:label".
// var orgFromSqlDb = TryGetOrganizationByLabel(user.Organization);
// if (orgFromSqlDb is not null)
// organizations.Add(orgFromSqlDb);
// // Find the RoR of the institute based on its "rdfs:label".
// var instFromSqlDb = TryGetOrganizationByLabel(user.Institute);
// if (instFromSqlDb is not null)
// institutes.Add(instFromSqlDb);
// return (
// organizations.DistinctBy(o => o.RorUrl).ToList(),
// institutes.DistinctBy(i => i.RorUrl).ToList()
// );
// }
// private Organization? TryGetOrganizationByLabel(string rdfsLabel)
// {
// if (string.IsNullOrWhiteSpace(rdfsLabel))
// {
// return null;
// }
// var _queryString = new SparqlParameterizedString()
// {
// CommandText = $@"
// PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
// SELECT DISTINCT ?ror
// WHERE {{
// VALUES ?name
// {{
// '{rdfsLabel}'
// }} .
// ?ror rdfs:label ?name .
// FILTER( CONTAINS( STR(?ror), 'ror.org' ) )
// }}
// "
// };
// using var results = Helpers.WrapRequest(() => RdfStoreConnector.QueryEndpoint.QueryWithResultSet(_queryString.ToString()));
// if (!results.IsEmpty)
// {
// var ror = results.Select(x => x.Value("ror").ToString()); // Get the value for ?ror
// if (ror.Any())
// {
// return FetchOrganizationByRor(ror.ToList()[0]);
// }
// }
// return null;
// }
// private DateTime? GetLatestActivity(Guid id)
// {
// var today = DateTime.Today;
// var latestLog = _logModel
// .GetAllWhere(l =>
// l.LogLevel.Equals("Analytics")
// && l.UserId.Equals(id)
// && l.ServerTimestamp < today)
// .OrderByDescending(a => a.ServerTimestamp)
// .FirstOrDefault();
// if (latestLog is not null)
// {
// return latestLog.ServerTimestamp;
// }
// else return null;
// }
// private List<Organization> CleanUpOther(List<Organization> affiliations)
// {
// // Special case until `https://ror.org/%20https://ror.org/` is moved to `https://ror.org/_other`
// if (affiliations.Any(o => o.RorUrl.Equals("https://ror.org/%20https://ror.org/") || o.RorUrl.Equals("https://ror.org/") || o.RorUrl.Equals("http://ror.org/")))
// {
// var cleanedUpAffiliations = affiliations;
// foreach (var entry in cleanedUpAffiliations)
// {
// if (entry.RorUrl.Equals("https://ror.org/%20https://ror.org/") || entry.RorUrl.Equals("https://ror.org/") || entry.RorUrl.Equals("http://ror.org/"))
// {
// cleanedUpAffiliations[cleanedUpAffiliations.IndexOf(entry)] = new()
// {
// Name = _otherOrganization.Name,
// RorUrl = _otherOrganization.RorUrl
// };
// }
// }
// return cleanedUpAffiliations;
// }
// return affiliations;
// }
//}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment