1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-07 13:48:00 -05:00
Ayymoss 0a8bbf2997 Add instance uptime to API info response
This update includes the instance uptime in the API info response.
2024-09-06 19:19:40 -05:00

58 lines
2.1 KiB
C#

using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Data.Models;
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, Reference.Game? game = null, 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, game, token);
var (maxConcurrent, maxConcurrentTime) = await _serverDataViewer.MaxConcurrentClientsAsync(overPeriod: duration, token: token);
var uptime = DateTime.Now - Process.GetCurrentProcess().StartTime;
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
},
Uptime = uptime,
};
return Json(response);
}
}