feat(api): StatRobotStateController обновлен респонс статистики доступности роботов. Учтена возможность нескольких роботов на разных серверах.
This commit is contained in:
@@ -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<RobotStateRequest> 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<ErrorModel> { new ErrorModel { Message = $"Ошибка при получении статистики" } }));
|
||||
|
||||
var listWithInaccessTime = CalcInaccessTime(result.ToList(), inaccessibilityTime);
|
||||
var groupingByRobotIp = result.GroupBy(t => t.Ip);
|
||||
|
||||
var response = new List<StatRobotStateResponse>();
|
||||
|
||||
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<UserBaseResponse>(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<List<StatRobotStateResponse>>(listWithInaccessTime, true));
|
||||
return Ok(new Response<List<StatRobotStateResponse>>(response.OrderBy(t => t.User?.Name).ToList(), true));
|
||||
}
|
||||
|
||||
|
||||
@@ -138,10 +164,10 @@ namespace PARR.API.Controllers.V1.Statistics
|
||||
/// <param name="data"></param>
|
||||
/// <param name="inaccessibilityTime"></param>
|
||||
/// <returns></returns>
|
||||
private List<StatRobotStateResponse> CalcInaccessTime(List<StatRobotStateResponse> data, TimeSpan inaccessibilityTime)
|
||||
private List<StatRobotStateData> CalcInaccessTime(List<StatRobotStateData> data, TimeSpan inaccessibilityTime)
|
||||
{
|
||||
|
||||
var inaccessTimeList = new List<StatRobotStateResponse>();
|
||||
var inaccessTimeList = new List<StatRobotStateData>();
|
||||
|
||||
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; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user