Skip to content
Draft
Show file tree
Hide file tree
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
1,690 changes: 857 additions & 833 deletions proto/gen/rill/runtime/v1/resources.pb.go

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions proto/gen/rill/runtime/v1/runtime.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5342,6 +5342,18 @@ definitions:
items:
type: string
description: Temporary to differentiate between "select" and "in list" modes. Expression will be replaced with UI specific state in the future.
pinnedFilters:
type: array
items:
type: string
title: Array of dimension or measure names
requiredFilters:
type: array
items:
type: string
description: |-
Array of dimension or measure names that must have a value before the explore can render.
Required filters are implicitly pinned.
timeRange:
type: string
description: |-
Expand Down
5 changes: 5 additions & 0 deletions proto/rill/runtime/v1/resources.proto
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,11 @@ message ExplorePreset {
optional Expression where = 11;
// Temporary to differentiate between "select" and "in list" modes. Expression will be replaced with UI specific state in the future.
repeated string dimensions_with_inlist_filter = 29;
// Array of dimension or measure names
repeated string pinned_filters = 39;
// Array of dimension or measure names that must have a value before the explore can render.
// Required filters are implicitly pinned.
repeated string required_filters = 40;

// Time range for the explore.
// It corresponds to the `range` property of the explore's `time_ranges`.
Expand Down
31 changes: 30 additions & 1 deletion runtime/parser/parse_explore.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import (
"time"

runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime/metricsview"
"github.com/rilldata/rill/runtime/metricsview/metricssql"
"github.com/rilldata/rill/runtime/pkg/rilltime"
"golang.org/x/exp/maps"
"gopkg.in/yaml.v3"
)

type ExploreYAML struct {
commonYAML `yaml:",inline"` // Not accessed here, only setting it so we can use KnownFields for YAML parsing
commonYAML `yaml:",inline"` // Not accessed here, only setting it so we can use KnownFields for YAML parsing
DisplayName string `yaml:"display_name"`
Title string `yaml:"title"` // Deprecated: use display_name
Description string `yaml:"description"`
Expand All @@ -32,6 +34,9 @@ type ExploreYAML struct {
TimeRange string `yaml:"time_range"`
ComparisonMode string `yaml:"comparison_mode"`
ComparisonDimension string `yaml:"comparison_dimension"`
Filter string `yaml:"filter"`
Pinned []string `yaml:"pinned"`
Required []string `yaml:"required"`
} `yaml:"defaults"`
Embeds struct {
HidePivot bool `yaml:"hide_pivot"`
Expand Down Expand Up @@ -246,6 +251,27 @@ func (p *Parser) parseExplore(node *Node) error {
if tmp.Defaults.ComparisonDimension != "" {
compareDim = &tmp.Defaults.ComparisonDimension
}

var filter *runtimev1.Expression
if tmp.Defaults.Filter != "" {
expr, err := metricssql.ParseFilter(tmp.Defaults.Filter)
if err != nil {
return fmt.Errorf("invalid filter expression: %q: %w", tmp.Defaults.Filter, err)
}

filter = metricsview.ExpressionToProto(expr)
}

var pinnedMeasuresOrDimensions []string
if len(tmp.Defaults.Pinned) > 0 {
pinnedMeasuresOrDimensions = tmp.Defaults.Pinned
}

var requiredFilters []string
if len(tmp.Defaults.Required) > 0 {
requiredFilters = tmp.Defaults.Required
}

defaultPreset = &runtimev1.ExplorePreset{
Dimensions: presetDimensions,
DimensionsSelector: presetDimensionsSelector,
Expand All @@ -254,6 +280,9 @@ func (p *Parser) parseExplore(node *Node) error {
TimeRange: tr,
ComparisonMode: mode,
ComparisonDimension: compareDim,
Where: filter,
PinnedFilters: pinnedMeasuresOrDimensions,
RequiredFilters: requiredFilters,
}
}

Expand Down
3 changes: 2 additions & 1 deletion web-common/src/features/canvas/stores/canvas-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
isChartComponentType,
isTableComponentType,
} from "../components/util";
import { FilterManager, flattenExpression } from "./filter-manager";
import { FilterManager } from "./filter-manager";
import { getFilterParam } from "./filter-state";
import { Grid } from "./grid";
import { TabGroup, type LayoutBlock } from "./tab-group";
Expand All @@ -50,6 +50,7 @@ import { DEFAULT_DASHBOARD_WIDTH, namePrefixFromPath } from "../layout-util";
import { createCustomMapStore } from "@rilldata/web-common/lib/custom-map-store";
import type { RuntimeClient } from "@rilldata/web-common/runtime-client/v2";
import { queryServiceConvertExpressionToMetricsSQL } from "@rilldata/web-common/runtime-client";
import { flattenExpression } from "@rilldata/web-common/features/dashboards/stores/filter-utils.ts";

export const lastVisitedState = new Map<string, string>();

Expand Down
67 changes: 3 additions & 64 deletions web-common/src/features/canvas/stores/filter-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,16 @@ import type {
V1Expression,
V1MetricsView,
} from "@rilldata/web-common/runtime-client";
import {
V1Operation,
type MetricsViewSpecMeasure,
} from "@rilldata/web-common/runtime-client";
import { type MetricsViewSpecMeasure } from "@rilldata/web-common/runtime-client";
import {
derived,
get,
writable,
type Readable,
writable,
type Writable,
} from "svelte/store";
import { getDimensionDisplayName } from "../../dashboards/filters/getDisplayName";
import {
createAndExpression,
flattenInExpressionValues,
} from "../../dashboards/stores/filter-utils";
import { flattenExpression } from "../../dashboards/stores/filter-utils";
import { ExploreStateURLParams } from "../../dashboards/url-state/url-params";
import type { ParsedFilters } from "./filter-state";
import { FilterState } from "./filter-state";
Expand Down Expand Up @@ -952,61 +946,6 @@ function isMeasureItemEmpty(item: MeasureFilterItem): boolean {
return !item.filter;
}

// This should be deprecated eventually in favor of better support for variously formatted expressions
export function flattenExpression(
expression: V1Expression | undefined,
): V1Expression {
if (!expression) {
return createAndExpression([]);
}

let root: V1Expression;

// Ensure top level is an OPERATION_AND
if (!expression.cond || expression.cond.op !== V1Operation.OPERATION_AND) {
root = createAndExpression([expression]);
} else {
root = expression;
}

const rootCond = root.cond;
if (
!rootCond ||
rootCond.op !== V1Operation.OPERATION_AND ||
!Array.isArray(rootCond.exprs)
) {
return root;
}

// Recursively flatten all nested ANDs, preserving order
rootCond.exprs = flattenAndExprs(rootCond.exprs);

// Normalize array-valued IN/NIN expressions into individual value expressions
rootCond.exprs = rootCond.exprs.map(flattenInExpressionValues);

return root;
}

function flattenAndExprs(exprs: V1Expression[]): V1Expression[] {
const result: V1Expression[] = [];

for (const expr of exprs) {
const cond = expr.cond;
if (
cond &&
cond.op === V1Operation.OPERATION_AND &&
Array.isArray(cond.exprs)
) {
// Inline children in order
result.push(...flattenAndExprs(cond.exprs));
} else {
result.push(expr);
}
}

return result;
}

// wip - bgh
function mergeFilters<T>(
metricsViewItems: Map<string, Map<string, T>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// But we need resolved start and end based on current time in dimension filters to get query for accurate results.
export let queryTimeStart: string | undefined = undefined;
export let queryTimeEnd: string | undefined = undefined;
export let pinnedFilters: Set<string> = new Set();

const runtimeClient = useRuntimeClient();

Expand All @@ -44,4 +45,5 @@
{displayTimeRange}
{queryTimeStart}
{queryTimeEnd}
{pinnedFilters}
/>
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The main feature-set component for dashboard filters
export let queryTimeEnd: string | undefined = undefined;
export let hasBoldTimeRange: boolean = true;
export let chipLayout: "wrap" | "scroll" = "wrap";
export let pinnedFilters: Set<string> = new Set();

let scrollContainer: HTMLDivElement;

Expand All @@ -44,6 +45,7 @@ The main feature-set component for dashboard filters
filters,
dimensionsWithInlistFilter,
metricsViewNames[0],
pinnedFilters,
);

$: measureIdMap = getMapFromArray(
Expand All @@ -53,6 +55,7 @@ The main feature-set component for dashboard filters
$: measureFilters = getMeasureFilters(
measureIdMap,
dimensionThresholdFilters,
pinnedFilters,
);

function handleWheel(event: WheelEvent) {
Expand Down
20 changes: 19 additions & 1 deletion web-common/src/features/dashboards/filters/Filters.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@
import { getPinnedTimeZones } from "../url-state/getDefaultExplorePreset";
import { useRuntimeClient } from "@rilldata/web-common/runtime-client/v2";
import { m } from "@rilldata/web-common/lib/i18n/gen/messages";
import { type YAMLOnlyExploreState } from "@rilldata/web-common/features/dashboards/stores/yaml-only-explore-state.svelte.ts";

const { rillTime } = featureFlags;

export let readOnly = false;
export let timeRanges: V1ExploreTimeRange[];
export let metricsViewName: string;
export let hasTimeSeries: boolean;
export let yamlOnlyState: YAMLOnlyExploreState;

/** the height of a row of chips */
const ROW_HEIGHT = "26px";
Expand Down Expand Up @@ -152,16 +154,21 @@
$: measures = $allMeasures;
$: measureIdMap = getMapFromArray(measures, (m) => m.name as string);

$: currentDimensionFilters = $getDimensionFilterItems(dimensionIdMap);
$: currentDimensionFilters = $getDimensionFilterItems(
dimensionIdMap,
new Set(yamlOnlyState.pinnedFilters.value),
);
$: allDimensionFilters = $getAllDimensionFilterItems(
currentDimensionFilters,
dimensionIdMap,
new Set(yamlOnlyState.pinnedFilters.value),
);

$: currentMeasureFilters = $getMeasureFilterItems(measureIdMap);
$: allMeasureFilters = $getAllMeasureFilterItems(
currentMeasureFilters,
measureIdMap,
new Set(yamlOnlyState.pinnedFilters.value),
);

// hasFilter only checks for complete filters and excludes temporary ones
Expand Down Expand Up @@ -429,6 +436,13 @@
const url = dashboardStateSync.getUrlForExploreState(exploreState);
return isUrlTooLong(url);
}

$: toggleFilterPin = yamlOnlyState?.editable
? (name: string) => yamlOnlyState.pinnedFilters.toggle(name)
: undefined;
$: toggleFilterRequired = yamlOnlyState?.editable
? (name: string) => yamlOnlyState.requiredFilters.toggle(name)
: undefined;
</script>

<div class="flex flex-col gap-y-2 size-full">
Expand Down Expand Up @@ -568,6 +582,8 @@
applyDimensionInListMode(name, values)}
applyDimensionContainsMode={async (name, searchText) =>
applyDimensionContainsMode(name, searchText)}
{toggleFilterPin}
{toggleFilterRequired}
isUrlTooLongAfterInListFilter={(values) =>
isUrlTooLongAfterInListFilter(filterData.name, values)}
/>
Expand All @@ -578,6 +594,8 @@
<MeasureFilter
{filterData}
allDimensions={dimensions}
{toggleFilterPin}
{toggleFilterRequired}
onRemove={() =>
removeMeasureFilter(filterData.dimensionName, filterData.name)}
onApply={({ dimension, oldDimension, filter }) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@ export const filterActions = {
* Clears all filters and resets related fields
*/
clearAllFilters,

setTemporaryFilterName,
};
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,16 @@ export type DimensionFilterItem = {
export function getDimensionFilterItems(
dashData: AtLeast<DashboardDataSources, "dashboard">,
) {
return (dimensionIdMap: Map<string, MetricsViewSpecDimension>) => {
return (
dimensionIdMap: Map<string, MetricsViewSpecDimension>,
pinnedFilters: Set<string> = new Set(),
) => {
return getDimensionFilters(
dimensionIdMap,
dashData.dashboard.whereFilter,
dashData.dashboard.dimensionsWithInlistFilter,
dashData.validExplore?.metricsView,
pinnedFilters,
);
};
}
Expand All @@ -162,6 +166,7 @@ export function getDimensionFiltersMap(
filter: V1Expression | undefined,
dimensionsWithInlistFilter: string[],
metricsViewName: string | undefined,
pinnedFilters: Set<string>,
): Map<string, DimensionFilterItem> {
if (!filter || !metricsViewName) return new Map();
const filteredDimensions: Map<string, DimensionFilterItem> = new Map();
Expand All @@ -186,7 +191,7 @@ export function getDimensionFiltersMap(
selectedValues: getValuesInExpression(e),
isInclude: e.cond?.op === V1Operation.OPERATION_IN,
inputText: undefined,
pinned: false,
pinned: pinnedFilters?.has(ident),

dimensions: new Map<string, MetricsViewSpecDimension>([
[metricsViewName, dim],
Expand All @@ -206,7 +211,7 @@ export function getDimensionFiltersMap(
dimensions: new Map<string, MetricsViewSpecDimension>([
[metricsViewName, dim],
]),
pinned: false,
pinned: pinnedFilters?.has(ident),
});
}
});
Expand All @@ -219,13 +224,15 @@ export function getDimensionFilters(
filter: V1Expression | undefined,
dimensionsWithInlistFilter: string[],
metricsViewName: string | undefined,
pinnedFilters: Set<string>,
) {
return Array.from(
getDimensionFiltersMap(
dimensionIdMap,
filter,
dimensionsWithInlistFilter,
metricsViewName,
pinnedFilters,
).values(),
);
}
Expand All @@ -236,6 +243,7 @@ export const getAllDimensionFilterItems = (
return (
dimensionFilterItem: DimensionFilterItem[],
dimensionIdMap: Map<string, MetricsViewSpecDimension>,
pinnedFilters: Set<string> = new Set(),
) => {
const allDimensionFilterItem = [...dimensionFilterItem];

Expand All @@ -259,7 +267,7 @@ export const getAllDimensionFilterItems = (
dimensionIdMap.get(dashData.dashboard.temporaryFilterName)!,
],
]),
pinned: false,
pinned: pinnedFilters.has(dashData.dashboard.temporaryFilterName),
});
}

Expand Down
Loading