From 280186e2fb4e67bdef85ef15f107194c0aa55e02 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 1 Dec 2025 18:17:30 -0600 Subject: add directory frequency --- src/slskd/Telemetry/API/StatisticsController.cs | 91 +++++++++++++++++++------ 1 file changed, 69 insertions(+), 22 deletions(-) (limited to 'src/slskd/Telemetry/API') diff --git a/src/slskd/Telemetry/API/StatisticsController.cs b/src/slskd/Telemetry/API/StatisticsController.cs index 7053c3a4..b535d31d 100644 --- a/src/slskd/Telemetry/API/StatisticsController.cs +++ b/src/slskd/Telemetry/API/StatisticsController.cs @@ -51,11 +51,11 @@ public class StatisticsController : ControllerBase /// /// Gets a summary of all transfer activity over the specified timeframe, grouped by direction and final state. /// - /// The start time of the summary window (default: 7 days ago). - /// The end time of the summary window (default: now). + /// The start time of the window (default: 7 days ago). + /// The end time of the window (default: now). /// An optional direction (Upload, Download) by which to filter records. /// An optional username by which to filter records. - /// A nested dictionary keyed by direction and state and containing summary information. + /// /// The request completed successfully. /// Bad request. /// An error occurred. @@ -111,12 +111,12 @@ public class StatisticsController : ControllerBase /// 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 of the histogram window (default: 7 days ago). - /// The end time of the histogram window (default: now). + /// The start time of the window (default: 7 days ago). + /// The end time of the window (default: now). /// The interval, in minutes (default: 60). /// An optional direction (Upload, Download) by which to filter records. /// An optional username by which to filter records. - /// A nested dictionary keyed by interval, direction, and state and containing summary information. + /// /// The request completed successfully. /// Bad request. /// An error occurred. @@ -189,13 +189,13 @@ public class StatisticsController : ControllerBase /// Gets the top N user summaries by count, total bytes, or average speed. /// /// The direction (Upload, Download). - /// The start time of the summary window (default: 12/30/2025). - /// The end time of the summary window (default: now). + /// The start time of the window (default: 12/30/2025). + /// The end time of the window (default: now). /// The property by which to sort (Count, TotalBytes, AverageSpeed. Default: Count). /// The sort order (ASC, DESC. Default: DESC). /// The number of records to return (Default: 25). /// The record offset (if paginating). - /// A list of user summaries. + /// /// The request completed successfully. /// Bad request. /// An error occurred. @@ -204,7 +204,7 @@ public class StatisticsController : ControllerBase [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 500)] - public IActionResult GetSuccessfulTransferSummaryByUsername( + public IActionResult GetTransferLeaderboard( [FromQuery] string direction, [FromQuery] DateTime? start = null, [FromQuery] DateTime? end = null, @@ -275,7 +275,7 @@ public class StatisticsController : ControllerBase /// The username of the user. /// The start time of the summary window (default: 7 days ago). /// The end time of the summary window (default: now). - /// A dictionary keyed by direction and containing a detailed summary. + /// /// The request completed successfully. /// Bad request. /// An error occurred. @@ -284,7 +284,7 @@ public class StatisticsController : ControllerBase [ProducesResponseType(typeof(Dictionary), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 500)] - public IActionResult GetUser( + public IActionResult GetUserDetails( [FromRoute] string username, [FromQuery] DateTime? start = null, [FromQuery] DateTime? end = null) @@ -497,14 +497,61 @@ public class StatisticsController : ControllerBase } } - // [HttpGet("transfers/directories")] - // [Authorize(Policy = AuthPolicy.Any)] - // [ProducesResponseType(typeof(Dictionary), 200)] - // public IActionResult GetTransferSummaryByDirectory( - // [FromQuery] int? limit = null, - // [FromQuery] int? offset = null) - // { - // // todo: get a list of all directories downloaded at least once, along with the number of times downloaded (doesn't matter what status) - // return null; - // } + /// + /// Gets the top N most frequently downloaded directories by total count and distinct users. + /// + /// The start time of the window (default: 12/30/2025). + /// The end time of the window (default: now). + /// An optional username by which to filter records. + /// The number of records to return (Default: 25). + /// The record offset (if paginating). + /// + /// The request completed successfully. + /// Bad request. + /// An error occurred. + [HttpGet("transfers/directories")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(List), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 500)] + public IActionResult GetTransferDirectoryFrequency( + [FromQuery] DateTime? start = null, + [FromQuery] DateTime? end = null, + [FromQuery] string username = null, + [FromQuery] int limit = 25, + [FromQuery] int offset = 0) + { + start ??= Program.GenesisDateTime; + end ??= DateTime.MaxValue; + + if (start >= end) + { + return BadRequest("End time must be later than start time"); + } + + if (limit <= 0) + { + return BadRequest("Limit must be greater than zero"); + } + + if (offset < 0) + { + return BadRequest("Offset must be greater than or equal to zero"); + } + + try + { + return Ok(Telemetry.Statistics.GetTransferDirectoryFrequency( + start: start, + end: end, + username: username, + limit: limit, + offset: offset)); + } + catch (Exception ex) + { + Log.Error(ex, "Error fetching transfer directories over {Start}-{End}: {Message}", start, end, ex.Message); + return StatusCode(500, ex.Message); + } + } } -- cgit v1.2.3