2024-07-22 16:59:15 +10:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using FluentValidation;
|
2024-05-28 14:35:39 +10:00
|
|
|
|
using InfluxDB.Client.Api.Domain;
|
|
|
|
|
|
using InfluxDB.Client.Writes;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2024-07-22 16:59:15 +10:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-05-28 14:35:39 +10:00
|
|
|
|
using PARR.API.Contracts.V1;
|
|
|
|
|
|
using PARR.API.Contracts.V1.Requests;
|
2024-05-28 16:56:42 +10:00
|
|
|
|
using PARR.API.Contracts.V1.Requests.Queries;
|
2024-07-22 16:59:15 +10:00
|
|
|
|
using PARR.API.Contracts.V1.Responses;
|
2024-05-28 14:35:39 +10:00
|
|
|
|
using PARR.API.Contracts.V1.Responses.Base;
|
2024-05-29 11:09:31 +10:00
|
|
|
|
using PARR.API.Contracts.V1.Responses.Statistics;
|
2024-05-28 14:35:39 +10:00
|
|
|
|
using PARR.API.Controllers.V1.Base;
|
2024-07-22 16:26:32 +10:00
|
|
|
|
using PARR.API.Services.Interfaces;
|
2024-05-29 11:09:31 +10:00
|
|
|
|
using PARR.API.Settings;
|
2026-04-14 16:27:27 +10:00
|
|
|
|
using PARR.Core.Common.Interfaces;
|
2024-07-22 16:59:15 +10:00
|
|
|
|
using PARR.DAL.Services.Interfaces;
|
2026-04-14 12:01:49 +10:00
|
|
|
|
using PARR.Domain.Common.Roles;
|
|
|
|
|
|
using PARR.Domain.Enums;
|
2026-04-14 16:27:27 +10:00
|
|
|
|
using PARR.Domain.Settings;
|
2024-05-28 14:35:39 +10:00
|
|
|
|
|
|
|
|
|
|
namespace PARR.API.Controllers.V1.Statistics
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Статистика доступности роботов
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Authorize(Roles = ParrRoles.EsppRobot.RoleOrAdmin)]
|
|
|
|
|
|
public class StatRobotStateController : BaseApiController
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IInfluxDbService influxDbService;
|
|
|
|
|
|
private readonly IValidator<RobotStateRequest> validator;
|
|
|
|
|
|
private readonly InfluxDbSettings influxDbSettings;
|
2024-05-29 11:09:31 +10:00
|
|
|
|
private readonly MonitoringSettings monitoringSettings;
|
2024-07-22 16:26:32 +10:00
|
|
|
|
private readonly IClientService clientService;
|
2024-07-22 16:59:15 +10:00
|
|
|
|
private readonly IUserService userService;
|
|
|
|
|
|
private readonly IMapper mapper;
|
2024-05-28 14:35:39 +10:00
|
|
|
|
|
|
|
|
|
|
public StatRobotStateController(
|
|
|
|
|
|
IInfluxDbService influxDbService,
|
|
|
|
|
|
IValidator<RobotStateRequest> validator,
|
2024-05-29 11:09:31 +10:00
|
|
|
|
InfluxDbSettings influxDbSettings,
|
2024-07-22 16:26:32 +10:00
|
|
|
|
MonitoringSettings monitoringSettings,
|
2024-07-22 16:59:15 +10:00
|
|
|
|
IClientService clientService,
|
|
|
|
|
|
IUserService userService,
|
|
|
|
|
|
IMapper mapper
|
2024-05-28 14:35:39 +10:00
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.influxDbService = influxDbService;
|
|
|
|
|
|
this.validator = validator;
|
|
|
|
|
|
this.influxDbSettings = influxDbSettings;
|
2024-05-29 11:09:31 +10:00
|
|
|
|
this.monitoringSettings = monitoringSettings;
|
2024-07-22 16:26:32 +10:00
|
|
|
|
this.clientService = clientService;
|
2024-07-22 16:59:15 +10:00
|
|
|
|
this.userService = userService;
|
|
|
|
|
|
this.mapper = mapper;
|
2024-05-28 14:35:39 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-05-28 16:56:42 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Доступность робота
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpGet(ApiRoutes.StatRobotState.Get)]
|
|
|
|
|
|
public async Task<IActionResult> Get([FromRoute] RobotsAllEnum robot, [FromQuery] RobotStateGetQuery query)
|
|
|
|
|
|
{
|
2024-05-29 11:09:31 +10:00
|
|
|
|
var startAt = string.IsNullOrEmpty(query.StartAt) ? "-2h" : query.StartAt;
|
|
|
|
|
|
|
|
|
|
|
|
var robotSettings = monitoringSettings.Robots.FirstOrDefault(t => t.Name == robot.ToString());
|
|
|
|
|
|
if (robotSettings == null)
|
|
|
|
|
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Ошибка при получении статистики. Неверно указаны настройки мониторинга." } }));
|
|
|
|
|
|
|
|
|
|
|
|
// var inaccessibilityTime = TimeSpan.Parse("00:03:00");
|
|
|
|
|
|
var inaccessibilityTime = robotSettings.InaccessibilityTime;
|
|
|
|
|
|
|
2024-05-28 16:56:42 +10:00
|
|
|
|
|
|
|
|
|
|
var result = await influxDbService.QueryAsync(async handler =>
|
|
|
|
|
|
{
|
|
|
|
|
|
//var flux = "from(bucket:\"robots\") " +
|
|
|
|
|
|
// "|> range(start: 0) " +
|
|
|
|
|
|
// "|> filter(fn: (r) => " +
|
|
|
|
|
|
// "r._measurement == \"altitude\" and " +
|
|
|
|
|
|
// "r._value > 3500)";
|
|
|
|
|
|
|
|
|
|
|
|
var query = $"from(bucket:\"{influxDbSettings.Robots!.Bucket}\") " +
|
2024-05-29 11:09:31 +10:00
|
|
|
|
//$"|> range(start: -3h) " +
|
|
|
|
|
|
$"|> range(start: {startAt}) " +
|
2024-05-28 16:56:42 +10:00
|
|
|
|
$"|> filter(fn: (r) => " +
|
|
|
|
|
|
$"r._measurement == \"robots\" " +
|
|
|
|
|
|
$"and r.robot == \"{robot.ToString()}\"" +
|
|
|
|
|
|
$")";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var tables = await handler.QueryAsync(query, influxDbSettings.Organization);
|
|
|
|
|
|
|
2024-05-29 11:09:31 +10:00
|
|
|
|
return tables.SelectMany(table =>
|
2024-07-22 16:59:15 +10:00
|
|
|
|
table.Records.Select(record => new InfuxRowData
|
2024-05-29 11:09:31 +10:00
|
|
|
|
{
|
|
|
|
|
|
Date = (DateTimeOffset)record.GetTimeInDateTime(),
|
2024-07-22 16:26:32 +10:00
|
|
|
|
IsUp = int.Parse(record.GetValue()?.ToString() ?? "0"),
|
|
|
|
|
|
Ip = record.GetValueByKey("ip")?.ToString()
|
2024-05-29 11:09:31 +10:00
|
|
|
|
})
|
|
|
|
|
|
);
|
2024-05-28 16:56:42 +10:00
|
|
|
|
}, influxDbSettings.Robots!.Token);
|
|
|
|
|
|
|
|
|
|
|
|
if (result == null)
|
|
|
|
|
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Ошибка при получении статистики" } }));
|
|
|
|
|
|
|
2024-07-22 16:59:15 +10:00
|
|
|
|
var groupingByRobotIp = result.GroupBy(t => t.Ip);
|
2024-05-28 16:56:42 +10:00
|
|
|
|
|
2024-07-22 16:59:15 +10:00
|
|
|
|
var response = new List<StatRobotStateResponse>();
|
2024-05-28 16:56:42 +10:00
|
|
|
|
|
2024-07-22 16:59:15 +10:00
|
|
|
|
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>>(response.OrderBy(t => t.User?.Name).ToList(), true));
|
2024-05-29 11:09:31 +10:00
|
|
|
|
}
|
2024-05-28 16:56:42 +10:00
|
|
|
|
|
|
|
|
|
|
|
2024-05-28 14:35:39 +10:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Статистика доступности роботов. Передать инф-у о доступности
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="request"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpPost(ApiRoutes.StatRobotState.Send)]
|
|
|
|
|
|
public async Task<IActionResult> Send([FromBody] RobotStateRequest request)
|
|
|
|
|
|
{
|
|
|
|
|
|
var resultValidate = await validator.ValidateAsync(request);
|
|
|
|
|
|
if (!resultValidate.IsValid)
|
|
|
|
|
|
return BadRequest(new Response(resultValidate.Errors));
|
|
|
|
|
|
|
|
|
|
|
|
var result = influxDbService.Write(write =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var point = PointData.Measurement("robots")
|
|
|
|
|
|
.Tag("robot", request.Robot.ToString())
|
2024-07-22 16:26:32 +10:00
|
|
|
|
.Tag("ip", clientService.GetClientIp()?.ToString())
|
2024-05-28 14:35:39 +10:00
|
|
|
|
//.Field("robot", (int)request.Robot)
|
|
|
|
|
|
.Field("isUp", 1)
|
|
|
|
|
|
.Timestamp(DateTime.UtcNow, WritePrecision.Ns);
|
|
|
|
|
|
|
|
|
|
|
|
write.WritePoint(point, bucket: influxDbSettings.Robots!.Bucket, org: influxDbSettings.Organization);
|
|
|
|
|
|
}, influxDbSettings.Robots!.Token);
|
|
|
|
|
|
|
|
|
|
|
|
if (!result)
|
2024-05-28 16:56:42 +10:00
|
|
|
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Ошибка при отправке данных" } }));
|
2024-05-28 14:35:39 +10:00
|
|
|
|
|
|
|
|
|
|
return Ok(new Response<string>("", true));
|
|
|
|
|
|
}
|
2024-05-29 11:09:31 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Рассчитывает время когда робот был недоступен
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="data"></param>
|
|
|
|
|
|
/// <param name="inaccessibilityTime"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2024-07-22 16:59:15 +10:00
|
|
|
|
private List<StatRobotStateData> CalcInaccessTime(List<StatRobotStateData> data, TimeSpan inaccessibilityTime)
|
2024-05-29 11:09:31 +10:00
|
|
|
|
{
|
|
|
|
|
|
|
2024-07-22 16:59:15 +10:00
|
|
|
|
var inaccessTimeList = new List<StatRobotStateData>();
|
2024-05-29 11:09:31 +10:00
|
|
|
|
|
|
|
|
|
|
DateTimeOffset? lastTime = null;
|
|
|
|
|
|
foreach (var item in data.OrderBy(t => t.Date))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!lastTime.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
lastTime = item.Date;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lastTime = lastTime.Value.Add(inaccessibilityTime);
|
|
|
|
|
|
while (lastTime < item.Date)
|
|
|
|
|
|
{
|
|
|
|
|
|
//есть время когда робот был недоступен, добаляем в список недоступности
|
2024-07-22 16:59:15 +10:00
|
|
|
|
inaccessTimeList.Add(new StatRobotStateData { Date = lastTime.Value, IsUp = 0 });
|
2024-05-29 11:09:31 +10:00
|
|
|
|
lastTime = lastTime.Value.Add(inaccessibilityTime);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// робот был доступен, все ок
|
|
|
|
|
|
lastTime = item.Date;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//if (lastTime == null)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// // не было вообще никакой статистики, считаем что он недоступен
|
|
|
|
|
|
// inaccessTimeList.Add(new StatRobotStateResponse { Date = DateTimeOffset.UtcNow, IsUp = 0 });
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
// Проверяем, вдруг с последней даты активности до сейчас прошло уже много времени и он не доступен
|
|
|
|
|
|
if (lastTime.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
lastTime = lastTime.Value.Add(inaccessibilityTime);
|
|
|
|
|
|
while (lastTime.Value < DateTimeOffset.UtcNow)
|
|
|
|
|
|
{
|
2024-07-22 16:59:15 +10:00
|
|
|
|
inaccessTimeList.Add(new StatRobotStateData { Date = lastTime.Value, IsUp = 0 });
|
2024-05-29 11:09:31 +10:00
|
|
|
|
lastTime = lastTime.Value.Add(inaccessibilityTime);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data.AddRange(inaccessTimeList);
|
|
|
|
|
|
|
|
|
|
|
|
return data.OrderBy(t => t.Date).ToList();
|
|
|
|
|
|
}
|
2024-05-28 14:35:39 +10:00
|
|
|
|
}
|
2024-07-22 16:59:15 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class InfuxRowData
|
|
|
|
|
|
{
|
|
|
|
|
|
public DateTimeOffset Date { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public int IsUp { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
public string? Ip { get; set; }
|
|
|
|
|
|
}
|
2024-05-28 14:35:39 +10:00
|
|
|
|
}
|