Repository navigation
Conversation
nishantmonu51
left a comment
There was a problem hiding this comment.
The branch conflicts with main in KPIProvider.svelte and kpi-grid/index.ts, and the rebase needs to carry more than a marker resolution. On main, totalQuery and comparisonTotalQuery are gated on supportsTotal (#9814), because a measure with required_dimensions has no single total and KPI.svelte hides its comparison block, and every request goes through mapEphemeralMeasuresForRequest / splitTimeSeriesMeasures (#9855, #9864) so adhoc_measures resolve. The new measureComparison branches check only showComparison && isValid && visible and send a raw [{ name: comparisonMeasureKey }], so once merged a kpi_grid measure with required_dimensions fires a comparison totals query the card never renders, and a compare_to pointing at an adhoc_measures entry is sent as a plain measure name and rejected by the runtime as unknown. The backend carries the mirror decision: the measures loop in validateKPIGrid on main accepts ephemeral names while the new compare_to check uses metricsViewHasMeasure only, so both sides need to agree on whether compare_to may name an ad-hoc measure.
One detail to preserve in the resolution: this PR moves .toLowerCase() out of KPI.svelte into the provider's time-comparison branch so a target's display name keeps its casing, while main still lowercases in KPI.svelte and that hunk auto-merges. The provider-side .toLowerCase() has to survive, or time-comparison labels change from "previous period" to "Previous period".
| if !ok { | ||
| return errors.New("each entry in 'measure_comparisons' must be an object with 'measure' and 'compare_to'") | ||
| } | ||
| if _, ok := pathutil.GetPathString(entry, "measure"); !ok { |
There was a problem hiding this comment.
measure is only checked for being a string, so a typo such as measure: revenu reconciles cleanly, KPIGrid.svelte finds no matching entry, and the card silently keeps the time comparison with no error anywhere. Validating it against the metrics view the way compare_to is validated a few lines below would catch that while preserving the intended behaviour that an entry for a measure removed from the grid is inert, since a removed measure is still a measure of the metrics view.
There was a problem hiding this comment.
Done in a5fa389. measure is now checked against the metrics view and the ad-hoc measures, with a test for the typo case. A measure removed from the grid is still valid, as you said.
| comparison?: ComponentComparisonOptions[]; | ||
| // Measure to compare against over the primary time range (e.g. a target), | ||
| // instead of the time comparison. Takes precedence over it when set. | ||
| comparison_measure?: string; |
There was a problem hiding this comment.
comparison_measure lands on KPISpec, which is also the shape of the standalone kpi component, and validateKPI does not know the key, so the two component types now validate the same concept differently. Since KPIProvider is only instantiated from KPIGrid, either keep this out of the public kpi spec or mark it as internal.
There was a problem hiding this comment.
Done in a5fa389. I kept it out of the spec: KPIGrid passes the target to KPIProvider as a prop, and KPISpec is the same as on main.
187a2f4 to
a5fa389
Compare
|
Thanks for the careful review. I rebased on main and put the changes in a second commit (a5fa389), so they are easy to see.
Happy to move the target to the metrics view later if you prefer. |
A canvas KPI can only compare a measure with itself, over an earlier time
range. But a budget, a target or a forecast is a second measure, so there is
no way to render "revenue vs target" today, and those cards end up drawn by
hand as Vega custom charts.
`measure_comparisons` says which measure a KPI is compared against:
```yaml
kpi_grid:
metrics_view: sales
measures: [revenue]
measure_comparisons:
- measure: revenue
compare_to: target_revenue
comparison: [previous, percent_change]
```
The comparison query then asks for that measure over the same time range,
instead of the same measure over an earlier range. The result is stored under
the name of the first measure, so the existing rendering path does not change.
Each pair is a list item, because other canvas widgets already store
per-measure settings that way.
Both measures must belong to the same metrics view, and the reconciler checks
`compare_to`. An entry for a measure the grid no longer shows is ignored, and
not an error: removing a measure in the inspector leaves its entry behind, and
breaking the resource for that would leave a state nobody can fix from the UI,
since the inspector does not show this option.
It also works without a time dimension, which the time comparison does not.
For a percentage measure use `delta`, since Rill already hides
`percent_change` there, and `delta` gives the difference in points.
- Gate the target's totals query on supportsTotal, for the KPI's own measure and for the target. A target without a single total shows no comparison. - Send the target through mapEphemeralMeasuresForRequest and splitTimeSeriesMeasures, so compare_to can name an adhoc_measures entry. The backend now accepts that too. - Validate the measure of each measure_comparisons entry against the metrics view and the ad-hoc measures. - Deleting an ad-hoc measure also drops the entries that compare against it, so the grid does not break on a leftover compare_to. - Pass the target to KPIProvider as a prop instead of adding comparison_measure to the public KPISpec.
a5fa389 to
8986e95
Compare
|
Hi @nishantmonu51, just rebased this so we keep it mergeable until it's done or closed. |
|
Thanks for merging main into the branch. The two review threads have my answers from September. The open design question is still the one from the description: should "compare against a target" live on the metrics view instead of the widget? If you prefer it there, I am happy to drop this and we keep it local. |
nishantmonu51
left a comment
There was a problem hiding this comment.
A few things on the target comparison.
| {m.kpi_vs_comparison({ | ||
| comparison: comparisonLabel?.toLowerCase() ?? "", | ||
| })} | ||
| {m.kpi_vs_comparison({ comparison: comparisonLabel ?? "" })} |
There was a problem hiding this comment.
If Revenue uses the raw number format and Budget uses currency, previous shows the budget without its currency formatting, including in the hover value. Could KPIProvider pass comparisonMeasure into KPI.svelte so the target value uses its own formatter? The delta can keep the primary measure’s format.
There was a problem hiding this comment.
Done in 88238d4. KPIProvider passes the comparison measure to KPI.svelte, so the target's value, its hover value and its point in the sparkline label use the target's own formatter. The delta and the percentage keep the card measure's format, since a difference is in its unit. KPI.spec.ts covers both sides, plus the no-target case, where the comparison still uses the card's measure.
| !!comparisonMeasure && measureSupportsTotalsQuery(comparisonMeasure); | ||
|
|
||
| $: comparisonEnabled = measureComparison | ||
| ? comparisonSupportsTotal |
There was a problem hiding this comment.
When compare_to names a measure with required_dimensions, the YAML reconciles but the target quietly disappears from the card. In the dashboard there’s no way to tell why it’s missing. Could KPI.svelte reuse its kpi_no_total_note hint for a configured target once we know it has no total?
There was a problem hiding this comment.
Done in 88238d4, in the same place and with the same mechanism, but with a new message instead of kpi_no_total_note. That one reads "This measure is computed per dimension value...", and on a card whose own number is right there it sounds like the card's measure has no total. The new one names the target:
The target {target} is computed per dimension value (for example, a rolling average produces one value per day), so it has no single total for the selected range and no comparison is shown.
where {target} is the target measure's display name. Say the word and I will reuse the existing string instead.
| time_filters: kpiGridProperties.time_filters, | ||
| })); | ||
|
|
||
| $: comparisonMeasureNames = kpis.map( |
There was a problem hiding this comment.
Could the existing kpis map return { spec, comparisonMeasureName } for each card? The render loop can destructure the pair and pass both props directly to KPIProvider. That keeps the target beside the measure it belongs to and removes the second array and index lookup.
There was a problem hiding this comment.
Done in 88238d4. The map returns { spec, comparisonMeasureName } per card and the render loop destructures the pair, so the second array and the index lookup are gone.
| showSparkline && | ||
| showComparison && | ||
| visible, | ||
| enabled: measureComparison |
There was a problem hiding this comment.
Could KPIProvider derive the comparison start and end once and use those values in both the sparkline request and its enabled check? The range is the only thing that differs across these two branches. The validity, grain, visibility, and display checks can live in one place.
There was a problem hiding this comment.
Done in 88238d4. comparisonStart and comparisonEnd are derived once, and the totals query, the sparkline query and both enabled checks read them. Each enabled check is a single expression now. A measure comparison on a metrics view with no time series still runs, so the range check keeps that case.
Keeps both sides in the two conflicting files: the KPI grid's size switcher next to measure_comparisons, and the workbook export next to the measure comparison in KPIProvider. The export reads the comparison total through comparisonTotalResult, since with a target configured the raw query keys the row by the target's name rather than the card's measure.
- The target carries its own format: KPIProvider passes the comparison measure to KPI.svelte, which formats the target's value, its hover value and its sparkline point with that measure's formatter. The delta and the percentage keep the card measure's format, since a difference is in its unit. - A target with required_dimensions has no single total, so the card shows no comparison. Its tooltip now says so, the way an unsummable measure explains its own missing total. - The KPI grid pairs each spec with its target in one map, instead of a second array looked up by index. - KPIProvider derives the comparison range once; the totals and sparkline queries read it, and each has a single enabled check. - KPI.spec.ts covers the formats on both sides and the tooltip note.
|
Thanks, all four are addressed in 88238d4. I also merged main, which conflicted in the same two files as before: the grid's size switcher next to Checks: |
nishantmonu51
left a comment
There was a problem hiding this comment.
Left a few notes on how the target shows up.
| showComparison && !$comparisonTotalQuery.isFetching | ||
| ? (($comparisonTotalQuery.data?.data?.[0]?.[measureName] ?? null) as | ||
| showComparison && !comparisonTotalResult.isFetching | ||
| ? ((comparisonTotalResult.data?.data?.[0]?.[measureName] ?? null) as |
There was a problem hiding this comment.
The KPI now displays a currency target like $10,800, but its Excel export still uses the primary measure's number format for the target. With raw Revenue and currency Budget, the workbook shows 10,800. Could KpiExportValue carry the comparison measure so collect.ts and writeKpiComparison can format the target separately while the delta keeps the primary format?
There was a problem hiding this comment.
Done in abf8d07. KpiExportValue carries the comparison measure, collect.ts sets a new WorkbookKpiValue.comparison_number_format, and writeKpiComparison styles the comparison row with it; Change keeps the KPI's format. Two things came with it: the target no longer sizes the card measure's format (it was one of excelNumberFormat's samples), and a long target name is now cut short instead of becoming "Previous", which reports a target as a period. Nine characters fit when two KPIs sit side by side, so Rolling budget already hit that.
Left open: the KPIs data sheet formats per row, so the comparison cell there still takes the row measure's format. Per-cell formats would mean changing WorkbookDataSheet, your call whether that belongs in this PR. I did rename that column to "Compared to", since it holds a period name or a target's name.
| <span | ||
| >{(i === 0 | ||
| ? formatterFunction | ||
| : (secondaryFormatterFunction ?? formatterFunction))( |
There was a problem hiding this comment.
When the comparison is a target, both points cover the same bucket. The sparkline still labels it vs. <date> below the chart, which reads like a period comparison and repeats the current date. Could KPI.svelte pass the target's label along with its formatter so Chart can show vs. Budget for that series?
There was a problem hiding this comment.
Done in abf8d07. Chart takes a secondarySeriesLabel beside the formatter, so the label under it reads Sep 2026 vs. Budget. The tooltip over the points repeated the date the same way (6,600 Sep 1, 2026 over $5,800 Sep 1, 2026); its second row now reads $5,800 Budget. Neither renders in jsdom, so no test for them: I checked both in a canvas, with a target and with a period comparison.
| // A target is a measure of its own, so its value is formatted with its own | ||
| // format rather than the primary measure's. The delta and the percentage | ||
| // stay on the primary measure's format: a difference is in its unit. | ||
| $: comparisonFormatterMeasure = comparisonMeasure ?? measure; |
There was a problem hiding this comment.
A regular period comparison builds the same two formatters twice here. Could comparisonValueFormatter and comparisonValueFormatterTooltip fall back to measureValueFormatter and measureValueFormatterTooltip and only construct a formatter when there's a comparisonMeasure? That also removes comparisonFormatterMeasure.
There was a problem hiding this comment.
Done in abf8d07. Both fall back to measureValueFormatter and measureValueFormatterTooltip, and comparisonFormatterMeasure is gone.
- The workbook formats a target with the target's own format. The
payload carries the comparison measure, collect.ts turns it into
WorkbookKpiValue.comparison_number_format, and writeKpiComparison
styles the comparison row with it. The change row keeps the KPI's
format, since a difference is in its unit.
- The target is also left out of the samples the KPI's own format is
sized from, so a target in another unit no longer decides how many
decimals the card measure gets.
- A long target name is cut short in the workbook rather than replaced
by "Previous", which would misreport it as a period. Two KPIs side by
side leave nine characters, so most target names hit this.
- The KPIs sheet column is "Compared to": it holds a period name or a
target's name.
- The sparkline names the target where it used to repeat the hovered
bucket's date, which a target shares with the primary series: both
under the chart ("vs. Budget") and in the tooltip over the points.
- KPI.svelte builds no comparison formatter for a period comparison: it
falls back to the card measure's, which is what it compares.
- kpi_grid rejects a measure compared against itself, which is always a
zero change, and a second measure_comparisons entry for one measure,
of which only the first ever applied.
|
All three are in abf8d07, with the extras noted in the threads. Two more from the same read, both in Checks: |
nishantmonu51
left a comment
There was a problem hiding this comment.
A few things inline.
| {#if comparisonPoint} | ||
| <span class="text-fg-muted"> | ||
| vs. {formatDate(comparisonPoint.originalDate)} | ||
| vs. {secondarySeriesLabel ?? |
There was a problem hiding this comment.
A long target name like “Rolling revenue budget for returning customers” can run into the next card when two KPIs sit side by side. The range label is positioned over the hovered bucket and can't wrap. Could we constrain and ellipsize the target in this label and leave the full name in the hover tooltip?
| `adhoc_measures`, and each measure takes at most one entry. If it has no single | ||
| total, because it has `required_dimensions`, the card shows no comparison and | ||
| its tooltip says why: |
There was a problem hiding this comment.
The validator now rejects measure: revenue with compare_to: revenue. Could we call that out here too? The agent docs already do, and this also makes it clear that the total requirement applies to the target.
| `adhoc_measures`, and each measure takes at most one entry. If it has no single | |
| total, because it has `required_dimensions`, the card shows no comparison and | |
| its tooltip says why: | |
| `adhoc_measures`. Each measure takes at most one entry and must target a different measure. | |
| If the target has `required_dimensions`, it has no single total. The card shows | |
| no comparison and explains why in its tooltip. |
| secondaryFormatterFunction={comparisonMeasure | ||
| ? comparisonValueFormatter | ||
| : undefined} |
There was a problem hiding this comment.
comparisonValueFormatter already falls back to measureValueFormatter for period comparisons. Can we pass it straight through and drop the second check here?
| secondaryFormatterFunction={comparisonMeasure | |
| ? comparisonValueFormatter | |
| : undefined} | |
| secondaryFormatterFunction={comparisonValueFormatter} |
We wanted to show a measure next to its target in a canvas KPI, and we could not find a way to do it. If there is one and we missed it please let us know, feel free to close this.
What we found: a canvas KPI can only compare a measure with itself, over an earlier time range. But a budget, a target or a forecast is a second measure, so today there is no way to render "revenue vs target". We drew those cards by hand instead, as Vega custom charts, about 35 lines of positioned text per card. This PR is what we use now, in case it is useful to you too.
How it works: the comparison query asks for the other measure over the same time range. It does not ask for the same measure over an earlier range. The result is then stored under the name of the first measure, so
KPI.svelterenders it as before and we did not have to touch it.Things we were not sure about
measure_comparisons. We avoidedcomparison_measuresbecause that name already means something else in the codebase. Rename it if you prefer.delta, notpercent_change. Rill already hidespercent_changefor percentage measures.deltathen gives the difference in points. We wrote this in the docs instead of adding a special case.Some questions. We do not know Rill well enough to answer these ourselves.
Happy to rename things or rework it, and just as happy to keep it on our side if you do not want it.