diff --git a/knip.config.ts b/knip.config.ts index a2ee5a77c268..8e79d169ee9d 100644 --- a/knip.config.ts +++ b/knip.config.ts @@ -87,6 +87,7 @@ const config: KnipConfig = { entry: [ ...productionEntryPoints.map(entry => `${entry}!`), ...frontendWorkspace.entry, + 'views/dashboards/widgetBuilder/components/visualize/traceMetrics/rowModel.ts!', '**/__mocks__/**/*.{js,ts,tsx}', '**/*.snapshots.tsx', '**/*.stories.{js,ts,tsx}', diff --git a/static/app/views/dashboards/widgetBuilder/components/visualize/traceMetrics/rowModel.spec.tsx b/static/app/views/dashboards/widgetBuilder/components/visualize/traceMetrics/rowModel.spec.tsx new file mode 100644 index 000000000000..a34e2fc98a4f --- /dev/null +++ b/static/app/views/dashboards/widgetBuilder/components/visualize/traceMetrics/rowModel.spec.tsx @@ -0,0 +1,453 @@ +import type {Column} from 'sentry/utils/discover/fields'; + +import { + addEquationRow, + addMetricRow, + deleteRow, + fieldsFromRows, + getEquationLabels, + getHiddenAggregates, + getReferenceMap, + isRowReferenced, + moveRow, + rowsFromFields, + setEquationExpression, + setRowAlias, + setRowField, + setRowVisible, + type MetricRow, + type TraceMetricRow, +} from './rowModel'; + +const SUM_FOO = 'sum(value,foo,counter,none)'; +const SUM_BAR = 'sum(value,bar,counter,none)'; +const COUNT_FOO = 'count(value,foo,counter,none)'; +const SUM_BAZ_FILTERED = 'sum_if(`release:1.0`,value,baz,counter,none)'; + +function fn(aggregate: string, alias?: string): Column { + const [name, args] = aggregate.replace(/\)$/, '').split('('); + return { + kind: 'function', + function: [name as any, ...(args!.split(',') as [string, ...string[]])], + alias, + }; +} + +function eq(expression: string, alias?: string): Column { + return {kind: 'equation', field: expression, alias}; +} + +function col(field: string): Column { + return {kind: 'field', field}; +} + +function letters(rows: TraceMetricRow[]): string[] { + return rows.map(row => (row.kind === 'metric' ? row.letter : row.kind)); +} + +function metric(rows: TraceMetricRow[], letter: string): MetricRow { + const row = rows.find(r => r.kind === 'metric' && r.letter === letter); + if (row?.kind !== 'metric') { + throw new Error(`No metric row ${letter}`); + } + return row; +} + +describe('rowModel', () => { + describe('rowsFromFields', () => { + it('gives visible aggregates letters in order and keeps columns', () => { + const rows = rowsFromFields([col('transaction'), fn(SUM_FOO), fn(SUM_BAR)]); + + expect(letters(rows)).toEqual(['column', 'A', 'B']); + expect(rows.every(row => row.id)).toBe(true); + }); + + it('makes a hidden row above the equation for an aggregate only it uses', () => { + const rows = rowsFromFields([fn(SUM_FOO), eq(`${SUM_FOO} / ${SUM_BAR}`)]); + + expect(letters(rows)).toEqual(['A', 'B', 'equation']); + expect(metric(rows, 'B').visible).toBe(false); + expect(rows[2]).toMatchObject({kind: 'equation', expression: 'A / B'}); + }); + + it('does not make hidden rows for aggregates a later field provides', () => { + const rows = rowsFromFields([ + eq(`${SUM_FOO} + ${SUM_BAR}`), + fn(SUM_FOO), + fn(SUM_BAR), + ]); + + expect(letters(rows)).toEqual(['equation', 'A', 'B']); + expect(rows[0]).toMatchObject({expression: 'A + B'}); + expect(getHiddenAggregates(rows)).toEqual([]); + }); + + it('gives repeated uses of a shared aggregate distinct letters', () => { + const rows = rowsFromFields([ + fn(SUM_FOO), + fn(SUM_FOO), + eq(`${SUM_FOO} + ${SUM_FOO}`), + ]); + + expect(rows[2]).toMatchObject({expression: 'A + B'}); + }); + + it('leaves functions without a metric as text', () => { + const rows = rowsFromFields([eq('performance_score() * 100')]); + + expect(rows).toHaveLength(1); + expect(rows[0]).toMatchObject({expression: 'performance_score() * 100'}); + }); + + it('keeps conditional aggregates intact', () => { + const rows = rowsFromFields([eq(`${SUM_BAZ_FILTERED} * 2`)]); + + expect(getHiddenAggregates(rows)).toEqual([SUM_BAZ_FILTERED]); + }); + }); + + describe('fieldsFromRows', () => { + it('round-trips the saved shape', () => { + const fields = [ + col('transaction'), + fn(SUM_FOO, 'Foo'), + eq(`${SUM_FOO} / ${SUM_BAR}`, 'Ratio'), + ]; + + expect(fieldsFromRows(rowsFromFields(fields))).toEqual(fields); + }); + + it('leaves hidden rows out', () => { + const rows = setRowVisible( + rowsFromFields([fn(SUM_FOO), eq(`${SUM_FOO} * 2`)]), + metric(rowsFromFields([fn(SUM_FOO)]), 'A').id, + false + ); + // The id above came from a different model, so nothing was hidden. + expect(fieldsFromRows(rows)).toHaveLength(2); + + const hidden = setRowVisible(rows, metric(rows, 'A').id, false); + expect(fieldsFromRows(hidden)).toEqual([eq(`${SUM_FOO} * 2`)]); + }); + }); + + describe('editing', () => { + it('keeps letters and the equation when rows are reordered', () => { + const rows = rowsFromFields([ + fn(SUM_FOO), + fn(SUM_BAR), + eq(`${SUM_FOO} + ${SUM_BAR}`), + ]); + + const moved = moveRow(rows, 1, 0); + + expect(letters(moved)).toEqual(['B', 'A', 'equation']); + expect(moved[2]).toMatchObject({expression: 'A + B'}); + expect(fieldsFromRows(moved)).toEqual([ + fn(SUM_BAR), + fn(SUM_FOO), + eq(`${SUM_FOO} + ${SUM_BAR}`), + ]); + }); + + it('keeps a hidden row in place with its letter', () => { + const rows = rowsFromFields([ + fn(SUM_FOO), + fn(SUM_BAR), + eq(`${SUM_FOO} + ${SUM_BAR}`), + ]); + + const hidden = setRowVisible(rows, metric(rows, 'A').id, false); + + expect(letters(hidden)).toEqual(['A', 'B', 'equation']); + expect(metric(hidden, 'A').visible).toBe(false); + expect(fieldsFromRows(hidden)).toEqual([ + fn(SUM_BAR), + eq(`${SUM_FOO} + ${SUM_BAR}`), + ]); + + const shown = setRowVisible(hidden, metric(hidden, 'A').id, true); + expect(fieldsFromRows(shown)).toEqual(fieldsFromRows(rows)); + }); + + it('does not touch the equation when a row changes aggregate', () => { + const rows = rowsFromFields([ + fn(SUM_FOO), + fn(SUM_FOO), + eq(`${SUM_FOO} + ${SUM_FOO}`), + ]); + + const edited = setRowField(rows, metric(rows, 'B').id, fn(COUNT_FOO)); + + expect(edited[2]).toMatchObject({expression: 'A + B'}); + expect(fieldsFromRows(edited)).toEqual([ + fn(SUM_FOO), + fn(COUNT_FOO), + eq(`${SUM_FOO} + ${COUNT_FOO}`), + ]); + }); + + it('gives a column a letter when it becomes an aggregate', () => { + const rows = rowsFromFields([col('transaction'), fn(SUM_FOO)]); + + const edited = setRowField(rows, rows[0]!.id, fn(SUM_BAR)); + + expect(letters(edited)).toEqual(['B', 'A']); + expect(edited[0]!.id).toBe(rows[0]!.id); + }); + + it('drops hidden rows an equation no longer names', () => { + const rows = rowsFromFields([fn(SUM_FOO), eq(`${SUM_FOO} / ${SUM_BAR}`)]); + expect(letters(rows)).toEqual(['A', 'B', 'equation']); + + const edited = setEquationExpression(rows, rows[2]!.id, 'A * 2'); + expect(letters(edited)).toEqual(['A', 'equation']); + + const deleted = deleteRow(rows, rows[2]!.id); + expect(letters(deleted)).toEqual(['A']); + }); + + it("resolves the equation with a hidden row's new aggregate", () => { + const rows = rowsFromFields([fn(SUM_FOO), eq(`${SUM_BAR} * 2`)]); + + const edited = setRowField(rows, metric(rows, 'B').id, fn(COUNT_FOO)); + + expect(metric(edited, 'B')).toMatchObject({visible: false}); + expect(fieldsFromRows(edited)).toEqual([fn(SUM_FOO), eq(`${COUNT_FOO} * 2`)]); + }); + + it("resolves the equation with a row's filtered aggregate", () => { + const rows = rowsFromFields([ + fn(SUM_FOO), + fn(SUM_BAR), + eq(`${SUM_FOO} / ${SUM_BAR}`), + ]); + + const filtered = setRowField(rows, metric(rows, 'A').id, fn(SUM_BAZ_FILTERED)); + + expect(fieldsFromRows(filtered)).toEqual([ + fn(SUM_BAZ_FILTERED), + fn(SUM_BAR), + eq(`${SUM_BAZ_FILTERED} / ${SUM_BAR}`), + ]); + }); + + it('keeps a hidden row two equations share until both are gone', () => { + const rows = rowsFromFields([ + fn(SUM_FOO), + eq(`${SUM_BAR} * 2`), + eq(`${SUM_BAR} + ${SUM_FOO}`), + ]); + expect(letters(rows)).toEqual(['A', 'B', 'equation', 'equation']); + + const oneLeft = deleteRow(rows, rows[2]!.id); + expect(letters(oneLeft)).toEqual(['A', 'B', 'equation']); + expect(oneLeft[2]).toMatchObject({expression: 'B + A'}); + + const noneLeft = deleteRow(oneLeft, oneLeft[2]!.id); + expect(letters(noneLeft)).toEqual(['A']); + }); + + it('keeps the alias and letter of a row through hide and show', () => { + let rows = rowsFromFields([fn(SUM_FOO, 'Foo'), fn(SUM_BAR), eq(`${SUM_FOO} * 2`)]); + const id = metric(rows, 'A').id; + + rows = setRowVisible(rows, id, false); + rows = moveRow(rows, 1, 2); + rows = setRowVisible(rows, id, true); + + expect(letters(rows)).toEqual(['A', 'equation', 'B']); + expect(fieldsFromRows(rows)).toEqual([ + fn(SUM_FOO, 'Foo'), + eq(`${SUM_FOO} * 2`), + fn(SUM_BAR), + ]); + }); + + it('leaves the equation alone when a duplicate of a referenced row is hidden', () => { + const rows = rowsFromFields([ + fn(SUM_FOO), + fn(SUM_FOO), + eq(`${SUM_FOO} + ${SUM_FOO}`), + ]); + + const hidden = setRowVisible(rows, metric(rows, 'B').id, false); + + expect(hidden[2]).toMatchObject({expression: 'A + B'}); + expect(fieldsFromRows(hidden)).toEqual([ + fn(SUM_FOO), + eq(`${SUM_FOO} + ${SUM_FOO}`), + ]); + }); + + it('reports which rows an equation names', () => { + const rows = rowsFromFields([fn(SUM_FOO), fn(SUM_BAR), eq(`${SUM_FOO} * 2`)]); + + expect(isRowReferenced(rows, metric(rows, 'A').id)).toBe(true); + expect(isRowReferenced(rows, metric(rows, 'B').id)).toBe(false); + }); + + it('adds rows with the next free letter', () => { + let rows = rowsFromFields([fn(SUM_FOO), fn(SUM_BAR)]); + rows = deleteRow(rows, metric(rows, 'A').id); + + rows = addMetricRow(rows, fn(COUNT_FOO)); + rows = addEquationRow(rows); + + expect(letters(rows)).toEqual(['B', 'A', 'equation']); + expect(getEquationLabels(rows).get(rows[2]!.id)).toBe('ƒ1'); + }); + + it('sets aliases on metric and equation rows', () => { + let rows = rowsFromFields([fn(SUM_FOO), eq(`${SUM_FOO} * 2`)]); + + rows = setRowAlias(rows, rows[0]!.id, 'Foo'); + rows = setRowAlias(rows, rows[1]!.id, 'Double'); + + expect(fieldsFromRows(rows)).toEqual([ + fn(SUM_FOO, 'Foo'), + eq(`${SUM_FOO} * 2`, 'Double'), + ]); + }); + + it('builds the reference map from every metric row', () => { + const rows = rowsFromFields([fn(SUM_FOO), eq(`${SUM_FOO} / ${SUM_BAR}`)]); + + expect(getReferenceMap(rows)).toEqual({A: SUM_FOO, B: SUM_BAR}); + }); + }); + + describe('reconciling with outside changes', () => { + it('keeps ids and letters when the state fields still match', () => { + const rows = rowsFromFields([ + fn(SUM_FOO), + fn(SUM_BAR), + eq(`${SUM_FOO} + ${SUM_BAR}`), + ]); + const moved = moveRow(rows, 1, 0); + + const next = rowsFromFields(fieldsFromRows(moved), moved); + + expect(next.map(row => row.id)).toEqual(moved.map(row => row.id)); + expect(letters(next)).toEqual(['B', 'A', 'equation']); + expect(next[2]).toMatchObject({expression: 'A + B'}); + }); + + it('keeps letters when a y-axis cap drops trailing rows', () => { + const rows = rowsFromFields([fn(SUM_FOO), fn(SUM_BAR), fn(COUNT_FOO)]); + const trimmed = rowsFromFields([fn(SUM_FOO), fn(SUM_BAR)], rows); + + expect(trimmed.map(row => row.id)).toEqual(rows.slice(0, 2).map(row => row.id)); + expect(letters(trimmed)).toEqual(['A', 'B']); + }); + + it('keeps a hidden row where it was when fields change elsewhere', () => { + const rows = rowsFromFields([ + fn(SUM_FOO), + fn(SUM_BAR), + eq(`${SUM_FOO} + ${SUM_BAR}`), + ]); + const hidden = setRowVisible(rows, metric(rows, 'A').id, false); + + // Something outside the list appended a row to the saved fields. + const next = rowsFromFields([...fieldsFromRows(hidden), fn(COUNT_FOO)], hidden); + + expect(letters(next)).toEqual(['A', 'B', 'equation', 'C']); + expect(metric(next, 'A')).toMatchObject({id: metric(rows, 'A').id, visible: false}); + expect(next[2]).toMatchObject({expression: 'A + B'}); + }); + + it('keeps the other rows when one is deleted outside the model', () => { + const rows = rowsFromFields([ + fn(SUM_FOO), + fn(SUM_BAR), + fn(COUNT_FOO), + eq(`${SUM_FOO} * 2`), + ]); + + // DELETE_AGGREGATE removed B from the saved fields. + const next = rowsFromFields( + [fn(SUM_FOO), fn(COUNT_FOO), eq(`${SUM_FOO} * 2`)], + rows + ); + + expect(letters(next)).toEqual(['A', 'C', 'equation']); + expect(next.map(row => row.id)).toEqual([rows[0]!.id, rows[2]!.id, rows[3]!.id]); + expect(next[2]).toMatchObject({expression: 'A * 2'}); + }); + + it('keeps letters when the saved fields are reordered outside the model', () => { + const rows = rowsFromFields([ + fn(SUM_FOO), + fn(SUM_BAR), + eq(`${SUM_FOO} - ${SUM_BAR}`), + ]); + + const next = rowsFromFields( + [fn(SUM_BAR), eq(`${SUM_FOO} - ${SUM_BAR}`), fn(SUM_FOO)], + rows + ); + + expect(letters(next)).toEqual(['B', 'equation', 'A']); + expect(next[1]).toMatchObject({expression: 'A - B'}); + }); + + it('keeps a hidden row that shares its aggregate with a visible row', () => { + const rows = rowsFromFields([ + fn(SUM_FOO), + fn(SUM_FOO), + eq(`${SUM_FOO} / ${SUM_FOO}`), + ]); + const hidden = setRowVisible(rows, metric(rows, 'A').id, false); + + // A display type switch round-trips the saved shape. + const next = rowsFromFields(fieldsFromRows(hidden), hidden); + + expect(letters(next)).toEqual(['A', 'B', 'equation']); + expect(metric(next, 'A')).toMatchObject({id: metric(rows, 'A').id, visible: false}); + expect(metric(next, 'B')).toMatchObject({id: metric(rows, 'B').id, visible: true}); + expect(next[2]).toMatchObject({expression: 'A / B'}); + }); + + it('does not let a new visible field claim a hidden row', () => { + const rows = rowsFromFields([ + fn(SUM_FOO), + fn(SUM_FOO), + eq(`${SUM_FOO} / ${SUM_FOO}`), + ]); + const hidden = setRowVisible(rows, metric(rows, 'A').id, false); + + // Something outside the list added another sum(foo). + const next = rowsFromFields([...fieldsFromRows(hidden), fn(SUM_FOO)], hidden); + + expect(letters(next)).toEqual(['A', 'B', 'equation', 'C']); + expect(metric(next, 'A').visible).toBe(false); + expect(next[2]).toMatchObject({expression: 'A / B'}); + }); + + it('hides a named row whose field was removed outside the model', () => { + const rows = rowsFromFields([ + fn(SUM_FOO), + fn(SUM_BAR), + eq(`${SUM_FOO} + ${SUM_BAR}`), + ]); + + // A y-axis cap dropped A's field but the equation still names it. + const next = rowsFromFields([fn(SUM_BAR), eq(`${SUM_FOO} + ${SUM_BAR}`)], rows); + + expect(letters(next)).toEqual(['A', 'B', 'equation']); + expect(metric(next, 'A')).toMatchObject({id: metric(rows, 'A').id, visible: false}); + expect(next[2]).toMatchObject({expression: 'A + B'}); + expect(fieldsFromRows(next)).toEqual([fn(SUM_BAR), eq(`${SUM_FOO} + ${SUM_BAR}`)]); + }); + + it('gives new letters to rows it cannot match', () => { + const rows = rowsFromFields([fn(SUM_FOO)]); + + const next = rowsFromFields([fn(SUM_BAR), fn(SUM_FOO)], rows); + + expect(letters(next)).toEqual(['B', 'A']); + expect(metric(next, 'A').id).toBe(rows[0]!.id); + }); + }); +}); diff --git a/static/app/views/dashboards/widgetBuilder/components/visualize/traceMetrics/rowModel.ts b/static/app/views/dashboards/widgetBuilder/components/visualize/traceMetrics/rowModel.ts new file mode 100644 index 000000000000..31e688af7885 --- /dev/null +++ b/static/app/views/dashboards/widgetBuilder/components/visualize/traceMetrics/rowModel.ts @@ -0,0 +1,558 @@ +import uniqueId from 'lodash/uniqueId'; + +import { + isTokenFreeText, + isTokenFunction, + isTokenReference, +} from 'sentry/components/arithmeticBuilder/token'; +import {tokenizeExpression} from 'sentry/components/arithmeticBuilder/tokenizer'; +import { + explodeFieldString, + generateFieldAsString, + type Column, +} from 'sentry/utils/discover/fields'; +import {extractTraceMetricFromColumn} from 'sentry/views/dashboards/widgetBuilder/utils/buildTraceMetricAggregate'; +import {FieldValueKind} from 'sentry/views/discover/table/types'; +import {getFunctionLabel} from 'sentry/views/explore/utils/getFunctionLabel'; + +/** + * The editing model behind the unified trace metrics visualize list. + * + * A saved widget is a flat list of columns where equations are plain text with + * the aggregates written inline. That shape has no row identity, so letters + * and hidden rows cannot survive edits. This model gives every row an id and + * gives metric rows a letter that never changes. Equations are kept in letter + * form while editing. `fieldsFromRows` turns the model back into the saved + * shape; `rowsFromFields` builds the model from it, reusing an earlier model + * when there is one so ids and letters are kept. + * + * @public used by the unified visualize list in a follow-up PR + */ +export interface MetricRow { + field: Column; + id: string; + kind: 'metric'; + /** + * Stable reference letter (A, B, ...). Handed out once, never reassigned. + */ + letter: string; + /** + * Hidden rows only feed equations. They are not saved on the widget. + */ + visible: boolean; +} + +/** + * A plain group-by column in a table. + * @public used by the unified visualize list in a follow-up PR + */ +export interface ColumnRow { + field: Column; + id: string; + kind: 'column'; +} + +/** + * An equation over the metric rows. + * @public used by the unified visualize list in a follow-up PR + */ +export interface EquationRow { + /** + * The equation in letter form, e.g. `A + B`. + */ + expression: string; + id: string; + kind: 'equation'; + alias?: string; +} + +export type TraceMetricRow = MetricRow | ColumnRow | EquationRow; + +/** + * Normalizes an aggregate string so equivalent spellings compare equal. + * @public used by the unified visualize list in a follow-up PR + */ +export function getAggregateKey(aggregate: string): string { + return generateFieldAsString(explodeFieldString(aggregate)); +} + +function newRowId(): string { + return uniqueId('trace-metric-row-'); +} + +function columnKey(field: Column): string { + return field.kind === FieldValueKind.FUNCTION + ? getAggregateKey(generateFieldAsString(field)) + : generateFieldAsString(field); +} + +function isMetricRow(row: TraceMetricRow): row is MetricRow { + return row.kind === 'metric'; +} + +function isEquationRow(row: TraceMetricRow): row is EquationRow { + return row.kind === 'equation'; +} + +function getLetters(rows: TraceMetricRow[]): Set { + return new Set(rows.filter(isMetricRow).map(row => row.letter)); +} + +function getNextLetter(rows: TraceMetricRow[]): string { + const used = getLetters(rows); + let index = 0; + while (used.has(getFunctionLabel(index))) { + index++; + } + return getFunctionLabel(index); +} + +/** + * Letter to aggregate text, for every metric row, visible or hidden. + * @public used by the unified visualize list in a follow-up PR + */ +export function getReferenceMap(rows: TraceMetricRow[]): Record { + return Object.fromEntries( + rows.filter(isMetricRow).map(row => [row.letter, generateFieldAsString(row.field)]) + ); +} + +/** + * Letters named by at least one equation. + */ +function getReferencedLetters(rows: TraceMetricRow[]): Set { + const letters = getLetters(rows); + const referenced = new Set(); + for (const row of rows) { + if (!isEquationRow(row)) { + continue; + } + for (const token of tokenizeExpression(row.expression, letters)) { + if (isTokenReference(token)) { + referenced.add(token.text); + } + } + } + return referenced; +} + +/** + * Whether an equation names this metric row. + * @public used by the unified visualize list in a follow-up PR + */ +export function isRowReferenced(rows: TraceMetricRow[], id: string): boolean { + const row = rows.find(r => r.id === id); + return !!row && isMetricRow(row) && getReferencedLetters(rows).has(row.letter); +} + +/** + * Equation labels (ƒ1, ƒ2, ...) by equation order, keyed by row id. + * @public used by the unified visualize list in a follow-up PR + */ +export function getEquationLabels(rows: TraceMetricRow[]): Map { + const labels = new Map(); + let index = 1; + for (const row of rows) { + if (isEquationRow(row)) { + labels.set(row.id, `ƒ${index++}`); + } + } + return labels; +} + +/** + * Aggregates of the hidden rows, as saved-form strings. + * @public used by the unified visualize list in a follow-up PR + */ +export function getHiddenAggregates(rows: TraceMetricRow[]): string[] { + return rows + .filter((row): row is MetricRow => isMetricRow(row) && !row.visible) + .map(row => generateFieldAsString(row.field)); +} + +/** + * Letter form to saved form. Letters that match no row are left as typed. + */ +function resolveExpression( + expression: string, + referenceMap: Record +): string { + return tokenizeExpression(expression, new Set(Object.keys(referenceMap))) + .map(token => { + if (isTokenReference(token) && token.text in referenceMap) { + return referenceMap[token.text]!; + } + return isTokenFreeText(token) ? '' : token.text; + }) + .filter(text => text.length > 0) + .join(' '); +} + +/** + * Saved form to letter form. When several rows share an aggregate, each + * repeated use takes the next such letter in turn, so `x + x` with rows A and + * B both on `x` reads as `A + B`. Functions without a row stay as text. + */ +function unresolveExpression( + expression: string, + referenceMap: Record +): string { + const lettersByKey = new Map(); + for (const [letter, aggregate] of Object.entries(referenceMap)) { + const key = getAggregateKey(aggregate); + lettersByKey.set(key, [...(lettersByKey.get(key) ?? []), letter]); + } + + const uses = new Map(); + return tokenizeExpression(expression) + .map(token => { + if (!isTokenFunction(token)) { + return token.text; + } + const key = getAggregateKey(token.text); + const letters = lettersByKey.get(key); + if (!letters?.length) { + return token.text; + } + const n = uses.get(key) ?? 0; + uses.set(key, n + 1); + return letters[n % letters.length]!; + }) + .filter(text => text.length > 0) + .join(' '); +} + +/** + * Unique metric aggregates inside a saved-form equation, in order of first use. + */ +function getEquationAggregates(equation: string): string[] { + const seen = new Set(); + const aggregates: string[] = []; + for (const token of tokenizeExpression(equation)) { + if (!isTokenFunction(token)) { + continue; + } + const key = getAggregateKey(token.text); + if (seen.has(key) || !extractTraceMetricFromColumn(explodeFieldString(token.text))) { + continue; + } + seen.add(key); + aggregates.push(token.text); + } + return aggregates; +} + +/** + * Drops hidden rows that no equation names any more. + */ +function pruneOrphanHiddenRows(rows: TraceMetricRow[]): TraceMetricRow[] { + const referenced = getReferencedLetters(rows); + return rows.filter( + row => !isMetricRow(row) || row.visible || referenced.has(row.letter) + ); +} + +/** + * The saved shape: visible metric rows, column rows and equations in row + * order, with equations resolved to inline text. Hidden rows are left out. + * @public used by the unified visualize list in a follow-up PR + */ +export function fieldsFromRows(rows: TraceMetricRow[]): Column[] { + const referenceMap = getReferenceMap(rows); + const fields: Column[] = []; + for (const row of rows) { + if (isEquationRow(row)) { + fields.push({ + kind: FieldValueKind.EQUATION, + field: resolveExpression(row.expression, referenceMap), + alias: row.alias, + }); + } else if (!isMetricRow(row) || row.visible) { + fields.push(row.field); + } + } + return fields; +} + +/** + * Builds the model from the saved shape. + * + * With `previous`, rows are matched back by kind and value, in order, so ids + * and letters are kept across changes made outside the list (a display type + * switch, a sort fix-up). An equation whose letters still produce its saved + * text keeps those letters and the hidden rows they name. Anything unmatched + * gets a new id and the next free letter. + * @public used by the unified visualize list in a follow-up PR + */ +export function rowsFromFields( + fields: Column[], + previous: TraceMetricRow[] = [] +): TraceMetricRow[] { + const unused = new Set(previous); + const take = (match: (row: TraceMetricRow) => boolean): TraceMetricRow | undefined => { + for (const row of unused) { + if (match(row)) { + unused.delete(row); + return row; + } + } + return undefined; + }; + + // An aggregate any visible field provides needs no hidden row, even when + // that field comes after the equation. + const visibleKeys = new Set( + fields + .filter(field => field.kind === FieldValueKind.FUNCTION) + .map(field => columnKey(field)) + ); + + // Pair equation fields with the previous equation rows, in order. Where the + // old letters still spell the saved text, reserve the rows they name that no + // visible field will match: hidden rows, so a visible field with the same + // aggregate cannot claim them, and visible rows whose field went away, which + // become hidden rows. + const previousMap = getReferenceMap(previous); + const previousLetters = new Set(Object.keys(previousMap)); + const previousEquations = previous.filter(isEquationRow); + const reusedHidden: Array<{index: number; row: MetricRow}> = []; + const equationMatches = new Map(); + fields.forEach((field, fieldIndex) => { + if (field.kind !== FieldValueKind.EQUATION) { + return; + } + const match = previousEquations[equationMatches.size]; + if (!match) { + return; + } + unused.delete(match); + const keepsLetters = resolveExpression(match.expression, previousMap) === field.field; + equationMatches.set(fieldIndex, {row: match, keepsLetters}); + if (!keepsLetters) { + return; + } + for (const token of tokenizeExpression(match.expression, previousLetters)) { + if (!isTokenReference(token)) { + continue; + } + const named = previous.find(row => isMetricRow(row) && row.letter === token.text); + if ( + named && + isMetricRow(named) && + unused.has(named) && + (!named.visible || !visibleKeys.has(columnKey(named.field))) + ) { + unused.delete(named); + reusedHidden.push({ + index: previous.indexOf(named), + row: {...named, visible: false}, + }); + } + } + }); + + const rows: TraceMetricRow[] = []; + const pendingEquations: Array<{reused: boolean; row: EquationRow; text: string}> = []; + + const hasMetric = (key: string) => + visibleKeys.has(key) || + rows.some(row => isMetricRow(row) && columnKey(row.field) === key) || + reusedHidden.some(({row}) => columnKey(row.field) === key); + + fields.forEach((field, fieldIndex) => { + if (field.kind === FieldValueKind.FIELD) { + const key = columnKey(field); + const match = take(row => row.kind === 'column' && columnKey(row.field) === key); + rows.push({id: match?.id ?? newRowId(), kind: 'column', field}); + return; + } + + if (field.kind === FieldValueKind.FUNCTION) { + const key = columnKey(field); + // Prefer a row that was already visible, so a hidden twin keeps its own + // id and letter. + const match = + take(row => isMetricRow(row) && row.visible && columnKey(row.field) === key) ?? + take(row => isMetricRow(row) && columnKey(row.field) === key); + rows.push({ + id: match?.id ?? newRowId(), + kind: 'metric', + letter: match && isMetricRow(match) ? match.letter : '', + field, + visible: true, + }); + return; + } + + if (field.kind === FieldValueKind.EQUATION) { + const match = equationMatches.get(fieldIndex); + if (!match?.keepsLetters) { + for (const aggregate of getEquationAggregates(field.field)) { + const key = getAggregateKey(aggregate); + if (hasMetric(key)) { + continue; + } + const column = explodeFieldString(aggregate); + const hidden = take(row => isMetricRow(row) && columnKey(row.field) === key); + if (hidden && isMetricRow(hidden)) { + reusedHidden.push({ + index: previous.indexOf(hidden), + row: {...hidden, field: column, visible: false}, + }); + } else { + rows.push({ + id: newRowId(), + kind: 'metric', + letter: '', + field: column, + visible: false, + }); + } + } + } + const row: EquationRow = { + id: match?.row.id ?? newRowId(), + kind: 'equation', + expression: match?.row.expression ?? '', + alias: field.alias, + }; + rows.push(row); + pendingEquations.push({row, reused: !!match, text: field.field}); + } + }); + + reusedHidden.sort((a, b) => a.index - b.index); + for (const {index, row} of reusedHidden) { + rows.splice(Math.min(index, rows.length), 0, row); + } + + // New metric rows take the next free letter, in display order. + for (const row of rows) { + if (isMetricRow(row) && !row.letter) { + row.letter = getNextLetter(rows); + } + } + + // An equation keeps the letters it had when they still mean the same text; + // otherwise the letters are derived from the text. + const referenceMap = getReferenceMap(rows); + for (const {row, reused, text} of pendingEquations) { + if (!(reused && resolveExpression(row.expression, referenceMap) === text)) { + row.expression = unresolveExpression(text, referenceMap); + } + } + + return pruneOrphanHiddenRows(rows); +} + +/** + * Moves the row at `from` to `to`. Letters travel with their rows. + * @public used by the unified visualize list in a follow-up PR + */ +export function moveRow( + rows: TraceMetricRow[], + from: number, + to: number +): TraceMetricRow[] { + const next = [...rows]; + const [row] = next.splice(from, 1); + if (!row) { + return rows; + } + next.splice(to, 0, row); + return next; +} + +/** + * Hides a metric row from the chart or shows it again. The row stays put. + * @public used by the unified visualize list in a follow-up PR + */ +export function setRowVisible( + rows: TraceMetricRow[], + id: string, + visible: boolean +): TraceMetricRow[] { + return rows.map(row => (row.id === id && isMetricRow(row) ? {...row, visible} : row)); +} + +/** + * Removes a row. Hidden rows no equation names afterwards go with it. + * @public used by the unified visualize list in a follow-up PR + */ +export function deleteRow(rows: TraceMetricRow[], id: string): TraceMetricRow[] { + return pruneOrphanHiddenRows(rows.filter(row => row.id !== id)); +} + +/** + * Replaces the column of a metric or column row. A row that changes kind, for + * example a table column becoming an aggregate, keeps its id and takes a + * letter when it becomes a metric row. + * @public used by the unified visualize list in a follow-up PR + */ +export function setRowField( + rows: TraceMetricRow[], + id: string, + field: Column +): TraceMetricRow[] { + return rows.map(row => { + if (row.id !== id || isEquationRow(row)) { + return row; + } + if (field.kind === FieldValueKind.FUNCTION) { + return isMetricRow(row) + ? {...row, field} + : {id: row.id, kind: 'metric', letter: getNextLetter(rows), field, visible: true}; + } + return {id: row.id, kind: 'column', field}; + }); +} + +/** + * Sets the legend alias of a row. + * @public used by the unified visualize list in a follow-up PR + */ +export function setRowAlias( + rows: TraceMetricRow[], + id: string, + alias: string | undefined +): TraceMetricRow[] { + return rows.map(row => { + if (row.id !== id) { + return row; + } + return isEquationRow(row) ? {...row, alias} : {...row, field: {...row.field, alias}}; + }); +} + +/** + * Sets an equation's letter-form expression. + * @public used by the unified visualize list in a follow-up PR + */ +export function setEquationExpression( + rows: TraceMetricRow[], + id: string, + expression: string +): TraceMetricRow[] { + return pruneOrphanHiddenRows( + rows.map(row => (row.id === id && isEquationRow(row) ? {...row, expression} : row)) + ); +} + +/** + * Appends a visible metric row with the next free letter. + * @public used by the unified visualize list in a follow-up PR + */ +export function addMetricRow(rows: TraceMetricRow[], field: Column): TraceMetricRow[] { + return [ + ...rows, + {id: newRowId(), kind: 'metric', letter: getNextLetter(rows), field, visible: true}, + ]; +} + +/** + * Appends an empty equation row. + * @public used by the unified visualize list in a follow-up PR + */ +export function addEquationRow(rows: TraceMetricRow[]): TraceMetricRow[] { + return [...rows, {id: newRowId(), kind: 'equation', expression: ''}]; +}