68 lines
2.5 KiB
C#
68 lines
2.5 KiB
C#
using FluentValidation;
|
||
using InfluxDB.Client.Api.Domain;
|
||
using InfluxDB.Client.Writes;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using PARR.API.Contracts.V1;
|
||
using PARR.API.Contracts.V1.Requests;
|
||
using PARR.API.Contracts.V1.Responses.Base;
|
||
using PARR.API.Controllers.V1.Base;
|
||
using PARR.Constants;
|
||
using PARR.DAL.InfluxDbServices;
|
||
using PARR.DAL.Settings;
|
||
|
||
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;
|
||
|
||
public StatRobotStateController(
|
||
IInfluxDbService influxDbService,
|
||
IValidator<RobotStateRequest> validator,
|
||
InfluxDbSettings influxDbSettings
|
||
)
|
||
{
|
||
this.influxDbService = influxDbService;
|
||
this.validator = validator;
|
||
this.influxDbSettings = influxDbSettings;
|
||
}
|
||
|
||
|
||
/// <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())
|
||
//.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)
|
||
return BadRequest($"Ошибка при отправке данных");
|
||
|
||
return Ok(new Response<string>("", true));
|
||
}
|
||
}
|
||
}
|