From 0b6bfdfbc6212491424c6cb0f0d2d2d3bc1f3872 Mon Sep 17 00:00:00 2001 From: Mikhail Trubnikov Date: Mon, 22 Jul 2024 16:59:15 +1000 Subject: [PATCH] =?UTF-8?q?feat(api):=20StatRobotStateController=20=D0=BE?= =?UTF-8?q?=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=20=D1=80=D0=B5=D1=81?= =?UTF-8?q?=D0=BF=D0=BE=D0=BD=D1=81=20=D1=81=D1=82=D0=B0=D1=82=D0=B8=D1=81?= =?UTF-8?q?=D1=82=D0=B8=D0=BA=D0=B8=20=D0=B4=D0=BE=D1=81=D1=82=D1=83=D0=BF?= =?UTF-8?q?=D0=BD=D0=BE=D1=81=D1=82=D0=B8=20=D1=80=D0=BE=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D0=B2.=20=D0=A3=D1=87=D1=82=D0=B5=D0=BD=D0=B0=20=D0=B2?= =?UTF-8?q?=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=D1=81=D1=82=D1=8C=20?= =?UTF-8?q?=D0=BD=D0=B5=D1=81=D0=BA=D0=BE=D0=BB=D1=8C=D0=BA=D0=B8=D1=85=20?= =?UTF-8?q?=D1=80=D0=BE=D0=B1=D0=BE=D1=82=D0=BE=D0=B2=20=D0=BD=D0=B0=20?= =?UTF-8?q?=D1=80=D0=B0=D0=B7=D0=BD=D1=8B=D1=85=20=D1=81=D0=B5=D1=80=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D0=B0=D1=85.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PARR.AIHITSyncer/Services/SyncerService.cs | 2 +- .../Statistics/StatRobotStateResponse.cs | 11 +++- .../V1/Statistics/StatRobotStateController.cs | 54 +++++++++++++++---- 3 files changed, 55 insertions(+), 12 deletions(-) diff --git a/PARR.AIHITSyncer/Services/SyncerService.cs b/PARR.AIHITSyncer/Services/SyncerService.cs index 2012050d..4a92cb20 100644 --- a/PARR.AIHITSyncer/Services/SyncerService.cs +++ b/PARR.AIHITSyncer/Services/SyncerService.cs @@ -393,7 +393,7 @@ namespace PARR.AIHITSyncer.Services if (!await appInHostService.CommitAsync()) logger.LogError($"Не удалось обновить ApplicationInHost для Host: {host.Ek}({host.IP})"); - logger.LogDebug($"Host: {host.Ek}({host.IP} конец синхронизации приложений"); + logger.LogDebug($"Host: {host.Ek}({host.IP}) конец синхронизации приложений"); } diff --git a/PARR.API/Contracts/V1/Responses/Statistics/StatRobotStateResponse.cs b/PARR.API/Contracts/V1/Responses/Statistics/StatRobotStateResponse.cs index 5735cf55..9e2b891b 100644 --- a/PARR.API/Contracts/V1/Responses/Statistics/StatRobotStateResponse.cs +++ b/PARR.API/Contracts/V1/Responses/Statistics/StatRobotStateResponse.cs @@ -1,11 +1,18 @@ namespace PARR.API.Contracts.V1.Responses.Statistics { public class StatRobotStateResponse + { + public string? RobotIp { get; set; } + + public UserBaseResponse? User { get; set; } + + public List? Data { get; set; } + } + + public class StatRobotStateData { public DateTimeOffset Date { get; set; } public int IsUp { get; set; } - - public string? Ip { get; set; } } } diff --git a/PARR.API/Controllers/V1/Statistics/StatRobotStateController.cs b/PARR.API/Controllers/V1/Statistics/StatRobotStateController.cs index 54b0a469..61e7f722 100644 --- a/PARR.API/Controllers/V1/Statistics/StatRobotStateController.cs +++ b/PARR.API/Controllers/V1/Statistics/StatRobotStateController.cs @@ -1,11 +1,14 @@ -using FluentValidation; +using AutoMapper; +using FluentValidation; using InfluxDB.Client.Api.Domain; using InfluxDB.Client.Writes; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using PARR.API.Contracts.V1; using PARR.API.Contracts.V1.Requests; using PARR.API.Contracts.V1.Requests.Queries; +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; @@ -13,6 +16,7 @@ using PARR.API.Services.Interfaces; using PARR.API.Settings; using PARR.Constants; using PARR.DAL.InfluxDbServices; +using PARR.DAL.Services.Interfaces; using PARR.DAL.Settings; namespace PARR.API.Controllers.V1.Statistics @@ -28,13 +32,17 @@ namespace PARR.API.Controllers.V1.Statistics private readonly InfluxDbSettings influxDbSettings; private readonly MonitoringSettings monitoringSettings; private readonly IClientService clientService; + private readonly IUserService userService; + private readonly IMapper mapper; public StatRobotStateController( IInfluxDbService influxDbService, IValidator validator, InfluxDbSettings influxDbSettings, MonitoringSettings monitoringSettings, - IClientService clientService + IClientService clientService, + IUserService userService, + IMapper mapper ) { this.influxDbService = influxDbService; @@ -42,6 +50,8 @@ namespace PARR.API.Controllers.V1.Statistics this.influxDbSettings = influxDbSettings; this.monitoringSettings = monitoringSettings; this.clientService = clientService; + this.userService = userService; + this.mapper = mapper; } @@ -82,7 +92,7 @@ namespace PARR.API.Controllers.V1.Statistics var tables = await handler.QueryAsync(query, influxDbSettings.Organization); return tables.SelectMany(table => - table.Records.Select(record => new StatRobotStateResponse + table.Records.Select(record => new InfuxRowData { Date = (DateTimeOffset)record.GetTimeInDateTime(), IsUp = int.Parse(record.GetValue()?.ToString() ?? "0"), @@ -94,10 +104,26 @@ namespace PARR.API.Controllers.V1.Statistics if (result == null) return BadRequest(new Response(false, new List { new ErrorModel { Message = $"Ошибка при получении статистики" } })); - var listWithInaccessTime = CalcInaccessTime(result.ToList(), inaccessibilityTime); + var groupingByRobotIp = result.GroupBy(t => t.Ip); + + var response = new List(); + + foreach (var robotItem in groupingByRobotIp) + { + var user = await userService.Get().FirstOrDefaultAsync(t => t.Ip == robotItem.Key); + + response.Add(new StatRobotStateResponse + { + RobotIp = robotItem.Key, + User = mapper.Map(user), + Data = CalcInaccessTime(robotItem.Select(t => new StatRobotStateData { Date = t.Date, IsUp = t.IsUp }).ToList(), inaccessibilityTime) + }); + } + + //var listWithInaccessTime = CalcInaccessTime(result.ToList(), inaccessibilityTime); - return Ok(new Response>(listWithInaccessTime, true)); + return Ok(new Response>(response.OrderBy(t => t.User?.Name).ToList(), true)); } @@ -138,10 +164,10 @@ namespace PARR.API.Controllers.V1.Statistics /// /// /// - private List CalcInaccessTime(List data, TimeSpan inaccessibilityTime) + private List CalcInaccessTime(List data, TimeSpan inaccessibilityTime) { - var inaccessTimeList = new List(); + var inaccessTimeList = new List(); DateTimeOffset? lastTime = null; foreach (var item in data.OrderBy(t => t.Date)) @@ -156,7 +182,7 @@ namespace PARR.API.Controllers.V1.Statistics while (lastTime < item.Date) { //есть время когда робот был недоступен, добаляем в список недоступности - inaccessTimeList.Add(new StatRobotStateResponse { Date = lastTime.Value, IsUp = 0 }); + inaccessTimeList.Add(new StatRobotStateData { Date = lastTime.Value, IsUp = 0 }); lastTime = lastTime.Value.Add(inaccessibilityTime); } @@ -177,7 +203,7 @@ namespace PARR.API.Controllers.V1.Statistics lastTime = lastTime.Value.Add(inaccessibilityTime); while (lastTime.Value < DateTimeOffset.UtcNow) { - inaccessTimeList.Add(new StatRobotStateResponse { Date = lastTime.Value, IsUp = 0 }); + inaccessTimeList.Add(new StatRobotStateData { Date = lastTime.Value, IsUp = 0 }); lastTime = lastTime.Value.Add(inaccessibilityTime); } } @@ -189,4 +215,14 @@ namespace PARR.API.Controllers.V1.Statistics return data.OrderBy(t => t.Date).ToList(); } } + + + class InfuxRowData + { + public DateTimeOffset Date { get; set; } + + public int IsUp { get; set; } + + public string? Ip { get; set; } + } }