aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-12-01 16:37:45 -0600
committerJP Dillingham <jp@dillingham.me>2025-12-01 16:37:45 -0600
commit1f2556793d5b2e9f3ace545d43a7c3e6ea865104 (patch)
treeae5fa72dfa613c3467a4c5c2104cc95b3c279fbb /src
parent5f21eaf17493d7c167e00bd833393227d7cace9e (diff)
downloadslskd-1f2556793d5b2e9f3ace545d43a7c3e6ea865104.tar.gz
implement user details
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Telemetry/API/StatisticsController.cs95
-rw-r--r--src/slskd/Telemetry/StatisticsService.cs2
-rw-r--r--src/slskd/Telemetry/Types/UserDirectionTransferSummary.cs39
-rw-r--r--src/slskd/Telemetry/Types/UserSummary.cs66
4 files changed, 124 insertions, 78 deletions
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
/// <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>
+ /// <returns>A list of user summaries.</returns>
+ /// <response code="200">The request completed successfully.</response>
+ /// <response code="400">Bad request.</response>
+ /// <response code="500">An error occurred.</response>
[HttpGet("transfers/leaderboard")]
[Authorize(Policy = AuthPolicy.Any)]
- [ProducesResponseType(typeof(List<TransferExceptionSummary>), 200)]
+ [ProducesResponseType(typeof(List<TransferSummary>), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 500)]
public IActionResult GetSuccessfulTransferSummaryByUsername(
@@ -266,6 +270,84 @@ public class StatisticsController : ControllerBase
}
/// <summary>
+ /// Gets detailed transfer activity for the specified user.
+ /// </summary>
+ /// <param name="username">The username of the user.</param>
+ /// <param name="start">The start time of the summary window (default: 7 days ago).</param>
+ /// <param name="end">The end time of the summary window (default: now).</param>
+ /// <returns>A dictionary keyed by direction and containing a detailed summary.</returns>
+ /// <response code="200">The request completed successfully.</response>
+ /// <response code="400">Bad request.</response>
+ /// <response code="500">An error occurred.</response>
+ [HttpGet("transfers/users/{username}")]
+ [Authorize(Policy = AuthPolicy.Any)]
+ [ProducesResponseType(typeof(Dictionary<TransferDirection, UserDirectionTransferSummary>), 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<TransferStates, TransferSummary> 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<TransferDirection, UserDirectionTransferSummary>();
+
+ 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);
+ }
+ }
+
+ /// <summary>
/// Gets a list of transfer exceptions by direction.
/// </summary>
/// <param name="direction">The direction.</param>
@@ -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 file="UserDirectionTransferSummary.cs" company="slskd Team">
+// 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/.
+// </copyright>
+
+namespace slskd.Telemetry;
+
+using System.Collections.Generic;
+using Soulseek;
+
+public record UserDirectionTransferSummary
+{
+ public Dictionary<TransferStates, TransferSummary> Summary { get; init; }
+ public UserDirectionTransferStatistics Statistics { get; init; }
+ public List<TransferExceptionSummary> 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 file="UserSummary.cs" company="slskd Team">
-// 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/.
-// </copyright>
-
-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<TransferDirection, UserTransferErrorSummary> Errors { get; init; }
-}
-
-public record UserTransferRatios
-{
- public double Count { get; init; }
- public double TotalBytes { get; init; }
-}
-
-public record UserTransferDirectionSummary
-{
- public Dictionary<TransferStates, TransferSummary> Summary { get; init; }
- public List<UserTransferErrorSummary> 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