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)); } } }