aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-11-30 13:13:06 -0600
committerJP Dillingham <jp@dillingham.me>2025-11-30 13:13:06 -0600
commitcd671b78c29f8eb90f906fdbfc0ee7a87d66a3ca (patch)
treeb3480a50013bc80af0394616b12c8992fb791c1f
parent56b8301a25ce937fd7977ea84333097608244445 (diff)
downloadslskd-cd671b78c29f8eb90f906fdbfc0ee7a87d66a3ca.tar.gz
require direction for pareto
-rw-r--r--src/slskd/Telemetry/API/StatisticsController.cs28
-rw-r--r--src/slskd/Telemetry/StatisticsService.cs30
2 files changed, 19 insertions, 39 deletions
diff --git a/src/slskd/Telemetry/API/StatisticsController.cs b/src/slskd/Telemetry/API/StatisticsController.cs
index ababddf2..302c0705 100644
--- a/src/slskd/Telemetry/API/StatisticsController.cs
+++ b/src/slskd/Telemetry/API/StatisticsController.cs
@@ -240,9 +240,9 @@ public class StatisticsController : ControllerBase
/// <summary>
/// Returns the top N exceptions by total count and direction.
/// </summary>
+ /// <param name="direction">The direction by which to filter activity.</param>
/// <param name="start">The start time.</param>
/// <param name="end">The end time.</param>
- /// <param name="direction">An optional direction by which to filter activity.</param>
/// <param name="username">An optional username by which to filter exceptions.</param>
/// <param name="limit">The number of records to return (Default: 25).</param>
/// <param name="offset">The record offset (if paginating).</param>
@@ -256,41 +256,39 @@ public class StatisticsController : ControllerBase
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 500)]
public IActionResult GetTransferExceptionsPareto(
+ [FromQuery] string direction,
[FromQuery] DateTime? start = null,
[FromQuery] DateTime? end = null,
- [FromQuery] string direction = null,
[FromQuery] string username = null,
[FromQuery] int limit = 25,
[FromQuery] int offset = 0)
{
- start ??= DateTime.MinValue;
- end ??= DateTime.MaxValue;
+ if (string.IsNullOrWhiteSpace(direction))
+ {
+ return BadRequest("Direction is required");
+ }
- if (start >= end)
+ if (!Enum.TryParse<TransferDirection>(direction, ignoreCase: true, out var parsedDirection))
{
- return BadRequest("End time must be later than start time");
+ return BadRequest($"Invalid direction; expected one of: {string.Join(", ", Enum.GetNames(typeof(TransferDirection)))}");
}
- TransferDirection? transferDirection = null;
+ start ??= DateTime.MinValue;
+ end ??= DateTime.MaxValue;
- if (!string.IsNullOrWhiteSpace(direction))
+ if (start >= end)
{
- if (!Enum.TryParse<TransferDirection>(direction, ignoreCase: true, out var parsedDirection))
- {
- return BadRequest($"Invalid direction; expected one of: {string.Join(", ", Enum.GetNames(typeof(TransferDirection)))}");
- }
-
- transferDirection = parsedDirection;
+ return BadRequest("End time must be later than start time");
}
try
{
return Ok(Telemetry.Statistics.GetTransferExceptionsPareto(
+ direction: parsedDirection,
start: start.Value,
end: end.Value,
limit: limit,
offset: offset,
- direction: transferDirection,
username: username));
}
catch (Exception ex)
diff --git a/src/slskd/Telemetry/StatisticsService.cs b/src/slskd/Telemetry/StatisticsService.cs
index 7db44893..3bd9b26b 100644
--- a/src/slskd/Telemetry/StatisticsService.cs
+++ b/src/slskd/Telemetry/StatisticsService.cs
@@ -293,9 +293,9 @@ public class StatisticsService
/// <summary>
/// Returns a summary of distinct transfer exceptions and the number of times they occurred.
/// </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="direction">The direction (Upload or Download) for the summary.</param>
/// <param name="username">The optional username by which to filter results.</param>
/// <param name="limit">The number of records to return (default: 25).</param>
/// <param name="offset">The record offset (if paginating).</param>
@@ -303,10 +303,10 @@ public class StatisticsService
/// <exception cref="ArgumentException">
/// Thrown if end time is not later than start time, or limit is not greater than zero.
/// </exception>
- public Dictionary<TransferDirection, List<TransferExceptionSummary>> GetTransferExceptionsPareto(
+ public List<TransferExceptionSummary> GetTransferExceptionsPareto(
+ TransferDirection direction,
DateTime? start = null,
DateTime? end = null,
- TransferDirection? direction = null,
string username = null,
int limit = 25,
int offset = 0)
@@ -319,15 +319,8 @@ public class StatisticsService
throw new ArgumentException("End time must be later than start time");
}
- var dict = new Dictionary<TransferDirection, List<TransferExceptionSummary>>()
- {
- { TransferDirection.Download, new List<TransferExceptionSummary>() },
- { TransferDirection.Upload, new List<TransferExceptionSummary>() },
- };
-
var sql = @$"
SELECT
- Direction,
CASE
WHEN INSTR(Exception, ':') > 0
THEN SUBSTR(Exception, INSTR(Exception, ':') + 2)
@@ -337,7 +330,7 @@ public class StatisticsService
COUNT(DISTINCT Username) as DistinctUsers
FROM transfers
WHERE state & ~48
- {(direction is not null ? "AND Direction = @Direction" : string.Empty)}
+ AND Direction = @Direction
{(username is not null ? "AND Username = @Username" : string.Empty)}
GROUP BY Direction,
CASE
@@ -362,19 +355,8 @@ public class StatisticsService
Offset = offset,
};
- var results = connection.Query<ExceptionSummaryRow>(sql, param);
-
- foreach (var result in results)
- {
- dict[result.Direction].Add(new TransferExceptionSummary
- {
- Exception = result.Exception,
- Count = result.Count,
- DistinctUsers = result.DistinctUsers,
- });
- }
-
- return dict;
+ var results = connection.Query<TransferExceptionSummary>(sql, param);
+ return results.ToList();
}
/// <summary>