diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-11-30 12:51:03 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2025-11-30 12:51:03 -0600 |
| commit | c84f12161fa46ee95c4e0d447864b9a5cf80c6d1 (patch) | |
| tree | 6937a9938af75eff3c76abbcfeb49673c642039a /src/slskd/Telemetry/StatisticsService.cs | |
| parent | bf8341a9f0eda057b81657017c2dd1f8646a08dd (diff) | |
| download | slskd-c84f12161fa46ee95c4e0d447864b9a5cf80c6d1.tar.gz | |
exceptions
Diffstat (limited to 'src/slskd/Telemetry/StatisticsService.cs')
| -rw-r--r-- | src/slskd/Telemetry/StatisticsService.cs | 195 |
1 files changed, 149 insertions, 46 deletions
diff --git a/src/slskd/Telemetry/StatisticsService.cs b/src/slskd/Telemetry/StatisticsService.cs index 0b78f57e..a5e55a9b 100644 --- a/src/slskd/Telemetry/StatisticsService.cs +++ b/src/slskd/Telemetry/StatisticsService.cs @@ -22,6 +22,7 @@ using System.Collections.Generic; using System.Linq; using Dapper; using Microsoft.Data.Sqlite; +using Serilog; using Soulseek; /// <summary> @@ -38,6 +39,7 @@ public class StatisticsService } private ConnectionStringDictionary ConnectionStrings { get; } + private ILogger Log { get; } = Serilog.Log.ForContext<MetricsController>(); /// <summary> /// Returns a summary of all transfer data grouped by direction and final transfer state. Includes only @@ -75,6 +77,7 @@ public class StatisticsService StateDescription, SUM(Size) AS TotalBytes, COUNT(*) AS Count, + COUNT(DISTINCT Username) AS DistinctUsers, AVG(AverageSpeed) AS AverageSpeed, AVG(strftime('%s', StartedAt) - strftime('%s', EnqueuedAt)) AS AverageWait, AVG(strftime('%s', EndedAt) - strftime('%s', StartedAt)) AS AverageDuration @@ -104,6 +107,7 @@ public class StatisticsService { TotalBytes = result.TotalBytes, Count = result.Count, + DistinctUsers = result.DistinctUsers, AverageSpeed = result.AverageSpeed, AverageWait = result.AverageWait, AverageDuration = result.AverageDuration, @@ -159,6 +163,7 @@ public class StatisticsService StateDescription, SUM(Size) AS TotalBytes, COUNT(*) AS Count, + COUNT(DISTINCT Username) AS Users, AVG(AverageSpeed) AS AverageSpeed, COALESCE(AVG(strftime('%s', StartedAt) - strftime('%s', EnqueuedAt)), 0.0) AS AverageWait, COALESCE(AVG(strftime('%s', EndedAt) - strftime('%s', StartedAt)), 0.0) AS AverageDuration @@ -190,6 +195,7 @@ public class StatisticsService { TotalBytes = result.TotalBytes, Count = result.Count, + DistinctUsers = result.DistinctUsers, AverageSpeed = result.AverageSpeed, AverageWait = result.AverageWait, AverageDuration = result.AverageDuration, @@ -211,22 +217,23 @@ public class StatisticsService } /// <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. + /// Returns a list of transfer exceptions. /// </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> - /// <returns>A dictionary keyed by username and containing summary information.</returns> + /// <returns>A dictionary keyed by direction and containing a list of exceptions and the number of times they occurred.</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, + public Dictionary<TransferDirection, List<TransferExceptionDetail>> GetTransferExceptions( DateTime? start = null, DateTime? end = null, + TransferDirection? direction = null, + string username = null, int limit = 25, int offset = 0) { @@ -238,28 +245,38 @@ public class StatisticsService throw new ArgumentException("End time must be later than start time"); } - if (limit <= 0) + var dict = new Dictionary<TransferDirection, List<TransferExceptionDetail>>() { - throw new ArgumentOutOfRangeException("Limit must be greater than zero"); - } + { TransferDirection.Download, new List<TransferExceptionDetail>() }, + { TransferDirection.Upload, new List<TransferExceptionDetail>() }, + }; var sql = @$" SELECT + Id, 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 Count DESC, TotalBytes DESC, Username ASC + Filename, + Size, + StartOffset, + State, + RequestedAt + EnqueuedAt, + StartedAt, + EndedAt, + BytesTransferred, + AverageSpeed, + CASE + WHEN INSTR(Exception, ':') > 0 + THEN SUBSTR(Exception, INSTR(Exception, ':') + 2) + ELSE Exception + END as Exception + 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 + ORDER BY EndedAt DESC LIMIT @Limit OFFSET @Offset "; @@ -269,29 +286,27 @@ public class StatisticsService var param = new { Direction = direction.ToString(), - Limit = limit, - Offset = offset, + Username = username, Start = start, End = end, + Limit = limit, + Offset = offset, }; - var results = connection.Query<TransferSummaryRow>(sql, param); + var results = connection.Query<TransferExceptionDetail>(sql, param); - var list = results.Select(row => new TransferSummary + Log.Debug("Result rows for {Method}, filters: {Params}: {Count}", nameof(GetTransferExceptions), param, results?.Count()); + + foreach (var result in results) { - Username = row.Username, - TotalBytes = row.TotalBytes, - Count = row.Count, - AverageSpeed = row.AverageSpeed, - AverageWait = row.AverageWait, - AverageDuration = row.AverageDuration, - }).ToList(); + dict[result.Direction].Add(result); + } - return list; + return dict; } /// <summary> - /// Returns a summary of distinct transfer errors and the number of times they occurred. + /// Returns a summary of distinct transfer exceptions and the number of times they occurred. /// </summary> /// <param name="start">The optional start time of the summary window.</param> /// <param name="end">The optional end time of the summary window.</param> @@ -299,11 +314,11 @@ public class StatisticsService /// <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> - /// <returns></returns> + /// <returns>A dictionary keyed by direction and containing a list of exceptions and the number of times they occurred.</returns> /// <exception cref="ArgumentException"> /// Thrown if end time is not later than start time, or limit is not greater than zero. /// </exception> - public Dictionary<string, long> GetTransferErrorSummary( + public Dictionary<TransferDirection, List<TransferExceptionSummary>> GetTransferExceptionsPareto( DateTime? start = null, DateTime? end = null, TransferDirection? direction = null, @@ -319,12 +334,11 @@ public class StatisticsService throw new ArgumentException("End time must be later than start time"); } - if (limit <= 0) + var dict = new Dictionary<TransferDirection, List<TransferExceptionSummary>>() { - throw new ArgumentOutOfRangeException("Limit must be greater than zero"); - } - - var dict = new Dictionary<string, long>(); + { TransferDirection.Download, new List<TransferExceptionSummary>() }, + { TransferDirection.Upload, new List<TransferExceptionSummary>() }, + }; var sql = @$" SELECT @@ -334,7 +348,8 @@ public class StatisticsService THEN SUBSTR(Exception, INSTR(Exception, ':') + 2) ELSE Exception END as Exception, - COUNT(*) as Count + COUNT(*) as Count, + COUNT(DISTINCT Username) as DistinctUsers FROM transfers WHERE state & ~48 {(direction is not null ? "AND Direction = @Direction" : string.Empty)} @@ -362,16 +377,101 @@ public class StatisticsService Offset = offset, }; - var results = connection.Query<ErrorSummaryRow>(sql, param); + var results = connection.Query<ExceptionSummaryRow>(sql, param); foreach (var result in results) { - dict.Add(result.Exception, result.Count); + dict[result.Direction].Add(new TransferExceptionSummary + { + Exception = result.Exception, + Count = result.Count, + DistinctUsers = result.DistinctUsers, + }); } return dict; } + /// <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="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, + 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 Count DESC, TotalBytes DESC, Username ASC + 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; } @@ -380,14 +480,17 @@ public class StatisticsService public TransferStates StateDescription { get; init; } public long TotalBytes { get; init; } public long Count { get; init; } + public long DistinctUsers { get; init; } public double AverageSpeed { get; init; } public double AverageWait { get; init; } public double AverageDuration { get; init; } } - private record ErrorSummaryRow + private record ExceptionSummaryRow { + public TransferDirection Direction { get; init; } public string Exception { get; init; } public long Count { get; init; } + public long DistinctUsers { get; init; } } }
\ No newline at end of file |
