diff --git a/PARR.API/Domain/InfluxDbModels/LogonDbModel.cs b/PARR.API/Domain/InfluxDbModels/LogonDbModel.cs new file mode 100644 index 00000000..7eb91a37 --- /dev/null +++ b/PARR.API/Domain/InfluxDbModels/LogonDbModel.cs @@ -0,0 +1,20 @@ +using InfluxDB.Client.Core; + +namespace PARR.API.Domain.InfluxDbModels +{ + /// + /// Модель хранение логонов в InfluxDb + /// + [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; } + } +} diff --git a/PARR.API/Services/Implementations/AuthService.cs b/PARR.API/Services/Implementations/AuthService.cs index 00e363d9..50963d3e 100644 --- a/PARR.API/Services/Implementations/AuthService.cs +++ b/PARR.API/Services/Implementations/AuthService.cs @@ -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));