feat(telemetry): Phase 3 — policy preview endpoint (count matching clients from recent events)

This commit is contained in:
saravanakumardb1 2026-02-17 10:59:13 -08:00
parent 2f61ea517c
commit 61c919a916

View File

@ -697,6 +697,62 @@ export async function telemetryRoutes(app: FastifyInstance) {
return { clusters, total: clusters.length };
});
// ── Admin: policy preview (count matching clients) ──────────
app.post('/telemetry/policies/preview', async req => {
requireAdmin(req);
const productId = getRequestProductId(req);
const targeting = (req.body as Record<string, unknown>)?.targeting as
| Record<string, unknown>
| undefined;
// Fetch recent events (last 7 days, up to 500)
const sevenDaysAgo = new Date(Date.now() - 7 * 86400000).toISOString();
const { events } = await repo.queryEvents(productId, {
from: sevenDaysAgo,
limit: 500,
});
// Filter events that match targeting
const matchedIds = new Set<string>();
for (const evt of events) {
if (
targeting?.platforms &&
Array.isArray(targeting.platforms) &&
targeting.platforms.length > 0
) {
if (!targeting.platforms.includes(evt.platform)) continue;
}
if (
targeting?.channels &&
Array.isArray(targeting.channels) &&
targeting.channels.length > 0
) {
if (!targeting.channels.includes(evt.channel)) continue;
}
if (
targeting?.osFamilies &&
Array.isArray(targeting.osFamilies) &&
targeting.osFamilies.length > 0
) {
if (!targeting.osFamilies.includes(evt.osFamily)) continue;
}
if (
targeting?.releaseChannels &&
Array.isArray(targeting.releaseChannels) &&
targeting.releaseChannels.length > 0
) {
if (!targeting.releaseChannels.includes(evt.releaseChannel)) continue;
}
matchedIds.add(evt.anonymousInstallId || evt.userId || evt.id);
}
return {
matchedClients: matchedIds.size,
totalClients: new Set(events.map(e => e.anonymousInstallId || e.userId || e.id)).size,
sampleSize: events.length,
};
});
// ── Admin: list policies ──────────────────────────────────────
app.get('/telemetry/policies', async req => {
requireAdmin(req);