api(api, dal): IInfluxDbService. StatRobotStateController мониторинг роботов
This commit is contained in:
@@ -218,6 +218,12 @@
|
|||||||
public const string GetConnectionStats = BaseStat + "/rabbitmq-connections/";
|
public const string GetConnectionStats = BaseStat + "/rabbitmq-connections/";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class StatRobotState
|
||||||
|
{
|
||||||
|
public const string Get = BaseStat + "/robot-states/";
|
||||||
|
public const string Send = BaseStat + "/robot-states/";
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Наряды
|
#region Наряды
|
||||||
|
|||||||
9
PARR.API/Contracts/V1/Requests/RobotStateRequest.cs
Normal file
9
PARR.API/Contracts/V1/Requests/RobotStateRequest.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using PARR.Constants;
|
||||||
|
|
||||||
|
namespace PARR.API.Contracts.V1.Requests
|
||||||
|
{
|
||||||
|
public class RobotStateRequest
|
||||||
|
{
|
||||||
|
public RobotsAllEnum Robot { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
17
PARR.API/Validators/RobotStateRequestValidator.cs
Normal file
17
PARR.API/Validators/RobotStateRequestValidator.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
using FluentValidation;
|
||||||
|
using PARR.API.Contracts.V1.Requests;
|
||||||
|
using PARR.Constants;
|
||||||
|
|
||||||
|
namespace PARR.API.Validators
|
||||||
|
{
|
||||||
|
public class RobotStateRequestValidator : AbstractValidator<RobotStateRequest>
|
||||||
|
{
|
||||||
|
public RobotStateRequestValidator()
|
||||||
|
{
|
||||||
|
RuleFor(t => t.Robot).NotNull().IsInEnum().WithMessage($"Допустимые значения: {(int)RobotsAllEnum.TemplateOrder} ({RobotsAllEnum.TemplateOrder})," +
|
||||||
|
$"{(int)RobotsAllEnum.ScheduleOrder} ({RobotsAllEnum.ScheduleOrder})," +
|
||||||
|
$"{(int)RobotsAllEnum.TemplatesExport} ({RobotsAllEnum.TemplatesExport})," +
|
||||||
|
$"{(int)RobotsAllEnum.ScheduleExport} ({RobotsAllEnum.ScheduleExport})");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,5 +30,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"InfluxDbSettings": {
|
||||||
|
"Url": "http://10.99.253.216:8086"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,5 +56,17 @@
|
|||||||
"UserCacheSettings": {
|
"UserCacheSettings": {
|
||||||
"UserCacheTtl": "00:05:00",
|
"UserCacheTtl": "00:05:00",
|
||||||
"UserBlockTtl": "00:03:00"
|
"UserBlockTtl": "00:03:00"
|
||||||
|
},
|
||||||
|
"InfluxDbSettings": {
|
||||||
|
"Url": "http://parr-influxdb:8086",
|
||||||
|
"Organization": "parr",
|
||||||
|
"Robots": {
|
||||||
|
"Bucket": "robots",
|
||||||
|
"Token": "jgEigdAYLlX6_Ey8FsleCyjfGnh-sdieQDs6JtA_XNweINMUhjPKWD-ibALyo4MG6oDBFAUfFNNpwoiUR0HwQA=="
|
||||||
|
},
|
||||||
|
"Logons": {
|
||||||
|
"Bucket": "logons",
|
||||||
|
"Token": "hjhOqZMAOm3UsAJURcROBvNqCRdR1Tp3jGYTdzduLXC4uSVrysjvEzLtM-n8jotdwuD7y0rxa2FzzOZKRGzKLg=="
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
28
PARR.Constants/RobotsAllEnum.cs
Normal file
28
PARR.Constants/RobotsAllEnum.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
namespace PARR.Constants
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Список всех роботов (в бд нет 3, 4)
|
||||||
|
/// </summary>
|
||||||
|
public enum RobotsAllEnum //: RobotsEnum
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Робот по созданию/изменению шаблона наряда ЕСПП
|
||||||
|
/// </summary>
|
||||||
|
TemplateOrder = 1,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Робот по созданию/изменению расписания шаблона наряда в ЕСПП
|
||||||
|
/// </summary>
|
||||||
|
ScheduleOrder = 2,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Робот экспорта шаблонов из ЕСПП в ПАРР
|
||||||
|
/// </summary>
|
||||||
|
TemplatesExport = 3,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Робот экспорта расписаний из ЕСПП в ПАРР
|
||||||
|
/// </summary>
|
||||||
|
ScheduleExport = 4
|
||||||
|
}
|
||||||
|
}
|
||||||
10
PARR.DAL/InfluxDbServices/IInfluxDbService.cs
Normal file
10
PARR.DAL/InfluxDbServices/IInfluxDbService.cs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
using InfluxDB.Client;
|
||||||
|
|
||||||
|
namespace PARR.DAL.InfluxDbServices
|
||||||
|
{
|
||||||
|
public interface IInfluxDbService
|
||||||
|
{
|
||||||
|
Task<T> QueryAsync<T>(Func<QueryApi, Task<T>> action, string token);
|
||||||
|
bool Write(Action<WriteApi> action, string token);
|
||||||
|
}
|
||||||
|
}
|
||||||
59
PARR.DAL/InfluxDbServices/InfluxDbService.cs
Normal file
59
PARR.DAL/InfluxDbServices/InfluxDbService.cs
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
using InfluxDB.Client;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using PARR.DAL.Settings;
|
||||||
|
|
||||||
|
namespace PARR.DAL.InfluxDbServices
|
||||||
|
{
|
||||||
|
internal class InfluxDbService : IInfluxDbService
|
||||||
|
{
|
||||||
|
private readonly InfluxDbSettings settings;
|
||||||
|
private readonly ILogger<InfluxDbService> logger;
|
||||||
|
|
||||||
|
public InfluxDbService(InfluxDbSettings settings, ILogger<InfluxDbService> logger)
|
||||||
|
{
|
||||||
|
this.settings = settings;
|
||||||
|
this.logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Write(Action<WriteApi> action, string token)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
//using var client = new InfluxDBClient(settings.Url, token);
|
||||||
|
//using var write = client.GetWriteApi();
|
||||||
|
|
||||||
|
using var client = new InfluxDBClient(settings.Url, token);
|
||||||
|
using (var write = client.GetWriteApi())
|
||||||
|
{
|
||||||
|
action(write);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
logger.LogError(ex, "Ошибка при записи в БД, InfluxDb");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<T> QueryAsync<T>(Func<QueryApi, Task<T>> action, string token)
|
||||||
|
{
|
||||||
|
//TODO:
|
||||||
|
throw new NotImplementedException();
|
||||||
|
//try
|
||||||
|
//{
|
||||||
|
using var client = InfluxDBClientFactory.Create(settings.Url, token);
|
||||||
|
var query = client.GetQueryApi();
|
||||||
|
|
||||||
|
return await action(query);
|
||||||
|
//}
|
||||||
|
//catch (Exception ex)
|
||||||
|
//{
|
||||||
|
// logger.LogError(ex, "Ошибка при запросе данных из БД, InfuxDb");
|
||||||
|
// // return Task.FromResult();
|
||||||
|
// return Task.CompletedTask;
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="InfluxDB.Client" Version="4.15.0" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.5">
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.5">
|
||||||
<PrivateAssets>all</PrivateAssets>
|
<PrivateAssets>all</PrivateAssets>
|
||||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using PARR.DAL.CacheServices;
|
|||||||
using PARR.DAL.Configurations.DbSettings;
|
using PARR.DAL.Configurations.DbSettings;
|
||||||
using PARR.DAL.Context;
|
using PARR.DAL.Context;
|
||||||
using PARR.DAL.Contracts;
|
using PARR.DAL.Contracts;
|
||||||
|
using PARR.DAL.InfluxDbServices;
|
||||||
using PARR.DAL.Services.Implementation;
|
using PARR.DAL.Services.Implementation;
|
||||||
using PARR.DAL.Services.Implementations;
|
using PARR.DAL.Services.Implementations;
|
||||||
using PARR.DAL.Services.Interfaces;
|
using PARR.DAL.Services.Interfaces;
|
||||||
@@ -29,6 +30,8 @@ namespace PARR.DAL
|
|||||||
.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))
|
.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))
|
||||||
);
|
);
|
||||||
|
|
||||||
|
#region Redis
|
||||||
|
|
||||||
services.AddStackExchangeRedisCache(opt =>
|
services.AddStackExchangeRedisCache(opt =>
|
||||||
{
|
{
|
||||||
opt.Configuration = configuration.GetConnectionString("RedisConnection");
|
opt.Configuration = configuration.GetConnectionString("RedisConnection");
|
||||||
@@ -36,6 +39,19 @@ namespace PARR.DAL
|
|||||||
|
|
||||||
services.AddTransient<IRedisCacheService, RedisCacheService>();
|
services.AddTransient<IRedisCacheService, RedisCacheService>();
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region InfluxDb
|
||||||
|
|
||||||
|
var influxDbSettings = new InfluxDbSettings();
|
||||||
|
configuration.GetSection(nameof(InfluxDbSettings)).Bind(influxDbSettings);
|
||||||
|
services.AddSingleton(influxDbSettings);
|
||||||
|
|
||||||
|
services.AddTransient<IInfluxDbService, InfluxDbService>();
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
// Entity services
|
// Entity services
|
||||||
services.AddTransient<IHostService, HostService>();
|
services.AddTransient<IHostService, HostService>();
|
||||||
services.AddTransient<IApplicationService, ApplicationService>();
|
services.AddTransient<IApplicationService, ApplicationService>();
|
||||||
|
|||||||
32
PARR.DAL/Settings/InfluxDbSettings.cs
Normal file
32
PARR.DAL/Settings/InfluxDbSettings.cs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
namespace PARR.DAL.Settings
|
||||||
|
{
|
||||||
|
public class InfluxDbSettings
|
||||||
|
{
|
||||||
|
public string Url { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
public string Organization { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
public InfluxDbBucketRobotsSettings? Robots { get; set; }
|
||||||
|
|
||||||
|
public InfluxDbBucketLogonsSettings? Logons { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class InfluxDbBucketRobotsSettings : IInfluxDbBucketSettings
|
||||||
|
{
|
||||||
|
public string Bucket { get; set; } = string.Empty;
|
||||||
|
public string Token { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class InfluxDbBucketLogonsSettings : IInfluxDbBucketSettings
|
||||||
|
{
|
||||||
|
public string Bucket { get; set; } = string.Empty;
|
||||||
|
public string Token { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IInfluxDbBucketSettings
|
||||||
|
{
|
||||||
|
public string Bucket { get; set; }
|
||||||
|
|
||||||
|
public string Token { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user