mirror of
https://github.com/okieselbach/AutopilotMonitor.git
synced 2026-10-25 05:00:07 +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>
76 lines
3.1 KiB
C#
76 lines
3.1 KiB
C#
using System.Net;
|
|
using AutopilotMonitor.Functions.Helpers;
|
|
using AutopilotMonitor.Shared.DataAccess;
|
|
using Microsoft.Azure.Functions.Worker;
|
|
using Microsoft.Azure.Functions.Worker.Http;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace AutopilotMonitor.Functions.Functions.Apps
|
|
{
|
|
/// <summary>
|
|
/// GET /api/apps/{appName}/analytics?days=30
|
|
/// Per-tenant drill-down for a single app: time series, version breakdown,
|
|
/// installer phase breakdown, top failure codes, device-model correlation.
|
|
/// </summary>
|
|
public class GetAppAnalyticsFunction
|
|
{
|
|
private readonly ILogger<GetAppAnalyticsFunction> _logger;
|
|
private readonly IMetricsRepository _metricsRepo;
|
|
private readonly ISessionRepository _sessionRepo;
|
|
private readonly IHardwareRejectionNotificationTracker _notificationTracker;
|
|
|
|
public GetAppAnalyticsFunction(
|
|
ILogger<GetAppAnalyticsFunction> logger,
|
|
IMetricsRepository metricsRepo,
|
|
ISessionRepository sessionRepo,
|
|
IHardwareRejectionNotificationTracker notificationTracker)
|
|
{
|
|
_logger = logger;
|
|
_metricsRepo = metricsRepo;
|
|
_sessionRepo = sessionRepo;
|
|
_notificationTracker = notificationTracker;
|
|
}
|
|
|
|
[Function("GetAppAnalytics")]
|
|
public async Task<HttpResponseData> Run(
|
|
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "apps/{appName}/analytics")] HttpRequestData req,
|
|
string appName)
|
|
{
|
|
try
|
|
{
|
|
var tenantId = TenantHelper.GetTenantId(req);
|
|
|
|
var decodedAppName = Uri.UnescapeDataString(appName ?? string.Empty);
|
|
if (string.IsNullOrWhiteSpace(decodedAppName))
|
|
{
|
|
return await req.BadRequestAsync("appName is required");
|
|
}
|
|
|
|
var query = req.Query;
|
|
var days = QueryParams.Int(query["days"], @default: 30, min: 1, max: 365);
|
|
|
|
var summaries = await AppsAnalyticsHelper.LoadSummariesAsync(_metricsRepo, tenantId, days);
|
|
// Active duration-regression episodes for this app (fail-soft: empty on error).
|
|
var versionRegressions = (await _notificationTracker.GetAppVersionRegressionsAsync(tenantId))
|
|
.Where(a => string.Equals(a.AppName, decodedAppName, StringComparison.OrdinalIgnoreCase))
|
|
.ToList();
|
|
var body = await AppsAnalyticsHelper.BuildAnalyticsResponseAsync(
|
|
summaries, _sessionRepo, decodedAppName, days, versionRegressions);
|
|
|
|
var response = req.CreateResponse(HttpStatusCode.OK);
|
|
await response.WriteAsJsonAsync(body);
|
|
return response;
|
|
}
|
|
catch (UnauthorizedAccessException ex)
|
|
{
|
|
_logger.LogWarning(ex, "Unauthorized apps/analytics request");
|
|
return await req.UnauthorizedAsync("Unauthorized");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return await req.InternalServerErrorAsync(_logger, ex, "GetAppAnalytics");
|
|
}
|
|
}
|
|
}
|
|
}
|