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 +++++++++++++++++++--- src/slskd/Telemetry/StatisticsService.cs | 2 +- .../Types/UserDirectionTransferSummary.cs | 39 +++++++++ src/slskd/Telemetry/Types/UserSummary.cs | 66 --------------- 4 files changed, 124 insertions(+), 78 deletions(-) create mode 100644 src/slskd/Telemetry/Types/UserDirectionTransferSummary.cs delete mode 100644 src/slskd/Telemetry/Types/UserSummary.cs (limited to 'src') 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; - // } } diff --git a/src/slskd/Telemetry/StatisticsService.cs b/src/slskd/Telemetry/StatisticsService.cs index d36de0c4..3199fd0c 100644 --- a/src/slskd/Telemetry/StatisticsService.cs +++ b/src/slskd/Telemetry/StatisticsService.cs @@ -257,7 +257,7 @@ public class StatisticsService SELECT Username, Direction, - StateDescription, + State, SUM(Size) AS TotalBytes, COUNT(*) AS Count, AVG(AverageSpeed) AS AverageSpeed, diff --git a/src/slskd/Telemetry/Types/UserDirectionTransferSummary.cs b/src/slskd/Telemetry/Types/UserDirectionTransferSummary.cs new file mode 100644 index 00000000..2e517be7 --- /dev/null +++ b/src/slskd/Telemetry/Types/UserDirectionTransferSummary.cs @@ -0,0 +1,39 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Telemetry; + +using System.Collections.Generic; +using Soulseek; + +public record UserDirectionTransferSummary +{ + public Dictionary Summary { get; init; } + public UserDirectionTransferStatistics Statistics { get; init; } + public List Exceptions { get; init; } +} + +public record UserDirectionTransferStatistics +{ + public long Total { get; init; } + public long Successful { get; init; } + public long Errored { get; init; } + public long Cancelled { get; init; } + public double SuccessRate => Successful / (double)Total; + public double ErrorRate => Errored / (double)Total; + public double CancelRate => Cancelled / (double)Total; +} \ No newline at end of file diff --git a/src/slskd/Telemetry/Types/UserSummary.cs b/src/slskd/Telemetry/Types/UserSummary.cs deleted file mode 100644 index 757b5ae7..00000000 --- a/src/slskd/Telemetry/Types/UserSummary.cs +++ /dev/null @@ -1,66 +0,0 @@ -// -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// - -namespace slskd.Telemetry; - -using System.Collections.Generic; -using Soulseek; - -public record UserSummary -{ - public UserTransferSummary Transfers { get; init; } -} - -public record UserTransferSummary -{ - public UserTransferDirectionSummary Upload { get; init; } - public UserTransferDirectionSummary Download { get; init; } - public UserTransferRatios Ratios { get; init; } - public Dictionary Errors { get; init; } -} - -public record UserTransferRatios -{ - public double Count { get; init; } - public double TotalBytes { get; init; } -} - -public record UserTransferDirectionSummary -{ - public Dictionary Summary { get; init; } - public List Errors { get; init; } - public UserTransferDirectionStatistics Statistics { get; init; } -} - -public record UserTransferErrorSummary -{ - public string Exception { get; init; } - public long Count { get; init; } -} - -public record UserTransferDirectionStatistics -{ - public double SuccessRate { get; init; } - public double ErrorRate { get; init; } - public double CancelRate { get; init; } -} - -public record UserMessageStatistics -{ - public int Sent { get; init; } - public int Received { get; init; } -} \ No newline at end of file -- cgit v1.2.3