feat(api): при авторизации без кэша, записывается статистика в influxdb

This commit is contained in:
Mikhail Trubnikov
2024-06-03 09:12:06 +10:00
parent 9dde13ceae
commit f4ec3448ae
2 changed files with 43 additions and 1 deletions

View File

@@ -0,0 +1,20 @@
using InfluxDB.Client.Core;
namespace PARR.API.Domain.InfluxDbModels
{
/// <summary>
/// Модель хранение логонов в InfluxDb
/// </summary>
[Measurement("logon")]
public class LogonDbModel
{
[Column(nameof(Ip))]
public required string Ip { get; set; }
[Column(nameof(UserId), IsTag = true)]
public Guid? UserId { get; set; }
[Column(nameof(HostId), IsTag = true)]
public Guid? HostId { get; set; }
}
}

View File

@@ -1,9 +1,12 @@
using PARR.API.Authentication.Models;
using PARR.API.Domain.InfluxDbModels;
using PARR.API.Services.Interfaces;
using PARR.API.Settings;
using PARR.Constants;
using PARR.DAL.CacheServices;
using PARR.DAL.InfluxDbServices;
using PARR.DAL.Services.Interfaces;
using PARR.DAL.Settings;
namespace PARR.API.Services.Implementations
{
@@ -14,13 +17,17 @@ namespace PARR.API.Services.Implementations
private readonly IHostService hostService;
private readonly UserCacheSettings userCacheSettings;
private readonly IClientService clientService;
private readonly IInfluxDbService influxDbService;
private readonly InfluxDbSettings influxDbSettings;
public AuthService(
IRedisCacheService cacheService,
IUserService userService,
IHostService hostService,
UserCacheSettings userCacheSettings,
IClientService clientService
IClientService clientService,
IInfluxDbService influxDbService,
InfluxDbSettings influxDbSettings
)
{
this.cacheService = cacheService;
@@ -28,6 +35,8 @@ namespace PARR.API.Services.Implementations
this.hostService = hostService;
this.userCacheSettings = userCacheSettings;
this.clientService = clientService;
this.influxDbService = influxDbService;
this.influxDbSettings = influxDbSettings;
}
@@ -118,10 +127,23 @@ namespace PARR.API.Services.Implementations
await cacheService.SetCachedDataAsync(GetAllowKey(ipAddress), cacheData, userCacheSettings.UserCacheTtl);
//тут записать в статистику входов в influxdb
WriteLogonStatistic(cacheData);
return cacheData;
}
private void WriteLogonStatistic(UserCache cacheData)
{
influxDbService.Write(write =>
{
var data = new LogonDbModel { HostId = cacheData.HostId, Ip = cacheData.UserIp, UserId = cacheData.UserId };
write.WriteMeasurement(data, InfluxDB.Client.Api.Domain.WritePrecision.Ns, bucket: influxDbSettings.Logons!.Bucket, org: influxDbSettings.Organization);
}, influxDbSettings.Logons!.Token);
}
public async Task UnlockUserAsync(string ipAddress)
{
await cacheService.DeleteCachedDataAsync(GetBlockKey(ipAddress));