diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-12-01 18:17:30 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2025-12-01 18:17:30 -0600 |
| commit | 280186e2fb4e67bdef85ef15f107194c0aa55e02 (patch) | |
| tree | feb7c6c90b9683e41213ecc62446b00edf750172 /src/slskd/Telemetry | |
| parent | 1f2556793d5b2e9f3ace545d43a7c3e6ea865104 (diff) | |
| download | slskd-280186e2fb4e67bdef85ef15f107194c0aa55e02.tar.gz | |
add directory frequency
Diffstat (limited to 'src/slskd/Telemetry')
| -rw-r--r-- | src/slskd/Telemetry/API/StatisticsController.cs | 91 | ||||
| -rw-r--r-- | src/slskd/Telemetry/StatisticsService.cs | 81 | ||||
| -rw-r--r-- | src/slskd/Telemetry/Types/DirectoryDetail.cs | 23 |
3 files changed, 173 insertions, 22 deletions
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 /// <summary> /// Gets a summary of all transfer activity over the specified timeframe, grouped by direction and final state. /// </summary> - /// <param name="start">The start time of the summary window (default: 7 days ago).</param> - /// <param name="end">The end time of the summary window (default: now).</param> + /// <param name="start">The start time of the window (default: 7 days ago).</param> + /// <param name="end">The end time of the window (default: now).</param> /// <param name="direction">An optional direction (Upload, Download) by which to filter records.</param> /// <param name="username">An optional username by which to filter records.</param> - /// <returns>A nested dictionary keyed by direction and state and containing summary information.</returns> + /// <returns></returns> /// <response code="200">The request completed successfully.</response> /// <response code="400">Bad request.</response> /// <response code="500">An error occurred.</response> @@ -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. /// </summary> - /// <param name="start">The start time of the histogram window (default: 7 days ago).</param> - /// <param name="end">The end time of the histogram window (default: now).</param> + /// <param name="start">The start time of the window (default: 7 days ago).</param> + /// <param name="end">The end time of the window (default: now).</param> /// <param name="interval">The interval, in minutes (default: 60).</param> /// <param name="direction">An optional direction (Upload, Download) by which to filter records.</param> /// <param name="username">An optional username by which to filter records.</param> - /// <returns>A nested dictionary keyed by interval, direction, and state and containing summary information.</returns> + /// <returns></returns> /// <response code="200">The request completed successfully.</response> /// <response code="400">Bad request.</response> /// <response code="500">An error occurred.</response> @@ -189,13 +189,13 @@ public class StatisticsController : ControllerBase /// Gets the top N user summaries by count, total bytes, or average speed. /// </summary> /// <param name="direction">The direction (Upload, Download).</param> - /// <param name="start">The start time of the summary window (default: 12/30/2025).</param> - /// <param name="end">The end time of the summary window (default: now).</param> + /// <param name="start">The start time of the window (default: 12/30/2025).</param> + /// <param name="end">The end time of the window (default: now).</param> /// <param name="sortBy">The property by which to sort (Count, TotalBytes, AverageSpeed. Default: Count).</param> /// <param name="sortOrder">The sort order (ASC, DESC. Default: DESC).</param> /// <param name="limit">The number of records to return (Default: 25).</param> /// <param name="offset">The record offset (if paginating).</param> - /// <returns>A list of user summaries.</returns> + /// <returns></returns> /// <response code="200">The request completed successfully.</response> /// <response code="400">Bad request.</response> /// <response code="500">An error occurred.</response> @@ -204,7 +204,7 @@ public class StatisticsController : ControllerBase [ProducesResponseType(typeof(List<TransferSummary>), 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 /// <param name="username">The username of the user.</param> /// <param name="start">The start time of the summary window (default: 7 days ago).</param> /// <param name="end">The end time of the summary window (default: now).</param> - /// <returns>A dictionary keyed by direction and containing a detailed summary.</returns> + /// <returns></returns> /// <response code="200">The request completed successfully.</response> /// <response code="400">Bad request.</response> /// <response code="500">An error occurred.</response> @@ -284,7 +284,7 @@ public class StatisticsController : ControllerBase [ProducesResponseType(typeof(Dictionary<TransferDirection, UserDirectionTransferSummary>), 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<string, int>), 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; - // } + /// <summary> + /// Gets the top N most frequently downloaded directories by total count and distinct users. + /// </summary> + /// <param name="start">The start time of the window (default: 12/30/2025).</param> + /// <param name="end">The end time of the window (default: now).</param> + /// <param name="username">An optional username by which to filter records.</param> + /// <param name="limit">The number of records to return (Default: 25).</param> + /// <param name="offset">The record offset (if paginating).</param> + /// <returns></returns> + /// <response code="200">The request completed successfully.</response> + /// <response code="400">Bad request.</response> + /// <response code="500">An error occurred.</response> + [HttpGet("transfers/directories")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(List<TransferDirectorySummary>), 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(); } + /// <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; } 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 file="DirectoryDetail.cs" company="slskd Team"> +// 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/. +// </copyright> + +public record TransferDirectorySummary +{ + public string Directory { get; init; } + public long Count { get; init; } + public long DistinctUsers { get; init; } +}
\ No newline at end of file |
