mirror of
https://github.com/okieselbach/AutopilotMonitor.git
synced 2026-10-26 05:29:43 +00:00
5163d7d6a0
73 sites in 68 files built the query collection with HttpUtility.ParseQueryString(req.Url.Query), 15 files used req.Query and five mixed both. HttpRequestData.Query is that same call, cached per request, and the ASP.NET Core request type does not override it, so the change is behaviour-neutral. - QueryParamGuardTests: ParseQueryString in the Functions project is limited to a named baseline of three sites that parse something other than the request query. - A fact pins req.Query against HttpUtility.ParseQueryString (repeated keys, encoded values, caching) so a worker update that changes the implementation fails a test. - Test mocks that set Url answer Query through CallBase; a loose mock returns null. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
79 lines
2.9 KiB
C#
79 lines
2.9 KiB
C#
using System.Net;
|
|
using AutopilotMonitor.Functions.Services;
|
|
using AutopilotMonitor.Functions.Helpers;
|
|
using Microsoft.ApplicationInsights;
|
|
using Microsoft.Azure.Functions.Worker;
|
|
using Microsoft.Azure.Functions.Worker.Http;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace AutopilotMonitor.Functions.Functions.Metrics
|
|
{
|
|
/// <summary>
|
|
/// GET /api/global/metrics/sla?tenantId={tenantId}&months=3
|
|
/// Returns SLA compliance metrics for any tenant (Global Admin only).
|
|
/// </summary>
|
|
public class GetGlobalSlaMetricsFunction
|
|
{
|
|
private readonly ILogger<GetGlobalSlaMetricsFunction> _logger;
|
|
private readonly SlaMetricsService _slaMetricsService;
|
|
private readonly TelemetryClient _telemetryClient;
|
|
|
|
public GetGlobalSlaMetricsFunction(
|
|
ILogger<GetGlobalSlaMetricsFunction> logger,
|
|
SlaMetricsService slaMetricsService,
|
|
TelemetryClient telemetryClient)
|
|
{
|
|
_logger = logger;
|
|
_slaMetricsService = slaMetricsService;
|
|
_telemetryClient = telemetryClient;
|
|
}
|
|
|
|
[Function("GetGlobalSlaMetrics")]
|
|
public async Task<HttpResponseData> Run(
|
|
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "global/metrics/sla")]
|
|
HttpRequestData req)
|
|
{
|
|
// Authentication + GlobalAdminOnly authorization enforced by PolicyEnforcementMiddleware
|
|
_logger.LogInformation("Global SLA metrics requested");
|
|
|
|
string? tenantId = null;
|
|
int months = 3;
|
|
bool fresh = false;
|
|
|
|
try
|
|
{
|
|
var qs = req.Query;
|
|
tenantId = qs.Get("tenantId");
|
|
|
|
if (string.IsNullOrWhiteSpace(tenantId))
|
|
{
|
|
return await req.BadRequestAsync("tenantId query parameter is required");
|
|
}
|
|
|
|
months = QueryParams.Int(qs.Get("months"), months, 1, SlaMetricsService.MaxMonths);
|
|
|
|
fresh = qs.Get("fresh") == "1";
|
|
|
|
var metrics = await _slaMetricsService.ComputeSlaMetricsAsync(tenantId, months, fresh);
|
|
|
|
_telemetryClient.TrackEvent("GlobalSlaMetricsRequested", new Dictionary<string, string>
|
|
{
|
|
["TenantId"] = tenantId,
|
|
["Months"] = months.ToString(),
|
|
["Fresh"] = fresh.ToString(),
|
|
["FromCache"] = metrics.FromCache.ToString(),
|
|
["ComputeDurationMs"] = metrics.ComputeDurationMs.ToString(),
|
|
});
|
|
|
|
var response = req.CreateResponse(HttpStatusCode.OK);
|
|
await response.WriteAsJsonAsync(metrics);
|
|
return response;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return await req.InternalServerErrorAsync(_logger, ex, "GetGlobalSlaMetrics");
|
|
}
|
|
}
|
|
}
|
|
}
|