Files
AutopilotMonitor/src/Backend/AutopilotMonitor.Functions/Functions/Metrics/UsageMetricsFunction.cs
T
Oliver Kieselbach 2b9bf3cd3f backend: one query-parameter parser (QueryParams) with a lenient and a strict contract, ratcheted (D-202)
Helpers/QueryParams replaces fourteen private days/limit parsers, the three
TryParseUtc copies, eleven identical pageSize blocks and ~50 inline TryParse sites
under Functions/ and Pagination/. Lenient (Int/IntOrNull/UtcInstant): absent, garbage
or below-minimum means the call site's default, above-maximum clamps. Strict
(TryInt/TryPageSize/TryUtcInstant): absent is null, anything else invalid is a 400
with a message that names the parameter. Integers are invariant-culture, instants are
invariant UTC (AssumeUniversal | AdjustToUniversal), whitespace counts as absent.
Defaults and ranges stay arguments at every call site.

Settled differences: the Apps windows clamp above 365 instead of falling back to 30,
days=0 or negative yields the default everywhere, the two global geo endpoints and
the session list cap days at 365, SLA months is capped at SlaMetricsService.MaxMonths
on the HTTP layer, a whitespace date means no filter on every route, the admin batch
sizes reject values above their maximum, and the three culture-dependent
DateTime.TryParse sites parse as UTC.

QueryParamGuardTests ratchets TryParse out of Functions/ and Pagination/ (baseline:
headers, raw cells, the agent version major, the se= of a stored SAS URL);
QueryParamsTests carries the cases the removed copies pinned.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-05 23:26:21 +02:00

60 lines
2.2 KiB
C#

using System.Net;
using AutopilotMonitor.Functions.Helpers;
using AutopilotMonitor.Functions.Services;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
namespace AutopilotMonitor.Functions.Functions.Metrics
{
/// <summary>
/// Function for retrieving tenant-specific usage metrics (Tenant Admin)
/// </summary>
public class UsageMetricsFunction
{
private readonly ILogger<UsageMetricsFunction> _logger;
private readonly UsageMetricsService _usageMetricsService;
public UsageMetricsFunction(
ILogger<UsageMetricsFunction> logger,
UsageMetricsService usageMetricsService)
{
_logger = logger;
_usageMetricsService = usageMetricsService;
}
/// <summary>
/// GET /api/metrics/usage?tenantId={tenantId} - Compute and return tenant-specific usage metrics
/// On-demand computation with 5-minute cache (Tenant Admin)
/// </summary>
[Function("GetTenantUsageMetrics")]
public async Task<HttpResponseData> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "metrics/usage")]
HttpRequestData req)
{
_logger.LogInformation("Tenant usage metrics requested");
try
{
// Authentication + MemberRead authorization enforced by PolicyEnforcementMiddleware
string tenantId = TenantHelper.GetTenantId(req);
string userIdentifier = TenantHelper.GetUserIdentifier(req);
var days = QueryParams.Int(req.Query["days"], @default: 90, min: 1, max: 365);
_logger.LogInformation("Fetching usage metrics for tenant {TenantId} by user {User} (days={Days})", tenantId, userIdentifier, days);
var metrics = await _usageMetricsService.ComputeTenantUsageMetricsAsync(tenantId, days);
var response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteAsJsonAsync(metrics);
return response;
}
catch (Exception ex)
{
return await req.InternalServerErrorAsync(_logger, ex, "UsageMetrics");
}
}
}
}