Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import {
getValuesInExpression,
createInExpression,
} from "@rilldata/web-common/features/dashboards/stores/filter-utils";
import type { V1Expression } from "@rilldata/web-common/runtime-client";
import type {
V1Expression,
V1MetricsViewSpec,
} from "@rilldata/web-common/runtime-client";
import { get, writable, type Readable } from "svelte/store";
import {
afterAll,
Expand All @@ -24,6 +27,7 @@ import {
dimKeyFromRow,
} from "../../../dashboards/pivot/pivot-click-selection";
import { createPivotClickToFilter } from "./pivot-click-to-filter";
import { DimensionFilterManager } from "@rilldata/web-common/features/dashboards/filters/dimension-filters/DimensionFilterManager.svelte.ts";
import { ExpressionFilterManager } from "@rilldata/web-common/features/dashboards/filters/ExpressionFilterManager.svelte.ts";
import type { MetricsViewsProvider } from "@rilldata/web-common/features/metrics-views/providers/MetricsViewsProvider.svelte.ts";
import { YAMLConfigProvider } from "@rilldata/web-common/features/dashboards/providers/YAMLConfigProvider.svelte.ts";
Expand All @@ -32,13 +36,31 @@ import {
createTestMetricsViewsProvider,
useMetricsViewMocks,
} from "@rilldata/web-common/features/metrics-views/providers/test/metrics-views-test-utils.svelte.ts";
import { PIVOT_METRICS_INIT, PIVOT_TEST_METRICS_NAME } from "./pivot-test-data";
import {
PIVOT_COUNTRY_DIMENSION,
PIVOT_METRICS_INIT,
PIVOT_TEST_METRICS_NAME,
PIVOT_TOTAL_MEASURE,
} from "./pivot-test-data";

// ---------------------------------------------------------------------------
// Shared test helpers
// ---------------------------------------------------------------------------

useMetricsViewMocks({ [PIVOT_TEST_METRICS_NAME]: PIVOT_METRICS_INIT });
// Second metrics view declaring the same dimensions, mirroring a canvas with
// two metrics views such as `duckdb_commits_metrics` and `rill_commits_metrics`
// in https://github.com/rilldata/rill/issues/9845.
const PIVOT_MIRROR_METRICS_NAME = "mv2";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: move these alongside PIVOT_METRICS_INIT in pivot-test-data.

const PIVOT_METRICS_MIRROR_INIT: V1MetricsViewSpec = {
...PIVOT_METRICS_INIT,
displayName: "PivotTest Mirror",
table: "PivotTest_Mirror_Source",
};

useMetricsViewMocks({
[PIVOT_TEST_METRICS_NAME]: PIVOT_METRICS_INIT,
[PIVOT_MIRROR_METRICS_NAME]: PIVOT_METRICS_MIRROR_INIT,
});

// The specs are read-only, so a single provider serves every test in this file.
// Each test still gets its own ExpressionFilterManager.
Expand Down Expand Up @@ -1341,3 +1363,96 @@ describe("header/cell mutual exclusivity", () => {
result.destroy();
});
});

// ---------------------------------------------------------------------------
// Canvas filter scope parity (https://github.com/rilldata/rill/issues/9845)
//
// The filter bar and a table click must write the same URL params: one chip
// for the dimension, fanned out to every metrics view that declares it.
// ---------------------------------------------------------------------------

describe("canvas filter scope parity", () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets not add a tests specifically for the issue.

  1. You can just add PIVOT_MIRROR_METRICS_NAME to the shared manager in the top level beforeAll.
  2. Add assertions in table variant tests, flat table: single-cell-per-row, nested table: multi-select and nested table: cross-parent selection isolation.
  3. Add a test in each where filter bar changes update pivot (maybe out of scope if this doesn't work)

let twoMvProvider: MetricsViewsProvider;
let destroyTwoMvProvider: () => void;

beforeAll(async () => {
const provider = await createTestMetricsViewsProvider([
PIVOT_TEST_METRICS_NAME,
PIVOT_MIRROR_METRICS_NAME,
]);
twoMvProvider = provider.value;
destroyTwoMvProvider = provider.destroy;
});

afterAll(() => destroyTwoMvProvider());

/** A real filter manager over both metrics views, torn down after the test. */
function createTwoMvFilterManager() {
const { value, destroy } = createInEffectRoot(() => {
return new ExpressionFilterManager(
twoMvProvider,
new YAMLConfigProvider(),
);
});
filterManagerCleanups.push(destroy);
return value;
}

/** Clicks the `country` cell of a one-row pivot and returns the URL params. */
function clickCountryCell() {
const filterManager = createTwoMvFilterManager();
const config = makeConfig({
rowDimensionNames: [PIVOT_COUNTRY_DIMENSION],
measureNames: [PIVOT_TOTAL_MEASURE],
isFlat: true,
});
const data: PivotDataRow[] = [{ country: "US", total: 100 }];
const result = createPivotClickToFilter(
createFactoryArgs({
pivotConfig: writable(config) as Readable<PivotDataStoreConfig>,
pivotDataStore: stubPivotDataStore(data),
filterManager,
activeComponent: writable<string | null>("pivot-1"),
}),
);

result.handleCellClickToFilter(
"0",
PIVOT_COUNTRY_DIMENSION,
false,
data[0],
);

const searchParams = new URLSearchParams();
filterManager.applyFilterToParams(searchParams);
result.destroy();
return searchParams;
}

it("table click writes the filter for every metrics view declaring the dimension", () => {
const searchParams = clickCountryCell();

expect(searchParams.get(`f.${PIVOT_TEST_METRICS_NAME}`)).toBe(
`${PIVOT_COUNTRY_DIMENSION} IN ('US')`,
);
expect(searchParams.get(`f.${PIVOT_MIRROR_METRICS_NAME}`)).toBe(
`${PIVOT_COUNTRY_DIMENSION} IN ('US')`,
);
});

it("filter bar writes byte-identical params for the same selection", () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redundant to checking individual metrics views. Adding assertion in existing tests should be enough.

// The filter bar edits the shared manager directly: AddExpressionFilterButton
// calls addNewFilter, then the DimensionFilter chip applies the selection.
const filterManager = createTwoMvFilterManager();
filterManager.addNewFilter(PIVOT_COUNTRY_DIMENSION);
const dfm = filterManager.filterManagersMap[PIVOT_COUNTRY_DIMENSION];
expect(dfm).toBeInstanceOf(DimensionFilterManager);
(dfm as DimensionFilterManager).setSelectedValues(["US"], false);

const searchParams = new URLSearchParams();
filterManager.applyFilterToParams(searchParams);

expect(searchParams.toString()).toBe(clickCountryCell().toString());
expect(filterManager.sortedFilterManagers.dimensions).toHaveLength(1);
});
});
Loading