From 65e800f30b37efba112e0bdb7bb2a08f14723306 Mon Sep 17 00:00:00 2001 From: Raisal P Wardana Date: Wed, 11 Nov 2026 21:40:25 +0700 Subject: [PATCH 1/3] fix(cloudflare): respect datasets the zone's plan does not include Zone discovery re-enabled every dataset on a watched zone, including ones\nwhose Cloudflare settings deny collection (e.g. `firewallEventsAdaptiveGroups`\non Free-plan zones: `enabled: false` / `maxDuration: 0`). Those rows planned no\npoll window, kept a stale error forever, and CFG-INTEG-02 failed permanently.\n\n- `@maple/domain/cloudflare-analytics`: `cloudflareDatasetUnavailable`, true only\n for an explicit denial (`enabled === false` or `maxDuration === 0`); missing or\n malformed settings stay unknown. No plan-name checks.\n- Discovery and zone-watch resume keep capability-denied datasets disabled; the\n daily settings refresh still covers them and re-enables a dataset once\n Cloudflare allows it; a vanished zone drops its cached settings.\n- A valid connection clears the stale error on capability-denied rows.\n- CFG-INTEG-02 lists unavailable datasets as such (pass with detail when nothing\n else fails) while real errors on available datasets still fail. --- .../CloudflareAnalyticsService.test.ts | 396 +++++++++++++++++- .../CloudflareAnalyticsService.ts | 94 +++-- .../services/org/SetupAuditService.test.ts | 39 ++ .../src/services/org/SetupAuditService.ts | 11 +- .../src/services/org/setup-audit.test.ts | 88 ++++ .../backend/src/services/org/setup-audit.ts | 36 +- packages/domain/package.json | 1 + packages/domain/src/cloudflare-analytics.ts | 19 + 8 files changed, 649 insertions(+), 35 deletions(-) create mode 100644 packages/domain/src/cloudflare-analytics.ts diff --git a/packages/backend/src/services/integrations/CloudflareAnalyticsService.test.ts b/packages/backend/src/services/integrations/CloudflareAnalyticsService.test.ts index 0036c6d19e..0a6830ebd6 100644 --- a/packages/backend/src/services/integrations/CloudflareAnalyticsService.test.ts +++ b/packages/backend/src/services/integrations/CloudflareAnalyticsService.test.ts @@ -221,6 +221,9 @@ interface HyperdriveConfigFixture { } interface FetchOptions { + settingsData?: unknown + zoneData?: unknown + readonly zones?: ReadonlyArray /** * When set, the `/zones` listing serves only the zones of the account whose id appears @@ -367,11 +370,11 @@ const mockCloudflareFetch = (options: FetchOptions = {}): typeof globalThis.fetc errors: options.graphqlFirstErrorOnly ? errors.slice(0, 1) : errors, }) } - if (Str.includes("MapleCfDatasetSettings")(body.query)) - return jsonResponse({ data: settingsData }) - if (Str.includes("MapleCfZoneAnalytics")(body.query)) return jsonResponse({ data: httpData }) - if (Str.includes("MapleCfAccountAnalytics")(body.query)) - return jsonResponse({ data: workersData }) + if (body.query.includes("MapleCfDatasetSettings")) + return jsonResponse({ data: options.settingsData ?? settingsData }) + if (body.query.includes("MapleCfZoneAnalytics")) + return jsonResponse({ data: options.zoneData ?? httpData }) + if (body.query.includes("MapleCfAccountAnalytics")) return jsonResponse({ data: workersData }) return jsonResponse({ data: null, errors: [{ message: "unknown query" }] }) } if (Str.includes("/hyperdrive/configs")(url)) { @@ -1327,6 +1330,52 @@ describe("CloudflareAnalyticsService", () => { }).pipe(Effect.provide(makeLayer(testDb, captured))) }) + it.effect("a paused unavailable zone does not reserve a busy sibling's call budget", () => { + const testDb = createTestDb(trackedDbs) + const IDLE = "acct-idle" + return Effect.gen(function* () { + yield* TestClock.setTime(T0) + yield* seedConnection(ANALYTICS_SCOPE, [ + { id: ACCOUNT_ID, name: "Busy Account" }, + { id: IDLE, name: "Idle Account" }, + ]) + yield* seedBehindAccount(ACCOUNT_ID, 40, 6 * 60 * MIN) + yield* seedBehindAccount(IDLE, 1, 0) + const database = yield* Database + yield* database.execute((db) => + db.run( + PG.update(CloudflareAnalyticsState) + .set({ + enabled: false, + settingsJson: '{"enabled":false}', + settingsFetchedAt: T0 - 25 * 60 * MIN, + }) + .where(($) => [$.orgId.eq(ORG), $.accountId.eq(IDLE), $.dataset.eq("http_requests")]), + ), + ) + yield* database.execute((db) => + db.run( + PG.insertInto(CloudflareZoneSettings).values({ + orgId: ORG, + accountId: IDLE, + watchNewZones: false, + zoneOverrides: {}, + createdAt: T0, + updatedAt: T0, + }), + ), + ) + yield* seedGrantCap(20) + const result = yield* CloudflareAnalyticsService.use((service) => service.pollOrg(ORG)) + assert.strictEqual(result.callsMade, 20) + const paused = (yield* loadStateRows).find( + (row) => row.accountId === IDLE && row.dataset === "http_requests", + )! + assert.isFalse(paused.enabled) + assert.strictEqual(paused.settingsFetchedAt, T0 - 25 * 60 * MIN) + }).pipe(Effect.provide(makeLayer(testDb, []))) + }) + it.effect("a caught-up account polls once 15 minutes are pending, not every tick", () => { const testDb = createTestDb(trackedDbs) const captured: CapturedIngest[] = [] @@ -1471,6 +1520,343 @@ describe("CloudflareAnalyticsService", () => { }).pipe(Effect.provide(makeLayer(testDb, captured))) }) + for (const unavailable of [ + { enabled: false, maxDuration: 3600 }, + { enabled: true, maxDuration: 0 }, + ]) { + it.effect( + `cached unavailable ${JSON.stringify(unavailable)} survives discovery and clears stale connection error`, + () => { + const testDb = createTestDb(trackedDbs) + const queries: string[] = [] + const enabledDuringRequests: boolean[] = [] + return Effect.gen(function* () { + yield* TestClock.setTime(T0) + yield* seedConnection() + yield* seedStateRow({ + dataset: "firewall_events", + enabled: unavailable.enabled, + zoneId: ZONE_ID, + zoneName: ZONE_NAME, + settingsJson: JSON.stringify(unavailable), + settingsFetchedAt: T0 - 5 * MIN, + lastError: "Cloudflare OAuth request timed out", + lastErrorAt: T0 - MIN, + }) + const summary = yield* CloudflareAnalyticsService.use((service) => service.pollOrg(ORG)) + const row = (yield* loadStateRows).find((row) => row.dataset === "firewall_events")! + assert.isFalse(row.enabled) + assert.isAbove(enabledDuringRequests.length, 0) + assert.isFalse(enabledDuringRequests.some(Boolean)) + assert.isNull(row.lastError) + assert.isNull(row.lastErrorAt) + assert.isNull(row.lastSuccessAt) + assert.strictEqual(summary.failures.length, 0) + assert.isFalse( + queries.some( + (query) => + query.includes("MapleCfZoneAnalytics") && + query.includes("firewallEventsAdaptiveGroups"), + ), + ) + }).pipe( + Effect.provide( + makeLayer(testDb, [], { + graphqlQueries: queries, + onGraphql: async () => { + const row = await queryFirstRow<{ enabled: boolean }>( + testDb, + "SELECT enabled FROM cloudflare_analytics_state WHERE dataset = 'firewall_events'", + ) + enabledDuringRequests.push(row!.enabled) + }, + }), + ), + ) + }, + ) + } + + it.effect( + "unavailable settings refresh after 24h resumes polling without discovery re-enabling the row", + () => { + const testDb = createTestDb(trackedDbs) + const queries: string[] = [] + const options: FetchOptions = { + zoneData: { viewer: { zones: [{ zoneTag: ZONE_ID, firewall: [] }] } }, + graphqlQueries: queries, + settingsData: { + viewer: { + ...settingsData.viewer, + zones: [ + { + zoneTag: ZONE_ID, + settings: { + ...settingsData.viewer.zones[0]!.settings, + firewallEventsAdaptiveGroups: { + enabled: true, + maxDuration: 3600, + notOlderThan: 7200, + }, + }, + }, + ], + }, + }, + } + return Effect.gen(function* () { + yield* TestClock.setTime(T0) + yield* seedConnection() + yield* seedStateRow({ + dataset: "workers_invocations", + discoveredAt: T0, + settingsFetchedAt: T0, + }) + yield* seedStateRow({ + dataset: "firewall_events", + zoneId: ZONE_ID, + zoneName: ZONE_NAME, + enabled: false, + settingsJson: JSON.stringify({ enabled: false, maxDuration: 0 }), + settingsFetchedAt: T0 - 25 * 60 * MIN, + }) + yield* CloudflareAnalyticsService.use((service) => service.pollOrg(ORG)) + const row = (yield* loadStateRows).find((row) => row.dataset === "firewall_events")! + assert.isTrue(row.enabled) + assert.strictEqual(row.lastSuccessAt, T0) + assert.isNotNull(row.watermarkAt) + assert.isTrue(queries.some((query) => query.includes("MapleCfDatasetSettings"))) + assert.isTrue( + queries.some( + (query) => + query.includes("MapleCfZoneAnalytics") && + query.includes("firewallEventsAdaptiveGroups"), + ), + ) + }).pipe(Effect.provide(makeLayer(testDb, [], options))) + }, + ) + + for (const next of [null, { enabled: false, maxDuration: 0 }, { enabled: true, maxDuration: 3600 }]) { + it.effect(`unavailable settings refresh preserves a paused zone ${JSON.stringify(next)}`, () => { + const testDb = createTestDb(trackedDbs) + const queries: string[] = [] + return Effect.gen(function* () { + yield* TestClock.setTime(T0) + yield* seedConnection() + yield* seedStateRow({ + dataset: "workers_invocations", + discoveredAt: T0, + settingsFetchedAt: T0, + }) + yield* seedStateRow({ + dataset: "firewall_events", + zoneId: ZONE_ID, + zoneName: ZONE_NAME, + enabled: false, + settingsJson: JSON.stringify({ enabled: false, maxDuration: 0 }), + settingsFetchedAt: T0 - 25 * 60 * MIN, + }) + const database = yield* Database + yield* database.execute((db) => + db.run( + PG.insertInto(CloudflareZoneSettings).values({ + orgId: ORG, + accountId: ACCOUNT_ID, + watchNewZones: false, + zoneOverrides: {}, + createdAt: T0, + updatedAt: T0, + }), + ), + ) + yield* CloudflareAnalyticsService.use((service) => service.pollOrg(ORG)) + const row = (yield* loadStateRows).find((row) => row.dataset === "firewall_events")! + assert.isFalse(row.enabled) + assert.isFalse(queries.some((query) => query.includes("MapleCfZoneAnalytics"))) + }).pipe( + Effect.provide( + makeLayer(testDb, [], { + graphqlQueries: queries, + settingsData: { + viewer: { + ...settingsData.viewer, + zones: [ + { zoneTag: ZONE_ID, settings: { firewallEventsAdaptiveGroups: next } }, + ], + }, + }, + }), + ), + ) + }) + } + + it.effect("unavailable settings refresh does not resume on an unknown upstream settings node", () => { + const testDb = createTestDb(trackedDbs) + const queries: string[] = [] + return Effect.gen(function* () { + yield* TestClock.setTime(T0) + yield* seedConnection() + yield* seedStateRow({ dataset: "workers_invocations", discoveredAt: T0, settingsFetchedAt: T0 }) + yield* seedStateRow({ + dataset: "firewall_events", + zoneId: ZONE_ID, + zoneName: ZONE_NAME, + enabled: false, + settingsJson: JSON.stringify({ enabled: false, maxDuration: 0 }), + settingsFetchedAt: T0 - 25 * 60 * MIN, + }) + yield* CloudflareAnalyticsService.use((service) => service.pollOrg(ORG)) + const row = (yield* loadStateRows).find((row) => row.dataset === "firewall_events")! + assert.include(row.settingsJson!, '"enabled":false') + assert.isFalse(row.enabled) + assert.isFalse(queries.some((query) => query.includes("MapleCfZoneAnalytics"))) + // The probe is stamped, so the denied row waits a full TTL instead of costing a settings call every tick. + assert.strictEqual(row.settingsFetchedAt, T0) + const probes = queries.filter((query) => query.includes("MapleCfDatasetSettings")).length + yield* TestClock.adjust(10 * MIN) + yield* CloudflareAnalyticsService.use((service) => service.pollOrg(ORG)) + assert.strictEqual( + queries.filter((query) => query.includes("MapleCfDatasetSettings")).length, + probes, + ) + }).pipe( + Effect.provide( + makeLayer(testDb, [], { + graphqlQueries: queries, + settingsData: { + viewer: { + ...settingsData.viewer, + zones: [{ zoneTag: ZONE_ID, settings: { firewallEventsAdaptiveGroups: null } }], + }, + }, + }), + ), + ) + }) + + it.effect( + "a denied account dataset keeps its denial on an indeterminate answer and recovers on a definitive one", + () => { + const testDb = createTestDb(trackedDbs) + const denied = JSON.stringify({ + enabled: false, + notOlderThan: 0, + maxDuration: 0, + availableFields: [], + }) + const answer = (enabled: boolean | null) => ({ + viewer: { + ...settingsData.viewer, + accounts: [ + { + settings: { + workersInvocationsAdaptive: { + enabled, + notOlderThan: 7200, + maxDuration: 3600, + availableFields: [], + }, + }, + }, + ], + }, + }) + // One layer for the whole test; each tick swaps the settings answer it returns. + const options: { settingsData: ReturnType } = { settingsData: answer(null) } + const workers = Effect.map(loadStateRows, (rows) => + rows.find((row) => row.dataset === "workers_invocations")!, + ) + return Effect.gen(function* () { + yield* TestClock.setTime(T0) + yield* seedConnection() + yield* seedStateRow({ + dataset: "workers_invocations", + discoveredAt: T0, + enabled: false, + settingsJson: denied, + settingsFetchedAt: T0 - 25 * 60 * MIN, + }) + + yield* CloudflareAnalyticsService.use((service) => service.pollOrg(ORG)) + const held = yield* workers + assert.isFalse(held.enabled) + assert.strictEqual(held.settingsJson, denied) + assert.strictEqual(held.settingsFetchedAt, T0) + + // Still eligible for the next daily refresh, which now answers definitively. + options.settingsData = answer(true) + yield* TestClock.adjust(25 * 60 * MIN) + yield* CloudflareAnalyticsService.use((service) => service.pollOrg(ORG)) + const back = yield* workers + assert.isTrue(back.enabled) + assert.include(back.settingsJson!, '"enabled":true') + }).pipe(Effect.provide(makeLayer(testDb, [], options))) + }, + ) + + it.effect("resuming a watched zone respects cached unavailable capability", () => { + const testDb = createTestDb(trackedDbs) + return Effect.gen(function* () { + yield* TestClock.setTime(T0) + yield* seedConnection() + yield* seedStateRow({ + dataset: "firewall_events", + zoneId: ZONE_ID, + zoneName: ZONE_NAME, + enabled: false, + settingsJson: JSON.stringify({ enabled: false, maxDuration: 0 }), + settingsFetchedAt: T0, + }) + const database = yield* Database + yield* database.execute((db) => + db.run( + PG.insertInto(CloudflareZoneSettings).values({ + orgId: ORG, + accountId: ACCOUNT_ID, + watchNewZones: false, + zoneOverrides: {}, + createdAt: T0, + updatedAt: T0, + }), + ), + ) + yield* CloudflareAnalyticsService.use((service) => + service.setZoneSettings(ORG, Schema.decodeUnknownSync(UserId)("user_1"), { + accountId: ACCOUNT_ID, + watchNewZones: true, + zones: [{ zoneId: ZONE_ID, watched: true }], + }), + ) + const row = (yield* loadStateRows).find((row) => row.dataset === "firewall_events")! + assert.isFalse(row.enabled) + }).pipe(Effect.provide(makeLayer(testDb, []))) + }) + + it.effect("disappeared unavailable zones invalidate settings and remain unpolled", () => { + const testDb = createTestDb(trackedDbs) + const queries: string[] = [] + return Effect.gen(function* () { + yield* TestClock.setTime(T0) + yield* seedConnection() + yield* seedStateRow({ + dataset: "firewall_events", + zoneId: ZONE_ID, + zoneName: ZONE_NAME, + enabled: false, + settingsJson: JSON.stringify({ enabled: false, maxDuration: 0 }), + settingsFetchedAt: T0, + }) + yield* CloudflareAnalyticsService.use((service) => service.pollOrg(ORG)) + const row = (yield* loadStateRows).find((row) => row.dataset === "firewall_events")! + assert.isFalse(row.enabled) + assert.isNull(row.settingsJson) + assert.isNull(row.settingsFetchedAt) + assert.isFalse(queries.some((query) => query.includes("MapleCfZoneAnalytics"))) + }).pipe(Effect.provide(makeLayer(testDb, [], { zones: [], graphqlQueries: queries }))) + }) + it.effect("pollOrg disables state rows for vanished zones", () => { const testDb = createTestDb(trackedDbs) const captured: CapturedIngest[] = [] diff --git a/packages/backend/src/services/integrations/CloudflareAnalyticsService.ts b/packages/backend/src/services/integrations/CloudflareAnalyticsService.ts index 95d958965a..2b2e97fddf 100644 --- a/packages/backend/src/services/integrations/CloudflareAnalyticsService.ts +++ b/packages/backend/src/services/integrations/CloudflareAnalyticsService.ts @@ -26,6 +26,7 @@ * row once — but a crash between the gateway accepting a batch and the frontier write landing * replays that window next tick (the warehouse has no dedupe key to absorb it). */ +import { cloudflareDatasetUnavailable } from "@maple/domain/cloudflare-analytics" import { randomUUID } from "node:crypto" import { CloudflareAnalyticsWorkersStatus, @@ -946,6 +947,7 @@ const planAccountBudgets = ( rows: ReadonlyArray, now: number, grantCap: number, + zoneSettings: ReadonlyMap, ): Array => { // Zero once accounts outnumber the budget: then the oldest-first order alone rotates them. const fairShare = Math.floor(grantCap / Math.max(1, accountIds.length)) @@ -957,11 +959,8 @@ const planAccountBudgets = ( const needsCall = accountRows.length === 0 || head.length > 0 || - Arr.some( - accountRows, - (row) => - row.enabled && - (row.settingsFetchedAt == null || now - row.settingsFetchedAt > SETTINGS_TTL_MS), + Arr.some(accountRows, (row) => + needsSettingsProbe(row, zoneSettings.get(accountId) ?? WATCH_ALL_ZONES, now), ) return { accountId, @@ -1272,6 +1271,20 @@ const WATCH_ALL_ZONES: ZoneWatchSettings = { watchNewZones: true, zoneOverrides: const isZoneWatched = (settings: ZoneWatchSettings, zoneId: string): boolean => settings.zoneOverrides[zoneId] ?? settings.watchNewZones +/** + * A row whose cached settings are older than the TTL and that the settings refresh will query: + * enabled, or denied by its cached settings (so the daily refresh can see it come back), and for a + * zone dataset only while the customer still watches the zone. + */ +const needsSettingsProbe = ( + row: CloudflareAnalyticsStateRow, + watch: ZoneWatchSettings, + now: number, +): boolean => + (row.enabled || cloudflareDatasetUnavailable(row.settingsJson)) && + (DATASET_BY_ID.get(row.dataset)?.scope !== "zone" || isZoneWatched(watch, row.zoneId)) && + (row.settingsFetchedAt == null || now - row.settingsFetchedAt > SETTINGS_TTL_MS) + interface ZoneSettingsUpdate { readonly accountId: string readonly watchNewZones: boolean @@ -1703,7 +1716,9 @@ export class CloudflareAnalyticsService extends Context.Service< const drifted = Arr.flatMap(ZONE_DATASETS, (dataset) => Arr.flatMap(zones, (zone) => { const existing = byDatasetZone.get(`${dataset.id}:${zone.id}`) - const watched = isZoneWatched(settings, zone.id) + const watched = + isZoneWatched(settings, zone.id) && + !cloudflareDatasetUnavailable(existing?.settingsJson) if (!existing || (existing.zoneName === zone.name && existing.enabled === watched)) return [] return [{ existing, zone, watched }] @@ -1712,8 +1727,7 @@ export class CloudflareAnalyticsService extends Context.Service< yield* Effect.forEach( drifted, ({ existing, zone, watched }) => - // Re-enable on reappearance; a dataset-level disable re-asserts itself on the - // next settings check, so this errs on the side of collecting again. + // Respect cached capability; the daily settings refresh owns recovery. Effect.andThen( updateRows([existing.id], { zoneName: zone.name, enabled: watched, updatedAt: now }), Effect.sync(() => { @@ -1733,21 +1747,24 @@ export class CloudflareAnalyticsService extends Context.Service< ? [] : Arr.filter( rows, - (row) => - DATASET_BY_ID.get(row.dataset)?.scope === "zone" && - row.enabled && - !seen.has(row.zoneId), + (row) => DATASET_BY_ID.get(row.dataset)?.scope === "zone" && !seen.has(row.zoneId), ) yield* updateRows( Arr.map(vanished, (row) => row.id), { enabled: false, + settingsJson: null, + settingsFetchedAt: null, lastError: "Zone no longer present on the Cloudflare account", lastErrorAt: now, updatedAt: now, }, ) - for (const row of vanished) row.enabled = false + for (const row of vanished) { + row.enabled = false + row.settingsJson = null + row.settingsFetchedAt = null + } yield* updateRows([anchorId], { discoveredAt: now, updatedAt: now }) @@ -1766,12 +1783,10 @@ export class CloudflareAnalyticsService extends Context.Service< budget: Ref.Ref, callLimit: number, ) { - const stale = Arr.filter( - rows, - (row) => - row.enabled && - (row.settingsFetchedAt == null || now - row.settingsFetchedAt > SETTINGS_TTL_MS), - ) + const first = rows[0] + if (first === undefined) return false + const watch = (yield* loadZoneSettings(first.orgId, accountId)).get(accountId) ?? WATCH_ALL_ZONES + const stale = Arr.filter(rows, (row) => needsSettingsProbe(row, watch, now)) if (stale.length === 0) return false const staleZone = Arr.filter(stale, (row) => DATASET_BY_ID.get(row.dataset)?.scope === "zone") @@ -1867,20 +1882,33 @@ export class CloudflareAnalyticsService extends Context.Service< if (!dataset) continue const settings = dataset.settingsNode(decoded, row) if (settings === undefined) continue - const settingsJson = settings == null ? null : encodeJsonSync(settings) + // A denied row stays denied until Cloudflare answers definitively: a missing node or an + // `enabled: null` answer keeps the cached denial, but the probe is still stamped so the row + // waits a full TTL instead of spending a settings call every tick. + const answered = settings == null ? null : encodeJsonSync(settings) + const keepsDenial = + cloudflareDatasetUnavailable(row.settingsJson) && + settings?.enabled !== true && + !cloudflareDatasetUnavailable(answered) + const settingsJson = keepsDenial ? row.settingsJson : answered const quantilesAvailable = quantilesFromAvailableFields( settings, dataset.availableFieldsNeedle, ) - const disabled = settings?.enabled === false + const disabled = cloudflareDatasetUnavailable(settingsJson) const set: StateRowSet = { settingsJson, settingsFetchedAt: now, quantilesAvailable, - ...(disabled ? { enabled: false } : undefined), + ...(disabled + ? { enabled: false, lastError: null, lastErrorAt: null } + : settings?.enabled === true && + cloudflareDatasetUnavailable(row.settingsJson) + ? { enabled: true } + : undefined), updatedAt: now, } - const key = `${settingsJson}|${quantilesAvailable}|${disabled ? "false" : ""}` + const key = `${settingsJson}|${quantilesAvailable}|${set.enabled ?? ""}` const group = updates.get(key) if (group) group.ids.push(row.id) else updates.set(key, { ids: [row.id], set }) @@ -2527,6 +2555,20 @@ export class CloudflareAnalyticsService extends Context.Service< callLimit, ) if (settingsWrote) rows = yield* loadStateRows(orgId, accountId) + // A valid connection cannot recover a dataset the upstream settings deny. Record that + // capability using the existing cache + enabled flag, not an old OAuth/query failure. + const unavailable = rows.filter((row) => cloudflareDatasetUnavailable(row.settingsJson)) + yield* updateRows( + unavailable + .filter((row) => row.enabled || row.lastError !== null || row.lastErrorAt !== null) + .map((row) => row.id), + { enabled: false, lastError: null, lastErrorAt: null, updatedAt: now }, + ) + for (const row of unavailable) { + row.enabled = false + row.lastError = null + row.lastErrorAt = null + } // The org's public ingest key authenticates the gateway POSTs (minted on first use). const keyResult = yield* Effect.result(getOrgIngestKey(orgId)) @@ -3105,6 +3147,7 @@ export class CloudflareAnalyticsService extends Context.Service< Arr.filter(stateRows, (row) => !disabled.has(row.dataset)), planNow, grantCap, + yield* loadZoneSettings(orgId), ) const sourceId = stampedSourceId(linkedSource) const backedOff = yield* Effect.forEach(backoffs, ([accountId, backoff]) => @@ -3653,7 +3696,10 @@ export class CloudflareAnalyticsService extends Context.Service< { enabled: false, updatedAt: now }, ) yield* updateRows( - Arr.map(resumed, (row) => row.id), + Arr.map( + Arr.filter(resumed, (row) => !cloudflareDatasetUnavailable(row.settingsJson)), + (row) => row.id, + ), { enabled: true, updatedAt: now }, ) // A resumed zone may have left Cloudflare while paused; rediscover before polling it. diff --git a/packages/backend/src/services/org/SetupAuditService.test.ts b/packages/backend/src/services/org/SetupAuditService.test.ts index 016d1dc4da..5745945cbc 100644 --- a/packages/backend/src/services/org/SetupAuditService.test.ts +++ b/packages/backend/src/services/org/SetupAuditService.test.ts @@ -548,3 +548,42 @@ describe("SetupAuditService telemetry mapping", () => { }, ) }) + +describe("Cloudflare unavailable dataset audit projection", () => { + it.effect("keeps unavailable disabled datasets visible and available errors failing", () => { + const testDb = createTestDb(trackedDbs) + return Effect.gen(function* () { + for (const [id, dataset, enabled, settings] of [ + ["cf-unavailable", "firewall_events", false, { enabled: false, maxDuration: 0 }], + ["cf-available", "http_requests", true, { enabled: true, maxDuration: 3600 }], + ] as const) { + yield* Effect.promise(() => + executeSql( + testDb, + `INSERT INTO cloudflare_analytics_state (id, org_id, dataset, zone_id, zone_name, enabled, settings_json, last_error, last_error_at, created_at, updated_at) + VALUES ($1, $2, $3, 'zone-audit', 'example.com', $4, $5, 'upstream timeout', now(), now(), now())`, + [id, ORG, dataset, enabled, JSON.stringify(settings)], + ), + ) + } + const report = yield* SetupAuditService.use((audit) => audit.run(tenant)) + const check = report.checks.find((check) => check.id === "CFG-INTEG-02")! + assert.strictEqual(check.status, "fail") + assert.include(check.detail!, "1 unavailable") + assert.isTrue( + check.affected.some( + (entity) => + entity.name === "firewall_events · example.com" && + entity.note?.includes("unavailable"), + ), + ) + assert.isTrue( + check.affected.some( + (entity) => + entity.name === "http_requests · example.com" && + entity.note?.includes("upstream timeout"), + ), + ) + }).pipe(Effect.provide(makeLayer(testDb, "present"))) + }) +}) diff --git a/packages/backend/src/services/org/SetupAuditService.ts b/packages/backend/src/services/org/SetupAuditService.ts index f9421532b1..a1832620a0 100644 --- a/packages/backend/src/services/org/SetupAuditService.ts +++ b/packages/backend/src/services/org/SetupAuditService.ts @@ -1,3 +1,4 @@ +import { cloudflareDatasetUnavailable } from "@maple/domain/cloudflare-analytics" import * as PG from "@maple-dev/effect-orm/postgres" import { AlertDestinations, @@ -263,6 +264,7 @@ const make: Effect.Effect state.enabled), + Arr.filter( + rows.cloudflare, + (state) => state.enabled || cloudflareDatasetUnavailable(state.settingsJson), + ), (state) => ({ dataset: state.dataset, + settingsJson: state.settingsJson, zoneName: state.zoneName, lastSuccessAt: state.lastSuccessAt, lastErrorAt: state.lastErrorAt, diff --git a/packages/backend/src/services/org/setup-audit.test.ts b/packages/backend/src/services/org/setup-audit.test.ts index d34749f9b5..eb66c61ae6 100644 --- a/packages/backend/src/services/org/setup-audit.test.ts +++ b/packages/backend/src/services/org/setup-audit.test.ts @@ -1412,6 +1412,94 @@ describe("integration checks", () => { expect(result.affected[0]?.note).toBe("missing permission (1 minute ago), never succeeded") }) + it.each([{ enabled: false }, { enabled: true, maxDuration: 0 }])( + "CFG-INTEG-02 lists unavailable settings %j without counting a stale error as failed", + (settings) => { + const state = { + dataset: "firewall_events", + zoneName: "example.com", + lastSuccessAt: null, + lastErrorAt: NOW - HOUR, + lastError: "OAuth request timed out", + settingsJson: JSON.stringify(settings), + } + const result = check(runSetupAudit(inputs({ cloudflareAnalytics: [state] })), "CFG-INTEG-02") + expect(result.status).toBe("pass") + expect(result.detail).toContain("unavailable") + expect(result.affected).toEqual([ + expect.objectContaining({ + name: "firewall_events · example.com", + note: expect.stringContaining("unavailable"), + }), + ]) + }, + ) + + it("CFG-INTEG-02 keeps available failures failing alongside unavailable datasets", () => { + const state = { + dataset: "firewall_events", + zoneName: "example.com", + lastSuccessAt: null, + lastErrorAt: NOW - HOUR, + lastError: "real upstream error", + } + const result = check( + runSetupAudit( + inputs({ + cloudflareAnalytics: [ + { ...state, settingsJson: JSON.stringify({ enabled: false }) }, + { + ...state, + dataset: "http_requests", + settingsJson: JSON.stringify({ enabled: true, maxDuration: 3600 }), + }, + ], + }), + ), + "CFG-INTEG-02", + ) + expect(result.status).toBe("fail") + expect(result.detail).toContain("1 Cloudflare dataset has failed") + expect(result.detail).toContain("1 unavailable") + expect(result.affected).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + name: "http_requests · example.com", + // The grouped note leads with the error and appends the success age. + note: expect.stringContaining("real upstream error"), + }), + expect.objectContaining({ + name: "firewall_events · example.com", + note: expect.stringContaining("unavailable"), + }), + ]), + ) + }) + + it.each([null, "invalid json", "{}", '{"enabled":"false"}', '{"maxDuration":null}'])( + "CFG-INTEG-02 does not suppress an error for unknown settings %s", + (settingsJson) => { + const result = check( + runSetupAudit( + inputs({ + cloudflareAnalytics: [ + { + dataset: "http_requests", + zoneName: null, + lastSuccessAt: null, + lastErrorAt: NOW - HOUR, + lastError: "real error", + settingsJson, + }, + ], + }), + ), + "CFG-INTEG-02", + ) + expect(result.status).toBe("fail") + }, + ) + it("CFG-INTEG-03 flags a repository whose sync errored", () => { const report = runSetupAudit( inputs({ diff --git a/packages/backend/src/services/org/setup-audit.ts b/packages/backend/src/services/org/setup-audit.ts index a0f62c5808..cd89d39119 100644 --- a/packages/backend/src/services/org/setup-audit.ts +++ b/packages/backend/src/services/org/setup-audit.ts @@ -17,6 +17,7 @@ // tested (delivery works fine without ever pressing Test), onboarding checklist incomplete (owned by // the onboarding surface). A check that fires on every healthy org is noise, not an audit. +import { cloudflareDatasetUnavailable } from "@maple/domain/cloudflare-analytics" import { Option, Order, pipe, Result, Array as Arr, Record as Rec, String as Str } from "effect" import type { IntegrationEntry, @@ -145,6 +146,7 @@ export interface ConfigAuditInputs { } | null readonly integrations: ReadonlyArray<{ readonly provider: string; readonly revokedAt: number | null }> readonly cloudflareAnalytics: ReadonlyArray<{ + readonly settingsJson?: string | null readonly dataset: string readonly zoneName: string | null readonly lastSuccessAt: number | null @@ -1035,10 +1037,19 @@ const integrationChecks: ReadonlyArray = [ fixHint: "A permission error means the token lacks a scope: reconnect Cloudflare in Sources and grant Analytics and Firewall read access. A 5xx from Cloudflare is retried every few minutes; reconnect if it lasts more than a few hours.", evaluate: ({ config, now }) => { + // A dataset whose settings mark it unavailable (the plan does not include it) + // is not collected on purpose: keep it visible as unavailable rather than reporting its + // silence as a failure. + const unavailable = Arr.filter(config.cloudflareAnalytics, (state) => + cloudflareDatasetUnavailable(state.settingsJson), + ) // An error is current only when no success landed after it; older ones were recovered from. const failing = Arr.filter( config.cloudflareAnalytics, - (state) => state.lastErrorAt !== null && state.lastErrorAt > (state.lastSuccessAt ?? 0), + (state) => + !cloudflareDatasetUnavailable(state.settingsJson) && + state.lastErrorAt !== null && + state.lastErrorAt > (state.lastSuccessAt ?? 0), ) // One poll failure is stamped on every dataset of a connection at once, so group by dataset // and message rather than listing each zone. @@ -1076,7 +1087,24 @@ const integrationChecks: ReadonlyArray = [ : `${error}, ${success}`, } }) - if (affected.length === 0) return PASS + const unavailableEntities = Arr.map(unavailable, (state): AuditAffectedEntity => ({ + kind: "integration", + name: state.zoneName ? `${state.dataset} · ${state.zoneName}` : state.dataset, + note: "unavailable — Cloudflare dataset settings deny collection", + })) + const capability = + unavailable.length === 0 + ? "" + : ` ${unavailable.length} unavailable (Cloudflare dataset settings deny collection).` + if (affected.length === 0) + return unavailable.length === 0 + ? PASS + : { + status: "pass", + detail: capability.trim(), + affected: unavailableEntities.slice(0, AUDIT_AFFECTED_LIMIT), + affectedCount: unavailableEntities.length, + } // The detail states the dataset count; affectedCount stays the row count v2 clients expect. return { ...fail( @@ -1084,8 +1112,8 @@ const integrationChecks: ReadonlyArray = [ losing ? `, ${losingCount === failing.length ? "all" : losingCount} for over ${DATA_LOSS_HOURS} hours, so that data is missing` : "" - }.`, - affected, + }.${capability}`, + [...affected, ...unavailableEntities], ), ...(losing ? { diff --git a/packages/domain/package.json b/packages/domain/package.json index e37b3e1a2a..813445fcf6 100644 --- a/packages/domain/package.json +++ b/packages/domain/package.json @@ -12,6 +12,7 @@ "./audit-trail": "./src/audit-trail.ts", "./billing": "./src/billing.ts", "./billing-tax-ids": "./src/billing-tax-ids.ts", + "./cloudflare-analytics": "./src/cloudflare-analytics.ts", "./clerk-js-url": "./src/clerk-js-url.ts", "./integration-features": "./src/integration-features.ts", "./cloud-platform": "./src/cloud-platform.ts", diff --git a/packages/domain/src/cloudflare-analytics.ts b/packages/domain/src/cloudflare-analytics.ts new file mode 100644 index 0000000000..5d2807da84 --- /dev/null +++ b/packages/domain/src/cloudflare-analytics.ts @@ -0,0 +1,19 @@ +import { Option, Schema } from "effect" + +const decodeSettings = Schema.decodeUnknownOption( + Schema.fromJsonString( + Schema.Struct({ + enabled: Schema.optionalKey(Schema.NullOr(Schema.Boolean)), + maxDuration: Schema.optionalKey(Schema.NullOr(Schema.Number)), + }), + ), +) + +/** Only an explicit upstream capability denial is unavailable; absent/malformed settings stay unknown. */ +export const cloudflareDatasetUnavailable = (settingsJson: string | null | undefined): boolean => + settingsJson == null + ? false + : Option.match(decodeSettings(settingsJson), { + onNone: () => false, + onSome: (settings) => settings.enabled === false || settings.maxDuration === 0, + }) From 39018179c1269c1303180a0a5f34bb11b4a16798 Mon Sep 17 00:00:00 2001 From: Raisal P Wardana Date: Mon, 16 Nov 2026 11:54:26 +0700 Subject: [PATCH 2/3] fix(setup-health): show unavailable datasets on passing checks Passing checks now show their detail and affected entities (capped, with "+N more"), so the Cloudflare datasets a zone's plan denies stay visible. Skipped rows keep a single line; the section header explains skips. Rows top-align so the icon and category stay on the title line. Lint repairs in the Cloudflare/setup-audit touch-ups use Arr/Str helpers. --- .../setup-health/setup-health.scene.test.ts | 66 +++++++++++++++ .../src/page/settings/setup-health/view.ts | 39 ++++++++- .../CloudflareAnalyticsService.test.ts | 81 +++++++++++-------- .../CloudflareAnalyticsService.ts | 17 ++-- .../services/org/SetupAuditService.test.ts | 14 ++-- .../src/services/org/setup-audit.test.ts | 15 ++++ .../backend/src/services/org/setup-audit.ts | 4 +- 7 files changed, 188 insertions(+), 48 deletions(-) diff --git a/apps/web/src/page/settings/setup-health/setup-health.scene.test.ts b/apps/web/src/page/settings/setup-health/setup-health.scene.test.ts index 92b52e358c..15bfc5ed26 100644 --- a/apps/web/src/page/settings/setup-health/setup-health.scene.test.ts +++ b/apps/web/src/page/settings/setup-health/setup-health.scene.test.ts @@ -5,6 +5,9 @@ import { describe, expect, test } from "vitest" import { healthSegments, orderFindings, resolvedChecks, setupHealthAction } from "./derive" import { Message } from "./model" import { init, update } from "./update" +import { view } from "./view" +import type { HtmlBuilder } from "foldkit/html" +import { click, expect as expectView, given, role, scene, text } from "foldkit/scene" import { Array as Arr, Result, Schema } from "effect" const names = (commands: ReadonlyArray<{ readonly name: string }> | undefined) => @@ -21,6 +24,69 @@ const report = Schema.decodeSync(V2SetupAudit)({ }) describe("setup health", () => { + test("opening passing checks shows unavailable capability detail, entities and the capped remainder, but no skip detail", () => { + const audit = Schema.decodeSync(V2SetupAudit)({ + ...report, + summary: { ...report.summary, pass: 1, skip: 1 }, + checks: [ + { + object: "setup_audit_check", + id: "CFG-INTEG-02", + category: "integrations", + title: "Cloudflare datasets are collecting", + severity: "warn", + status: "pass", + detail: "2 unavailable (Cloudflare dataset settings deny collection).", + affected: [ + { + kind: "integration", + id: null, + name: "firewall_events · example.com", + note: "unavailable", + }, + ], + affected_count: 2, + fix_hint: "Review Cloudflare settings.", + }, + { + object: "setup_audit_check", + id: "TRC-01", + category: "traces", + title: "Traces have complete parents", + severity: "warn", + status: "skip", + detail: "Requires telemetry data, which is currently unavailable.", + affected: [], + affected_count: 0, + fix_hint: "Restore warehouse access.", + }, + ], + }) + const model = update( + init().model, + Message.CompletedFetchAudit({ result: Result.succeed(audit) }), + ).model + scene( + { + update, + view: (model, h: HtmlBuilder) => + h.submodel({ + slotId: "audit", + model, + view, + toParentMessage: (message) => message, + viewInputs: { nowMs: 0, timeZone: "UTC" }, + }), + }, + given(model), + click(role("button", { name: /Passing/ })), + expectView(text("2 unavailable (Cloudflare dataset settings deny collection).")).toExist(), + expectView(text("firewall_events · example.com")).toExist(), + expectView(text("+1 more")).toExist(), + expectView(text("Skipped")).toExist(), + expectView(text("Requires telemetry data, which is currently unavailable.")).not.toExist(), + ) + }) test("entry runs the audit; Re-run keeps the report up and asks once while it re-runs", () => { expect(names(init().commands)).toEqual(["FetchSetupAudit"]) const loaded = update( diff --git a/apps/web/src/page/settings/setup-health/view.ts b/apps/web/src/page/settings/setup-health/view.ts index 18b6be6eb9..4d451bb297 100644 --- a/apps/web/src/page/settings/setup-health/view.ts +++ b/apps/web/src/page/settings/setup-health/view.ts @@ -329,7 +329,7 @@ const passingDisclosure = ( h, { gap: "3", - align: "center", + align: "start", as: "li", className: "px-4 py-2.5", attributes: [h.Key(check.id)], @@ -344,7 +344,42 @@ const passingDisclosure = ( size: 14, className: "text-foreground-subtle shrink-0", }), - h.span([h.Class("min-w-0 flex-1 text-xs")], [check.title]), + vstack(h, { gap: "1.5", shrink: true, grow: true }, [ + typography(h, { size: "xs" }, [check.title]), + ...(check.detail === null || check.status === "skip" + ? [] + : [ + typography( + h, + { size: "xs", tone: "muted", as: "p" }, + [check.detail], + ), + ]), + ...(check.affected.length === 0 + ? [] + : [ + hstack(h, { gap: "1.5", wrap: true }, [ + ...Arr.map(check.affected, (entity, index) => + affectedChip(h, entity, index), + ), + ...(check.affected_count > + check.affected.length + ? [ + typography( + h, + { + size: "2xs", + tone: "subtle", + }, + [ + `+${check.affected_count - check.affected.length} more`, + ], + ), + ] + : []), + ]), + ]), + ]), ...(check.status === "skip" ? [ badge(h, { variant: "secondary", size: "xs" }, [ diff --git a/packages/backend/src/services/integrations/CloudflareAnalyticsService.test.ts b/packages/backend/src/services/integrations/CloudflareAnalyticsService.test.ts index 0a6830ebd6..105aaf4db5 100644 --- a/packages/backend/src/services/integrations/CloudflareAnalyticsService.test.ts +++ b/packages/backend/src/services/integrations/CloudflareAnalyticsService.test.ts @@ -367,14 +367,15 @@ const mockCloudflareFetch = (options: FetchOptions = {}): typeof globalThis.fetc if (errors.length > 0) { return jsonResponse({ data: null, - errors: options.graphqlFirstErrorOnly ? errors.slice(0, 1) : errors, + errors: options.graphqlFirstErrorOnly ? Arr.take(errors, 1) : errors, }) } - if (body.query.includes("MapleCfDatasetSettings")) + if (Str.includes("MapleCfDatasetSettings")(body.query)) return jsonResponse({ data: options.settingsData ?? settingsData }) - if (body.query.includes("MapleCfZoneAnalytics")) + if (Str.includes("MapleCfZoneAnalytics")(body.query)) return jsonResponse({ data: options.zoneData ?? httpData }) - if (body.query.includes("MapleCfAccountAnalytics")) return jsonResponse({ data: workersData }) + if (Str.includes("MapleCfAccountAnalytics")(body.query)) + return jsonResponse({ data: workersData }) return jsonResponse({ data: null, errors: [{ message: "unknown query" }] }) } if (Str.includes("/hyperdrive/configs")(url)) { @@ -1368,9 +1369,12 @@ describe("CloudflareAnalyticsService", () => { yield* seedGrantCap(20) const result = yield* CloudflareAnalyticsService.use((service) => service.pollOrg(ORG)) assert.strictEqual(result.callsMade, 20) - const paused = (yield* loadStateRows).find( - (row) => row.accountId === IDLE && row.dataset === "http_requests", - )! + const paused = Option.getOrThrow( + Arr.findFirst( + yield* loadStateRows, + (row) => row.accountId === IDLE && row.dataset === "http_requests", + ), + ) assert.isFalse(paused.enabled) assert.strictEqual(paused.settingsFetchedAt, T0 - 25 * 60 * MIN) }).pipe(Effect.provide(makeLayer(testDb, []))) @@ -1544,19 +1548,22 @@ describe("CloudflareAnalyticsService", () => { lastErrorAt: T0 - MIN, }) const summary = yield* CloudflareAnalyticsService.use((service) => service.pollOrg(ORG)) - const row = (yield* loadStateRows).find((row) => row.dataset === "firewall_events")! + const row = Option.getOrThrow( + Arr.findFirst(yield* loadStateRows, (row) => row.dataset === "firewall_events"), + ) assert.isFalse(row.enabled) assert.isAbove(enabledDuringRequests.length, 0) - assert.isFalse(enabledDuringRequests.some(Boolean)) + assert.isFalse(Arr.some(enabledDuringRequests, Boolean)) assert.isNull(row.lastError) assert.isNull(row.lastErrorAt) assert.isNull(row.lastSuccessAt) assert.strictEqual(summary.failures.length, 0) assert.isFalse( - queries.some( + Arr.some( + queries, (query) => - query.includes("MapleCfZoneAnalytics") && - query.includes("firewallEventsAdaptiveGroups"), + Str.includes("MapleCfZoneAnalytics")(query) && + Str.includes("firewallEventsAdaptiveGroups")(query), ), ) }).pipe( @@ -1592,7 +1599,7 @@ describe("CloudflareAnalyticsService", () => { { zoneTag: ZONE_ID, settings: { - ...settingsData.viewer.zones[0]!.settings, + ...settingsData.viewer.zones[0].settings, firewallEventsAdaptiveGroups: { enabled: true, maxDuration: 3600, @@ -1621,16 +1628,19 @@ describe("CloudflareAnalyticsService", () => { settingsFetchedAt: T0 - 25 * 60 * MIN, }) yield* CloudflareAnalyticsService.use((service) => service.pollOrg(ORG)) - const row = (yield* loadStateRows).find((row) => row.dataset === "firewall_events")! + const row = Option.getOrThrow( + Arr.findFirst(yield* loadStateRows, (row) => row.dataset === "firewall_events"), + ) assert.isTrue(row.enabled) assert.strictEqual(row.lastSuccessAt, T0) assert.isNotNull(row.watermarkAt) - assert.isTrue(queries.some((query) => query.includes("MapleCfDatasetSettings"))) + assert.isTrue(Arr.some(queries, Str.includes("MapleCfDatasetSettings"))) assert.isTrue( - queries.some( + Arr.some( + queries, (query) => - query.includes("MapleCfZoneAnalytics") && - query.includes("firewallEventsAdaptiveGroups"), + Str.includes("MapleCfZoneAnalytics")(query) && + Str.includes("firewallEventsAdaptiveGroups")(query), ), ) }).pipe(Effect.provide(makeLayer(testDb, [], options))) @@ -1671,9 +1681,11 @@ describe("CloudflareAnalyticsService", () => { ), ) yield* CloudflareAnalyticsService.use((service) => service.pollOrg(ORG)) - const row = (yield* loadStateRows).find((row) => row.dataset === "firewall_events")! + const row = Option.getOrThrow( + Arr.findFirst(yield* loadStateRows, (row) => row.dataset === "firewall_events"), + ) assert.isFalse(row.enabled) - assert.isFalse(queries.some((query) => query.includes("MapleCfZoneAnalytics"))) + assert.isFalse(Arr.some(queries, Str.includes("MapleCfZoneAnalytics"))) }).pipe( Effect.provide( makeLayer(testDb, [], { @@ -1708,19 +1720,18 @@ describe("CloudflareAnalyticsService", () => { settingsFetchedAt: T0 - 25 * 60 * MIN, }) yield* CloudflareAnalyticsService.use((service) => service.pollOrg(ORG)) - const row = (yield* loadStateRows).find((row) => row.dataset === "firewall_events")! + const row = Option.getOrThrow( + Arr.findFirst(yield* loadStateRows, (row) => row.dataset === "firewall_events"), + ) assert.include(row.settingsJson!, '"enabled":false') assert.isFalse(row.enabled) - assert.isFalse(queries.some((query) => query.includes("MapleCfZoneAnalytics"))) + assert.isFalse(Arr.some(queries, Str.includes("MapleCfZoneAnalytics"))) // The probe is stamped, so the denied row waits a full TTL instead of costing a settings call every tick. assert.strictEqual(row.settingsFetchedAt, T0) - const probes = queries.filter((query) => query.includes("MapleCfDatasetSettings")).length + const probes = Arr.filter(queries, Str.includes("MapleCfDatasetSettings")).length yield* TestClock.adjust(10 * MIN) yield* CloudflareAnalyticsService.use((service) => service.pollOrg(ORG)) - assert.strictEqual( - queries.filter((query) => query.includes("MapleCfDatasetSettings")).length, - probes, - ) + assert.strictEqual(Arr.filter(queries, Str.includes("MapleCfDatasetSettings")).length, probes) }).pipe( Effect.provide( makeLayer(testDb, [], { @@ -1764,9 +1775,9 @@ describe("CloudflareAnalyticsService", () => { }, }) // One layer for the whole test; each tick swaps the settings answer it returns. - const options: { settingsData: ReturnType } = { settingsData: answer(null) } + const options = { settingsData: answer(null) } const workers = Effect.map(loadStateRows, (rows) => - rows.find((row) => row.dataset === "workers_invocations")!, + Option.getOrThrow(Arr.findFirst(rows, (row) => row.dataset === "workers_invocations")), ) return Effect.gen(function* () { yield* TestClock.setTime(T0) @@ -1823,13 +1834,15 @@ describe("CloudflareAnalyticsService", () => { ), ) yield* CloudflareAnalyticsService.use((service) => - service.setZoneSettings(ORG, Schema.decodeUnknownSync(UserId)("user_1"), { + service.setZoneSettings(ORG, Schema.decodeSync(UserId)("user_1"), { accountId: ACCOUNT_ID, watchNewZones: true, zones: [{ zoneId: ZONE_ID, watched: true }], }), ) - const row = (yield* loadStateRows).find((row) => row.dataset === "firewall_events")! + const row = Option.getOrThrow( + Arr.findFirst(yield* loadStateRows, (row) => row.dataset === "firewall_events"), + ) assert.isFalse(row.enabled) }).pipe(Effect.provide(makeLayer(testDb, []))) }) @@ -1849,11 +1862,13 @@ describe("CloudflareAnalyticsService", () => { settingsFetchedAt: T0, }) yield* CloudflareAnalyticsService.use((service) => service.pollOrg(ORG)) - const row = (yield* loadStateRows).find((row) => row.dataset === "firewall_events")! + const row = Option.getOrThrow( + Arr.findFirst(yield* loadStateRows, (row) => row.dataset === "firewall_events"), + ) assert.isFalse(row.enabled) assert.isNull(row.settingsJson) assert.isNull(row.settingsFetchedAt) - assert.isFalse(queries.some((query) => query.includes("MapleCfZoneAnalytics"))) + assert.isFalse(Arr.some(queries, Str.includes("MapleCfZoneAnalytics"))) }).pipe(Effect.provide(makeLayer(testDb, [], { zones: [], graphqlQueries: queries }))) }) diff --git a/packages/backend/src/services/integrations/CloudflareAnalyticsService.ts b/packages/backend/src/services/integrations/CloudflareAnalyticsService.ts index 2b2e97fddf..19916ac2d0 100644 --- a/packages/backend/src/services/integrations/CloudflareAnalyticsService.ts +++ b/packages/backend/src/services/integrations/CloudflareAnalyticsService.ts @@ -1908,7 +1908,7 @@ export class CloudflareAnalyticsService extends Context.Service< : undefined), updatedAt: now, } - const key = `${settingsJson}|${quantilesAvailable}|${set.enabled ?? ""}` + const key = `${settingsJson}|${quantilesAvailable}|${disabled ? "false" : settings?.enabled === true && cloudflareDatasetUnavailable(row.settingsJson) ? "true" : ""}` const group = updates.get(key) if (group) group.ids.push(row.id) else updates.set(key, { ids: [row.id], set }) @@ -2557,11 +2557,15 @@ export class CloudflareAnalyticsService extends Context.Service< if (settingsWrote) rows = yield* loadStateRows(orgId, accountId) // A valid connection cannot recover a dataset the upstream settings deny. Record that // capability using the existing cache + enabled flag, not an old OAuth/query failure. - const unavailable = rows.filter((row) => cloudflareDatasetUnavailable(row.settingsJson)) + const unavailable = Arr.filter(rows, (row) => cloudflareDatasetUnavailable(row.settingsJson)) yield* updateRows( - unavailable - .filter((row) => row.enabled || row.lastError !== null || row.lastErrorAt !== null) - .map((row) => row.id), + Arr.map( + Arr.filter( + unavailable, + (row) => row.enabled || row.lastError !== null || row.lastErrorAt !== null, + ), + (row) => row.id, + ), { enabled: false, lastError: null, lastErrorAt: null, updatedAt: now }, ) for (const row of unavailable) { @@ -3136,6 +3140,7 @@ export class CloudflareAnalyticsService extends Context.Service< : [], ), ) + const zoneSettings = yield* loadZoneSettings(orgId) const plan = pollable.length === 0 ? [] @@ -3147,7 +3152,7 @@ export class CloudflareAnalyticsService extends Context.Service< Arr.filter(stateRows, (row) => !disabled.has(row.dataset)), planNow, grantCap, - yield* loadZoneSettings(orgId), + zoneSettings, ) const sourceId = stampedSourceId(linkedSource) const backedOff = yield* Effect.forEach(backoffs, ([accountId, backoff]) => diff --git a/packages/backend/src/services/org/SetupAuditService.test.ts b/packages/backend/src/services/org/SetupAuditService.test.ts index 5745945cbc..0b8da1ca09 100644 --- a/packages/backend/src/services/org/SetupAuditService.test.ts +++ b/packages/backend/src/services/org/SetupAuditService.test.ts @@ -567,21 +567,25 @@ describe("Cloudflare unavailable dataset audit projection", () => { ) } const report = yield* SetupAuditService.use((audit) => audit.run(tenant)) - const check = report.checks.find((check) => check.id === "CFG-INTEG-02")! + const check = Option.getOrThrow( + Arr.findFirst(report.checks, (check) => check.id === "CFG-INTEG-02"), + ) assert.strictEqual(check.status, "fail") assert.include(check.detail!, "1 unavailable") assert.isTrue( - check.affected.some( + Arr.some( + check.affected, (entity) => entity.name === "firewall_events · example.com" && - entity.note?.includes("unavailable"), + Str.includes("unavailable")(entity.note ?? ""), ), ) assert.isTrue( - check.affected.some( + Arr.some( + check.affected, (entity) => entity.name === "http_requests · example.com" && - entity.note?.includes("upstream timeout"), + Str.includes("upstream timeout")(entity.note ?? ""), ), ) }).pipe(Effect.provide(makeLayer(testDb, "present"))) diff --git a/packages/backend/src/services/org/setup-audit.test.ts b/packages/backend/src/services/org/setup-audit.test.ts index eb66c61ae6..5caa7ee91f 100644 --- a/packages/backend/src/services/org/setup-audit.test.ts +++ b/packages/backend/src/services/org/setup-audit.test.ts @@ -1435,6 +1435,21 @@ describe("integration checks", () => { }, ) + it("CFG-INTEG-02 caps unavailable entities on a passing result and retains the total", () => { + const states = Arr.makeBy(25, (index) => ({ + dataset: "firewall_events", + zoneName: `zone-${index}.example.com`, + lastSuccessAt: null, + lastErrorAt: null, + lastError: null, + settingsJson: JSON.stringify({ enabled: false }), + })) + const result = check(runSetupAudit(inputs({ cloudflareAnalytics: states })), "CFG-INTEG-02") + expect(result.status).toBe("pass") + expect(result.affected).toHaveLength(20) + expect(result.affectedCount).toBe(25) + }) + it("CFG-INTEG-02 keeps available failures failing alongside unavailable datasets", () => { const state = { dataset: "firewall_events", diff --git a/packages/backend/src/services/org/setup-audit.ts b/packages/backend/src/services/org/setup-audit.ts index cd89d39119..95e7b2464b 100644 --- a/packages/backend/src/services/org/setup-audit.ts +++ b/packages/backend/src/services/org/setup-audit.ts @@ -1101,8 +1101,8 @@ const integrationChecks: ReadonlyArray = [ ? PASS : { status: "pass", - detail: capability.trim(), - affected: unavailableEntities.slice(0, AUDIT_AFFECTED_LIMIT), + detail: Str.trim(capability), + affected: Arr.take(unavailableEntities, AUDIT_AFFECTED_LIMIT), affectedCount: unavailableEntities.length, } // The detail states the dataset count; affectedCount stays the row count v2 clients expect. From 1ea223ffa1e2f2721a41a784a31272c01703f621 Mon Sep 17 00:00:00 2001 From: Raisal P Wardana Date: Mon, 16 Nov 2026 14:46:44 +0700 Subject: [PATCH 3/3] refactor(setup-health): one affectedChips kit piece for both check rows The failing-finding row and the passing-check row drew the same affected chips and capped "+N more" separately. Both now use page/kit/affected-chips. The page loses its last layout-only class, so it leaves the UI-kit baseline. --- apps/web/src/page/kit/affected-chips.ts | 63 +++++++++++++++ .../src/page/settings/setup-health/view.ts | 78 +------------------ packages/views/src/ui/INDEX.md | 1 + scripts/oxlint-plugins/ui-kit-baseline.json | 1 - 4 files changed, 67 insertions(+), 76 deletions(-) create mode 100644 apps/web/src/page/kit/affected-chips.ts diff --git a/apps/web/src/page/kit/affected-chips.ts b/apps/web/src/page/kit/affected-chips.ts new file mode 100644 index 0000000000..a77d69181a --- /dev/null +++ b/apps/web/src/page/kit/affected-chips.ts @@ -0,0 +1,63 @@ +import type { V2SetupAuditCheck } from "@maple/domain/http/v2" +import { cn } from "@maple/ui/lib/utils" +import type { Html, HtmlBuilder } from "foldkit/html" +import { inlineCode, inlineCodeClassName } from "@maple/views/ui/inline-code" +import { hstack } from "@maple/views/ui/stack" +import { typography } from "@maple/views/ui/typography" +import { Array as Arr } from "effect" + +type Entity = V2SetupAuditCheck["affected"][number] + +const chipClassName = "text-2xs leading-4" + +const affectedChip = (h: HtmlBuilder, entity: Entity, index: number): Html => { + const title = entity.note === null ? [] : [h.Title(entity.note)] + const key = h.Key(entity.id === null ? `index:${index}` : `id:${entity.kind}:${entity.id}`) + if (entity.kind === "service") + return h.a( + [key, h.Href(`/services/${encodeURIComponent(entity.name)}`), ...title], + [ + inlineCode( + h, + { + className: cn( + chipClassName, + "text-foreground underline decoration-muted-foreground/40 underline-offset-2 hover:decoration-foreground active:decoration-foreground", + ), + }, + [entity.name], + ), + ], + ) + return h.code( + [ + key, + ...title, + h.Class(inlineCodeClassName("chip", cn(chipClassName, "text-muted-foreground"))), + h.DataAttribute("slot", "inline-code"), + ], + [entity.name], + ) +} + +/** The entities a setup-health check names, as chips with a "+N more" for the rest past the cap. + * Renders nothing when the check names none; `className` is the caller's spacing. */ +export const affectedChips = ( + h: HtmlBuilder, + check: Pick, + options: Readonly<{ className?: string }> = {}, +): ReadonlyArray => + check.affected.length === 0 + ? [] + : [ + hstack(h, { gap: "1.5", wrap: true, className: options.className }, [ + ...Arr.map(check.affected, (entity, index) => affectedChip(h, entity, index)), + ...(check.affected_count > check.affected.length + ? [ + typography(h, { size: "2xs", tone: "subtle" }, [ + `+${check.affected_count - check.affected.length} more`, + ]), + ] + : []), + ]), + ] diff --git a/apps/web/src/page/settings/setup-health/view.ts b/apps/web/src/page/settings/setup-health/view.ts index 4d451bb297..bda0d1892f 100644 --- a/apps/web/src/page/settings/setup-health/view.ts +++ b/apps/web/src/page/settings/setup-health/view.ts @@ -10,7 +10,6 @@ import { badge } from "@maple/views/ui/display" import { empty, emptyDescription, emptyHeader, emptyMedia, emptyTitle } from "@maple/views/ui/empty" import { errorState } from "@maple/views/ui/error-state" import { icon, type IconName } from "@maple/views/ui/icon" -import { inlineCode, inlineCodeClassName } from "@maple/views/ui/inline-code" import { panel } from "@maple/views/ui/panel" import { statusDot } from "@maple/views/ui/status-dot" import { refreshButton } from "@maple/views/ui/refresh-button" @@ -19,6 +18,7 @@ import { relativeTime } from "../../kit/relative-time" import { docsLink } from "../../kit/signal-empty-state" import { emptyActions } from "@maple/views/ui/signal-empty-state" import { settingsSection, settingsSections } from "../../kit/settings-section" +import { affectedChips } from "../../kit/affected-chips" import { type HealthSegment, healthSegments, @@ -84,38 +84,6 @@ const actionButton = (h: H, action: SetupHealthAction): Html => [action.label, icon(h, "ArrowRightIcon", { size: 14 })], ) -const chipClassName = "text-2xs leading-4" - -const affectedChip = (h: H, entity: V2SetupAuditCheck["affected"][number], index: number): Html => { - const title = entity.note === null ? [] : [h.Title(entity.note)] - const key = h.Key(entity.id === null ? `index:${index}` : `id:${entity.kind}:${entity.id}`) - if (entity.kind === "service") - return h.a( - [key, h.Href(`/services/${encodeURIComponent(entity.name)}`), ...title], - [ - inlineCode( - h, - { - className: cn( - chipClassName, - "text-foreground underline decoration-muted-foreground/40 underline-offset-2 hover:decoration-foreground active:decoration-foreground", - ), - }, - [entity.name], - ), - ], - ) - return h.code( - [ - key, - ...title, - h.Class(inlineCodeClassName("chip", cn(chipClassName, "text-muted-foreground"))), - h.DataAttribute("slot", "inline-code"), - ], - [entity.name], - ) -} - const findingRow = (h: H, check: V2SetupAuditCheck): Html => { const severity = SEVERITY[check.severity] const action = setupHealthAction(check) @@ -145,24 +113,7 @@ const findingRow = (h: H, check: V2SetupAuditCheck): Html => { typography(h, { size: "xs", tone: "subtle", as: "p", className: "leading-relaxed" }, [ check.fix_hint, ]), - ...(check.affected.length === 0 - ? [] - : [ - hstack(h, { gap: "1.5", align: "center", wrap: true, className: "pt-0.5" }, [ - ...Arr.map(check.affected, (entity, index) => - affectedChip(h, entity, index), - ), - ...(check.affected_count > check.affected.length - ? [ - typography(h, { size: "2xs", tone: "subtle" }, [ - "+", - String(check.affected_count - check.affected.length), - " more", - ]), - ] - : []), - ]), - ]), + ...affectedChips(h, check, { className: "pt-0.5" }), ]), ]), ...(action === null ? [] : [h.div([h.Class("pl-7 sm:pl-0")], [actionButton(h, action)])]), @@ -355,30 +306,7 @@ const passingDisclosure = ( [check.detail], ), ]), - ...(check.affected.length === 0 - ? [] - : [ - hstack(h, { gap: "1.5", wrap: true }, [ - ...Arr.map(check.affected, (entity, index) => - affectedChip(h, entity, index), - ), - ...(check.affected_count > - check.affected.length - ? [ - typography( - h, - { - size: "2xs", - tone: "subtle", - }, - [ - `+${check.affected_count - check.affected.length} more`, - ], - ), - ] - : []), - ]), - ]), + ...affectedChips(h, check), ]), ...(check.status === "skip" ? [ diff --git a/packages/views/src/ui/INDEX.md b/packages/views/src/ui/INDEX.md index 303ab63fb7..7354c33017 100644 --- a/packages/views/src/ui/INDEX.md +++ b/packages/views/src/ui/INDEX.md @@ -191,6 +191,7 @@ existing copies onto it (see `apps/web/CLAUDE.md`). ## apps/web/src/page/kit - `apps/web/src/page/kit/advanced-filter.ts` `advancedFilterButton`, `appliedWhereClause`, `advancedFilterDialog`, `whereClauseWarnings`: The pieces around a list page's advanced filter dialog (`/traces`, `/logs`). +- `apps/web/src/page/kit/affected-chips.ts` `affectedChips`: The entities a setup-health check names, as chips with a "+N more" for the rest past the cap. - `apps/web/src/page/kit/alert-collections.ts` `ruleRows`, `ruleStateRows`, `incidentRows`, `destinationRows`: The alert rules, incidents and destinations as live streams. - `apps/web/src/page/kit/alert-links.ts` `ruleHref`, `incidentHref`, `alertsHref`, `createHref`: A rule's page. - `apps/web/src/page/kit/alert-test-rows.ts` `hubAt`, `ruleId`, `ruleRow`, `stateRow`, `incidentRow`, `destinationRow`: Synced alert rows for the alerts pages' scene tests, the shape Electric delivers. diff --git a/scripts/oxlint-plugins/ui-kit-baseline.json b/scripts/oxlint-plugins/ui-kit-baseline.json index 40f8eb520b..95ce434d34 100644 --- a/scripts/oxlint-plugins/ui-kit-baseline.json +++ b/scripts/oxlint-plugins/ui-kit-baseline.json @@ -207,7 +207,6 @@ "apps/web/src/page/settings/mcp/view-clients.ts": 1, "apps/web/src/page/settings/mcp/view-tools.ts": 1, "apps/web/src/page/settings/mcp/view.ts": 1, - "apps/web/src/page/settings/setup-health/view.ts": 1, "apps/web/src/page/share/view.ts": 1, "apps/web/src/page/sources/kit/rules-view.ts": 1, "apps/web/src/page/status-pages/editor/view-live.ts": 1,