aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Telemetry/API
diff options
context:
space:
mode:
Diffstat (limited to 'src/slskd/Telemetry/API')
-rw-r--r--src/slskd/Telemetry/API/MetricsController.cs (renamed from src/slskd/Telemetry/API/TelemetryController.cs)82
-rw-r--r--src/slskd/Telemetry/API/ReportsController.cs557
2 files changed, 614 insertions, 25 deletions
diff --git a/src/slskd/Telemetry/API/TelemetryController.cs b/src/slskd/Telemetry/API/MetricsController.cs
index 2207ac8c..3ed7c1e2 100644
--- a/src/slskd/Telemetry/API/TelemetryController.cs
+++ b/src/slskd/Telemetry/API/MetricsController.cs
@@ -1,4 +1,4 @@
-// <copyright file="TelemetryController.cs" company="slskd Team">
+// <copyright file="MetricsController.cs" company="slskd Team">
// Copyright (c) slskd Team. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
@@ -23,30 +23,33 @@ using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
+using Serilog;
/// <summary>
/// Telemetry.
/// </summary>
-[Route("api/v{version:apiVersion}/[controller]")]
+[Route("api/v{version:apiVersion}/telemetry/[controller]")]
+[Tags("Telemetry")]
[ApiVersion("0")]
[ApiController]
[Produces("application/json")]
-public class TelemetryController : ControllerBase
+public class MetricsController : ControllerBase
{
/// <summary>
- /// Initializes a new instance of the <see cref="TelemetryController"/> class.
+ /// Initializes a new instance of the <see cref="MetricsController"/> class.
/// </summary>
- /// <param name="telemetryService"></param>
- public TelemetryController(TelemetryService telemetryService)
+ public MetricsController(TelemetryService telemetryService)
{
Telemetry = telemetryService;
}
private TelemetryService Telemetry { get; }
+ private ILogger Log { get; } = Serilog.Log.ForContext<MetricsController>();
/// <summary>
- /// KPIs for the app, based on the Prometheus dashboard
+ /// Gets KPIs for the app, based on the Prometheus dashboard
/// * process_Working_set_bytes = total memory used
/// * dotnet_total_memory_bytes = sum of all managed memory
/// * process_cpu_seconds_total = total cpu time spent (not a great measure)
@@ -57,10 +60,10 @@ public class TelemetryController : ControllerBase
/// * node_filesystem_avail_bytes = how much space is left on the system
/// * node_network = info about network adapters on the system
/// Other
- /// * process_start_time_seconds = unix timestamp of start time
+ /// * process_start_time_seconds = unix timestamp of start time.
/// </summary>
- private List<Regex> KpiRegexes { get; } = new List<Regex>
- {
+ private List<Regex> KpiRegexes { get; } =
+ [
new Regex("slskd_.*", RegexOptions.Compiled),
new Regex("node_(?!cpu)", RegexOptions.Compiled),
new Regex("process_.*", RegexOptions.Compiled),
@@ -68,36 +71,65 @@ public class TelemetryController : ControllerBase
new Regex("system_runtime_[cpu_usage|working_set|alloc_total]", RegexOptions.Compiled),
new Regex("system_net_sockets.*", RegexOptions.Compiled),
new Regex("microsoft_aspnetcore_server_kestrel_[current|total]_connections", RegexOptions.Compiled),
- };
+ ];
/// <summary>
- /// Gets application metrics.
+ /// Gets all application metrics.
/// </summary>
- /// <returns></returns>
- [HttpGet("metrics")]
+ /// <remarks>
+ /// If the 'Accept' header is set to 'text/plain', the response is in Prometheus format. Otherwise if 'application/json' is set,
+ /// the metrics are formatted into a dictionary.
+ /// </remarks>
+ /// <returns>A flat Prometheus-formatted list of all metrics given text/plain, a dictionary keyed by metric name otherwise.</returns>
+ /// <response code="200">The request completed successfully.</response>
+ /// <response code="500">An error occurred.</response>
+ [HttpGet("")]
[Authorize(Policy = AuthPolicy.Any)]
[Produces("text/plain", "application/json")]
+ [ProducesResponseType(typeof(string), 200, "text/plain")]
+ [ProducesResponseType(typeof(Dictionary<string, PrometheusMetric>), 200, "application/json")]
+ [ProducesResponseType(typeof(string), 500)]
public async Task<IActionResult> Get()
{
- if (Request.Headers.Accept.ToString().Equals("application/json", StringComparison.OrdinalIgnoreCase))
+ try
{
- Dictionary<string, PrometheusMetric> dict = await Telemetry.Prometheus.GetMetricsAsObject();
- return Ok(dict);
- }
+ if (Request.Headers.Accept.ToString().Equals("application/json", StringComparison.OrdinalIgnoreCase))
+ {
+ Dictionary<string, PrometheusMetric> dict = await Telemetry.Prometheus.GetMetricsAsObject();
+ return Ok(dict);
+ }
- var response = await Telemetry.Prometheus.GetMetricsAsString();
- return Content(response, "text/plain");
+ var response = await Telemetry.Prometheus.GetMetricsAsString();
+ return Content(response, "text/plain");
+ }
+ catch (Exception ex)
+ {
+ Log.Error(ex, "Error fetching metrics: {Message}", ex.Message);
+ return StatusCode(500, ex.Message);
+ }
}
/// <summary>
- /// Gets gets key performance indicators for the application.
+ /// Gets gets key performance indicators (KPIs) for the application.
/// </summary>
- /// <returns></returns>
- [HttpGet("metrics/kpi")]
+ /// <returns>A dictionary keyed by metric name.</returns>
+ /// <response code="200">The request completed successfully.</response>
+ /// <response code="500">An error occurred.</response>
+ [HttpGet("kpis")]
[Authorize(Policy = AuthPolicy.Any)]
+ [ProducesResponseType(typeof(Dictionary<string, PrometheusMetric>), 200)]
+ [ProducesResponseType(typeof(string), 500)]
public async Task<IActionResult> GetKpis()
{
- Dictionary<string, PrometheusMetric> dict = await Telemetry.Prometheus.GetMetricsAsObject(include: KpiRegexes);
- return Ok(dict);
+ try
+ {
+ Dictionary<string, PrometheusMetric> dict = await Telemetry.Prometheus.GetMetricsAsObject(include: KpiRegexes);
+ return Ok(dict);
+ }
+ catch (Exception ex)
+ {
+ Log.Error(ex, "Error fetching KPIs: {Message}", ex.Message);
+ return StatusCode(500, ex.Message);
+ }
}
}
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 file="ReportsController.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>
+
+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;
+
+/// <summary>
+/// Reports.
+/// </summary>
+[Route("api/v{version:apiVersion}/telemetry/[controller]")]
+[Tags("Telemetry")]
+[ApiVersion("0")]
+[ApiController]
+[Produces("application/json")]
+public class ReportsController : ControllerBase
+{
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ReportsController"/> class.
+ /// </summary>
+ public ReportsController(TelemetryService telemetryService)
+ {
+ Telemetry = telemetryService;
+ }
+
+ private TelemetryService Telemetry { get; }
+ private ILogger Log { get; } = Serilog.Log.ForContext<ReportsController>();
+
+ /// <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 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></returns>
+ /// <response code="200">The request completed successfully.</response>
+ /// <response code="400">Bad request.</response>
+ /// <response code="500">An error occurred.</response>
+ [HttpGet("transfers/summary")]
+ [Authorize(Policy = AuthPolicy.Any)]
+ [ProducesResponseType(typeof(Dictionary<TransferDirection, Dictionary<TransferStates, TransferSummary>>), 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<TransferDirection>(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);
+ }
+ }
+
+ /// <summary>
+ /// 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 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></returns>
+ /// <response code="200">The request completed successfully.</response>
+ /// <response code="400">Bad request.</response>
+ /// <response code="500">An error occurred.</response>
+ [HttpGet("transfers/histogram")]
+ [Authorize(Policy = AuthPolicy.Any)]
+ [ProducesResponseType(typeof(Dictionary<DateTime, Dictionary<TransferDirection, Dictionary<TransferStates, TransferSummary>>>), 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<TransferDirection>(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);
+ }
+ }
+
+ /// <summary>
+ /// 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 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></returns>
+ /// <response code="200">The request completed successfully.</response>
+ /// <response code="400">Bad request.</response>
+ /// <response code="500">An error occurred.</response>
+ [HttpGet("transfers/leaderboard")]
+ [Authorize(Policy = AuthPolicy.Any)]
+ [ProducesResponseType(typeof(List<TransferSummary>), 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<TransferDirection>(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<LeaderboardSortBy>(sortBy, out var parsedSortBy))
+ {
+ return BadRequest($"Invalid sortBy; expected one of: {string.Join(", ", Enum.GetNames(typeof(LeaderboardSortBy)))}");
+ }
+
+ if (!Enum.TryParse<SortOrder>(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);
+ }
+ }
+
+ /// <summary>
+ /// Gets detailed transfer activity for the specified user.
+ /// </summary>
+ /// <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></returns>
+ /// <response code="200">The request completed successfully.</response>
+ /// <response code="400">Bad request.</response>
+ /// <response code="500">An error occurred.</response>
+ [HttpGet("transfers/users/{username}")]
+ [Authorize(Policy = AuthPolicy.Any)]
+ [ProducesResponseType(typeof(Dictionary<TransferDirection, UserDirectionTransferSummary>), 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<TransferStates, TransferSummary> 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<TransferDirection, UserDirectionTransferSummary>();
+
+ 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);
+ }
+ }
+
+ /// <summary>
+ /// Gets a list of transfer exceptions by direction.
+ /// </summary>
+ /// <param name="direction">The direction.</param>
+ /// <param name="start">The start time.</param>
+ /// <param name="end">The end time.</param>
+ /// <param name="username">An optional username by which to filter exceptions.</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></returns>
+ /// <response code="200">The request completed successfully.</response>
+ /// <response code="400">Bad request.</response>
+ /// <response code="500">An error occurred.</response>
+ [HttpGet("transfers/exceptions")]
+ [Authorize(Policy = AuthPolicy.Any)]
+ [ProducesResponseType(typeof(List<TransferExceptionDetail>), 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<TransferDirection>(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>(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);
+ }
+ }
+
+ /// <summary>
+ /// Gets the top N exceptions by total count and direction.
+ /// </summary>
+ /// <param name="direction">The direction.</param>
+ /// <param name="start">The start time.</param>
+ /// <param name="end">The end time.</param>
+ /// <param name="username">An optional username by which to filter exceptions.</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/exceptions/pareto")]
+ [Authorize(Policy = AuthPolicy.Any)]
+ [ProducesResponseType(typeof(List<TransferExceptionSummary>), 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<TransferDirection>(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);
+ }
+ }
+
+ /// <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.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);
+ }
+ }
+}