Skip to content
Snippets Groups Projects
Commit f16cfdb2 authored by L. Ellenbeck's avatar L. Ellenbeck
Browse files

New: added analytics logger (coscine/issues#2001)

parent 0c93fe15
Branches
Tags
2 merge requests!26Release: Sprint/2022 05 :robot:,!24New: added analytics logger (coscine/issues#2001)
......@@ -6,14 +6,15 @@ using Coscine.ApiCommons;
using Coscine.Configuration;
using Coscine.Database.DataModel;
using Coscine.Database.Models;
using Coscine.Logging;
using Coscine.Metadata;
using Coscine.ResourceLoader;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
namespace Coscine.Api.Admin.Controllers
{
/// <summary>
......@@ -33,16 +34,17 @@ namespace Coscine.Api.Admin.Controllers
private readonly string _adminRole;
private readonly ResourceModel _resourceModel;
private readonly ProjectModel _projectModel;
private readonly ProjectRoleModel _projectRoleModel;
private readonly ProjectQuotaModel _projectQuotaModel;
private readonly ResourceTypeModel _resourceTypeModel;
private readonly Emitter _emitter;
private readonly float _oneGb = 1024 * 1024 * 1024;
private readonly CoscineLogger _coscineLogger;
/// <summary>
/// Default Constructor.
/// </summary>
public AdminController()
public AdminController(ILogger<AdminController> logger)
{
_authenticator = new Authenticator(this, Program.Configuration);
_configuration = Program.Configuration;
......@@ -54,10 +56,11 @@ namespace Coscine.Api.Admin.Controllers
_adminRole = "supportAdmin";
_resourceModel = new ResourceModel();
_projectModel = new ProjectModel();
_projectRoleModel = new ProjectRoleModel();
_projectQuotaModel = new ProjectQuotaModel();
_resourceTypeModel = new ResourceTypeModel();
_emitter = new Emitter(Program.Configuration);
_coscineLogger = new CoscineLogger(logger);
}
/// <summary>
......@@ -82,12 +85,7 @@ namespace Coscine.Api.Admin.Controllers
// Get the role based on the blank node and compare it to the requested role
var roleTriples = graph.GetTriplesWithSubjectPredicate(userSubject, graph.CreateUriNode(new Uri(_roleUrl)));
if (roleTriples?.FirstOrDefault()?.Object.ToString() != $"{_roleUrlPrefix}{role}")
{
return false;
}
return true;
return (roleTriples?.FirstOrDefault()?.Object.ToString()) == _roleUrlPrefix + role;
}
/// <summary>
......@@ -129,10 +127,10 @@ namespace Coscine.Api.Admin.Controllers
}
/// <summary>
/// Find the project related to the the projectString(Guid or slug)
/// Find the project related to the projectString(GUID or slug)
/// </summary>
/// <param name="projectString">Either the id (guid) of the project or the slug.</param>
/// <returns>Json list of all quotas.</returns>
/// <param name="projectString">Either the id (GUID) of the project or the slug.</param>
/// <returns>JSON list of all quotas.</returns>
[HttpGet("[controller]/{projectString}")]
public ActionResult<ProjectObject> GetProject(string projectString)
{
......@@ -147,7 +145,8 @@ namespace Coscine.Api.Admin.Controllers
if (Guid.TryParse(projectString, out Guid guid))
{
project = _projectModel.GetById(guid);
} else
}
else
{
project = _projectModel.GetBySlug(projectString);
}
......@@ -164,7 +163,8 @@ namespace Coscine.Api.Admin.Controllers
GUID = project.Id,
Name = project.ProjectName,
ShortName = project.DisplayName,
Quotas = quotas.Select(x => new ProjectQuotaObject {
Quotas = quotas.Select(x => new ProjectQuotaObject
{
QuotaId = x.RelationId,
ResourceType = _resourceTypeModel.GetById(x.ResourceTypeId).DisplayName,
Quota = x.Quota,
......@@ -178,13 +178,13 @@ namespace Coscine.Api.Admin.Controllers
/// <summary>
/// Update the project quota
/// </summary>
/// <param name="updateQuotaParameter">Json object for updatin quota.</param>
/// <returns>NoContent (204) ond success.</returns>
/// <param name="updateQuotaParameter">JSOM object for updating quota.</param>
/// <returns>NoContent (204) on success.</returns>
[HttpPost("[controller]/")]
public IActionResult UpdateQuota([FromBody] UpdateQuotaParameterObject updateQuotaParameter)
{
var user = _authenticator.GetUserId();
if (!HasRole(user, _adminRole))
var user = _authenticator.GetUser();
if (!HasRole(user.Id.ToString(), _adminRole))
{
return Unauthorized($@"User has not the role: ""{_adminRole}"".");
}
......@@ -203,7 +203,38 @@ namespace Coscine.Api.Admin.Controllers
_emitter.EmitQuotaChanged(new AdminEventArgs(Program.Configuration) { ProjectId = projectQuota.ProjectId });
if (Request.Query != null && Request.Query["noanalyticslog"] != "true")
{
var project = _projectModel.GetById(projectQuota.ProjectId);
LogAnalyticsAdminProjectQuotaChange(project, user);
}
return NoContent();
}
private void LogAnalyticsAdminProjectQuotaChange(Project project, User user)
{
var quotas = _projectQuotaModel.GetAllWhere(x => x.ProjectId == project.Id);
var quotaObjects = quotas.Select(x => new ProjectQuotaObject
{
QuotaId = x.RelationId,
ResourceType = _resourceTypeModel.GetById(x.ResourceTypeId).DisplayName,
Quota = x.Quota,
MaxQuota = x.MaxQuota,
Used = x.Quota,
Allocated = CalculateAllocatedForAll(_resourceTypeModel.GetById(x.ResourceTypeId), project.Id)
}).ToList();
_coscineLogger.AnalyticsLog(
new AnalyticsLogObject
{
Type = "Action",
Operation = "Admin Project Quota Change",
RoleId = _projectRoleModel.GetGetUserRoleForProject(project.Id, user.Id).ToString(),
UserId = user.Id.ToString(),
ProjectId = project.Id.ToString(),
QuotaSize = quotaObjects.Select(x => $"{x.ResourceType}: {x.Used}/{x.Allocated}").ToList()
});
}
}
}
\ 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