feat(dal, api): в таблицу Units добавлено поле LastLogon. Переделана авторизация для хостов (ЭК), теперь берется ip из связанных Units. Переделаны запросы авторизации в кеш. Переделан запрос и модель в influxdb. Обновлена статистика по юнитам и полям.

This commit is contained in:
Mikhail Trubnikov
2025-08-26 14:04:49 +10:00
parent a6385ea39a
commit c7bc8f60ef
19 changed files with 3994 additions and 137 deletions

View File

@@ -1,4 +1,5 @@
using PARR.API.Authentication.Models;
using Microsoft.EntityFrameworkCore;
using PARR.API.Authentication.Models;
using PARR.API.Domain.InfluxDbModels;
using PARR.API.Services.Interfaces;
using PARR.API.Settings;
@@ -6,6 +7,7 @@ using PARR.Constants;
using PARR.DAL.CacheServices;
using PARR.DAL.InfluxDbServices;
using PARR.DAL.Services.Interfaces;
using PARR.DAL.Services.Interfaces.Unit;
using PARR.DAL.Settings;
namespace PARR.API.Services.Implementations
@@ -14,29 +16,29 @@ namespace PARR.API.Services.Implementations
{
private readonly IRedisCacheService cacheService;
private readonly IUserService userService;
private readonly IHostService hostService;
private readonly UserCacheSettings userCacheSettings;
private readonly IClientService clientService;
private readonly IInfluxDbService influxDbService;
private readonly InfluxDbSettings influxDbSettings;
private readonly IUnitService unitService;
public AuthService(
IRedisCacheService cacheService,
IUserService userService,
IHostService hostService,
UserCacheSettings userCacheSettings,
IClientService clientService,
IInfluxDbService influxDbService,
InfluxDbSettings influxDbSettings
InfluxDbSettings influxDbSettings,
IUnitService unitService
)
{
this.cacheService = cacheService;
this.userService = userService;
this.hostService = hostService;
this.userCacheSettings = userCacheSettings;
this.clientService = clientService;
this.influxDbService = influxDbService;
this.influxDbSettings = influxDbSettings;
this.unitService = unitService;
}
@@ -84,17 +86,19 @@ namespace PARR.API.Services.Implementations
await userService.CommitAsync();
}
var host = await hostService.GetByIpAsync(ipAddress);
if (host != null)
var unit = await unitService.Get()
.Include(t => t.UnitValues).ThenInclude(t => t.Value)
.FirstOrDefaultAsync(t => t.UnitValues.FirstOrDefault(v => v.Value!.Value == ipAddress) != null);
if (unit != null)
{
roles.Add(new UserRoleInCache { Name = ParrRoles.Agent.Role, Description = ParrRoles.Agent.Description });
// сохраняем время входа
host.LastLogon = DateTimeOffset.UtcNow;
await hostService.CommitAsync();
unit.LastLogon = DateTimeOffset.UtcNow;
await unitService.CommitAsync();
}
if (user == null && host == null)
if (user == null && unit == null)
{
// если это не пользователь и не хост, добавляем в кэш на блокировку
await cacheService.SetCachedDataAsync(GetBlockKey(ipAddress), DateTimeOffset.UtcNow, userCacheSettings.UserBlockTtl);
@@ -109,22 +113,21 @@ namespace PARR.API.Services.Implementations
Roles = roles
};
if (host != null)
if (unit != null)
{
cacheData.Name = host.Ek;
cacheData.HostId = host.Id;
cacheData.LastLogon = host.LastLogon;
cacheData.Name = unit.Name;
cacheData.UnitId = unit.Id;
cacheData.LastLogon = unit.LastLogon;
}
if (user != null)
{
cacheData.Name = user.Name + (host != null ? $", {host.Ek}" : string.Empty);
cacheData.Name = user.Name + (unit != null ? $", {unit.Name}" : string.Empty);
cacheData.Description = user.Description ?? string.Empty;
cacheData.UserId = user.Id;
cacheData.LastLogon = user.LastLogon;
}
await cacheService.SetCachedDataAsync(GetAllowKey(ipAddress), cacheData, userCacheSettings.UserCacheTtl);
//тут записать в статистику входов в influxdb
@@ -138,7 +141,7 @@ namespace PARR.API.Services.Implementations
{
influxDbService.Write(write =>
{
var data = new LogonDbModel { HostId = cacheData.HostId, Ip = cacheData.UserIp, UserId = cacheData.UserId };
var data = new LogonDbModel { UnitId = cacheData.UnitId, 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);
}