From f2460e95bfdcf7c800a2ef2b26af17dcf9210e65 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 7 Dec 2025 16:08:34 -0600 Subject: Statistics -> Reports --- src/slskd/Telemetry/API/ReportsController.cs | 557 +++++++++++++++++++++++++++ 1 file changed, 557 insertions(+) create mode 100644 src/slskd/Telemetry/API/ReportsController.cs (limited to 'src/slskd/Telemetry/API/ReportsController.cs') diff --git a/src/slskd/Telemetry/API/ReportsController.cs b/src/slskd/Telemetry/API/ReportsController.cs new file mode 100644 index 00000000..da088800 --- /dev/null +++ b/src/slskd/Telemetry/API/ReportsController.cs @@ -0,0 +1,557 @@ +// +// 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/. +// + +namespace slskd.Telemetry; + +using System; +using System.Collections.Generic; +using System.Linq; +using Asp.Versioning; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Serilog; +using Soulseek; + +/// +/// Reports. +/// +[Route("api/v{version:apiVersion}/telemetry/[controller]")] +[Tags("Telemetry")] +[ApiVersion("0")] +[ApiController] +[Produces("application/json")] +public class ReportsController : ControllerBase +{ + /// + /// Initializes a new instance of the class. + /// + public ReportsController(TelemetryService telemetryService) + { + Telemetry = telemetryService; + } + + private TelemetryService Telemetry { get; } + private ILogger Log { get; } = Serilog.Log.ForContext(); + + /// + /// Gets a summary of all transfer activity over the specified timeframe, grouped by direction and final state. + /// + /// 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. + /// + /// The request completed successfully. + /// Bad request. + /// An error occurred. + [HttpGet("transfers/summary")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(Dictionary>), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 500)] + public IActionResult GetTransferSummary( + [FromQuery] DateTime? start = null, + [FromQuery] DateTime? end = null, + [FromQuery] string direction = null, + [FromQuery] string username = null) + { + var now = DateTime.UtcNow; + + start ??= now.AddDays(-7); + end ??= now; + + if (start >= end) + { + return BadRequest("End time must be later than start time"); + } + + TransferDirection? transferDirection = null; + + if (!string.IsNullOrWhiteSpace(direction)) + { + if (!Enum.TryParse(direction, ignoreCase: true, out var parsedDirection)) + { + return BadRequest($"Invalid direction; expected one of: {string.Join(", ", Enum.GetNames(typeof(TransferDirection)))}"); + } + + transferDirection = parsedDirection; + } + + try + { + return Ok(Telemetry.Reports.GetTransferSummary( + start: start.Value, + end: end, + direction: transferDirection, + username: username)); + } + catch (Exception ex) + { + Log.Error(ex, "Error fetching transfer summary over {Start}-{End}: {Message}", start, end, ex.Message); + return StatusCode(500, ex.Message); + } + } + + /// + /// 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 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. + /// + /// The request completed successfully. + /// Bad request. + /// An error occurred. + [HttpGet("transfers/histogram")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(Dictionary>>), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 500)] + public IActionResult GetTransferHistogram( + [FromQuery] DateTime? start = null, + [FromQuery] DateTime? end = null, + [FromQuery] int interval = 60, + [FromQuery] string direction = null, + [FromQuery] string username = null) + { + var now = DateTime.UtcNow; + + start ??= now.AddDays(-7); + end ??= now; + + if (start >= end) + { + return BadRequest("End time must be later than start time"); + } + + // clamp the start time to the earliest reasonable date to avoid returning a ton of empty intervals + // and introducing performance issues client side + if (start < Program.GenesisDateTime) + { + Log.Warning("A start time prior to the genesis date of the application was supplied; the start time has been adjusted to the genesis time {Genesis}", Program.GenesisDateTime); + start = Program.GenesisDateTime; + } + + if (interval < 5) + { + return BadRequest("Interval must be greater than or equal to 5"); + } + + var intervalTimeSpan = TimeSpan.FromMinutes(interval); + + TransferDirection? transferDirection = null; + + if (!string.IsNullOrWhiteSpace(direction)) + { + if (!Enum.TryParse(direction, ignoreCase: true, out var parsedDirection)) + { + return BadRequest($"Invalid direction; expected one of: {string.Join(", ", Enum.GetNames(typeof(TransferDirection)))}"); + } + + transferDirection = parsedDirection; + } + + try + { + return Ok(Telemetry.Reports.GetTransferHistogram( + start: start.Value, + end: end.Value, + interval: intervalTimeSpan, + direction: transferDirection, + username: username)); + } + catch (Exception ex) + { + Log.Error(ex, "Error fetching transfer histogram over {Start}-{End}/{Interval}: {Message}", start, end, interval, ex.Message); + return StatusCode(500, ex.Message); + } + } + + /// + /// Gets the top N user summaries by count, total bytes, or average speed. + /// + /// The direction (Upload, Download). + /// 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). + /// + /// The request completed successfully. + /// Bad request. + /// An error occurred. + [HttpGet("transfers/leaderboard")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(List), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 500)] + public IActionResult GetTransferLeaderboard( + [FromQuery] string direction, + [FromQuery] DateTime? start = null, + [FromQuery] DateTime? end = null, + [FromQuery] string sortBy = "Count", + [FromQuery] string sortOrder = "DESC", + [FromQuery] int limit = 25, + [FromQuery] int offset = 0) + { + if (string.IsNullOrWhiteSpace(direction)) + { + return BadRequest("Direction is required"); + } + + if (!Enum.TryParse(direction, ignoreCase: true, out var parsedDirection)) + { + return BadRequest($"Invalid direction; expected one of: {string.Join(", ", Enum.GetNames(typeof(TransferDirection)))}"); + } + + start ??= Program.GenesisDateTime; + end ??= DateTime.UtcNow; + + if (start >= end) + { + return BadRequest("End time must be later than start time"); + } + + if (!Enum.TryParse(sortBy, out var parsedSortBy)) + { + return BadRequest($"Invalid sortBy; expected one of: {string.Join(", ", Enum.GetNames(typeof(LeaderboardSortBy)))}"); + } + + if (!Enum.TryParse(sortOrder, out var parsedSortOrder)) + { + return BadRequest($"Invalid sortOrder; expected one of: {string.Join(", ", Enum.GetNames(typeof(SortOrder)))}"); + } + + 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.Reports.GetTransferLeaderboard( + direction: parsedDirection, + start: start.Value, + end: end, + sortBy: parsedSortBy, + sortOrder: parsedSortOrder, + limit: limit, + offset: offset)); + } + catch (Exception ex) + { + Log.Error(ex, "Error fetching transfer leaderboard over {Start}-{End}: {Message}", start, end, ex.Message); + return StatusCode(500, ex.Message); + } + } + + /// + /// Gets detailed transfer activity for the specified user. + /// + /// 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). + /// + /// The request completed successfully. + /// Bad request. + /// An error occurred. + [HttpGet("transfers/users/{username}")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(Dictionary), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 500)] + public IActionResult GetUserDetails( + [FromRoute] string username, + [FromQuery] DateTime? start = null, + [FromQuery] DateTime? end = null) + { + if (string.IsNullOrWhiteSpace(username)) + { + return BadRequest("Username is required"); + } + + start ??= Program.GenesisDateTime; + end ??= DateTime.UtcNow; + + if (start >= end) + { + return BadRequest("End time must be later than start time"); + } + + static UserDirectionTransferStatistics GetStatistics(Dictionary summary) + { + var totalTransfers = summary.Values.Sum(x => x.Count); + var successful = summary.Where(x => x.Key == TransferStates.Succeeded).Sum(x => x.Value.Count); + var errored = summary.Where(x => new[] { TransferStates.Errored, TransferStates.TimedOut, TransferStates.Rejected, TransferStates.Aborted }.Contains(x.Key)).Sum(x => x.Value.Count); + var cancelled = summary.Where(x => x.Key == TransferStates.Cancelled).Sum(x => x.Value.Count); + + return new UserDirectionTransferStatistics + { + Total = totalTransfers, + Successful = successful, + Errored = errored, + Cancelled = cancelled, + }; + } + + try + { + var results = new Dictionary(); + + var summary = Telemetry.Reports.GetTransferSummary(start, end, username: username); + + results.Add(TransferDirection.Upload, new UserDirectionTransferSummary + { + Summary = summary[TransferDirection.Upload], + Statistics = GetStatistics(summary[TransferDirection.Upload]), + Exceptions = Telemetry.Reports.GetTransferExceptionsPareto(TransferDirection.Upload, start, end, username: username, limit: 25, offset: 0), + }); + + results.Add(TransferDirection.Download, new UserDirectionTransferSummary + { + Summary = summary[TransferDirection.Download], + Statistics = GetStatistics(summary[TransferDirection.Download]), + Exceptions = Telemetry.Reports.GetTransferExceptionsPareto(TransferDirection.Download, start, end, username: username, limit: 25, offset: 0), + }); + + return Ok(results); + } + catch (Exception ex) + { + Log.Error(ex, "Error fetching user details for {Username}: {Message}", username, ex.Message); + return StatusCode(500, ex.Message); + } + } + + /// + /// Gets a list of transfer exceptions by direction. + /// + /// The direction. + /// The start time. + /// The end time. + /// An optional username by which to filter exceptions. + /// The sort order (ASC, DESC. Default: DESC). + /// The number of records to return (Default: 25). + /// The record offset (if paginating). + /// + /// The request completed successfully. + /// Bad request. + /// An error occurred. + [HttpGet("transfers/exceptions")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(List), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 500)] + public IActionResult GetTransferExceptions( + [FromQuery] string direction = null, + [FromQuery] DateTime? start = null, + [FromQuery] DateTime? end = null, + [FromQuery] string username = null, + [FromQuery] string sortOrder = "DESC", + [FromQuery] int limit = 25, + [FromQuery] int offset = 0) + { + if (string.IsNullOrWhiteSpace(direction)) + { + return BadRequest("Direction is required"); + } + + if (!Enum.TryParse(direction, ignoreCase: true, out var parsedDirection)) + { + return BadRequest($"Invalid direction; expected one of: {string.Join(", ", Enum.GetNames(typeof(TransferDirection)))}"); + } + + start ??= DateTime.MinValue; + end ??= DateTime.MaxValue; + + if (start >= end) + { + return BadRequest("End time must be later than start time"); + } + + if (!Enum.TryParse(sortOrder, out var parsedSortOrder)) + { + return BadRequest($"Invalid sortOrder; expected one of: {string.Join(", ", Enum.GetNames(typeof(SortOrder)))}"); + } + + 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.Reports.GetTransferExceptions( + direction: parsedDirection, + start: start.Value, + end: end.Value, + username: username, + sortOrder: parsedSortOrder, + limit: limit, + offset: offset)); + } + catch (Exception ex) + { + Log.Error(ex, "Error fetching transfer exceptions over {Start}-{End}: {Message}", start, end, ex.Message); + return StatusCode(500, ex.Message); + } + } + + /// + /// Gets the top N exceptions by total count and direction. + /// + /// The direction. + /// The start time. + /// The end time. + /// An optional username by which to filter exceptions. + /// The number of records to return (Default: 25). + /// The record offset (if paginating). + /// + /// The request completed successfully. + /// Bad request. + /// An error occurred. + [HttpGet("transfers/exceptions/pareto")] + [Authorize(Policy = AuthPolicy.Any)] + [ProducesResponseType(typeof(List), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 500)] + public IActionResult GetTransferExceptionsPareto( + [FromQuery] string direction, + [FromQuery] DateTime? start = null, + [FromQuery] DateTime? end = null, + [FromQuery] string username = null, + [FromQuery] int limit = 25, + [FromQuery] int offset = 0) + { + if (string.IsNullOrWhiteSpace(direction)) + { + return BadRequest("Direction is required"); + } + + if (!Enum.TryParse(direction, ignoreCase: true, out var parsedDirection)) + { + return BadRequest($"Invalid direction; expected one of: {string.Join(", ", Enum.GetNames(typeof(TransferDirection)))}"); + } + + start ??= DateTime.MinValue; + 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.Reports.GetTransferExceptionsPareto( + direction: parsedDirection, + start: start.Value, + end: end.Value, + limit: limit, + offset: offset, + username: username)); + } + catch (Exception ex) + { + Log.Error(ex, "Error fetching transfer exceptions over {Start}-{End}: {Message}", start, end, ex.Message); + return StatusCode(500, ex.Message); + } + } + + /// + /// 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.Reports.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); + } + } +} -- cgit v1.2.3