From 1f2556793d5b2e9f3ace545d43a7c3e6ea865104 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 1 Dec 2025 16:37:45 -0600 Subject: implement user details --- src/slskd/Telemetry/API/StatisticsController.cs | 95 ++++++++++++++++++++++--- 1 file changed, 84 insertions(+), 11 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 a1d7203d..7053c3a4 100644 --- a/src/slskd/Telemetry/API/StatisticsController.cs +++ b/src/slskd/Telemetry/API/StatisticsController.cs @@ -19,6 +19,7 @@ namespace slskd.Telemetry; using System; using System.Collections.Generic; +using System.Linq; using Asp.Versioning; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; @@ -194,10 +195,13 @@ public class StatisticsController : ControllerBase /// 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. [HttpGet("transfers/leaderboard")] [Authorize(Policy = AuthPolicy.Any)] - [ProducesResponseType(typeof(List), 200)] + [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 500)] public IActionResult GetSuccessfulTransferSummaryByUsername( @@ -265,6 +269,84 @@ public class StatisticsController : ControllerBase } } + /// + /// Gets detailed transfer activity for the specified user. + /// + /// 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. + [HttpGet("transfers/users/{username}")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(Dictionary), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 500)] + public IActionResult GetUser( + [FromRoute] string username, + [FromQuery] DateTime? start = null, + [FromQuery] DateTime? end = null) + { + if (string.IsNullOrWhiteSpace(username)) + { + return BadRequest("Username is required"); + } + + start ??= Program.GenesisDateTime; + end ??= DateTime.UtcNow; + + if (start >= end) + { + return BadRequest("End time must be later than start time"); + } + + static UserDirectionTransferStatistics GetStatistics(Dictionary summary) + { + var totalTransfers = summary.Values.Sum(x => x.Count); + var successful = summary.Where(x => x.Key == TransferStates.Succeeded).Sum(x => x.Value.Count); + var errored = summary.Where(x => new[] { TransferStates.Errored, TransferStates.TimedOut, TransferStates.Rejected, TransferStates.Aborted }.Contains(x.Key)).Sum(x => x.Value.Count); + var cancelled = summary.Where(x => x.Key == TransferStates.Cancelled).Sum(x => x.Value.Count); + + return new UserDirectionTransferStatistics + { + Total = totalTransfers, + Successful = successful, + Errored = errored, + Cancelled = cancelled, + }; + } + + try + { + var results = new Dictionary(); + + var summary = Telemetry.Statistics.GetTransferSummary(start, end, username: username); + + results.Add(TransferDirection.Upload, new UserDirectionTransferSummary + { + Summary = summary[TransferDirection.Upload], + Statistics = GetStatistics(summary[TransferDirection.Upload]), + Exceptions = Telemetry.Statistics.GetTransferExceptionsPareto(TransferDirection.Upload, start, end, username: username, limit: 25, offset: 0), + }); + + results.Add(TransferDirection.Download, new UserDirectionTransferSummary + { + Summary = summary[TransferDirection.Download], + Statistics = GetStatistics(summary[TransferDirection.Download]), + Exceptions = Telemetry.Statistics.GetTransferExceptionsPareto(TransferDirection.Download, start, end, username: username, limit: 25, offset: 0), + }); + + return Ok(results); + } + catch (Exception ex) + { + Log.Error(ex, "Error fetching user details for {Username}: {Message}", username, ex.Message); + return StatusCode(500, ex.Message); + } + } + /// /// Gets a list of transfer exceptions by direction. /// @@ -425,13 +507,4 @@ 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; - // } } -- cgit v1.2.3