fix: dashboard reads from configured Analytics Engine dataset name#251
Open
rbonestell wants to merge 1 commit into
Open
fix: dashboard reads from configured Analytics Engine dataset name#251rbonestell wants to merge 1 commit into
rbonestell wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Dashboards showed 0 sites and no analytics data for any install that customized the Cloudflare Analytics Engine dataset name via
npx @counterscale/cli install --advanced. The tracker correctly wrote to the user-chosen dataset (through theWEB_COUNTER_AEbinding), but every SQL query in the dashboard read path hadFROM metricsDatasethardcoded, so reads always hit the default dataset, regardless of what the binding pointed at.This PR threads the dataset name end-to-end via a new
CF_DATASET_NAMEWorker var so the read path always matches the binding. Default-name installs continue to work with no migration required.Changes by Package
@counterscale/clistageDeployConfignow writesvars.CF_DATASET_NAMEinto the generatedwrangler.jsonalongside the existinganalytics_engine_datasets[0].dataset, sourced from the sameanalyticsDatasetvalue so the two fields can't drift on a fresh install.DATASET_NAME_PATTERN(/^[A-Za-z0-9_]+$/) and validate the dataset name instageDeployConfig, so invalid input fails at install time with a clear error instead of at Worker boot.--advancedinstall now prints a warning before prompting, reminding upgraders to re-enter their previous custom worker name and dataset to avoid silently repointing their deployment at a fresh, empty dataset.config.test.tsplaceholder values from"new-dataset"(hyphenated, not a valid CF AE identifier) to"newDataset", and added an assertion thatwrittenConfig.vars.CF_DATASET_NAMEmatches the dataset binding.@counterscale/serverAnalyticsEngineAPInow takes an optionaldatasetconstructor argument (exported asDEFAULT_DATASET_NAME = "metricsDataset"), validated against the same regex as the CLI, with safe fallback on empty/undefined.FROM metricsDatasetinquery.tsreplaced withFROM ${this.dataset}.load-context.tspassesenv.CF_DATASET_NAMEinto the constructor; default handling stays in one place (the ctor).workers/app.ts) forwardsenv.CF_DATASET_NAMEintoextractAsArrow, which in turn forwards it to the innerAnalyticsEngineAPIso the daily R2 rollup also reads from the correct dataset.workers/lib/arrow.tsnow readsprocess.env.CF_DATASET_NAMEso manual script runs respect the configured dataset.CF_DATASET_NAMEtowrangler.jsonvars(defaulted to"metricsDataset"),worker-configuration.d.ts(matches whatwrangler typesregenerates fromvars), and.dev.vars.example.query.test.tscovering: default behavior, empty-string fallback, custom-name pass-through, invalid-name rejection, and an end-to-end assertion thatgetCountsemitsFROM counterscaleMetrics(notmetricsDataset) when constructed with a custom dataset.@counterscale/trackerNo changes, the tracker writes through the
WEB_COUNTER_AEbinding, which already respected the user's chosen dataset. The bug was strictly on the read path.Other Changes
None.
Additional Notes
Backward compatibility: Existing installs on the default
metricsDatasetcontinue to work without redeploy becauseDEFAULT_DATASET_NAMEis"metricsDataset"and the constructor falls back to it when the var is absent. A redeploy via the updated CLI will additionally populatevars.CF_DATASET_NAMEto make this explicit.For affected users (custom dataset): Re-running
npx @counterscale/cli install --advancedwith the new CLI version and entering the same custom dataset name they originally chose will restore their dashboard. The newly added warning at the top of the advanced prompt calls this out.Why duplicate the dataset name (binding
dataset+vars.CF_DATASET_NAME)? Worker JS only sees the binding as an opaqueAnalyticsEngineDatasethandle exposingwriteDataPoint, there is no way to read the bound dataset's string at runtime. The dashboard's read path uses the Analytics Engine SQL API over HTTPS and needs the literal name inFROM <dataset>, so it has to come from a var. The CLI writes both fields from the same source value so they're guaranteed consistent on install.Out of scope / follow-up candidates:
wrangler.jsonis never written, the staging config lives in a tmp dir that's removed on exit). This means the advanced-install prompt can't auto-default to a user's previously chosen dataset name. Mitigated here by a user-facing warning; a proper fix could introduce a small state file and is a candidate for a separate PR.Resolves #248