feat(api): статистика запросов на авторизацию не из КЭШа
This commit is contained in:
@@ -224,6 +224,12 @@
|
||||
public const string Send = BaseStat + "/robot-states/";
|
||||
}
|
||||
|
||||
public static class StatAuth
|
||||
{
|
||||
public const string Get = BaseStat + "/auth";
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Наряды
|
||||
|
||||
10
PARR.API/Contracts/V1/Requests/Queries/StatAuthGetQuery.cs
Normal file
10
PARR.API/Contracts/V1/Requests/Queries/StatAuthGetQuery.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace PARR.API.Contracts.V1.Requests.Queries
|
||||
{
|
||||
public class StatAuthGetQuery
|
||||
{
|
||||
/// <summary>
|
||||
/// Начало данных (-3d, -2h, -15m). По умолчанию -2h
|
||||
/// </summary>
|
||||
public string? StartAt { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace PARR.API.Contracts.V1.Responses.Statistics
|
||||
{
|
||||
public class StatAuthResponse
|
||||
{
|
||||
public DateTimeOffset Date { get; set; }
|
||||
|
||||
public int Count { get; set; }
|
||||
}
|
||||
}
|
||||
70
PARR.API/Controllers/V1/Statistics/StatAuthController.cs
Normal file
70
PARR.API/Controllers/V1/Statistics/StatAuthController.cs
Normal file
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Статистика по авторизации
|
||||
/// </summary>
|
||||
[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;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Кол-во запросов на авторизацию не из КЭШа
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet(ApiRoutes.StatAuth.Get)]
|
||||
public async Task<IActionResult> 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<ErrorModel> { new ErrorModel { Message = $"Ошибка при получении статистики" } }));
|
||||
|
||||
|
||||
return Ok(new Response<List<StatAuthResponse>>(result.ToList(), true));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user