# File download bandwidth Bandwidth is an unlimited, display-only metric: it shows in the dashboards, the statistics API and usage trends, and is never billed or limited. It should cost as little as possible to collect. ## Source on the hosted deployment: zone HTTP analytics The Workers cache answers almost every `/files/read` request before the files handler runs, so counting downloads in the handler (`createStatsBandwidth` into the Analytics Engine `bandwidth_usage` dataset) missed most of them (about 73% of the bytes in Sep-Nov 2026). The Cloudflare zones' HTTP analytics see every eyeball request, cache hit or not, so the hosted deployment reads bandwidth there, in batches, with no per-request work at all: ``` cron_tasks 'zone_bandwidth' (hourly, minute 7) -> public.enqueue_zone_bandwidth_jobs() -> pgmq queue cron_bandwidth_zone: {date, apps: [{app_id, owner_org}] (<= 40)} -> cron_tasks 'zone_bandwidth_queue' -> queue_consumer -> triggers/cron_bandwidth_zone (API worker) one GraphQL httpRequestsAdaptiveGroups request per zone -> daily_bandwidth (app_id, date, bandwidth): absolute value, overwritten ``` - `FILES_BANDWIDTH_ZONE_TAGS` (worker var, comma-separated zone tags) turns it on. It is set in the prod envs of the API worker (where the trigger runs), the files worker and every regional plugin worker, to capgo.app (`7fe39404d772c5a0b4444f5a0475a586`) and usecapgo.com (`df17bdd2be16ca09f5232058ce5790ce`). When set: - the files worker and the plugin never call `createStatsBandwidth` (no Analytics Engine write, no database write); - `cron_stat_app` neither reads Analytics Engine bandwidth nor writes `daily_bandwidth` (MAU, version usage and storage are unchanged); - `cron_bandwidth_zone` fills `daily_bandwidth`. - Unset (self-hosted, Supabase functions, preprod, local), requests count bandwidth and `cron_stat_app` copies it to `daily_bandwidth` as before; the zone cron still runs but its trigger returns without work. - Code: `supabase/functions/_backend/utils/zoneBandwidth.ts` (query builders, parser, estimate), `triggers/cron_bandwidth_zone.ts`, migration `20261201120301_zone_bandwidth_cron.sql`. ### Token The trigger authenticates with `CF_ZONE_ANALYTICS_TOKEN`, falling back to `CF_ANALYTICS_TOKEN`. The token needs **Zone > Analytics > Read** on both zones (the Analytics Engine SQL API permission of `CF_ANALYTICS_TOKEN` is not enough). Without it, the GraphQL answer has no zone and the job fails and is retried. ### What a job reads For each (owner_org, app_id) pair, one aliased sub-query per zone over `[date, date + 1 day)`: - `requestSource: "eyeball"` (no worker subrequests), `GET`, status 200 or 206; - path under `/files/read/attachments/`, `/private/files/read/attachments/`, `/files/upload/attachments/` or `/private/files/upload/attachments/`, then `orgs//apps//`, with `_`, `%` and `\` escaped so the app ID matches exactly (the `_like` filter honours `\_`); - not a `.preprod.` or `.dev.` host of either zone; - a non-empty `device_id` query parameter, like the files handler required. usecapgo.com's plan cannot filter on the query string: there, reads without a `device_id` (console, CLI) are counted too. The bytes of an app and day are the sum over zones and owner orgs of `edgeResponseBytes - 1,500 x requests`, never below zero, written to `daily_bandwidth` per app_id. ### Schedule and limits - Hourly, `enqueue_zone_bandwidth_jobs()` queues today (UTC) and, during hours 0-2 UTC, yesterday too, so each day gets a final pass after it ends. A day that still has messages in the queue is skipped. - Candidates of a day: every pair of `apps` and `deleted_apps` whose app has a `daily_mau` or `daily_bandwidth` row that day or the day before. - One message is at most 40 apps (`APPS_PER_REQUEST`: 50 aliases pass the GraphQL node quota, 100 do not) and one request per zone, inside the 15-second queue dispatch timeout. The first job of an isolate also reads each zone's retention settings. - Messages become visible 10 per minute (`send_batch` delays) and the queue task reads 10 per run: about 20 GraphQL requests a minute, inside the GraphQL rate limit (300 queries per 5 minutes), also during a backfill. - A depleted GraphQL budget, an HTTP error or a GraphQL error throws: the queue retries the message (at most 5 reads, like every queue). - Charts read `daily_bandwidth` through `get_app_metrics` and `get_total_metrics`, whose caches expire after 5 minutes: nothing to invalidate. ## History backfill Zone analytics keep 90 days on capgo.app and 31 on usecapgo.com. To rebuild the history (for instance right after the first deploy), run as `service_role` or `postgres`: ```sql SELECT public.enqueue_zone_bandwidth_jobs('2026-09-02', CURRENT_DATE); ``` It queues every day in `[p_from, p_to)`, clipped to the last 90 days, newest first, and returns the number of messages. A day older than a zone's retention is skipped for that zone only (usecapgo.com reads stop after 31 days). When every zone keeps the day, the job overwrites it with its absolute value (zero for a queried app without reads). When only some zones do, the stored value is only raised, never lowered, so usage of the expired zone is not erased. Running it again is safe either way. At 10 messages a minute, 90 days of a few thousand apps take several hours to drain; `pgmq.metrics('cron_bandwidth_zone')` shows progress. ## Accuracy - `httpRequestsAdaptiveGroups` is sampled adaptively on busy zones: daily sums are estimates, typically within a few percent. - The dataset has no body bytes: `edgeResponseBytes` includes the response headers, so 1,500 bytes per response are taken off (~1,460 measured on delta reads), which keeps the estimate on the low side. - HEAD requests are not counted (no body). - Days before the backfill window keep whatever was counted then (Analytics Engine, cache misses only from 2026-07-07). Old Analytics Engine `bandwidth_usage` points stay readable by `readBandwidthUsageCF` (self-hosted and history).