88 lines
3.5 KiB
C#
88 lines
3.5 KiB
C#
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));
|
|
}
|
|
}
|
|
}
|