From b0675a3846401925d6559c065f778ec465c80a58 Mon Sep 17 00:00:00 2001 From: Mikhail Trubnikov Date: Fri, 11 Oct 2024 11:06:05 +1000 Subject: [PATCH] =?UTF-8?q?feat(api):=20StatTemplateAutoControlController?= =?UTF-8?q?=20=D1=81=D1=82=D0=B0=D1=82=D0=B8=D1=81=D1=82=D0=B8=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=B0=D0=B2=D1=82=D0=BE-=D0=BA=D0=BE=D0=BD=D1=82=D1=80?= =?UTF-8?q?=D0=BE=D0=BB=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PARR.API/Contracts/V1/ApiRoutes.cs | 5 + .../StatTemplateAutoControlResponse.cs | 10 ++ .../StatTemplateAutoControlController.cs | 129 ++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 PARR.API/Contracts/V1/Responses/Statistics/StatTemplateAutoControlResponse.cs create mode 100644 PARR.API/Controllers/V1/Statistics/StatTemplateAutoControlController.cs diff --git a/PARR.API/Contracts/V1/ApiRoutes.cs b/PARR.API/Contracts/V1/ApiRoutes.cs index a225fdb6..5ca8e0b7 100644 --- a/PARR.API/Contracts/V1/ApiRoutes.cs +++ b/PARR.API/Contracts/V1/ApiRoutes.cs @@ -220,6 +220,11 @@ public const string GetForPeriod = BaseStat + "/templates/period"; } + public static class StatTemplateAutoControl + { + public const string GetForPeriod = BaseStat + "/template-auto-controls/"; + } + public static class StatHost { public const string Get = BaseStat + "/hosts/"; diff --git a/PARR.API/Contracts/V1/Responses/Statistics/StatTemplateAutoControlResponse.cs b/PARR.API/Contracts/V1/Responses/Statistics/StatTemplateAutoControlResponse.cs new file mode 100644 index 00000000..d56f67b0 --- /dev/null +++ b/PARR.API/Contracts/V1/Responses/Statistics/StatTemplateAutoControlResponse.cs @@ -0,0 +1,10 @@ +namespace PARR.API.Contracts.V1.Responses.Statistics +{ + public class StatTemplateAutoControlResponse + { + public DateOnly Date { get; set; } + public int Сreations { get; set; } + public int Activations { get; set; } + public int Deactivations { get; set; } + } +} diff --git a/PARR.API/Controllers/V1/Statistics/StatTemplateAutoControlController.cs b/PARR.API/Controllers/V1/Statistics/StatTemplateAutoControlController.cs new file mode 100644 index 00000000..90a5d2d3 --- /dev/null +++ b/PARR.API/Controllers/V1/Statistics/StatTemplateAutoControlController.cs @@ -0,0 +1,129 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using PARR.API.Contracts.V1; +using PARR.API.Contracts.V1.Requests.BaseRequests; +using PARR.API.Contracts.V1.Responses.Base; +using PARR.API.Contracts.V1.Responses.Statistics; +using PARR.API.Controllers.V1.Base; +using PARR.BLL.Services.Interfaces; +using PARR.Constants; +using PARR.DAL.Services.Interfaces; + +namespace PARR.API.Controllers.V1.Statistics +{ + public class StatTemplateAutoControlController : BaseApiController + { + private readonly ITemplateService templateService; + private readonly ITemplateHistoryService templateHistoryService; + private readonly ICalendarService calendarService; + + public StatTemplateAutoControlController( + ITemplateService templateService, + ITemplateHistoryService templateHistoryService, + ICalendarService calendarService + ) + { + this.templateService = templateService; + this.templateHistoryService = templateHistoryService; + this.calendarService = calendarService; + } + + + /// + /// Получить статистику авто-контроля по РР + /// + /// + /// + /// + [HttpGet(ApiRoutes.StatTemplateAutoControl.GetForPeriod)] + public async Task GetForPeriod([FromQuery] TimeZoneOffsetClient timeZoneQuery, [FromQuery] int startPeriodDays = 3) + { + var endPeriod = calendarService.GetDateWithTimeZoneOffset(DateTimeOffset.Now, timeZoneQuery.TimeZoneOffsetHours); + var startPeriod = endPeriod.AddDays(-startPeriodDays); + + var templates = await templateService.Get() + .Where(t => + t.InitiatorParrComponentId == Constants.ParrComponentsEnum.JobAutoControl + && + (( + // измененные + t.DateModified.HasValue + && t.DateModified.Value.DateTime.AddHours(timeZoneQuery.TimeZoneOffsetHours).Date >= startPeriod.Date + && t.DateModified.Value.DateTime.AddHours(timeZoneQuery.TimeZoneOffsetHours).Date <= endPeriod.Date + ) || ( + // созданные + t.DateCreated.DateTime.AddHours(timeZoneQuery.TimeZoneOffsetHours).Date >= startPeriod.Date + && t.DateCreated.DateTime.AddHours(timeZoneQuery.TimeZoneOffsetHours).Date <= endPeriod.Date + )) + ) + .Select(t => new QueryResult + { + //TemplateId = t.Id, + DateCreated = t.DateCreated, + DateModified = t.DateModified, + InitiatorComment = t.InitiatorComment, + //InitiatorParrComponentId = t.InitiatorParrComponentId + }) + .ToListAsync(); + + var history = await templateHistoryService.Get() + .Where(t => + t.InitiatorParrComponentId == Constants.ParrComponentsEnum.JobAutoControl + && t.DateModified.HasValue + && t.DateModified.Value.DateTime.AddHours(timeZoneQuery.TimeZoneOffsetHours).Date >= startPeriod.Date + && t.DateModified.Value.DateTime.AddHours(timeZoneQuery.TimeZoneOffsetHours).Date <= endPeriod.Date + ) + .Select(t => new QueryResult + { + //TemplateId = t.ParentId, + //DateCreated = t.DateCreated, + DateModified = t.DateModified, + InitiatorComment = t.InitiatorComment, + //InitiatorParrComponentId = t.InitiatorParrComponentId + }) + .ToListAsync(); + + var objs = new List(); + objs.AddRange(templates); + objs.AddRange(history); + + // создано + var creations = objs.Where(t => t.DateCreated.HasValue).GroupBy(t => t.DateCreated!.Value.AddHours(timeZoneQuery.TimeZoneOffsetHours).Date) + .Select(t => new { Date = t.Key, Created = t.Count() }); + + // активаций (сколько раз, а не сколько объектов) + var activations = objs.Where(t => t.DateModified.HasValue && t.InitiatorComment != null && t.InitiatorComment.Contains(TemplateActivatorActionsEnum.ActivateAllForAutoControl.ToString())) + .GroupBy(t => t.DateModified!.Value.AddHours(timeZoneQuery.TimeZoneOffsetHours).Date) + .Select(t => new { Date = t.Key, Actiovations = t.Count() }); + + // деактиваций (сколько раз, а не сколько объектов) + var deactivations = objs.Where(t => t.DateModified.HasValue && t.InitiatorComment != null && t.InitiatorComment.Contains(TemplateActivatorActionsEnum.DeactivateAllForAutoControl.ToString())) + .GroupBy(t => t.DateModified!.Value.AddHours(timeZoneQuery.TimeZoneOffsetHours).Date) + .Select(t => new { Date = t.Key, Deactivations = t.Count() }); + + var daysList = calendarService.GetDatesForPeriod(startPeriod, endPeriod); + var response = new List(); + + daysList.ForEach(date => response.Add(new StatTemplateAutoControlResponse + { + Date = date, + Activations = activations.FirstOrDefault(t => DateOnly.FromDateTime(t.Date) == date)?.Actiovations ?? 0, + Сreations = creations.FirstOrDefault(t => DateOnly.FromDateTime(t.Date) == date)?.Created ?? 0, + Deactivations = deactivations.FirstOrDefault(t => DateOnly.FromDateTime(t.Date) == date)?.Deactivations ?? 0, + })); + + response = response.OrderBy(t => t.Date).ToList(); + + return Ok(new Response>(response, true)); + } + + + class QueryResult + { + //public Guid TemplateId { get; set; } + public DateTimeOffset? DateCreated { get; set; } + public DateTimeOffset? DateModified { get; set; } + public string? InitiatorComment { get; set; } + } + } +}