Files
AutopilotMonitor/src/Backend/AutopilotMonitor.Functions/Functions/GetPlatformStatsFunction.cs
T
Oliver Kieselbach 19e9713a31 added platform stats
2026-02-18 12:19:52 +01:00

78 lines
3.2 KiB
C#

using System.Net;
using AutopilotMonitor.Functions.Services;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
namespace AutopilotMonitor.Functions.Functions
{
/// <summary>
/// Public endpoint - no authentication required.
/// Returns pre-computed platform stats for the landing page.
/// </summary>
public class GetPlatformStatsFunction
{
private readonly ILogger<GetPlatformStatsFunction> _logger;
private readonly TableStorageService _storageService;
public GetPlatformStatsFunction(ILogger<GetPlatformStatsFunction> logger, TableStorageService storageService)
{
_logger = logger;
_storageService = storageService;
}
[Function("GetPlatformStats")]
public async Task<HttpResponseData> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "platform-stats")] HttpRequestData req)
{
try
{
var stats = await _storageService.GetPlatformStatsAsync();
var response = req.CreateResponse(HttpStatusCode.OK);
if (stats == null)
{
// No stats computed yet - return zeros
// TODO: get values from azure blob storage /stats/platform-stats.json, calculation is done nightly in maintenance function and not here
// maybe used later for SWA-API passthrough - andvantage same does for usage metrics
// alternative landing receives stats from blob storage and not from this endpoint, then we can remove this endpoint and call blob storage directly from landing page (CORS)
await response.WriteAsJsonAsync(new
{
totalEnrollments = 0,
totalUsers = 0,
totalTenants = 0,
uniqueDeviceModels = 0,
totalEventsProcessed = 0,
successfulEnrollments = 0,
issuesDetected = 0,
lastUpdated = (DateTime?)null
});
}
else
{
await response.WriteAsJsonAsync(new
{
totalEnrollments = stats.TotalEnrollments,
totalUsers = stats.TotalUsers,
totalTenants = stats.TotalTenants,
uniqueDeviceModels = stats.UniqueDeviceModels,
totalEventsProcessed = stats.TotalEventsProcessed,
successfulEnrollments = stats.SuccessfulEnrollments,
issuesDetected = stats.IssuesDetected
});
}
return response;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error fetching platform stats");
var errorResponse = req.CreateResponse(HttpStatusCode.InternalServerError);
await errorResponse.WriteAsJsonAsync(new { error = "Failed to retrieve platform stats" });
return errorResponse;
}
}
}
}