From 280186e2fb4e67bdef85ef15f107194c0aa55e02 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 1 Dec 2025 18:17:30 -0600 Subject: add directory frequency --- src/slskd/Telemetry/API/StatisticsController.cs | 91 +++++++++++++++++++------ src/slskd/Telemetry/StatisticsService.cs | 81 ++++++++++++++++++++++ src/slskd/Telemetry/Types/DirectoryDetail.cs | 23 +++++++ 3 files changed, 173 insertions(+), 22 deletions(-) create mode 100644 src/slskd/Telemetry/Types/DirectoryDetail.cs (limited to 'src') diff --git a/src/slskd/Telemetry/API/StatisticsController.cs b/src/slskd/Telemetry/API/StatisticsController.cs index 7053c3a4..b535d31d 100644 --- a/src/slskd/Telemetry/API/StatisticsController.cs +++ b/src/slskd/Telemetry/API/StatisticsController.cs @@ -51,11 +51,11 @@ public class StatisticsController : ControllerBase /// /// Gets a summary of all transfer activity over the specified timeframe, grouped by direction and final state. /// - /// The start time of the summary window (default: 7 days ago). - /// The end time of the summary window (default: now). + /// The start time of the window (default: 7 days ago). + /// The end time of the window (default: now). /// An optional direction (Upload, Download) by which to filter records. /// An optional username by which to filter records. - /// A nested dictionary keyed by direction and state and containing summary information. + /// /// The request completed successfully. /// Bad request. /// An error occurred. @@ -111,12 +111,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. /// - /// The start time of the histogram window (default: 7 days ago). - /// The end time of the histogram window (default: now). + /// The start time of the window (default: 7 days ago). + /// The end time of the window (default: now). /// The interval, in minutes (default: 60). /// An optional direction (Upload, Download) by which to filter records. /// An optional username by which to filter records. - /// A nested dictionary keyed by interval, direction, and state and containing summary information. + /// /// The request completed successfully. /// Bad request. /// An error occurred. @@ -189,13 +189,13 @@ public class StatisticsController : ControllerBase /// Gets the top N user summaries by count, total bytes, or average speed. /// /// The direction (Upload, Download). - /// The start time of the summary window (default: 12/30/2025). - /// The end time of the summary window (default: now). + /// The start time of the window (default: 12/30/2025). + /// The end time of the window (default: now). /// The property by which to sort (Count, TotalBytes, AverageSpeed. Default: Count). /// The sort order (ASC, DESC. Default: DESC). /// The number of records to return (Default: 25). /// The record offset (if paginating). - /// A list of user summaries. + /// /// The request completed successfully. /// Bad request. /// An error occurred. @@ -204,7 +204,7 @@ public class StatisticsController : ControllerBase [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 500)] - public IActionResult GetSuccessfulTransferSummaryByUsername( + public IActionResult GetTransferLeaderboard( [FromQuery] string direction, [FromQuery] DateTime? start = null, [FromQuery] DateTime? end = null, @@ -275,7 +275,7 @@ public class StatisticsController : ControllerBase /// The username of the user. /// The start time of the summary window (default: 7 days ago). /// The end time of the summary window (default: now). - /// A dictionary keyed by direction and containing a detailed summary. + /// /// The request completed successfully. /// Bad request. /// An error occurred. @@ -284,7 +284,7 @@ public class StatisticsController : ControllerBase [ProducesResponseType(typeof(Dictionary), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 500)] - public IActionResult GetUser( + public IActionResult GetUserDetails( [FromRoute] string username, [FromQuery] DateTime? start = null, [FromQuery] DateTime? end = null) @@ -497,14 +497,61 @@ public class StatisticsController : ControllerBase } } - // [HttpGet("transfers/directories")] - // [Authorize(Policy = AuthPolicy.Any)] - // [ProducesResponseType(typeof(Dictionary), 200)] - // public IActionResult GetTransferSummaryByDirectory( - // [FromQuery] int? limit = null, - // [FromQuery] int? offset = null) - // { - // // todo: get a list of all directories downloaded at least once, along with the number of times downloaded (doesn't matter what status) - // return null; - // } + /// + /// Gets the top N most frequently downloaded directories by total count and distinct users. + /// + /// The start time of the window (default: 12/30/2025). + /// The end time of the window (default: now). + /// An optional username by which to filter records. + /// The number of records to return (Default: 25). + /// The record offset (if paginating). + /// + /// The request completed successfully. + /// Bad request. + /// An error occurred. + [HttpGet("transfers/directories")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(List), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 500)] + public IActionResult GetTransferDirectoryFrequency( + [FromQuery] DateTime? start = null, + [FromQuery] DateTime? end = null, + [FromQuery] string username = null, + [FromQuery] int limit = 25, + [FromQuery] int offset = 0) + { + start ??= Program.GenesisDateTime; + end ??= DateTime.MaxValue; + + if (start >= end) + { + return BadRequest("End time must be later than start time"); + } + + if (limit <= 0) + { + return BadRequest("Limit must be greater than zero"); + } + + if (offset < 0) + { + return BadRequest("Offset must be greater than or equal to zero"); + } + + try + { + return Ok(Telemetry.Statistics.GetTransferDirectoryFrequency( + start: start, + end: end, + username: username, + limit: limit, + offset: offset)); + } + catch (Exception ex) + { + Log.Error(ex, "Error fetching transfer directories over {Start}-{End}: {Message}", start, end, ex.Message); + return StatusCode(500, ex.Message); + } + } } 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(); } + /// + /// Returns a list of directories and the frequency at which they have been downloaded. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// Thrown if end time is not later than start time, or limit is not greater than zero. + /// + public List 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(sql, param); + return results.ToList(); + } + private record TransferSummaryRow { public DateTime? Interval { get; init; } diff --git a/src/slskd/Telemetry/Types/DirectoryDetail.cs b/src/slskd/Telemetry/Types/DirectoryDetail.cs new file mode 100644 index 00000000..54901507 --- /dev/null +++ b/src/slskd/Telemetry/Types/DirectoryDetail.cs @@ -0,0 +1,23 @@ +// +// 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/. +// + +public record TransferDirectorySummary +{ + public string Directory { get; init; } + public long Count { get; init; } + public long DistinctUsers { get; init; } +} \ No newline at end of file -- cgit v1.2.3