Build / Build (push) Successful in 35s
All 384 routes, the Mastodon API, the client API and the frozen federation paths, are read from one class with one nested class per controller. Their values are unchanged, checked one by one against the old literals; decePubClient keeps its own APIs class. Checked against GoToSocial in the pasture: 64 passed, 0 failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W4YtEJrgxG12Pc39Fai8Bd
79 lines
3.5 KiB
C#
79 lines
3.5 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using PrivaPub.ClientModels;
|
|
using PrivaPub.Domain.Statistics;
|
|
using PrivaPub.Infrastructure.Jobs;
|
|
using PrivaPub.Infrastructure.Statistics;
|
|
using PrivaPub.Models.Jobs;
|
|
|
|
using System.Globalization;
|
|
|
|
namespace PrivaPub.Controllers.ClientToServer
|
|
{
|
|
[ApiController,
|
|
Route(ApiPaths.Statistics.Prefix),
|
|
Authorize(Policy = Policies.IsAdmin)]
|
|
public class StatisticsController(
|
|
StatisticsQueries queries,
|
|
IJobQueue queue,
|
|
Microsoft.Extensions.Options.IOptionsMonitor<StatisticsOptions> options) : ControllerBase
|
|
{
|
|
[HttpGet, Route(ApiPaths.Statistics.GET_Crawler)]
|
|
public async Task<IActionResult> Crawler(CancellationToken token) =>
|
|
Ok(await queries.Crawler(options.CurrentValue.Crawler.Enabled, options.CurrentValue.Crawler.RevisitDays, token));
|
|
|
|
[HttpGet, Route(ApiPaths.Statistics.GET_Overview)]
|
|
public async Task<IActionResult> Overview([FromServices] Domain.Media.IMediaService media, [FromQuery] int days = 30, CancellationToken token = default)
|
|
{
|
|
var overview = await queries.Overview(days, token);
|
|
overview.Media.ProxyCacheBytes = Bytes(media.ProxyRoot);
|
|
overview.Media.TrashBytes = Bytes(media.TrashRoot);
|
|
return Ok(overview);
|
|
}
|
|
|
|
static long Bytes(string root) =>
|
|
Directory.Exists(root) ? new DirectoryInfo(root).EnumerateFiles("*", SearchOption.AllDirectories).Sum(f => f.Length) : 0;
|
|
|
|
[HttpGet, Route(ApiPaths.Statistics.GET_Hosts)]
|
|
public async Task<IActionResult> Hosts([FromQuery] int days = 30, [FromQuery] string sort = default, [FromQuery] string software = default,
|
|
[FromQuery] int page = 1, [FromQuery] int limit = 50, CancellationToken token = default) =>
|
|
Ok(await queries.Hosts(days, sort, software, page, limit, token));
|
|
|
|
[HttpGet, Route(ApiPaths.Statistics.GET_M_Host)]
|
|
public async Task<IActionResult> Host(string host, [FromQuery] int days = 90, CancellationToken token = default) =>
|
|
await queries.Host(host, days, token) is { } found ? Ok(found) : NotFound();
|
|
|
|
[HttpGet, Route(ApiPaths.Statistics.GET_Events)]
|
|
public async Task<IActionResult> Events([FromQuery] string host = default, [FromQuery] string channel = default, [FromQuery] string outcome = default,
|
|
[FromQuery] string reason = default, [FromQuery] DateTime? before = default, [FromQuery] int limit = 100, CancellationToken token = default) =>
|
|
Ok(await queries.Events(host, channel, outcome, reason, before, limit, token));
|
|
|
|
[HttpGet, Route(ApiPaths.Statistics.GET_Server)]
|
|
public async Task<IActionResult> Server([FromQuery] int days = 30, CancellationToken token = default) =>
|
|
Ok(await queries.Server(days, token));
|
|
|
|
[HttpPost, Route(ApiPaths.Statistics.POST_M_Rollup)]
|
|
public async Task<IActionResult> Rollup(string day, CancellationToken token)
|
|
{
|
|
if (!DateTime.TryParseExact(day, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal, out var parsed)
|
|
|| parsed.Date >= DateTime.UtcNow.Date)
|
|
return BadRequest();
|
|
var job = RollupJob.For(parsed.Date, $"rollup|{day}|rerun|{DateTime.UtcNow.Ticks}");
|
|
job.RunAt = DateTime.UtcNow;
|
|
await queue.EnqueueMany(new[] { job }, token);
|
|
return Accepted();
|
|
}
|
|
|
|
[HttpPost, Route(ApiPaths.Statistics.POST_M_Describe)]
|
|
public async Task<IActionResult> Describe(string host, CancellationToken token)
|
|
{
|
|
host = Interactions.Host(host);
|
|
if (host == Interactions.Unknown)
|
|
return BadRequest();
|
|
await queue.Enqueue(JobKind.DescribeInstance, host, host, $"describe|{host}|admin|{DateTime.UtcNow.Ticks}", token);
|
|
return Accepted();
|
|
}
|
|
}
|
|
}
|