Add info api for #231
This commit is contained in:
parent
e32e97b9e6
commit
064879fead
21
WebfrontCore/Controllers/API/Dtos/InfoResponse.cs
Normal file
21
WebfrontCore/Controllers/API/Dtos/InfoResponse.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System;
|
||||
|
||||
namespace WebfrontCore.Controllers.API.Dtos;
|
||||
|
||||
public class InfoResponse
|
||||
{
|
||||
public int TotalConnectedClients { get; set; }
|
||||
public int TotalClientSlots { get; set; }
|
||||
public int TotalTrackedClients { get; set; }
|
||||
public MetricSnapshot<int> TotalRecentClients { get; set; }
|
||||
|
||||
public MetricSnapshot<int?> MaxConcurrentClients { get; set; }
|
||||
}
|
||||
|
||||
public class MetricSnapshot<T>
|
||||
{
|
||||
public T Value { get; set; }
|
||||
public DateTime? Time { get; set; }
|
||||
public DateTime? StartAt { get; set; }
|
||||
public DateTime? EndAt { get; set; }
|
||||
}
|
53
WebfrontCore/Controllers/API/Info.cs
Normal file
53
WebfrontCore/Controllers/API/Info.cs
Normal file
@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SharedLibraryCore;
|
||||
using SharedLibraryCore.Interfaces;
|
||||
using WebfrontCore.Controllers.API.Dtos;
|
||||
|
||||
namespace WebfrontCore.Controllers.API;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class Info : BaseController
|
||||
{
|
||||
private readonly IServerDataViewer _serverDataViewer;
|
||||
|
||||
public Info(IManager manager, IServerDataViewer serverDataViewer) : base(manager)
|
||||
{
|
||||
_serverDataViewer = serverDataViewer;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Get(int period = 24, CancellationToken token = default)
|
||||
{
|
||||
// todo: this is hardcoded currently because the cache doesn't take into consideration the duration, so
|
||||
// we could impact the webfront usage too
|
||||
var duration = TimeSpan.FromHours(24);
|
||||
var (totalClients, totalRecentClients) =
|
||||
await _serverDataViewer.ClientCountsAsync(duration, token);
|
||||
var (maxConcurrent, maxConcurrentTime) = await _serverDataViewer.MaxConcurrentClientsAsync(overPeriod: duration, token: token);
|
||||
var response = new InfoResponse
|
||||
{
|
||||
TotalTrackedClients = totalClients,
|
||||
TotalConnectedClients = Manager.GetActiveClients().Count,
|
||||
TotalClientSlots = Manager.GetServers().Sum(server => server.MaxClients),
|
||||
MaxConcurrentClients = new MetricSnapshot<int?>
|
||||
{
|
||||
Value = maxConcurrent, Time = maxConcurrentTime,
|
||||
EndAt = DateTime.UtcNow,
|
||||
StartAt = DateTime.UtcNow - duration
|
||||
},
|
||||
TotalRecentClients = new MetricSnapshot<int>
|
||||
{
|
||||
Value = totalRecentClients,
|
||||
EndAt = DateTime.UtcNow,
|
||||
StartAt = DateTime.UtcNow - duration
|
||||
}
|
||||
};
|
||||
|
||||
return Json(response);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user