feat(api): статистика распределения регламентных работ

This commit is contained in:
Mikhail Trubnikov
2024-07-29 10:25:57 +10:00
parent 89de232cb4
commit c8a524b28f
3 changed files with 108 additions and 0 deletions

View File

@@ -237,6 +237,10 @@
public const string Get = BaseStat + "/auth";
}
public static class StatTemplateDistribution
{
public const string Get = BaseStat + "/template-distribution/{applicationInWorkId}";
}
#endregion

View File

@@ -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<StatTemplateDistributorDateStat>? Stat { get; set; }
}
public class StatTemplateDistributorDateStat
{
public DateTimeOffset Date { get; set; }
public int Count { get; set; }
}
}

View File

@@ -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
{
/// <summary>
/// Статистика распределения шаблонов
/// </summary>
[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;
}
/// <summary>
/// Статистика распределения шаблонов по JobId (ApplicationInWorkId)
/// </summary>
/// <param name="applicationInWorkId"></param>
/// <returns></returns>
[HttpGet(ApiRoutes.StatTemplateDistribution.Get)]
public async Task<IActionResult> 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<WorkGroupResponse>(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<List<StatTemplateDistributorResponse>>(response, true));
}
}
}