aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-12-01 15:49:29 -0600
committerJP Dillingham <jp@dillingham.me>2025-12-01 15:49:29 -0600
commit446954d439c0cae5ed054882f5dba398a2607cab (patch)
tree5f37606ba7fe3a9519968da0df9c9964a8f4f4dd /src
parentd5f278bbf94f06ffb35c4db16602f74462f19bf6 (diff)
downloadslskd-446954d439c0cae5ed054882f5dba398a2607cab.tar.gz
tweaks
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Telemetry/API/StatisticsController.cs57
-rw-r--r--src/slskd/Telemetry/StatisticsService.cs183
-rw-r--r--src/slskd/Telemetry/Types/ErrorSummary.cs24
3 files changed, 124 insertions, 140 deletions
diff --git a/src/slskd/Telemetry/API/StatisticsController.cs b/src/slskd/Telemetry/API/StatisticsController.cs
index 5da2f979..68bcb46c 100644
--- a/src/slskd/Telemetry/API/StatisticsController.cs
+++ b/src/slskd/Telemetry/API/StatisticsController.cs
@@ -50,11 +50,11 @@ public class StatisticsController : ControllerBase
/// <summary>
/// Gets a summary of all transfer activity over the specified timeframe, grouped by direction and final state.
/// </summary>
- /// <param name="start">The start time (default: 7 days ago).</param>
- /// <param name="end">The end time (default: now).</param>
- /// <param name="direction">An optional direction by which to filter activity.</param>
- /// <param name="username">An optional username by which to filter activity.</param>
- /// <returns>A dictionary keyed by direction and state and containing summary information.</returns>
+ /// <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>
+ /// <param name="direction">An optional direction (Upload, Download) by which to filter records.</param>
+ /// <param name="username">An optional username by which to filter records.</param>
+ /// <returns>A nested dictionary keyed by direction and state and containing summary information.</returns>
/// <response code="200">The request completed successfully.</response>
/// <response code="400">Bad request.</response>
/// <response code="500">An error occurred.</response>
@@ -110,12 +110,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.
/// </summary>
- /// <param name="start">The start time (default: 7 days ago).</param>
- /// <param name="end">The end time (default: now).</param>
+ /// <param name="start">The start time of the histogram window (default: 7 days ago).</param>
+ /// <param name="end">The end time of the histogram window (default: now).</param>
/// <param name="interval">The interval, in minutes (default: 60).</param>
- /// <param name="direction">An optional direction by which to filter activity.</param>
- /// <param name="username">An optional username by which to filter activity.</param>
- /// <returns>A dictionary keyed by direction and state and containing summary information.</returns>
+ /// <param name="direction">An optional direction (Upload, Download) by which to filter records.</param>
+ /// <param name="username">An optional username by which to filter records.</param>
+ /// <returns>A nested dictionary keyed by interval, direction, and state and containing summary information.</returns>
/// <response code="200">The request completed successfully.</response>
/// <response code="400">Bad request.</response>
/// <response code="500">An error occurred.</response>
@@ -124,7 +124,7 @@ public class StatisticsController : ControllerBase
[ProducesResponseType(typeof(Dictionary<DateTime, Dictionary<TransferDirection, Dictionary<TransferStates, TransferSummary>>>), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 500)]
- public IActionResult GetTransferSummaryHistogram(
+ public IActionResult GetTransferHistogram(
[FromQuery] DateTime? start = null,
[FromQuery] DateTime? end = null,
[FromQuery] int interval = 60,
@@ -170,7 +170,7 @@ public class StatisticsController : ControllerBase
try
{
- return Ok(Telemetry.Statistics.GetTransferSummaryHistogram(
+ return Ok(Telemetry.Statistics.GetTransferHistogram(
start: start.Value,
end: end.Value,
interval: intervalTimeSpan,
@@ -185,11 +185,11 @@ public class StatisticsController : ControllerBase
}
/// <summary>
- /// Gets the top N user summaries by total count and direction.
+ /// Gets the top N user summaries by count, total bytes, or average speed.
/// </summary>
/// <param name="direction">The direction (Upload, Download).</param>
- /// <param name="start">The start time.</param>
- /// <param name="end">The end time.</param>
+ /// <param name="start">The start time of the summary window (default: 12/30/2025).</param>
+ /// <param name="end">The end time of the summary window (default: now).</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>
@@ -197,7 +197,6 @@ public class StatisticsController : ControllerBase
/// <returns></returns>
[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)]
@@ -206,7 +205,7 @@ public class StatisticsController : ControllerBase
[FromQuery] DateTime? start = null,
[FromQuery] DateTime? end = null,
[FromQuery] string sortBy = "Count",
- [FromQuery] string sortOrder = "ASC",
+ [FromQuery] string sortOrder = "DESC",
[FromQuery] int limit = 25,
[FromQuery] int offset = 0)
{
@@ -220,8 +219,8 @@ public class StatisticsController : ControllerBase
return BadRequest($"Invalid direction; expected one of: {string.Join(", ", Enum.GetNames(typeof(TransferDirection)))}");
}
- start ??= DateTime.MinValue;
- end ??= DateTime.MaxValue;
+ start ??= Program.GenesisDateTime;
+ end ??= DateTime.UtcNow;
if (start >= end)
{
@@ -250,7 +249,7 @@ public class StatisticsController : ControllerBase
try
{
- return Ok(Telemetry.Statistics.GetTransferRanking(
+ return Ok(Telemetry.Statistics.GetTransferLeaderboard(
direction: parsedDirection,
start: start.Value,
end: end,
@@ -273,6 +272,7 @@ public class StatisticsController : ControllerBase
/// <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="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>
@@ -281,7 +281,7 @@ public class StatisticsController : ControllerBase
/// <response code="500">An error occurred.</response>
[HttpGet("transfers/exceptions")]
[Authorize(Policy = AuthPolicy.Any)]
- [ProducesResponseType(typeof(List<TransferExceptionSummary>), 200)]
+ [ProducesResponseType(typeof(List<TransferExceptionDetail>), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 500)]
public IActionResult GetTransferExceptions(
@@ -289,6 +289,7 @@ public class StatisticsController : ControllerBase
[FromQuery] DateTime? start = null,
[FromQuery] DateTime? end = null,
[FromQuery] string username = null,
+ [FromQuery] string sortOrder = "DESC",
[FromQuery] int limit = 25,
[FromQuery] int offset = 0)
{
@@ -310,6 +311,11 @@ public class StatisticsController : ControllerBase
return BadRequest("End time must be later than start time");
}
+ 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");
@@ -323,12 +329,13 @@ public class StatisticsController : ControllerBase
try
{
return Ok(Telemetry.Statistics.GetTransferExceptions(
+ direction: parsedDirection,
start: start.Value,
end: end.Value,
+ username: username,
+ sortOrder: parsedSortOrder,
limit: limit,
- offset: offset,
- direction: parsedDirection,
- username: username));
+ offset: offset));
}
catch (Exception ex)
{
@@ -352,7 +359,7 @@ public class StatisticsController : ControllerBase
/// <response code="500">An error occurred.</response>
[HttpGet("transfers/exceptions/pareto")]
[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(
diff --git a/src/slskd/Telemetry/StatisticsService.cs b/src/slskd/Telemetry/StatisticsService.cs
index c528b8f3..d36de0c4 100644
--- a/src/slskd/Telemetry/StatisticsService.cs
+++ b/src/slskd/Telemetry/StatisticsService.cs
@@ -120,17 +120,17 @@ public class StatisticsService
}
/// <summary>
- /// Returns a summary of all transfer data grouped by direction and final transfer state. Includes only
- /// completed transfers.
+ /// Returns a histogram of all transfer data, aggregated into fixed size time intervals and grouped by
+ /// direction and final transfer state. Includes only completed transfers.
/// </summary>
- /// <param name="start">The start time of the summary window.</param>
- /// <param name="end">The end time of the summary window.</param>
+ /// <param name="start">The start time of the histogram window.</param>
+ /// <param name="end">The end time of the histogram window.</param>
/// <param name="interval">The interval for the histogram.</param>
- /// <param name="direction">The optional direction (Upload or Download) for the summary.</param>
+ /// <param name="direction">The optional direction (Upload or Download) by which to filter results.</param>
/// <param name="username">The optional username by which to filter results.</param>
/// <returns>A nested dictionary keyed by direction and state and containing summary information.</returns>
/// <exception cref="ArgumentException">Thrown if end time is not later than start time.</exception>
- public Dictionary<DateTime, Dictionary<TransferDirection, Dictionary<TransferStates, TransferSummary>>> GetTransferSummaryHistogram(
+ public Dictionary<DateTime, Dictionary<TransferDirection, Dictionary<TransferStates, TransferSummary>>> GetTransferHistogram(
DateTime start,
DateTime end,
TimeSpan interval,
@@ -218,12 +218,96 @@ public class StatisticsService
}
/// <summary>
+ /// Returns the top N users summaries by count, total bytes, or average speed. Only successful transfers are included in counts.
+ /// </summary>
+ /// <param name="direction">The direction (Upload or Download).</param>
+ /// <param name="start">The optional start time of the summary window.</param>
+ /// <param name="end">The optional end time of the summary window.</param>
+ /// <param name="sortBy">The property by which to sort.</param>
+ /// <param name="sortOrder">The sort order.</param>
+ /// <param name="limit">The number of records to return (default: 25).</param>
+ /// <param name="offset">The record offset (if paginating).</param>
+ /// <returns>A dictionary keyed by username and containing summary information.</returns>
+ /// <exception cref="ArgumentException">
+ /// Thrown if end time is not later than start time, or limit is not greater than zero.
+ /// </exception>
+ public List<TransferSummary> GetTransferLeaderboard(
+ TransferDirection direction,
+ DateTime? start = null,
+ DateTime? end = null,
+ LeaderboardSortBy sortBy = LeaderboardSortBy.Count,
+ SortOrder sortOrder = SortOrder.DESC,
+ int limit = 25,
+ int offset = 0)
+ {
+ start ??= DateTime.MinValue;
+ end ??= DateTime.MaxValue;
+
+ if (end <= start)
+ {
+ throw new ArgumentException("End time must be later than start time");
+ }
+
+ if (limit <= 0)
+ {
+ throw new ArgumentOutOfRangeException("Limit must be greater than zero");
+ }
+
+ var sql = @$"
+ SELECT
+ Username,
+ Direction,
+ StateDescription,
+ SUM(Size) AS TotalBytes,
+ COUNT(*) AS Count,
+ AVG(AverageSpeed) AS AverageSpeed,
+ AVG(strftime('%s', StartedAt) - strftime('%s', EnqueuedAt)) AS AverageWait,
+ AVG(strftime('%s', EndedAt) - strftime('%s', StartedAt)) AS AverageDuration
+ FROM Transfers
+ WHERE Direction = @Direction
+ AND State = 48
+ AND EndedAt IS NOT NULL
+ AND EndedAt BETWEEN @Start AND @End
+ GROUP BY Username, Direction, StateDescription
+ ORDER BY {sortBy} {sortOrder}, Username DESC
+ LIMIT @Limit
+ OFFSET @Offset
+ ";
+
+ using var connection = new SqliteConnection(ConnectionStrings[Database.Transfers]);
+
+ var param = new
+ {
+ Direction = direction.ToString(),
+ Limit = limit,
+ Offset = offset,
+ Start = start,
+ End = end,
+ };
+
+ var results = connection.Query<TransferSummaryRow>(sql, param);
+
+ var list = results.Select(row => new TransferSummary
+ {
+ Username = row.Username,
+ TotalBytes = row.TotalBytes,
+ Count = row.Count,
+ AverageSpeed = row.AverageSpeed,
+ AverageWait = row.AverageWait,
+ AverageDuration = row.AverageDuration,
+ }).ToList();
+
+ return list;
+ }
+
+ /// <summary>
/// Returns a list of transfer exceptions.
/// </summary>
/// <param name="direction">The direction (Upload or Download).</param>
/// <param name="start">The optional start time of the summary window.</param>
/// <param name="end">The optional end time of the summary window.</param>
/// <param name="username">The optional username by which to filter results.</param>
+ /// <param name="sortOrder">The sort order.</param>
/// <param name="limit">The number of records to return (default: 25).</param>
/// <param name="offset">The record offset (if paginating).</param>
/// <returns>A list of exceptions.</returns>
@@ -235,6 +319,7 @@ public class StatisticsService
DateTime? start = null,
DateTime? end = null,
string username = null,
+ SortOrder sortOrder = SortOrder.DESC,
int limit = 25,
int offset = 0)
{
@@ -270,7 +355,7 @@ public class StatisticsService
WHERE state & ~48
AND Direction = @Direction
{(username is not null ? "AND Username = @Username" : string.Empty)}
- ORDER BY EndedAt DESC
+ ORDER BY EndedAt {sortOrder}
LIMIT @Limit
OFFSET @Offset
";
@@ -360,90 +445,6 @@ public class StatisticsService
return results.ToList();
}
- /// <summary>
- /// Returns the top <paramref name="limit"/> user summaries by total count and direction, order by total count
- /// descending. Only successful transfers are included in counts.
- /// </summary>
- /// <param name="direction">The direction (Upload or Download) for the summary.</param>
- /// <param name="start">The optional start time of the summary window.</param>
- /// <param name="end">The optional end time of the summary window.</param>
- /// <param name="sortBy">The property by which to sort.</param>
- /// <param name="sortOrder">The sort order.</param>
- /// <param name="limit">The number of records to return (default: 25).</param>
- /// <param name="offset">The record offset (if paginating).</param>
- /// <returns>A dictionary keyed by username and containing summary information.</returns>
- /// <exception cref="ArgumentException">
- /// Thrown if end time is not later than start time, or limit is not greater than zero.
- /// </exception>
- public List<TransferSummary> GetTransferRanking(
- TransferDirection direction,
- DateTime? start = null,
- DateTime? end = null,
- LeaderboardSortBy sortBy = LeaderboardSortBy.Count,
- SortOrder sortOrder = SortOrder.DESC,
- int limit = 25,
- int offset = 0)
- {
- start ??= DateTime.MinValue;
- end ??= DateTime.MaxValue;
-
- if (end <= start)
- {
- throw new ArgumentException("End time must be later than start time");
- }
-
- if (limit <= 0)
- {
- throw new ArgumentOutOfRangeException("Limit must be greater than zero");
- }
-
- var sql = @$"
- SELECT
- Username,
- Direction,
- StateDescription,
- SUM(Size) AS TotalBytes,
- COUNT(*) AS Count,
- AVG(AverageSpeed) AS AverageSpeed,
- AVG(strftime('%s', StartedAt) - strftime('%s', EnqueuedAt)) AS AverageWait,
- AVG(strftime('%s', EndedAt) - strftime('%s', StartedAt)) AS AverageDuration
- FROM Transfers
- WHERE Direction = @Direction
- AND State = 48
- AND EndedAt IS NOT NULL
- AND EndedAt BETWEEN @Start AND @End
- GROUP BY Username, Direction, StateDescription
- ORDER BY {sortBy} {sortOrder}, Username DESC
- LIMIT @Limit
- OFFSET @Offset
- ";
-
- using var connection = new SqliteConnection(ConnectionStrings[Database.Transfers]);
-
- var param = new
- {
- Direction = direction.ToString(),
- Limit = limit,
- Offset = offset,
- Start = start,
- End = end,
- };
-
- var results = connection.Query<TransferSummaryRow>(sql, param);
-
- var list = results.Select(row => new TransferSummary
- {
- Username = row.Username,
- TotalBytes = row.TotalBytes,
- Count = row.Count,
- AverageSpeed = row.AverageSpeed,
- AverageWait = row.AverageWait,
- AverageDuration = row.AverageDuration,
- }).ToList();
-
- return list;
- }
-
private record TransferSummaryRow
{
public DateTime? Interval { get; init; }
diff --git a/src/slskd/Telemetry/Types/ErrorSummary.cs b/src/slskd/Telemetry/Types/ErrorSummary.cs
deleted file mode 100644
index b5806611..00000000
--- a/src/slskd/Telemetry/Types/ErrorSummary.cs
+++ /dev/null
@@ -1,24 +0,0 @@
-// <copyright file="ErrorSummary.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;
-
-public record ErrorSummary
-{
- public string Exception { get; init; }
- public long Count { get; init; }
-} \ No newline at end of file