diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-11-30 18:46:20 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2025-11-30 18:46:20 -0600 |
| commit | 91cb8165cb59744d959337a4a0b2793afcc88fe4 (patch) | |
| tree | 72e7d88db03101d7b8adc4c9fc5a515144dc9a29 /src/slskd/Telemetry/API | |
| parent | 47b106fcd88200755c15eed8b9014761c48b2e24 (diff) | |
| download | slskd-91cb8165cb59744d959337a4a0b2793afcc88fe4.tar.gz | |
add leaderboard
Diffstat (limited to 'src/slskd/Telemetry/API')
| -rw-r--r-- | src/slskd/Telemetry/API/StatisticsController.cs | 154 |
1 files changed, 112 insertions, 42 deletions
diff --git a/src/slskd/Telemetry/API/StatisticsController.cs b/src/slskd/Telemetry/API/StatisticsController.cs index 302c0705..5da2f979 100644 --- a/src/slskd/Telemetry/API/StatisticsController.cs +++ b/src/slskd/Telemetry/API/StatisticsController.cs @@ -119,7 +119,7 @@ public class StatisticsController : ControllerBase /// <response code="200">The request completed successfully.</response> /// <response code="400">Bad request.</response> /// <response code="500">An error occurred.</response> - [HttpGet("transfers/summary/histogram")] + [HttpGet("transfers/histogram")] [Authorize(Policy = AuthPolicy.Any)] [ProducesResponseType(typeof(Dictionary<DateTime, Dictionary<TransferDirection, Dictionary<TransferStates, TransferSummary>>>), 200)] [ProducesResponseType(typeof(string), 400)] @@ -141,6 +141,14 @@ public class StatisticsController : ControllerBase return BadRequest("End time must be later than start time"); } + // clamp the start time to the earliest reasonable date to avoid returning a ton of empty intervals + // and introducing performance issues client side + if (start < Program.GenesisDateTime) + { + Log.Warning("A start time prior to the genesis date of the application was supplied; the start time has been adjusted to the genesis time {Genesis}", Program.GenesisDateTime); + start = Program.GenesisDateTime; + } + if (interval < 5) { return BadRequest("Interval must be greater than or equal to 5"); @@ -177,28 +185,28 @@ public class StatisticsController : ControllerBase } /// <summary> - /// Returns a list of transfer exceptions by direction. + /// Gets the top N user summaries by total count and direction. /// </summary> - /// <param name="direction">The direction by which to filter activity.</param> + /// <param name="direction">The direction (Upload, Download).</param> /// <param name="start">The start time.</param> /// <param name="end">The end time.</param> - /// <param name="username">An optional username by which to filter exceptions.</param> + /// <param name="sortBy">The property by which to sort (Count, TotalBytes, AverageSpeed. Default: Count).</param> + /// <param name="sortOrder">The sort order (ASC, DESC. Default: DESC).</param> /// <param name="limit">The number of records to return (Default: 25).</param> /// <param name="offset">The record offset (if paginating).</param> /// <returns></returns> - /// <response code="200">The request completed successfully.</response> - /// <response code="400">Bad request.</response> - /// <response code="500">An error occurred.</response> - [HttpGet("transfers/exceptions")] + [HttpGet("transfers/leaderboard")] [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(List<UserTransferSummary>), 200)] [ProducesResponseType(typeof(List<TransferExceptionSummary>), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 500)] - public IActionResult GetTransferExceptions( - [FromQuery] string direction = null, + public IActionResult GetSuccessfulTransferSummaryByUsername( + [FromQuery] string direction, [FromQuery] DateTime? start = null, [FromQuery] DateTime? end = null, - [FromQuery] string username = null, + [FromQuery] string sortBy = "Count", + [FromQuery] string sortOrder = "ASC", [FromQuery] int limit = 25, [FromQuery] int offset = 0) { @@ -220,27 +228,48 @@ public class StatisticsController : ControllerBase return BadRequest("End time must be later than start time"); } + if (!Enum.TryParse<LeaderboardSortBy>(sortBy, out var parsedSortBy)) + { + return BadRequest($"Invalid sortBy; expected one of: {string.Join(", ", Enum.GetNames(typeof(LeaderboardSortBy)))}"); + } + + if (!Enum.TryParse<SortOrder>(sortOrder, out var parsedSortOrder)) + { + return BadRequest($"Invalid sortOrder; expected one of: {string.Join(", ", Enum.GetNames(typeof(SortOrder)))}"); + } + + 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.GetTransferExceptions( + return Ok(Telemetry.Statistics.GetTransferRanking( + direction: parsedDirection, start: start.Value, - end: end.Value, + end: end, + sortBy: parsedSortBy, + sortOrder: parsedSortOrder, limit: limit, - offset: offset, - direction: parsedDirection, - username: username)); + offset: offset)); } catch (Exception ex) { - Log.Error(ex, "Error fetching transfer exceptions over {Start}-{End}: {Message}", start, end, ex.Message); + Log.Error(ex, "Error fetching transfer leaderboard over {Start}-{End}: {Message}", start, end, ex.Message); return StatusCode(500, ex.Message); } } /// <summary> - /// Returns the top N exceptions by total count and direction. + /// Gets a list of transfer exceptions by direction. /// </summary> - /// <param name="direction">The direction by which to filter activity.</param> + /// <param name="direction">The direction.</param> /// <param name="start">The start time.</param> /// <param name="end">The end time.</param> /// <param name="username">An optional username by which to filter exceptions.</param> @@ -250,13 +279,13 @@ public class StatisticsController : ControllerBase /// <response code="200">The request completed successfully.</response> /// <response code="400">Bad request.</response> /// <response code="500">An error occurred.</response> - [HttpGet("transfers/exceptions/pareto")] + [HttpGet("transfers/exceptions")] [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(Dictionary<TransferDirection, List<TransferExceptionSummary>>), 200)] + [ProducesResponseType(typeof(List<TransferExceptionSummary>), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 500)] - public IActionResult GetTransferExceptionsPareto( - [FromQuery] string direction, + public IActionResult GetTransferExceptions( + [FromQuery] string direction = null, [FromQuery] DateTime? start = null, [FromQuery] DateTime? end = null, [FromQuery] string username = null, @@ -281,14 +310,24 @@ public class StatisticsController : ControllerBase 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.GetTransferExceptionsPareto( - direction: parsedDirection, + return Ok(Telemetry.Statistics.GetTransferExceptions( start: start.Value, end: end.Value, limit: limit, offset: offset, + direction: parsedDirection, username: username)); } catch (Exception ex) @@ -299,26 +338,43 @@ public class StatisticsController : ControllerBase } /// <summary> - /// Returns the top N user summaries by total count and direction. + /// Gets the top N exceptions by total count and direction. /// </summary> + /// <param name="direction">The direction.</param> /// <param name="start">The start time.</param> /// <param name="end">The end time.</param> + /// <param name="username">An optional username by which to filter exceptions.</param> /// <param name="limit">The number of records to return (Default: 25).</param> /// <param name="offset">The record offset (if paginating).</param> /// <returns></returns> - [HttpGet("transfers/users")] + /// <response code="200">The request completed successfully.</response> + /// <response code="400">Bad request.</response> + /// <response code="500">An error occurred.</response> + [HttpGet("transfers/exceptions/pareto")] [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(Dictionary<TransferDirection, List<UserTransferSummary>>), 200)] - public IActionResult GetSuccessfulTransferSummaryByUsername( + [ProducesResponseType(typeof(Dictionary<TransferDirection, List<TransferExceptionSummary>>), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 500)] + public IActionResult GetTransferExceptionsPareto( + [FromQuery] string direction, [FromQuery] DateTime? start = null, [FromQuery] DateTime? end = null, - [FromQuery] int? limit = null, - [FromQuery] int? offset = null) + [FromQuery] string username = null, + [FromQuery] int limit = 25, + [FromQuery] int offset = 0) { + if (string.IsNullOrWhiteSpace(direction)) + { + return BadRequest("Direction is required"); + } + + if (!Enum.TryParse<TransferDirection>(direction, ignoreCase: true, out var parsedDirection)) + { + return BadRequest($"Invalid direction; expected one of: {string.Join(", ", Enum.GetNames(typeof(TransferDirection)))}"); + } + start ??= DateTime.MinValue; end ??= DateTime.MaxValue; - limit ??= 25; - offset ??= 0; if (start >= end) { @@ -335,16 +391,21 @@ public class StatisticsController : ControllerBase return BadRequest("Offset must be greater than or equal to zero"); } - var downloads = Telemetry.Statistics.GetTransferRanking(direction: TransferDirection.Download, start.Value, end, limit: limit.Value, offset: offset.Value); - var uploads = Telemetry.Statistics.GetTransferRanking(direction: TransferDirection.Upload, start.Value, end, limit: limit.Value, offset: offset.Value); - - var dict = new Dictionary<TransferDirection, List<TransferSummary>>() + try { - { TransferDirection.Download, downloads }, - { TransferDirection.Upload, uploads }, - }; - - return Ok(dict); + return Ok(Telemetry.Statistics.GetTransferExceptionsPareto( + direction: parsedDirection, + start: start.Value, + end: end.Value, + limit: limit, + offset: offset, + username: username)); + } + catch (Exception ex) + { + Log.Error(ex, "Error fetching transfer exceptions over {Start}-{End}: {Message}", start, end, ex.Message); + return StatusCode(500, ex.Message); + } } [HttpGet("transfers/directories")] @@ -357,4 +418,13 @@ public class StatisticsController : ControllerBase // 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; } + + [HttpGet("users/{base64Name}")] + [Authorize(Policy = AuthPolicy.Any)] + public IActionResult GetUser( + [FromRoute] string base64Name) + { + // todo: get everything we can about the user + return null; + } } |
