diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-08-31 19:06:27 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2025-08-31 19:06:27 -0500 |
| commit | a9a62dc2c540bf06ee245c9c163616d3fd6a4053 (patch) | |
| tree | b2d9808f49e5c3c3d499174f4f940b2da184959c /src/slskd/Telemetry/API/TelemetryController.cs | |
| parent | 46348446aa6a4b07bac05653e96b9b088f72ac87 (diff) | |
| download | slskd-a9a62dc2c540bf06ee245c9c163616d3fd6a4053.tar.gz | |
add new routes to telemetry controller
Diffstat (limited to 'src/slskd/Telemetry/API/TelemetryController.cs')
| -rw-r--r-- | src/slskd/Telemetry/API/TelemetryController.cs | 100 |
1 files changed, 99 insertions, 1 deletions
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; /// <summary> /// Telemetry. @@ -93,11 +95,107 @@ public class TelemetryController : ControllerBase /// Gets gets key performance indicators for the application. /// </summary> /// <returns></returns> - [HttpGet("metrics/kpi")] + [HttpGet("metrics/kpis")] [Authorize(Policy = AuthPolicy.Any)] public async Task<IActionResult> GetKpis() { Dictionary<string, PrometheusMetric> dict = await Telemetry.Prometheus.GetMetricsAsObject(include: KpiRegexes); return Ok(dict); } + + /// <summary> + /// Summarizes transfer statistics for all users for the specified time range. + /// </summary> + /// <param name="start">The start time.</param> + /// <param name="end">The end time.</param> + /// <returns>A dictionary keyed by direction and state and containing summary information.</returns> + [HttpGet("statistics/transfers")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(Dictionary<TransferDirection, Dictionary<TransferStates, TransferSummary>>), 200)] + public async Task<IActionResult> 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)); + } + + /// <summary> + /// Summarizes transfer statistics for the specified direction (Upload or Download) for all users and for the + /// specified time range. + /// </summary> + /// <param name="direction">The direction (Upload or Download).</param> + /// <param name="start">The start time.</param> + /// <param name="end">The end time.</param> + /// <returns>A dictionary keyed by state and containing summary information.</returns> + [HttpGet("statistics/transfers/{direction}")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(Dictionary<TransferStates, TransferSummary>), 200)] + public async Task<IActionResult> GetTransferSummaryByDirection( + [FromRoute, Required] string direction, + [FromQuery] DateTime? start = null, + [FromQuery] DateTime? end = null) + { + start ??= DateTime.MinValue; + end ??= DateTime.MaxValue; + + if (!Enum.TryParse<TransferDirection>(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<string, TransferSummary>), 200)] + public async Task<IActionResult> GetSuccessfulTransferSummaryByDirectionAndUsername( + [FromRoute] string direction, + [FromQuery] DateTime? start = null, + [FromQuery] DateTime? end = null) + { + start ??= DateTime.MinValue; + end ??= DateTime.MaxValue; + + if (!Enum.TryParse<TransferDirection>(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<TransferDirection, Dictionary<TransferStates, TransferSummary>>), 200)] + public async Task<IActionResult> 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)); + } } |
