Files
AutopilotMonitor/src/Backend/AutopilotMonitor.Functions/Functions/Apps/GetAppSessionsFunction.cs
T
Oliver Kieselbach 5163d7d6a0 refactor(backend): read the request query through req.Query only
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>
2026-09-21 22:28:11 +02:00

80 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}/sessions?days=30&amp;status=failed&amp;model=...&amp;version=...&amp;offset=0&amp;limit=50
/// Returns paginated sessions that (tried to) install a given app for the caller's tenant.
/// </summary>
public class GetAppSessionsFunction
{
private const int DefaultLimit = 50;
private const int MaxLimit = 200;
private readonly ILogger<GetAppSessionsFunction> _logger;
private readonly IMetricsRepository _metricsRepo;
private readonly ISessionRepository _sessionRepo;
public GetAppSessionsFunction(
ILogger<GetAppSessionsFunction> logger,
IMetricsRepository metricsRepo,
ISessionRepository sessionRepo)
{
_logger = logger;
_metricsRepo = metricsRepo;
_sessionRepo = sessionRepo;
}
[Function("GetAppSessions")]
public async Task<HttpResponseData> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "apps/{appName}/sessions")] 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 statusFilter = (query["status"] ?? "all").Trim().ToLowerInvariant();
var modelFilter = query["model"];
var versionFilter = query["version"];
var offset = QueryParams.Int(query["offset"], @default: 0, min: 0, max: int.MaxValue);
var limit = QueryParams.Int(query["limit"], DefaultLimit, 1, MaxLimit);
var summaries = await AppsAnalyticsHelper.LoadSummariesAsync(_metricsRepo, tenantId, days);
var body = await AppsAnalyticsHelper.BuildSessionsResponseAsync(
summaries, _sessionRepo, decodedAppName, days,
statusFilter, modelFilter, versionFilter, offset, limit);
var response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteAsJsonAsync(body);
return response;
}
catch (UnauthorizedAccessException ex)
{
_logger.LogWarning(ex, "Unauthorized apps/sessions request");
return await req.UnauthorizedAsync("Unauthorized");
}
catch (Exception ex)
{
return await req.InternalServerErrorAsync(_logger, ex, "GetAppSessions");
}
}
}
}