diff --git a/PARR.API/Contracts/V1/ApiRoutes.cs b/PARR.API/Contracts/V1/ApiRoutes.cs index c0fec12e..864ac14b 100644 --- a/PARR.API/Contracts/V1/ApiRoutes.cs +++ b/PARR.API/Contracts/V1/ApiRoutes.cs @@ -237,6 +237,10 @@ public const string Get = BaseStat + "/auth"; } + public static class StatTemplateDistribution + { + public const string Get = BaseStat + "/template-distribution/{applicationInWorkId}"; + } #endregion diff --git a/PARR.API/Contracts/V1/Responses/Statistics/StatTemplateDistributorResponse.cs b/PARR.API/Contracts/V1/Responses/Statistics/StatTemplateDistributorResponse.cs new file mode 100644 index 00000000..22f126be --- /dev/null +++ b/PARR.API/Contracts/V1/Responses/Statistics/StatTemplateDistributorResponse.cs @@ -0,0 +1,17 @@ +namespace PARR.API.Contracts.V1.Responses.Statistics +{ + public class StatTemplateDistributorResponse + { + public WorkGroupResponse? WorkGroup { get; set; } + + public int CountTemplates { get; set; } + + public List? Stat { get; set; } + } + + public class StatTemplateDistributorDateStat + { + public DateTimeOffset Date { get; set; } + public int Count { get; set; } + } +} diff --git a/PARR.API/Controllers/V1/Statistics/StatTemplateDistributionController.cs b/PARR.API/Controllers/V1/Statistics/StatTemplateDistributionController.cs new file mode 100644 index 00000000..e3673e37 --- /dev/null +++ b/PARR.API/Controllers/V1/Statistics/StatTemplateDistributionController.cs @@ -0,0 +1,87 @@ +using AutoMapper; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using PARR.API.Contracts.V1; +using PARR.API.Contracts.V1.Responses; +using PARR.API.Contracts.V1.Responses.Base; +using PARR.API.Contracts.V1.Responses.Statistics; +using PARR.API.Controllers.V1.Base; +using PARR.Constants; +using PARR.DAL.Services.Interfaces; + +namespace PARR.API.Controllers.V1.Statistics +{ + /// + /// Статистика распределения шаблонов + /// + [Authorize(Roles = ParrRoles.Administrator.Role)] + public class StatTemplateDistributionController : BaseApiController + { + private readonly ITemplateService templateService; + private readonly IWorkGroupService workGroupService; + private readonly IMapper mapper; + + public StatTemplateDistributionController( + ITemplateService templateService, + IWorkGroupService workGroupService, + IMapper mapper + ) + { + this.templateService = templateService; + this.workGroupService = workGroupService; + this.mapper = mapper; + } + + + /// + /// Статистика распределения шаблонов по JobId (ApplicationInWorkId) + /// + /// + /// + [HttpGet(ApiRoutes.StatTemplateDistribution.Get)] + public async Task Get([FromRoute] Guid applicationInWorkId) + { + // f87dbe9f-cabf-4108-9c93-12068d4b48a5 + + var query = templateService.Get() + .Include(t => t.Host) + .Where(t => t.ApplicationInWorkId == applicationInWorkId) + .GroupBy(t => t.Host!.WorkGroupId) + .Select(x => new + { + WorkGroupId = x.Key, + CountTemplates = x.Count(), + GroupingByDate = + x.GroupBy(d => d.NextRun).OrderBy(d => d.Key).Select(d => + new + { + Date = d.Key, + Count = d.Count() + } + ) + + }); + + var statResult = await query.ToListAsync(); + + var workGroups = await workGroupService.Get() + .Include(t => t.ResponseArea) + .Where(t => statResult.Select(x => x.WorkGroupId).Any(w => w == t.Id)) + .ToListAsync(); + + + var response = statResult.Select(stat => new StatTemplateDistributorResponse + { + WorkGroup = mapper.Map(workGroups.FirstOrDefault(x => x.Id == stat.WorkGroupId)), + CountTemplates = stat.CountTemplates, + Stat = stat.GroupingByDate.Select(x => new StatTemplateDistributorDateStat { Count = x.Count, Date = x.Date }).ToList() + }) + .OrderBy(t => t.WorkGroup?.Name) + .ToList(); + + + return Ok(new Response>(response, true)); + } + } +}