From a9a62dc2c540bf06ee245c9c163616d3fd6a4053 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 31 Aug 2025 19:06:27 -0500 Subject: add new routes to telemetry controller --- src/slskd/Telemetry/API/TelemetryController.cs | 100 ++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) (limited to 'src/slskd/Telemetry/API/TelemetryController.cs') diff --git a/src/slskd/Telemetry/API/TelemetryController.cs b/src/slskd/Telemetry/API/TelemetryController.cs index 2207ac8c..03b081cc 100644 --- a/src/slskd/Telemetry/API/TelemetryController.cs +++ b/src/slskd/Telemetry/API/TelemetryController.cs @@ -19,11 +19,13 @@ namespace slskd.Telemetry; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Text.RegularExpressions; using System.Threading.Tasks; using Asp.Versioning; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Soulseek; /// /// Telemetry. @@ -93,11 +95,107 @@ public class TelemetryController : ControllerBase /// Gets gets key performance indicators for the application. /// /// - [HttpGet("metrics/kpi")] + [HttpGet("metrics/kpis")] [Authorize(Policy = AuthPolicy.Any)] public async Task GetKpis() { Dictionary dict = await Telemetry.Prometheus.GetMetricsAsObject(include: KpiRegexes); return Ok(dict); } + + /// + /// Summarizes transfer statistics for all users for the specified time range. + /// + /// The start time. + /// The end time. + /// A dictionary keyed by direction and state and containing summary information. + [HttpGet("statistics/transfers")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(Dictionary>), 200)] + public async Task GetTransferSummary( + [FromQuery] DateTime? start = null, + [FromQuery] DateTime? end = null) + { + start ??= DateTime.MinValue; + end ??= DateTime.MaxValue; + + if (start >= end) + { + return BadRequest("End time must be later than start time"); + } + + return Ok(Telemetry.Statistics.GetTransferSummary(start.Value, end)); + } + + /// + /// Summarizes transfer statistics for the specified direction (Upload or Download) for all users and for the + /// specified time range. + /// + /// The direction (Upload or Download). + /// The start time. + /// The end time. + /// A dictionary keyed by state and containing summary information. + [HttpGet("statistics/transfers/{direction}")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(Dictionary), 200)] + public async Task GetTransferSummaryByDirection( + [FromRoute, Required] string direction, + [FromQuery] DateTime? start = null, + [FromQuery] DateTime? end = null) + { + start ??= DateTime.MinValue; + end ??= DateTime.MaxValue; + + if (!Enum.TryParse(direction, ignoreCase: true, out var directionEnum)) + { + return BadRequest($"Direction must be one of: {string.Join(", ", Enum.GetNames(typeof(TransferDirection)))}"); + } + + if (start >= end) + { + return BadRequest("End time must be later than start time"); + } + + var data = Telemetry.Statistics.GetTransferSummary(start.Value, end, directionEnum); + + return Ok(data[directionEnum]); + } + + [HttpGet("statistics/transfers/{direction}/users")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(Dictionary), 200)] + public async Task GetSuccessfulTransferSummaryByDirectionAndUsername( + [FromRoute] string direction, + [FromQuery] DateTime? start = null, + [FromQuery] DateTime? end = null) + { + start ??= DateTime.MinValue; + end ??= DateTime.MaxValue; + + if (!Enum.TryParse(direction, ignoreCase: true, out var directionEnum)) + { + return BadRequest($"Direction must be one of: {string.Join(", ", Enum.GetNames(typeof(TransferDirection)))}"); + } + + if (start >= end) + { + return BadRequest("End time must be later than start time"); + } + + return Ok(Telemetry.Statistics.GetSuccessfulTransferSummaryByDirectionAndUsername(directionEnum, start.Value, end)); + } + + [HttpGet("statistics/transfers/users/{username}")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(Dictionary>), 200)] + public async Task GetTransferSummaryByUsername( + [FromRoute] string username, + [FromQuery] DateTime? start = null, + [FromQuery] DateTime? end = null) + { + start ??= DateTime.MinValue; + end ??= DateTime.MaxValue; + + return Ok(Telemetry.Statistics.GetTransferSummary(start, end, username: username)); + } } -- cgit v1.2.3