From f4ec3448ae59841399f33592188dc5b77c7f4247 Mon Sep 17 00:00:00 2001 From: Mikhail Trubnikov Date: Mon, 3 Jun 2024 09:12:06 +1000 Subject: [PATCH] =?UTF-8?q?feat(api):=20=D0=BF=D1=80=D0=B8=20=D0=B0=D0=B2?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20=D0=B1?= =?UTF-8?q?=D0=B5=D0=B7=20=D0=BA=D1=8D=D1=88=D0=B0,=20=D0=B7=D0=B0=D0=BF?= =?UTF-8?q?=D0=B8=D1=81=D1=8B=D0=B2=D0=B0=D0=B5=D1=82=D1=81=D1=8F=20=D1=81?= =?UTF-8?q?=D1=82=D0=B0=D1=82=D0=B8=D1=81=D1=82=D0=B8=D0=BA=D0=B0=20=D0=B2?= =?UTF-8?q?=20influxdb?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Domain/InfluxDbModels/LogonDbModel.cs | 20 ++++++++++++++++ .../Services/Implementations/AuthService.cs | 24 ++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 PARR.API/Domain/InfluxDbModels/LogonDbModel.cs 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));