aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Telemetry/StatisticsService.cs
diff options
context:
space:
mode:
authorJP Dillingham <jp@dillingham.me>2025-12-01 18:17:30 -0600
committerJP Dillingham <jp@dillingham.me>2025-12-01 18:17:30 -0600
commit280186e2fb4e67bdef85ef15f107194c0aa55e02 (patch)
treefeb7c6c90b9683e41213ecc62446b00edf750172 /src/slskd/Telemetry/StatisticsService.cs
parent1f2556793d5b2e9f3ace545d43a7c3e6ea865104 (diff)
downloadslskd-280186e2fb4e67bdef85ef15f107194c0aa55e02.tar.gz
add directory frequency
Diffstat (limited to 'src/slskd/Telemetry/StatisticsService.cs')
-rw-r--r--src/slskd/Telemetry/StatisticsService.cs81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/slskd/Telemetry/StatisticsService.cs b/src/slskd/Telemetry/StatisticsService.cs
index 3199fd0c..53b543f4 100644
--- a/src/slskd/Telemetry/StatisticsService.cs
+++ b/src/slskd/Telemetry/StatisticsService.cs
@@ -331,6 +331,11 @@ public class StatisticsService
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
Id,
@@ -405,6 +410,11 @@ public class StatisticsService
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
CASE
@@ -445,6 +455,77 @@ public class StatisticsService
return results.ToList();
}
+ /// <summary>
+ /// Returns a list of directories and the frequency at which they have been downloaded.
+ /// </summary>
+ /// <param name="start"></param>
+ /// <param name="end"></param>
+ /// <param name="username"></param>
+ /// <param name="sortOrder"></param>
+ /// <param name="limit"></param>
+ /// <param name="offset"></param>
+ /// <returns></returns>
+ /// <exception cref="ArgumentException">
+ /// Thrown if end time is not later than start time, or limit is not greater than zero.
+ /// </exception>
+ public List<TransferDirectorySummary> GetTransferDirectoryFrequency(
+ DateTime? start = null,
+ DateTime? end = null,
+ string username = null,
+ 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 = @$"
+ WITH directories AS (
+ SELECT
+ RTRIM(
+ RTRIM(filename, REPLACE(REPLACE(filename, '/', ''), '\', '')), '/\'
+ ) AS Directory,
+ Username
+ FROM transfers
+ WHERE Direction = 'Upload'
+ AND State = 48
+ AND EndedAt IS NOT NULL
+ AND EndedAt BETWEEN @Start AND @End
+ {(username is not null ? "AND Username = @Username" : string.Empty)}
+ )
+ SELECT Directory, COUNT(*) AS Count, COUNT(DISTINCT Username) AS DistinctUsers
+ FROM directories
+ GROUP BY Directory
+ ORDER BY DistinctUsers DESC, Count DESC
+ LIMIT @Limit
+ OFFSET @Offset
+ ";
+
+ using var connection = new SqliteConnection(ConnectionStrings[Database.Transfers]);
+
+ var param = new
+ {
+ Start = start,
+ End = end,
+ Limit = limit,
+ Offset = offset,
+ Username = username,
+ };
+
+ var results = connection.Query<TransferDirectorySummary>(sql, param);
+ return results.ToList();
+ }
+
private record TransferSummaryRow
{
public DateTime? Interval { get; init; }