[HDX-5080] Chart editor UI for metric formulas - #2909
Conversation
Expose metric formulas (HDX-5078 schema, HDX-5079 rendering) in the chart editor for metric sources: - Formula rows in the builder (Add Formula / Remove Formula) with a letter-ref expression input, alias, and per-formula number format - Inline validation via the structured validator (malformed expressions, unknown series refs) plus save-time validation in validateChartForm - Letter badges (A, B, C) on metric series rows so refs are discoverable - Show input series toggle (showOperandSeries) for formula-only output; Number tiles default to hidden operands since they render the first value column - Formulas and the As Ratio toggle are mutually exclusive in the editor - normalizeChartConfig strips formulas for non-metric sources and display types the composed metric query does not render - useChartNumberFormats / DBTableChart / DBTimeChart updated for the formula-aware value-column projection (operands first unless hidden, then formula columns)
🦋 Changeset detectedLatest commit: 2067484 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Greptile SummaryThe PR exposes metric formulas in the metric chart editor and updates positional chart consumers for formula result columns.
Confidence Score: 4/5The positional formula rebinding defect should be fixed before merging because ordinary series editing can silently change the meaning of saved formulas. Formula letters resolve by current series position, but existing reorder and insertion controls mutate those positions without rewriting or invalidating formula expressions, allowing charts to save and display derived values from the wrong operands. Files Needing Attention: packages/app/src/components/DBEditTimeChartForm/ChartSeriesEditor.tsx, packages/app/src/components/DBEditTimeChartForm/ChartEditorControls.tsx
|
| Filename | Overview |
|---|---|
| packages/app/src/components/DBEditTimeChartForm/ChartEditorControls.tsx | Adds formula field-array controls, visibility toggling, and ratio exclusion; series mutations are not coordinated with positional formulas. |
| packages/app/src/components/DBEditTimeChartForm/ChartSeriesEditor.tsx | Adds positional reference badges while leaving reorder, duplicate, and removal actions capable of silently rebinding formulas. |
| packages/app/src/components/ChartEditor/utils.ts | Adds formula normalization and save-time validation, but count-based validation cannot detect semantically rebound in-range references. |
| packages/app/src/source.ts | Adds formula-aware result-column counting and number-format mapping. |
| packages/app/src/components/DBTableChart.tsx | Uses formula-aware value-column counts for group-by inference and avoids color mapping onto hidden operands. |
| packages/app/src/components/DBTimeChart.tsx | Avoids attributing formula-only values to operand expressions during drill-down. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart LR
S[Metric series A, B, ...] --> E[Chart formula editor]
E --> V[Formula validation]
V --> C[Saved chart config]
C --> Q[Composed metric query]
Q --> O[Operand columns]
Q --> F[Formula columns]
O --> R[Table / Time / Number renderer]
F --> R
Comments Outside Diff (1)
-
packages/app/src/components/DBEditTimeChartForm/ChartSeriesEditor.tsx, line 288-327 (link)Series edits rebind formula operands
When a chart already has a positional formula, moving or duplicating a series changes which operand each letter identifies without updating the expression, causing the saved chart to compute a different value while still passing validation. Removing a referenced series instead leaves the formula invalid and blocks saving until it is repaired manually.
Knowledge Base Used: App Components and Charts
Reviews (1): Last reviewed commit: "feat: chart editor UI for metric formula..." | Re-trigger Greptile
Deep Review✅ No critical issues found. The formula-editor wiring holds up on the highest-risk paths. Verified directly against the diff and the composed-query renderer: the 🟡 P2 — recommended
🟡 P2 — recommended
🔵 P3 nitpicks (1)
Reviewers (3): maintainability, learnings-researcher, orchestrator verification. Testing gaps:
|
Summary
Exposes metric formulas (HDX-5078's
formulasconfig, rendered by HDX-5079) in the chart editor for metric sources, so a derived series likeA / (A + B + C) * 100can be built, validated, saved, and reloaded from the UI.Stacked on #2908 — only the last commit is new here.
Editor
ChartFormulaEditor) on metric-source builder charts (time series / table / number): "Add Formula" appends a row with a monospace letter-ref expression input, an alias, a per-formula number format (reuses the per-series format drawer), and "Remove Formula".validateFormula): malformed expressions, unknown series refs, constant-only expressions, etc. surface live under the input;validateChartFormblocks save/run with the same messages so an invalid expression can never reach ClickHouse.A,B,C, ...) on metric series rows so formula refs are discoverable.showOperandSeries(formula + raw operand series vs formula column(s) only). Adding a formula on a Number tile defaults operands to hidden, since Number tiles render the first value column.A / (A + B + C)can be built.normalizeChartConfigstripsformulas/showOperandSerieson save for non-metric sources and for display types the composed metric query does not render (pie/bar/heatmap/search/patterns), mirroring the existingmetricName/havingstripping. The form state keeps them, so switching back restores the rows.Rendering consumers (positional value-column contract)
The composed metric query projects operand columns (unless hidden) then formula columns, ahead of group-by passthrough columns. Updated the consumers that map columns positionally:
useChartNumberFormats: operand columns →select[i].numberFormat, formula columns →formulas[j].numberFormat, both falling back to the chart-wide format; chart-wide axis format prefers formula formats when operands are hidden.getBuilderValueColumnCounthelper (formula/ratio-aware) used byDBTableChartfor group-by column inference; per-column color mapping skips hidden-operand formula configs.DBTimeChartdrill-down skips the value-range filter when operands are hidden (formula columns don't map ontoselectexpressions).alias || expression).Persistence needs no API changes: tiles validate against
SavedChartConfigSchema, which already carriesformulas/showOperandSeries, andbuilderToRawSqlalready rejects formula configs with a clear message on the Builder → SQL switch.Testing
make ci-lint,make ci-unitpass.DBEditTimeChartForm.test.tsx: Add/Remove Formula, inline validation (malformed / unknown ref / clears when fixed), save round-trip, save blocked on invalid expression, ratio mutual exclusion,showOperandSeriestoggle, non-metric sources show no formula controls.ChartEditor/utils.test.ts:validateChartFormformula rules (including the Number-tile cap lift) and normalization stripping/round-trip.source.test.ts:useChartNumberFormatsformula-column mapping (operands shown/hidden, ratio precedence, chart-format fallbacks) andgetBuilderValueColumnCount.make dev-e2e FILE=dashboard GREP="Metric formulas", passing): creates a metric table tile with two gauge series +A / (A + B) * 100, asserts the inline error for an invalid ref, hides operands, saves, reloads the page, verifies the formula column renders with a finite value, and reopens the editor to verify the round-trip.How to test on Vercel preview
A/Bbadges), click Add Formula, enterA / (A + B) * 100.A / Cto see the inline error; toggle Show input series; save, reload, and confirm the tile renders the formula series.References