|
| 1 | +using Microsoft.AspNetCore.Authorization; |
| 2 | +using Microsoft.AspNetCore.Mvc; |
| 3 | +using Taskdeck.Api.Contracts; |
| 4 | +using Taskdeck.Api.Extensions; |
| 5 | +using Taskdeck.Application.DTOs; |
| 6 | +using Taskdeck.Application.Interfaces; |
| 7 | +using Taskdeck.Application.Services; |
| 8 | + |
| 9 | +namespace Taskdeck.Api.Controllers; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Board metrics endpoints: throughput, cycle time, WIP, blocked trends. |
| 13 | +/// </summary> |
| 14 | +[ApiController] |
| 15 | +[Authorize] |
| 16 | +[Route("api/[controller]")] |
| 17 | +[Produces("application/json")] |
| 18 | +public class MetricsController : AuthenticatedControllerBase |
| 19 | +{ |
| 20 | + private readonly IBoardMetricsService _metricsService; |
| 21 | + |
| 22 | + public MetricsController(IBoardMetricsService metricsService, IUserContext userContext) |
| 23 | + : base(userContext) |
| 24 | + { |
| 25 | + _metricsService = metricsService; |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Get board metrics (throughput, cycle time, WIP, blocked) for a date range. |
| 30 | + /// </summary> |
| 31 | + /// <param name="boardId">The board to compute metrics for.</param> |
| 32 | + /// <param name="from">Start of date range (ISO 8601).</param> |
| 33 | + /// <param name="to">End of date range (ISO 8601).</param> |
| 34 | + /// <param name="labelId">Optional label filter.</param> |
| 35 | + /// <returns>Aggregated board metrics.</returns> |
| 36 | + /// <response code="200">Metrics computed successfully.</response> |
| 37 | + /// <response code="400">Invalid query parameters.</response> |
| 38 | + /// <response code="401">Authentication required.</response> |
| 39 | + /// <response code="403">No read access to the board.</response> |
| 40 | + /// <response code="404">Board not found.</response> |
| 41 | + [HttpGet("boards/{boardId}")] |
| 42 | + [ProducesResponseType(typeof(BoardMetricsResponse), StatusCodes.Status200OK)] |
| 43 | + [ProducesResponseType(typeof(ApiErrorResponse), StatusCodes.Status400BadRequest)] |
| 44 | + [ProducesResponseType(typeof(ApiErrorResponse), StatusCodes.Status401Unauthorized)] |
| 45 | + [ProducesResponseType(typeof(ApiErrorResponse), StatusCodes.Status403Forbidden)] |
| 46 | + [ProducesResponseType(typeof(ApiErrorResponse), StatusCodes.Status404NotFound)] |
| 47 | + public async Task<IActionResult> GetBoardMetrics( |
| 48 | + Guid boardId, |
| 49 | + [FromQuery] DateTimeOffset? from, |
| 50 | + [FromQuery] DateTimeOffset? to, |
| 51 | + [FromQuery] Guid? labelId) |
| 52 | + { |
| 53 | + if (!TryGetCurrentUserId(out var userId, out var errorResult)) |
| 54 | + return errorResult!; |
| 55 | + |
| 56 | + // Default to last 30 days if not specified |
| 57 | + var toDate = to ?? DateTimeOffset.UtcNow; |
| 58 | + var fromDate = from ?? toDate.AddDays(-30); |
| 59 | + |
| 60 | + var query = new BoardMetricsQuery(boardId, fromDate, toDate, labelId); |
| 61 | + var result = await _metricsService.GetBoardMetricsAsync(query, userId); |
| 62 | + return result.IsSuccess ? Ok(result.Value) : result.ToErrorActionResult(); |
| 63 | + } |
| 64 | +} |
0 commit comments