diff --git a/PARR.API/Contracts/V1/ApiRoutes.cs b/PARR.API/Contracts/V1/ApiRoutes.cs index f07a4151..2ae50e0e 100644 --- a/PARR.API/Contracts/V1/ApiRoutes.cs +++ b/PARR.API/Contracts/V1/ApiRoutes.cs @@ -224,6 +224,12 @@ public const string Send = BaseStat + "/robot-states/"; } + public static class StatAuth + { + public const string Get = BaseStat + "/auth"; + } + + #endregion #region Наряды diff --git a/PARR.API/Contracts/V1/Requests/Queries/StatAuthGetQuery.cs b/PARR.API/Contracts/V1/Requests/Queries/StatAuthGetQuery.cs new file mode 100644 index 00000000..56df919f --- /dev/null +++ b/PARR.API/Contracts/V1/Requests/Queries/StatAuthGetQuery.cs @@ -0,0 +1,10 @@ +namespace PARR.API.Contracts.V1.Requests.Queries +{ + public class StatAuthGetQuery + { + /// + /// Начало данных (-3d, -2h, -15m). По умолчанию -2h + /// + public string? StartAt { get; set; } + } +} diff --git a/PARR.API/Contracts/V1/Responses/Statistics/StatAuthResponse.cs b/PARR.API/Contracts/V1/Responses/Statistics/StatAuthResponse.cs new file mode 100644 index 00000000..a29353c7 --- /dev/null +++ b/PARR.API/Contracts/V1/Responses/Statistics/StatAuthResponse.cs @@ -0,0 +1,9 @@ +namespace PARR.API.Contracts.V1.Responses.Statistics +{ + public class StatAuthResponse + { + public DateTimeOffset Date { get; set; } + + public int Count { get; set; } + } +} diff --git a/PARR.API/Controllers/V1/Statistics/StatAuthController.cs b/PARR.API/Controllers/V1/Statistics/StatAuthController.cs new file mode 100644 index 00000000..86fb58a5 --- /dev/null +++ b/PARR.API/Controllers/V1/Statistics/StatAuthController.cs @@ -0,0 +1,70 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using PARR.API.Contracts.V1; +using PARR.API.Contracts.V1.Requests.Queries; +using PARR.API.Contracts.V1.Responses.Base; +using PARR.API.Contracts.V1.Responses.Statistics; +using PARR.API.Controllers.V1.Base; +using PARR.Constants; +using PARR.DAL.InfluxDbServices; +using PARR.DAL.Settings; + +namespace PARR.API.Controllers.V1.Statistics +{ + /// + /// Статистика по авторизации + /// + [Authorize(Roles = ParrRoles.EsppRobot.RoleOrAdmin)] + public class StatAuthController : BaseApiController + { + private readonly IInfluxDbService influxDbService; + private readonly InfluxDbSettings influxDbSettings; + + public StatAuthController( + IInfluxDbService influxDbService, + InfluxDbSettings influxDbSettings + ) + { + this.influxDbService = influxDbService; + this.influxDbSettings = influxDbSettings; + } + + + /// + /// Кол-во запросов на авторизацию не из КЭШа + /// + /// + [HttpGet(ApiRoutes.StatAuth.Get)] + public async Task Get([FromQuery] StatAuthGetQuery query) + { + var startAt = string.IsNullOrEmpty(query.StartAt) ? "-2h" : query.StartAt; + + var result = await influxDbService.QueryAsync(async handler => + { + var query = $"from(bucket: \"{influxDbSettings.Logons!.Bucket}\") " + + $"|> range(start: {startAt}) " + + $"|> filter(fn: (r) => r._measurement == \"logon\") " + + $"|> drop(columns: [\"HostId\", \"UserId\"]) " + + $"|> aggregateWindow(every: 5m, fn: count)"; + + var tables = await handler.QueryAsync(query, influxDbSettings.Organization); + + return tables.SelectMany(table => + + table.Records.Select(record => new StatAuthResponse + { + Date = (DateTimeOffset)record.GetTimeInDateTime(), + Count = int.Parse(record.GetValue()?.ToString() ?? "0") + }) + ); + }, influxDbSettings.Logons!.Token); + + if (result == null) + return BadRequest(new Response(false, new List { new ErrorModel { Message = $"Ошибка при получении статистики" } })); + + + return Ok(new Response>(result.ToList(), true)); + } + + } +}