aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-09-01 19:11:42 -0500
committerJP Dillingham <jp@dillingham.me>2025-09-01 19:11:42 -0500
commit6943d88786c733a83924e02084552b952e50c491 (patch)
tree2035487dbb8442366839c524ffdc822d7166208f /src
parentca5c6e3c5a970a987c06ad66ede1df34767bc93c (diff)
downloadslskd-6943d88786c733a83924e02084552b952e50c491.tar.gz
add error summary
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Telemetry/API/TelemetryController.cs96
-rw-r--r--src/slskd/Telemetry/StatisticsService.cs70
-rw-r--r--src/slskd/Telemetry/Types/ErrorSummary.cs7
3 files changed, 136 insertions, 37 deletions
diff --git a/src/slskd/Telemetry/API/TelemetryController.cs b/src/slskd/Telemetry/API/TelemetryController.cs
index 8a58d3e7..a02b3e05 100644
--- a/src/slskd/Telemetry/API/TelemetryController.cs
+++ b/src/slskd/Telemetry/API/TelemetryController.cs
@@ -19,7 +19,6 @@ namespace slskd.Telemetry;
using System;
using System.Collections.Generic;
-using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Asp.Versioning;
@@ -132,53 +131,66 @@ public class TelemetryController : ControllerBase
}
/// <summary>
- /// Summarizes transfer statistics for the specified direction, grouped by final transfer state,
- /// for all users, and for the specified time range.
+ /// Returns the top N user summaries by total count and direction.
/// </summary>
- /// <param name="direction">The direction (Upload or Download).</param>
/// <param name="start">The start time.</param>
/// <param name="end">The end time.</param>
- /// <returns>A dictionary keyed by state and containing summary information.</returns>
- [HttpGet("statistics/transfers/{direction}")]
+ /// <param name="limit">The number of records to return (Default: 25).</param>
+ /// <param name="offset">The record offset (if paginating).</param>
+ /// <returns></returns>
+ [HttpGet("statistics/transfers/users")]
[Authorize(Policy = AuthPolicy.Any)]
- [ProducesResponseType(typeof(Dictionary<TransferStates, TransferSummary>), 200)]
- public IActionResult GetTransferSummaryByDirection(
- [FromRoute, Required] string direction,
+ [ProducesResponseType(typeof(Dictionary<TransferDirection, List<UserTransferSummary>>), 200)]
+ public IActionResult GetSuccessfulTransferSummaryByUsername(
[FromQuery] DateTime? start = null,
- [FromQuery] DateTime? end = null)
+ [FromQuery] DateTime? end = null,
+ [FromQuery] int? limit = null,
+ [FromQuery] int? offset = null)
{
start ??= DateTime.MinValue;
end ??= DateTime.MaxValue;
+ limit ??= 25;
+ offset ??= 0;
- if (!Enum.TryParse<TransferDirection>(direction, ignoreCase: true, out var directionEnum))
+ if (start >= end)
{
- return BadRequest($"Direction must be one of: {string.Join(", ", Enum.GetNames(typeof(TransferDirection)))}");
+ return BadRequest("End time must be later than start time");
}
- if (start >= end)
+ if (limit <= 0)
{
- return BadRequest("End time must be later than start time");
+ return BadRequest("Limit must be greater than zero");
}
- var data = Telemetry.Statistics.GetTransferSummary(start.Value, end, directionEnum);
+ if (offset < 0)
+ {
+ return BadRequest("Offset must be greater than or equal to zero");
+ }
- return Ok(data[directionEnum]);
+ var downloads = Telemetry.Statistics.GetSuccessfulTransferSummaryByDirectionAndUsername(direction: TransferDirection.Download, start.Value, end, limit: limit.Value, offset: offset.Value);
+ var uploads = Telemetry.Statistics.GetSuccessfulTransferSummaryByDirectionAndUsername(direction: TransferDirection.Upload, start.Value, end, limit: limit.Value, offset: offset.Value);
+
+ var dict = new Dictionary<TransferDirection, List<TransferSummary>>()
+ {
+ { TransferDirection.Download, downloads },
+ { TransferDirection.Upload, uploads },
+ };
+
+ return Ok(dict);
}
/// <summary>
- /// Returns the top N user summaries for the specified direction, by total count and direction.
+ /// Returns the top N errors by total count and direction.
/// </summary>
- /// <param name="direction">The direction (Upload or Download).</param>
/// <param name="start">The start time.</param>
/// <param name="end">The end time.</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("statistics/transfers/{direction}/users")]
+ [HttpGet("statistics/transfers/errors")]
[Authorize(Policy = AuthPolicy.Any)]
- [ProducesResponseType(typeof(List<UserTransferSummary>), 200)]
- public IActionResult GetSuccessfulTransferSummaryByDirectionAndUsername(
- [FromRoute] string direction,
+ [ProducesResponseType(typeof(Dictionary<TransferDirection, Dictionary<TransferStates, TransferSummary>>), 200)]
+ public IActionResult GetTransferErrorSummary(
[FromQuery] DateTime? start = null,
[FromQuery] DateTime? end = null,
[FromQuery] int? limit = null,
@@ -189,30 +201,40 @@ public class TelemetryController : ControllerBase
limit ??= 25;
offset ??= 0;
- if (!Enum.TryParse<TransferDirection>(direction, ignoreCase: true, out var directionEnum))
+ if (start >= end)
{
- return BadRequest($"Direction must be one of: {string.Join(", ", Enum.GetNames(typeof(TransferDirection)))}");
+ return BadRequest("End time must be later than start time");
}
- if (start >= end)
+ if (limit <= 0)
{
- return BadRequest("End time must be later than start time");
+ return BadRequest("Limit must be greater than zero");
}
- return Ok(Telemetry.Statistics.GetSuccessfulTransferSummaryByDirectionAndUsername(directionEnum, start.Value, end, limit: limit.Value, offset: offset.Value));
+ if (offset < 0)
+ {
+ return BadRequest("Offset must be greater than or equal to zero");
+ }
+
+ var downloads = Telemetry.Statistics.GetTransferErrorSummary(start, end, TransferDirection.Download, limit: limit.Value, offset: offset.Value);
+ var uploads = Telemetry.Statistics.GetTransferErrorSummary(start, end, TransferDirection.Upload, limit: limit.Value, offset: offset.Value);
+
+ var dict = new Dictionary<TransferDirection, Dictionary<string, long>>()
+ {
+ { TransferDirection.Download, downloads },
+ { TransferDirection.Upload, uploads },
+ };
+
+ return Ok(dict);
}
- [HttpGet("statistics/transfers/users/{username}")]
+ [HttpGet("statistics/transfers/errors")]
[Authorize(Policy = AuthPolicy.Any)]
- [ProducesResponseType(typeof(Dictionary<TransferDirection, Dictionary<TransferStates, TransferSummary>>), 200)]
- public IActionResult GetTransferSummaryByUsername(
- [FromRoute] string username,
- [FromQuery] DateTime? start = null,
- [FromQuery] DateTime? end = null)
+ [ProducesResponseType(typeof(UserSummary), 200)]
+ public IActionResult GetUserStatistics(
+ [FromQuery] string username)
{
- start ??= DateTime.MinValue;
- end ??= DateTime.MaxValue;
-
- return Ok(Telemetry.Statistics.GetTransferSummary(start, end, username: username));
+ // todo: return everything we know about this user
+ return Ok();
}
}
diff --git a/src/slskd/Telemetry/StatisticsService.cs b/src/slskd/Telemetry/StatisticsService.cs
index 57c44ac4..ae072685 100644
--- a/src/slskd/Telemetry/StatisticsService.cs
+++ b/src/slskd/Telemetry/StatisticsService.cs
@@ -193,6 +193,70 @@ public class StatisticsService
return list;
}
+ public Dictionary<string, long> GetTransferErrorSummary(
+ DateTime? start = null,
+ DateTime? end = null,
+ TransferDirection? direction = null,
+ string username = null,
+ 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");
+ }
+
+ var dict = new Dictionary<string, long>();
+
+ var sql = @$"
+ SELECT
+ Direction,
+ CASE
+ WHEN INSTR(Exception, ':') > 0
+ THEN SUBSTR(Exception, INSTR(Exception, ':') + 2)
+ ELSE Exception
+ END as Exception,
+ COUNT(*) as Count
+ FROM transfers
+ WHERE state & ~48
+ {(direction is not null ? "AND Direction = @Direction" : string.Empty)}
+ {(username is not null ? "AND Username = @Username" : string.Empty)}
+ GROUP BY Direction,
+ CASE
+ WHEN INSTR(Exception, ':') > 0
+ THEN SUBSTR(Exception, INSTR(Exception, ':') + 2)
+ ELSE Exception
+ END
+ ORDER BY Count DESC
+ LIMIT @Limit
+ OFFSET @Offset
+ ";
+
+ using var connection = new SqliteConnection(ConnectionStrings[Database.Transfers]);
+
+ var param = new
+ {
+ Direction = direction.ToString(),
+ Username = username,
+ Start = start,
+ End = end,
+ Limit = limit,
+ Offset = offset,
+ };
+
+ var results = connection.Query<ErrorSummaryRow>(sql, param);
+
+ foreach (var result in results)
+ {
+ dict.Add(result.Exception, result.Count);
+ }
+
+ return dict;
+ }
+
private record TransferSummaryRow
{
public string Username { get; init; }
@@ -204,4 +268,10 @@ public class StatisticsService
public double AverageWait { get; init; }
public double AverageDuration { get; init; }
}
+
+ private record ErrorSummaryRow
+ {
+ public string Exception { get; init; }
+ public long Count { get; init; }
+ }
} \ No newline at end of file
diff --git a/src/slskd/Telemetry/Types/ErrorSummary.cs b/src/slskd/Telemetry/Types/ErrorSummary.cs
new file mode 100644
index 00000000..f39d4902
--- /dev/null
+++ b/src/slskd/Telemetry/Types/ErrorSummary.cs
@@ -0,0 +1,7 @@
+namespace slskd.Telemetry;
+
+public record ErrorSummary
+{
+ public string Exception { get; init; }
+ public long Count { get; init; }
+} \ No newline at end of file