From 592660a5ca6f88375f5ec7b7543b110bbf8479ac Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 26 May 2022 16:51:38 -0500 Subject: add an endpoint to get configuration debug view --- src/slskd/Core/API/Controllers/OptionsController.cs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/slskd/Core/API/Controllers') diff --git a/src/slskd/Core/API/Controllers/OptionsController.cs b/src/slskd/Core/API/Controllers/OptionsController.cs index 062de8f7..b0856ed3 100644 --- a/src/slskd/Core/API/Controllers/OptionsController.cs +++ b/src/slskd/Core/API/Controllers/OptionsController.cs @@ -21,8 +21,10 @@ namespace slskd.Core.API { using System; using System.IO; + using System.Reflection; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; + using Microsoft.Extensions.Configuration; using Serilog; using slskd.Validation; using IOFile = System.IO.File; @@ -74,6 +76,24 @@ namespace slskd.Core.API return Ok(OptionsAtStartup.Redact()); } + /// + /// Gets the debug view of the current application options. + /// + /// + [HttpGet] + [Route("debug")] + [Authorize] + [ProducesResponseType(typeof(string), 200)] + public IActionResult Debug() + { + // retrieve the IConfigurationRoot instance with reflection to avoid + // exposing it as a public member of Program. + var property = typeof(Program).GetProperty("Configuration", BindingFlags.NonPublic | BindingFlags.Static); + var configurationRoot = (IConfigurationRoot)property.GetValue(null, null); + + return Ok(configurationRoot.GetDebugView()); + } + [HttpGet] [Authorize] [Route("yaml/location")] -- cgit v1.2.3 From 388792b916fdba7b0200d04e7d12ca8f22e33fcc Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 26 May 2022 19:07:29 -0500 Subject: move rescan share function, implement share summary --- src/slskd/Application.cs | 7 ---- .../Core/API/Controllers/ApplicationController.cs | 15 ------- .../Shares/API/Controllers/SharesController.cs | 48 +++++++++++++++++++++- src/slskd/Shares/API/DTO/SummarizedShare.cs | 32 +++++++++++++++ 4 files changed, 78 insertions(+), 24 deletions(-) create mode 100644 src/slskd/Shares/API/DTO/SummarizedShare.cs (limited to 'src/slskd/Core/API/Controllers') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 1064e56a..c2a8632b 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -48,7 +48,6 @@ namespace slskd public interface IApplication : IHostedService { public Task CheckVersionAsync(); - public Task RescanSharesAsync(); public void CollectGarbage(); } @@ -226,12 +225,6 @@ namespace slskd } } - /// - /// Re-scans shared directories. - /// - /// The operation context. - public Task RescanSharesAsync() => SharedFileCache.FillAsync(); - async Task IHostedService.StartAsync(CancellationToken cancellationToken) { Log.Information("Configuring client"); diff --git a/src/slskd/Core/API/Controllers/ApplicationController.cs b/src/slskd/Core/API/Controllers/ApplicationController.cs index cd03d96f..3cf3c0a3 100644 --- a/src/slskd/Core/API/Controllers/ApplicationController.cs +++ b/src/slskd/Core/API/Controllers/ApplicationController.cs @@ -136,20 +136,5 @@ namespace slskd.Core.API return Ok(); } - - [HttpPut] - [Route("shares")] - [Authorize] - public IActionResult RescanSharesAsync() - { - if (ApplicationStateMonitor.CurrentValue.SharedFileCache.Filling) - { - return Conflict("A share scan is already in progress."); - } - - _ = Application.RescanSharesAsync(); - - return Ok(); - } } } diff --git a/src/slskd/Shares/API/Controllers/SharesController.cs b/src/slskd/Shares/API/Controllers/SharesController.cs index 06a02de3..c569a150 100644 --- a/src/slskd/Shares/API/Controllers/SharesController.cs +++ b/src/slskd/Shares/API/Controllers/SharesController.cs @@ -33,17 +33,46 @@ namespace slskd.Shares.API [Consumes("application/json")] public class SharesController : ControllerBase { - public SharesController(IShareService shareService) + public SharesController( + IShareService shareService, + IStateMonitor applicationStateMonitor) { Shares = shareService; + ApplicationStateMonitor = applicationStateMonitor; } private IShareService Shares { get; } + private IStateMonitor ApplicationStateMonitor { get; } [HttpGet("")] public IActionResult List() { - return Ok(Shares.Cache.Shares); + var browse = Shares.Cache.Browse(); + + var summary = Shares.Cache.Shares.Select(share => + { + var directories = browse.Where(directory => directory.Name.StartsWith(share.RemotePath)); + var fileCount = directories.Aggregate(seed: 0, (sum, directory) => + { + sum += directory.FileCount; + return sum; + }); + + return new SummarizedShare() + { + Id = share.Id, + Alias = share.Alias, + IsExcluded = share.IsExcluded, + LocalPath = share.LocalPath, + Mask = share.Mask, + Raw = share.Raw, + RemotePath = share.RemotePath, + Directories = directories.Count(), + Files = fileCount, + }; + }); + + return Ok(summary); } [HttpGet("{id}")] @@ -86,5 +115,20 @@ namespace slskd.Shares.API return Ok(contents); } + + [HttpPut] + [Route("")] + [Authorize] + public IActionResult RescanSharesAsync() + { + if (ApplicationStateMonitor.CurrentValue.SharedFileCache.Filling) + { + return Conflict("A share scan is already in progress."); + } + + _ = Shares.Cache.FillAsync(); + + return Ok(); + } } } diff --git a/src/slskd/Shares/API/DTO/SummarizedShare.cs b/src/slskd/Shares/API/DTO/SummarizedShare.cs new file mode 100644 index 00000000..a1d6ed7b --- /dev/null +++ b/src/slskd/Shares/API/DTO/SummarizedShare.cs @@ -0,0 +1,32 @@ +// +// 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.Shares +{ + public record SummarizedShare + { + public string Id { get; init; } + public string Alias { get; init; } + public bool IsExcluded { get; init; } + public string LocalPath { get; init; } + public string Mask { get; init; } + public string Raw { get; init; } + public string RemotePath { get; init; } + public int Directories { get; init; } + public int Files { get; init; } + } +} \ No newline at end of file -- cgit v1.2.3 From d52ec889fcf717014e7b29359e56ff15eabd0077 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 20 Jul 2022 20:41:02 -0500 Subject: add endpoint for memory dumps --- src/slskd/Core/API/Controllers/ApplicationController.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/slskd/Core/API/Controllers') diff --git a/src/slskd/Core/API/Controllers/ApplicationController.cs b/src/slskd/Core/API/Controllers/ApplicationController.cs index 3cf3c0a3..ac8be802 100644 --- a/src/slskd/Core/API/Controllers/ApplicationController.cs +++ b/src/slskd/Core/API/Controllers/ApplicationController.cs @@ -136,5 +136,15 @@ namespace slskd.Core.API return Ok(); } + + [HttpGet("dump")] + [Authorize] + public async Task DumpMemory() + { + using var dumper = new Dumper(); + var file = await dumper.DumpAsync(); + + return PhysicalFile(file, "application/octet-stream", "slskd.dmp"); + } } } -- cgit v1.2.3 From 47324cbcaabbff71d738ab97081513608aa02fde Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 28 Aug 2022 00:38:32 -0500 Subject: stub metrics controller --- .../Core/API/Controllers/MetricsController.cs | 54 ++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/slskd/Core/API/Controllers/MetricsController.cs (limited to 'src/slskd/Core/API/Controllers') diff --git a/src/slskd/Core/API/Controllers/MetricsController.cs b/src/slskd/Core/API/Controllers/MetricsController.cs new file mode 100644 index 00000000..a4b9ecd8 --- /dev/null +++ b/src/slskd/Core/API/Controllers/MetricsController.cs @@ -0,0 +1,54 @@ +// +// 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.Core.API +{ + using System.IO; + using System.Threading.Tasks; + using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.Mvc; + + /// + /// Logs. + /// + [Route("api/v{version:apiVersion}/[controller]")] + [ApiVersion("0")] + [ApiController] + [Produces("application/json")] + [Consumes("application/json")] + public class MetricsController : ControllerBase + { + /// + /// Gets application metrics. + /// + /// + [HttpGet] + [Authorize] + public async Task Metrics() + { + using var stream = new MemoryStream(); + using var reader = new StreamReader(stream); + + await Prometheus.Metrics.DefaultRegistry.CollectAndExportAsTextAsync(stream); + stream.Position = 0; + + var response = await reader.ReadToEndAsync(); + + return Ok(response); + } + } +} -- cgit v1.2.3 From 97865d7952506ac59ad86916ae0595797502c188 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 28 Aug 2022 13:32:22 -0500 Subject: return metrics in plain text --- src/slskd/Core/API/Controllers/MetricsController.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/slskd/Core/API/Controllers') diff --git a/src/slskd/Core/API/Controllers/MetricsController.cs b/src/slskd/Core/API/Controllers/MetricsController.cs index a4b9ecd8..aeccd97a 100644 --- a/src/slskd/Core/API/Controllers/MetricsController.cs +++ b/src/slskd/Core/API/Controllers/MetricsController.cs @@ -28,8 +28,8 @@ namespace slskd.Core.API [Route("api/v{version:apiVersion}/[controller]")] [ApiVersion("0")] [ApiController] - [Produces("application/json")] - [Consumes("application/json")] + [Produces("text/plain")] + [Consumes("text/plain")] public class MetricsController : ControllerBase { /// @@ -48,7 +48,7 @@ namespace slskd.Core.API var response = await reader.ReadToEndAsync(); - return Ok(response); + return Content(response, "text/plain"); } } } -- cgit v1.2.3 From 558c550761bc30665af76c7b01d20f6606105a27 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Mon, 29 Aug 2022 12:49:43 -0500 Subject: use BuildAsync() in controller --- src/slskd/Core/API/Controllers/MetricsController.cs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) (limited to 'src/slskd/Core/API/Controllers') diff --git a/src/slskd/Core/API/Controllers/MetricsController.cs b/src/slskd/Core/API/Controllers/MetricsController.cs index aeccd97a..da2707c1 100644 --- a/src/slskd/Core/API/Controllers/MetricsController.cs +++ b/src/slskd/Core/API/Controllers/MetricsController.cs @@ -17,7 +17,6 @@ namespace slskd.Core.API { - using System.IO; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -38,16 +37,9 @@ namespace slskd.Core.API /// [HttpGet] [Authorize] - public async Task Metrics() + public async Task Get() { - using var stream = new MemoryStream(); - using var reader = new StreamReader(stream); - - await Prometheus.Metrics.DefaultRegistry.CollectAndExportAsTextAsync(stream); - stream.Position = 0; - - var response = await reader.ReadToEndAsync(); - + var response = await Metrics.BuildAsync(); return Content(response, "text/plain"); } } -- cgit v1.2.3