Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Disallow sort by hidden measure in leaderboard #6425

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
import DashboardVisibilityDropdown from "@rilldata/web-common/components/menu/shadcn/DashboardVisibilityDropdown.svelte";
import * as Select from "@rilldata/web-common/components/select";
import { LeaderboardContextColumn } from "@rilldata/web-common/features/dashboards/leaderboard-context-column";
import { getFilteredSimpleMeasures } from "@rilldata/web-common/features/dashboards/state-managers/selectors/measures";
import { metricsExplorerStore } from "web-common/src/features/dashboards/stores/dashboard-stores";
import { getStateManagers } from "../state-managers/state-managers";

export let exploreName: string;

const {
selectors: {
measures: {
filteredSimpleMeasures,
leaderboardMeasureName,
getMeasureByName,
},
measures: { leaderboardMeasureName, getMeasureByName, visibleMeasures },
dimensions: { visibleDimensions, allDimensions },
},
actions: {
Expand All @@ -26,7 +23,7 @@

let active = false;

$: measures = $filteredSimpleMeasures();
$: measures = getFilteredSimpleMeasures($visibleMeasures);

$: metricsExplorer = $metricsExplorerStore.entities[exploreName];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ export const toggleMeasureVisibility = (
const deleted = dashboard.visibleMeasureKeys.delete(measureName);
if (!deleted) {
dashboard.visibleMeasureKeys.add(measureName);
} else if (
dashboard.leaderboardMeasureName === measureName &&
dashboard.visibleMeasureKeys.size > 0
) {
dashboard.leaderboardMeasureName = dashboard.visibleMeasureKeys
.keys()
.next().value;
}
} else {
const allSelected =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ export const getIndependentMeasures = (
return [...measures];
};

export const getFilteredSimpleMeasures = (
measures: MetricsViewSpecMeasureV2[],
) => {
return (
measures?.filter(
(m) =>
!m.window &&
m.type !== MetricsViewSpecMeasureType.MEASURE_TYPE_TIME_COMPARISON,
) ?? []
);
};

export const measureSelectors = {
/**
* Get all measures in the dashboard.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export const AD_BIDS_PRESET: V1ExplorePreset = {
compareTimeRange: "rill-PP",
measures: [AD_BIDS_IMPRESSIONS_MEASURE],
dimensions: [AD_BIDS_PUBLISHER_DIMENSION],
exploreSortBy: AD_BIDS_BID_PRICE_MEASURE,
exploreSortBy: AD_BIDS_IMPRESSIONS_MEASURE,
exploreSortAsc: true,
exploreSortType: V1ExploreSortType.EXPLORE_SORT_TYPE_PERCENT,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,20 @@ export const AD_BIDS_SORT_DESC_BY_IMPRESSIONS: TestDashboardMutation = (
setLeaderboardMeasureName(mut, AD_BIDS_IMPRESSIONS_MEASURE);
setSortDescending(mut);
};
export const AD_BIDS_SORT_ASC_BY_IMPRESSIONS: TestDashboardMutation = (mut) => {
setLeaderboardMeasureName(mut, AD_BIDS_IMPRESSIONS_MEASURE);
setSortDescending(mut);
toggleSort(mut, mut.dashboard.dashboardSortType);
};
export const AD_BIDS_SORT_ASC_BY_BID_PRICE: TestDashboardMutation = (mut) => {
setLeaderboardMeasureName(mut, AD_BIDS_BID_PRICE_MEASURE);
setSortDescending(mut);
toggleSort(mut, mut.dashboard.dashboardSortType);
};
export const AD_BIDS_SORT_DESC_BY_BID_PRICE: TestDashboardMutation = (mut) => {
setLeaderboardMeasureName(mut, AD_BIDS_BID_PRICE_MEASURE);
setSortDescending(mut);
};
export const AD_BIDS_SORT_BY_VALUE: TestDashboardMutation = (mut) => {
toggleSort(mut, DashboardState_LeaderboardSortType.VALUE);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,16 @@ function fromExploreUrlParams(
if (searchParams.has(ExploreStateURLParams.SortBy)) {
const sortBy = searchParams.get(ExploreStateURLParams.SortBy) as string;
if (measures.has(sortBy)) {
preset.exploreSortBy = sortBy;
if (
(preset.measures && preset.measures.includes(sortBy)) ||
!preset.measures
) {
preset.exploreSortBy = sortBy;
} else {
errors.push(
getSingleFieldError("sort by measure", sortBy, "It is hidden."),
);
}
} else {
errors.push(getSingleFieldError("sort by measure", sortBy));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
export function getSingleFieldError(fieldLabel: string, field: string) {
return new Error(`Selected ${fieldLabel}: "${field}" is not valid.`);
export function getSingleFieldError(
fieldLabel: string,
field: string,
suffix?: string,
) {
return new Error(
`Selected ${fieldLabel}: "${field}" is not valid.${suffix ? " " + suffix : ""}`,
);
}

export function getMultiFieldError(fieldLabel: string, fields: string[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ import {
AD_BIDS_SET_PREVIOUS_PERIOD_COMPARE_TIME_RANGE_FILTER,
AD_BIDS_SET_PREVIOUS_WEEK_COMPARE_TIME_RANGE_FILTER,
AD_BIDS_SORT_ASC_BY_BID_PRICE,
AD_BIDS_SORT_ASC_BY_IMPRESSIONS,
AD_BIDS_SORT_BY_DELTA_ABS_VALUE,
AD_BIDS_SORT_BY_PERCENT_VALUE,
AD_BIDS_SORT_BY_VALUE,
AD_BIDS_SORT_DESC_BY_BID_PRICE,
AD_BIDS_SORT_DESC_BY_IMPRESSIONS,
AD_BIDS_SORT_PIVOT_BY_IMPRESSIONS_DESC,
AD_BIDS_SORT_PIVOT_BY_TIME_DAY_ASC,
Expand Down Expand Up @@ -198,7 +200,7 @@ const TestCases: {
{
title:
"Leaderboard configs with preset and leaderboard sort measure in state same as preset",
mutations: [AD_BIDS_SORT_BY_PERCENT_VALUE, AD_BIDS_SORT_ASC_BY_BID_PRICE],
mutations: [AD_BIDS_SORT_BY_PERCENT_VALUE, AD_BIDS_SORT_ASC_BY_IMPRESSIONS],
preset: AD_BIDS_PRESET,
expectedUrl: "http://localhost/",
},
Expand All @@ -207,11 +209,11 @@ const TestCases: {
"Leaderboard configs with preset and leaderboard sort measure in state different than preset",
mutations: [
AD_BIDS_SORT_BY_DELTA_ABS_VALUE,
AD_BIDS_SORT_DESC_BY_IMPRESSIONS,
AD_BIDS_SORT_DESC_BY_BID_PRICE,
],
preset: AD_BIDS_PRESET,
expectedUrl:
"http://localhost/?sort_by=impressions&sort_type=delta_abs&sort_dir=DESC",
"http://localhost/?sort_by=bid_price&sort_type=delta_abs&sort_dir=DESC",
},

{
Expand Down
Loading