feat(api): при авторизации без кэша, записывается статистика в influxdb
This commit is contained in:
20
PARR.API/Domain/InfluxDbModels/LogonDbModel.cs
Normal file
20
PARR.API/Domain/InfluxDbModels/LogonDbModel.cs
Normal 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; }
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user