Files
AutopilotMonitor/src/Backend/AutopilotMonitor.Functions/Functions/Apps/GetGlobalAppAnalyticsFunction.cs
T
Oliver Kieselbach a0ad769231 RealmJoin packages count as app installs next to Intune apps
App install rows now carry their install channel. The ingest folds the RealmJoin
watcher's package started/completed events into AppInstallSummaries rows (channel
"realmjoin", keyed by scope + package id; duration = completion minus the observed
start). IME rows keep their key and have no channel column, so they read as before.

Every per-app aggregate groups on (channel, name): apps list, app analytics and
sessions (new ?source=, default ime), metrics/app, SLA top failing apps and the
version-regression radar (tracker key, episode key, bell link, ops event). The app
install SLA rate includes RealmJoin packages. The ESP-blocking stamp stays IME-only.
RealmJoin packages do not feed the software inventory or CVE correlation.

Web: a RealmJoin pill and an All / Intune / RealmJoin filter on Software > Installs,
the pill on app detail, Fleet Health and SLA, source-aware app links and PDF meta
line, and a hint in the empty Inventory tab. The pill moves out of InstallProgress
into a shared component. MCP: get_app_install_metrics names the source field.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-28 10:19:18 +02:00

93 lines
4.3 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.Apps
{
/// <summary>
/// GET /api/global/apps/{appName}/analytics?days=30[&amp;tenantId=GUID]
/// Global Admin variant of <see cref="GetAppAnalyticsFunction"/>.
/// Authorization: GlobalAdminOnly.
/// </summary>
public class GetGlobalAppAnalyticsFunction
{
private readonly ILogger<GetGlobalAppAnalyticsFunction> _logger;
private readonly IMetricsRepository _metricsRepo;
private readonly ISessionRepository _sessionRepo;
private readonly IHardwareRejectionNotificationTracker _notificationTracker;
public GetGlobalAppAnalyticsFunction(
ILogger<GetGlobalAppAnalyticsFunction> logger,
IMetricsRepository metricsRepo,
ISessionRepository sessionRepo,
IHardwareRejectionNotificationTracker notificationTracker)
{
_logger = logger;
_metricsRepo = metricsRepo;
_sessionRepo = sessionRepo;
_notificationTracker = notificationTracker;
}
[Function("GetGlobalAppAnalytics")]
public async Task<HttpResponseData> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "global/apps/{appName}/analytics")] HttpRequestData req,
string appName)
{
try
{
var userEmail = TenantHelper.GetUserIdentifier(req);
var decodedAppName = Uri.UnescapeDataString(appName ?? string.Empty);
if (string.IsNullOrWhiteSpace(decodedAppName))
{
return await req.BadRequestAsync("appName is required");
}
var query = req.Query;
var scopedTenantId = query["tenantId"];
if (!AppsAnalyticsHelper.IsValidOptionalTenantIdQueryParam(scopedTenantId))
{
return await req.BadRequestAsync("tenantId must be a valid GUID");
}
var days = QueryParams.Int(query["days"], @default: 30, min: 1, max: 365);
if (!AppsAnalyticsHelper.TryParseSourceQueryParam(query["source"], out var source))
{
return await req.BadRequestAsync($"source must be one of: {string.Join(", ", AppInstallSources.All)}");
}
_logger.LogInformation(
"Global apps/{App}/analytics requested (user: {User}, tenantId: {TenantId}, days: {Days})",
decodedAppName, userEmail, scopedTenantId ?? "<all>", days);
var summaries = await AppsAnalyticsHelper.LoadSummariesAsync(_metricsRepo, scopedTenantId, days);
// Episodes are per-tenant tracker rows; without a tenant scope the aggregated
// cross-tenant view carries none (mirrors the rule-stats global route).
var versionRegressions = string.IsNullOrEmpty(scopedTenantId)
? new List<AppVersionRegressionAlert>()
: (await _notificationTracker.GetAppVersionRegressionsAsync(scopedTenantId!))
.Where(a => string.Equals(a.AppName, decodedAppName, StringComparison.OrdinalIgnoreCase)
&& AppInstallSources.Normalize(a.Source) == source)
.ToList();
var body = await AppsAnalyticsHelper.BuildAnalyticsResponseAsync(
summaries, _sessionRepo, decodedAppName, source, days, versionRegressions);
var response = req.CreateResponse(HttpStatusCode.OK);
await response.WriteAsJsonAsync(body);
return response;
}
catch (UnauthorizedAccessException ex)
{
_logger.LogWarning(ex, "Unauthorized global/apps/analytics request");
return await req.UnauthorizedAsync("Unauthorized");
}
catch (Exception ex)
{
return await req.InternalServerErrorAsync(_logger, ex, "GetGlobalAppAnalytics");
}
}
}
}