api(api, dal): IInfluxDbService. StatRobotStateController мониторинг роботов

This commit is contained in:
Mikhail Trubnikov
2024-05-28 14:35:39 +10:00
parent 0007170079
commit e5215b7dc2
12 changed files with 260 additions and 0 deletions

View 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);
}
}

View 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;
//}
}
}
}

View File

@@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="InfluxDB.Client" Version="4.15.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@@ -5,6 +5,7 @@ using PARR.DAL.CacheServices;
using PARR.DAL.Configurations.DbSettings;
using PARR.DAL.Context;
using PARR.DAL.Contracts;
using PARR.DAL.InfluxDbServices;
using PARR.DAL.Services.Implementation;
using PARR.DAL.Services.Implementations;
using PARR.DAL.Services.Interfaces;
@@ -29,6 +30,8 @@ namespace PARR.DAL
.UseNpgsql(configuration.GetConnectionString("DefaultConnection"))
);
#region Redis
services.AddStackExchangeRedisCache(opt =>
{
opt.Configuration = configuration.GetConnectionString("RedisConnection");
@@ -36,6 +39,19 @@ namespace PARR.DAL
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
services.AddTransient<IHostService, HostService>();
services.AddTransient<IApplicationService, ApplicationService>();

View 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; }
}
}