From bf8341a9f0eda057b81657017c2dd1f8646a08dd Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 29 Nov 2025 18:33:16 -0600 Subject: docs, error handling and tweaks for /summary and /histogram --- src/slskd/Telemetry/API/StatisticsController.cs | 76 +++++++++++++++++++------ 1 file changed, 59 insertions(+), 17 deletions(-) (limited to 'src/slskd/Telemetry/API/StatisticsController.cs') diff --git a/src/slskd/Telemetry/API/StatisticsController.cs b/src/slskd/Telemetry/API/StatisticsController.cs index 54ed99a8..c0158ed4 100644 --- a/src/slskd/Telemetry/API/StatisticsController.cs +++ b/src/slskd/Telemetry/API/StatisticsController.cs @@ -23,6 +23,7 @@ using Asp.Versioning; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Serilog; using Soulseek; /// @@ -44,20 +45,27 @@ public class StatisticsController : ControllerBase } private TelemetryService Telemetry { get; } + private ILogger Log { get; } = Serilog.Log.ForContext(); /// - /// Summarizes transfer statistics, grouped by direction and final transfer state, for all users, and for the - /// specified time range. + /// Gets a summary of all transfer activity over the specified timeframe, grouped by direction and final state. /// - /// The start time. - /// The end time. + /// The start time (default: 7 days ago). + /// The end time (default: now). + /// An optional username by which to filter activity. /// A dictionary keyed by direction and state and containing summary information. - [HttpGet("transfers")] + /// The request completed successfully. + /// Bad request. + /// An error occurred. + [HttpGet("transfers/summary")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(Dictionary>), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 500)] public IActionResult GetTransferSummary( [FromQuery] DateTime? start = null, - [FromQuery] DateTime? end = null) + [FromQuery] DateTime? end = null, + [FromQuery] string username = null) { var now = DateTime.UtcNow; @@ -69,32 +77,66 @@ public class StatisticsController : ControllerBase return BadRequest("End time must be later than start time"); } - return Ok(Telemetry.Statistics.GetTransferSummary(start.Value, end)); + try + { + return Ok(Telemetry.Statistics.GetTransferSummary(start.Value, end, username: username)); + } + catch (Exception ex) + { + Log.Error(ex, "Error fetching transfer summary over {Start}-{End}: {Message}", start, end, ex.Message); + return StatusCode(500, ex.Message); + } } - [HttpGet("transfers/history")] + /// + /// Gets a histogram of all transfer activity over the specified timeframe, aggregated into fixed size time intervals + /// and grouped by direction and final state. + /// + /// The start time (default: 7 days ago). + /// The end time (default: now). + /// The interval, in minutes (default: 60). + /// An optional username by which to filter activity. + /// A dictionary keyed by direction and state and containing summary information. + /// The request completed successfully. + /// Bad request. + /// An error occurred. + [HttpGet("transfers/histogram")] [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(Dictionary>), 200)] + [ProducesResponseType(typeof(Dictionary>>), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 500)] public IActionResult GetTransferSummaryHistogram( [FromQuery] DateTime? start = null, [FromQuery] DateTime? end = null, - [FromQuery] int? interval = null) + [FromQuery] int interval = 60, + [FromQuery] string username = null) { + var now = DateTime.UtcNow; + + start ??= now.AddDays(-7); + end ??= now; + if (start >= end) { return BadRequest("End time must be later than start time"); } - start ??= DateTime.UtcNow.AddDays(-7); - end ??= DateTime.UtcNow; - var intervalTimeSpan = TimeSpan.FromMinutes(interval ?? 60); - - if (start >= end) + if (interval < 5) { - return BadRequest("End time must be later than start time"); + return BadRequest("Interval must be greater than or equal to 5"); } - return Ok(Telemetry.Statistics.GetTransferSummaryHistogram(start.Value, end.Value, intervalTimeSpan)); + var intervalTimeSpan = TimeSpan.FromMinutes(interval); + + try + { + return Ok(Telemetry.Statistics.GetTransferSummaryHistogram(start.Value, end.Value, interval: intervalTimeSpan, username: username)); + } + catch (Exception ex) + { + Log.Error(ex, "Error fetching transfer histogram over {Start}-{End}/{Interval}: {Message}", start, end, interval, ex.Message); + return StatusCode(500, ex.Message); + } } /// -- cgit v1.2.3