Files
NTBug/backend/src/Helpdesk.Api/Controllers/StatisticsController.cs
T

61 lines
3.0 KiB
C#
Raw Normal View History

using System.ComponentModel.DataAnnotations;
using Helpdesk.Api.Infrastructure;
using Helpdesk.Application.Statistics;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Helpdesk.Api.Controllers;
/// <summary>Statistiky pro vedení školy. Období v UTC zahrnuje začátek a vylučuje konec.</summary>
[ApiController]
[Authorize(Roles = Roles.Management)]
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
[Route("api/statistics")]
[ProducesResponseType<ProblemDetails>(StatusCodes.Status400BadRequest)]
[ProducesResponseType<ProblemDetails>(StatusCodes.Status401Unauthorized)]
[ProducesResponseType<ProblemDetails>(StatusCodes.Status403Forbidden)]
public sealed class StatisticsController(StatisticsService statistics) : ControllerBase
{
/// <summary>Počty podle aktuálního stavu a průměrné časy pro závady nahlášené v období.</summary>
[HttpGet("overview")]
public Task<StatisticsOverview> GetOverview([FromQuery] StatisticsQuery query, CancellationToken ct) =>
statistics.GetOverviewAsync(query.ToPeriod(), ct);
/// <summary>Počty nahlášených závad a průměrná doba vyřešení podle kategorie.</summary>
[HttpGet("issues/by-category")]
public Task<IReadOnlyList<IssueCategoryStatistics>> GetByCategory([FromQuery] StatisticsQuery query, CancellationToken ct) =>
statistics.GetByCategoryAsync(query.ToPeriod(), ct);
/// <summary>Počty nahlášených závad podle budovy včetně závad bez budovy.</summary>
[HttpGet("issues/by-building")]
public Task<IReadOnlyList<IssueBuildingStatistics>> GetByBuilding([FromQuery] StatisticsQuery query, CancellationToken ct) =>
statistics.GetByBuildingAsync(query.ToPeriod(), ct);
/// <summary>Otevřené, vyřešené a zamítnuté závady podle aktuálního řešitele.</summary>
[HttpGet("issues/by-assignee")]
public Task<IReadOnlyList<IssueAssigneeStatistics>> GetByAssignee([FromQuery] StatisticsQuery query, CancellationToken ct) =>
statistics.GetByAssigneeAsync(query.ToPeriod(), ct);
/// <summary>Počty vytvoření a uzavření závad po měsících podle data události.</summary>
[HttpGet("issues/trend")]
public Task<IReadOnlyList<IssueTrendPoint>> GetTrend([FromQuery] StatisticsQuery query, CancellationToken ct,
[FromQuery, AllowedValues("month")] string granularity = "month") =>
statistics.GetTrendAsync(query.ToPeriod(), ct);
/// <summary>Návrhy podané v období podle aktuálního skóre, počtu hlasů pro a data podání.</summary>
[HttpGet("proposals/top")]
public Task<IReadOnlyList<TopProposal>> GetTopProposals([FromQuery] StatisticsQuery query, CancellationToken ct,
[FromQuery, Range(1, 100)] int limit = 10) =>
statistics.GetTopProposalsAsync(query.ToPeriod(), limit, ct);
}
public sealed class StatisticsQuery
{
[FromQuery(Name = "from")]
public DateTimeOffset? From { get; init; }
[FromQuery(Name = "to")]
public DateTimeOffset? To { get; init; }
internal StatisticsPeriod ToPeriod() => new(From, To);
}