mirror of
https://github.com/okieselbach/AutopilotMonitor.git
synced 2026-10-24 12:42:38 +00:00
2b9bf3cd3f
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>
77 lines
3.1 KiB
C#
77 lines
3.1 KiB
C#
using System.Net;
|
|
using AutopilotMonitor.Functions.Helpers;
|
|
using AutopilotMonitor.Shared.DataAccess;
|
|
using AutopilotMonitor.Shared.Models;
|
|
using Microsoft.Azure.Functions.Worker;
|
|
using Microsoft.Azure.Functions.Worker.Http;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace AutopilotMonitor.Functions.Functions.Metrics;
|
|
|
|
public class MetricsSummaryFunction
|
|
{
|
|
private readonly ILogger<MetricsSummaryFunction> _logger;
|
|
private readonly IMetricsRepository _metricsRepo;
|
|
|
|
public MetricsSummaryFunction(ILogger<MetricsSummaryFunction> logger, IMetricsRepository metricsRepo)
|
|
{
|
|
_logger = logger;
|
|
_metricsRepo = metricsRepo;
|
|
}
|
|
|
|
[Function("MetricsSummary")]
|
|
public async Task<HttpResponseData> Run(
|
|
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "metrics/summary")] HttpRequestData req)
|
|
{
|
|
try
|
|
{
|
|
var tenantId = TenantHelper.GetTenantId(req);
|
|
var days = QueryParams.Int(req.Query["days"], @default: 30, min: 1, max: 365);
|
|
var summary = await _metricsRepo.GetMetricsSummaryAsync(tenantId, days);
|
|
|
|
var response = req.CreateResponse(HttpStatusCode.OK);
|
|
await response.WriteAsJsonAsync(new MetricsSummaryResponse { Success = true, Summary = summary, WindowDays = days });
|
|
return response;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return await req.InternalServerErrorAsync(_logger, ex, "Get metrics summary");
|
|
}
|
|
}
|
|
|
|
[Function("MetricsSummaryGlobal")]
|
|
public async Task<HttpResponseData> RunGlobal(
|
|
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "global/metrics/summary")] HttpRequestData req)
|
|
{
|
|
try
|
|
{
|
|
var days = QueryParams.Int(req.Query["days"], @default: 30, min: 1, max: 365);
|
|
// Optional tenantId filter — when set, GA scopes the cross-tenant summary to one
|
|
// tenant. When absent, returns the full cross-tenant view (null → all tenants).
|
|
var query = System.Web.HttpUtility.ParseQueryString(req.Url.Query);
|
|
var filterTenantId = query["tenantId"];
|
|
if (!string.IsNullOrWhiteSpace(filterTenantId))
|
|
{
|
|
// Query value is interpolated into an OData filter downstream — GUID-gate it here
|
|
// (belt) in addition to ODataSanitizer at the sink (braces).
|
|
if (!Guid.TryParse(filterTenantId, out var parsedTenantId))
|
|
{
|
|
return await req.BadRequestAsync("tenantId must be a GUID");
|
|
}
|
|
filterTenantId = parsedTenantId.ToString("D");
|
|
}
|
|
var summary = await _metricsRepo.GetMetricsSummaryAsync(
|
|
string.IsNullOrEmpty(filterTenantId) ? null : filterTenantId,
|
|
days);
|
|
|
|
var response = req.CreateResponse(HttpStatusCode.OK);
|
|
await response.WriteAsJsonAsync(new MetricsSummaryResponse { Success = true, Summary = summary, WindowDays = days });
|
|
return response;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return await req.InternalServerErrorAsync(_logger, ex, "Get global metrics summary");
|
|
}
|
|
}
|
|
}
|