Skip to content

Commit

Permalink
Move all state loading decision to DashboardURLStateSync
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaHegde committed Dec 12, 2024
1 parent d88e4ba commit 6b526ae
Show file tree
Hide file tree
Showing 21 changed files with 314 additions and 385 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import { fetchMagicAuthToken } from "@rilldata/web-admin/features/projects/selectors";
import { getDashboardStateFromUrl } from "@rilldata/web-common/features/dashboards/proto-state/fromProto";
import { getUpdatedUrlForExploreState } from "@rilldata/web-common/features/dashboards/url-state/getUpdatedUrlForExploreState";
import type { MetricsExplorerEntity } from "@rilldata/web-common/features/dashboards/stores/metrics-explorer-entity";
import { fetchExploreSpec } from "@rilldata/web-common/features/explores/selectors";
import { error } from "@sveltejs/kit";

export const load = async ({
params: { token, organization, project },
parent,
}) => {
export const load = async ({ params: { token }, parent }) => {
const { runtime } = await parent();

try {
Expand All @@ -23,41 +20,28 @@ export const load = async ({
explore,
metricsView,
defaultExplorePreset,
initExploreState,
initLoadedOutsideOfURL,
} = await fetchExploreSpec(
runtime?.instanceId,
exploreName,
`__${organization}__${project}`,
);
exploreStateFromYAMLConfig,
} = await fetchExploreSpec(runtime?.instanceId, exploreName);
const metricsViewSpec = metricsView.metricsView?.state?.validSpec ?? {};
const exploreSpec = explore.explore?.state?.validSpec ?? {};

let initExploreState: Partial<MetricsExplorerEntity> | undefined =
undefined;
if (tokenData.token?.state) {
const exploreStateFromToken = getDashboardStateFromUrl(
initExploreState = getDashboardStateFromUrl(
tokenData.token?.state,
metricsViewSpec,
exploreSpec,
{}, // TODO
);
Object.assign(initExploreState, exploreStateFromToken);
}
const initUrlSearch =
initLoadedOutsideOfURL || !!tokenData.token?.state
? getUpdatedUrlForExploreState(
exploreSpec,
defaultExplorePreset,
initExploreState,
new URLSearchParams(),
)
: "";

return {
explore,
metricsView,
defaultExplorePreset,
exploreStateFromYAMLConfig,
initExploreState,
initUrlSearch,
token: tokenData?.token,
};
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
$: ({
defaultExplorePreset,
initExploreState,
initUrlSearch,
partialExploreState,
urlSearchForPartial,
exploreStateFromYAMLConfig,
partialExploreStateFromUrl,
exploreStateFromSessionStorage,
token: { resourceName },
} = data);
$: ({ organization, project } = $page.params);
Expand Down Expand Up @@ -63,9 +63,9 @@
exploreName={resourceName}
{defaultExplorePreset}
{initExploreState}
{initUrlSearch}
{partialExploreState}
{urlSearchForPartial}
{exploreStateFromYAMLConfig}
{partialExploreStateFromUrl}
{exploreStateFromSessionStorage}
>
<DashboardThemeProvider>
<Dashboard
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { MetricsExplorerEntity } from "@rilldata/web-common/features/dashboards/stores/metrics-explorer-entity";
import { convertURLToExploreState } from "@rilldata/web-common/features/dashboards/url-state/convertPresetToExploreState";
import { getExploreStores } from "@rilldata/web-common/features/explores/selectors";

export const load = async ({ url, parent, params }) => {
const { explore, metricsView, defaultExplorePreset, token } = await parent();
Expand All @@ -8,31 +7,12 @@ export const load = async ({ url, parent, params }) => {
const metricsViewSpec = metricsView.metricsView?.state?.validSpec;
const exploreSpec = explore.explore?.state?.validSpec;

// Get Explore state from URL params
let partialExploreState: Partial<MetricsExplorerEntity> = {};
let urlSearchForPartial = "";
const errors: Error[] = [];
if (metricsViewSpec && exploreSpec) {
const {
partialExploreState: partialExploreStateFromUrl,
urlSearchForPartial: _urlSearchForPartial,
errors: errorsFromConvert,
} = convertURLToExploreState(
exploreName,
`__${organization}__${project}`,
url.searchParams,
metricsViewSpec,
exploreSpec,
defaultExplorePreset,
);
partialExploreState = partialExploreStateFromUrl;
errors.push(...errorsFromConvert);
urlSearchForPartial = _urlSearchForPartial;
}

return {
partialExploreState,
urlSearchForPartial,
errors,
};
return getExploreStores(
exploreName,
`${organization}__${project}__`,
url.searchParams,
metricsViewSpec,
exploreSpec,
defaultExplorePreset,
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
} from "@rilldata/web-admin/features/bookmarks/selectors";
import { getDashboardStateFromUrl } from "@rilldata/web-common/features/dashboards/proto-state/fromProto";
import type { MetricsExplorerEntity } from "@rilldata/web-common/features/dashboards/stores/metrics-explorer-entity";
import { getUpdatedUrlForExploreState } from "@rilldata/web-common/features/dashboards/url-state/getUpdatedUrlForExploreState";
import { fetchExploreSpec } from "@rilldata/web-common/features/explores/selectors";
import {
type V1ExplorePreset,
Expand All @@ -14,36 +13,30 @@ import {
export const load = async ({ params, depends, parent }) => {
const { project, runtime } = await parent();

const { organization, project: projectName, dashboard: exploreName } = params;
const { dashboard: exploreName } = params;

depends(exploreName, "explore");

let explore: V1Resource | undefined;
let metricsView: V1Resource | undefined;
let defaultExplorePreset: V1ExplorePreset | undefined;
let initExploreState: Partial<MetricsExplorerEntity> = {};
let initLoadedOutsideOfURL = false;
let exploreStateFromYAMLConfig: Partial<MetricsExplorerEntity> = {};
let initExploreState: Partial<MetricsExplorerEntity> | undefined = undefined;
try {
({
explore,
metricsView,
defaultExplorePreset,
initExploreState,
initLoadedOutsideOfURL,
} = await fetchExploreSpec(
runtime?.instanceId,
exploreName,
`__${organization}__${projectName}`,
));
exploreStateFromYAMLConfig,
} = await fetchExploreSpec(runtime?.instanceId, exploreName));
} catch {
// error handled in +page.svelte for now
// TODO: move it here
return {
explore: <V1Resource>{},
metricsView: <V1Resource>{},
defaultExplorePreset: <V1ExplorePreset>{},
initExploreState: {},
initLoadedOutsideOfURL: false,
exploreStateFromYAMLConfig,
};
}

Expand All @@ -55,32 +48,22 @@ export const load = async ({ params, depends, parent }) => {
const homeBookmark = bookmarks.find(isHomeBookmark);

if (homeBookmark) {
const exploreStateFromBookmark = getDashboardStateFromUrl(
initExploreState = getDashboardStateFromUrl(
homeBookmark.data ?? "",
metricsViewSpec,
exploreSpec,
{}, // TODO
);
Object.assign(initExploreState, exploreStateFromBookmark);
initLoadedOutsideOfURL = true;
}
} catch {
// TODO
}
const initUrlSearch = initLoadedOutsideOfURL
? getUpdatedUrlForExploreState(
exploreSpec,
defaultExplorePreset,
initExploreState,
new URLSearchParams(),
)
: "";

return {
explore,
metricsView,
defaultExplorePreset,
exploreStateFromYAMLConfig,
initExploreState,
initUrlSearch,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
$: ({
defaultExplorePreset,
initExploreState,
initUrlSearch,
partialExploreState,
urlSearchForPartial,
exploreStateFromYAMLConfig,
partialExploreStateFromUrl,
exploreStateFromSessionStorage,
errors,
exploreName,
} = data);
Expand Down Expand Up @@ -107,9 +107,9 @@
{exploreName}
{defaultExplorePreset}
{initExploreState}
{initUrlSearch}
{partialExploreState}
{urlSearchForPartial}
{exploreStateFromYAMLConfig}
{partialExploreStateFromUrl}
{exploreStateFromSessionStorage}
>
<DashboardThemeProvider>
<Dashboard {metricsViewName} {exploreName} />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import { convertURLToExploreState } from "@rilldata/web-common/features/dashboards/url-state/convertPresetToExploreState";
import { getExploreStores } from "@rilldata/web-common/features/explores/selectors";

export const load = async ({ url, parent, params }) => {
const { explore, metricsView, defaultExplorePreset } = await parent();
const { organization, project, dashboard: exploreName } = params;
const metricsViewSpec = metricsView.metricsView?.state?.validSpec;
const exploreSpec = explore.explore?.state?.validSpec;

const { partialExploreState, urlSearchForPartial, errors } =
convertURLToExploreState(
return {
exploreName,
...getExploreStores(
exploreName,
`__${organization}__${project}`,
`${organization}__${project}__`,
url.searchParams,
metricsViewSpec,
exploreSpec,
defaultExplorePreset,
);

return {
partialExploreState,
urlSearchForPartial,
errors,
exploreName,
),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
$: copyValue = measureValueFormatterUnabridged(value) ?? "no data";
$: tooltipValue = measureValueFormatterTooltip(value) ?? "no data";
$: ttdHref = `?${ExploreStateURLParams.WebView}=ttd&${ExploreStateURLParams.ExpandedMeasure}=${measure.name}`;
$: tddHref = `?${ExploreStateURLParams.WebView}=tdd&${ExploreStateURLParams.ExpandedMeasure}=${measure.name}`;
function shiftClickHandler(number: string | undefined) {
if (number === undefined) return;
Expand Down Expand Up @@ -128,7 +128,7 @@
}, 1000);
},
})}
href={ttdHref}
href={tddHref}
>
<h2
class="line-clamp-2 ui-header-primary font-semibold whitespace-normal"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,13 @@ import {
type MetricsExplorerEntity,
} from "@rilldata/web-common/features/dashboards/stores/metrics-explorer-entity";
import { getPersistentDashboardStateForKey } from "@rilldata/web-common/features/dashboards/stores/persistent-dashboard-state";
import { convertPresetToExploreState } from "@rilldata/web-common/features/dashboards/url-state/convertPresetToExploreState";
import { getDefaultExplorePreset } from "@rilldata/web-common/features/dashboards/url-state/getDefaultExplorePreset";
import type {
V1ExploreSpec,
V1MetricsViewSpec,
V1MetricsViewTimeRangeResponse,
} from "@rilldata/web-common/runtime-client";
import type { V1ExploreSpec } from "@rilldata/web-common/runtime-client";

// TODO: Remove this in favour of just `getBasePreset`
export function getDefaultExploreState(
name: string,
metricsView: V1MetricsViewSpec,
explore: V1ExploreSpec,
fullTimeRange: V1MetricsViewTimeRangeResponse | undefined,
defaultExplorePreset = getDefaultExplorePreset(explore, fullTimeRange),
initState: Partial<MetricsExplorerEntity>,
): MetricsExplorerEntity {
const { partialExploreState } = convertPresetToExploreState(
metricsView,
explore,
defaultExplorePreset,
);
return {
// fields filled here are the ones that are not stored and loaded to/from URL
name,
Expand All @@ -36,7 +22,7 @@ export function getDefaultExploreState(

lastDefinedScrubRange: undefined,

...partialExploreState,
...initState,
} as MetricsExplorerEntity;
}

Expand Down
27 changes: 15 additions & 12 deletions web-common/src/features/dashboards/stores/dashboard-stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { getDashboardStateFromUrl } from "@rilldata/web-common/features/dashboar
import { getProtoFromDashboardState } from "@rilldata/web-common/features/dashboards/proto-state/toProto";
import { getWhereFilterExpressionIndex } from "@rilldata/web-common/features/dashboards/state-managers/selectors/dimension-filters";
import { AdvancedMeasureCorrector } from "@rilldata/web-common/features/dashboards/stores/AdvancedMeasureCorrector";
import { getDefaultExploreState } from "@rilldata/web-common/features/dashboards/stores/dashboard-store-defaults";
import {
createAndExpression,
filterExpressions,
forEachIdentifier,
} from "@rilldata/web-common/features/dashboards/stores/filter-utils";
import type { MetricsExplorerEntity } from "@rilldata/web-common/features/dashboards/stores/metrics-explorer-entity";
import {
contextColWidthDefaults,
type MetricsExplorerEntity,
} from "@rilldata/web-common/features/dashboards/stores/metrics-explorer-entity";
import { TDDChart } from "@rilldata/web-common/features/dashboards/time-dimension-details/types";
import type {
DashboardTimeControls,
Expand All @@ -21,7 +23,6 @@ import type {
V1ExploreSpec,
V1Expression,
V1MetricsViewSpec,
V1MetricsViewTimeRangeResponse,
V1TimeGrain,
} from "@rilldata/web-common/runtime-client";
import {
Expand Down Expand Up @@ -166,18 +167,20 @@ function syncDimensions(
}

const metricsViewReducers = {
init(
name: string,
metricsView: V1MetricsViewSpec,
explore: V1ExploreSpec,
fullTimeRange: V1MetricsViewTimeRangeResponse | undefined,
initState: Partial<MetricsExplorerEntity> = {},
) {
init(name: string, initState: Partial<MetricsExplorerEntity> = {}) {
update((state) => {
if (state.entities[name]) return state;

state.entities[name] = {
...getDefaultExploreState(name, metricsView, explore, fullTimeRange),
state.entities[name] = <MetricsExplorerEntity>{
// fields filled here are the ones that are not stored and loaded to/from URL
name,
dimensionFilterExcludeMode: new Map(),
leaderboardContextColumn: LeaderboardContextColumn.HIDDEN,

temporaryFilterName: null,
contextColumnWidths: { ...contextColWidthDefaults },

lastDefinedScrubRange: undefined,
...initState,
};

Expand Down
Loading

0 comments on commit 6b526ae

Please sign in to comment.