Skip to content

Commit

Permalink
Update token and emebd
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaHegde committed Dec 10, 2024
1 parent 1a46aca commit fbe200d
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
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 { fetchExploreSpec } from "@rilldata/web-common/features/explores/selectors";
import { error } from "@sveltejs/kit";

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

try {
Expand All @@ -12,16 +17,48 @@ export const load = async ({ params: { token }, parent }) => {
throw new Error("Token does not have an associated resource name");
}

const { explore, metricsView, defaultExplorePreset } =
await fetchExploreSpec(
runtime.instanceId as string,
tokenData.token.resourceName,
const exploreName = tokenData.token?.resourceName;

const {
explore,
metricsView,
defaultExplorePreset,
initExploreState,
initLoadedOutsideOfURL,
} = await fetchExploreSpec(
runtime?.instanceId,
exploreName,
`__${organization}__${project}`,
);
const metricsViewSpec = metricsView.metricsView?.state?.validSpec ?? {};
const exploreSpec = explore.explore?.state?.validSpec ?? {};

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

return {
explore,
metricsView,
defaultExplorePreset,
initExploreState,
initUrlSearch,
token: tokenData?.token,
};
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
$: ({
defaultExplorePreset,
initExploreState,
initUrlSearch,
partialExploreState,
loadedOutsideOfURL,
urlSearchForPartial,
token: { resourceName },
} = data);
$: ({ organization, project } = $page.params);
Expand Down Expand Up @@ -57,9 +59,13 @@
exploreName={resourceName}
>
<DashboardURLStateSync
metricsViewName={explore.metricsView.meta.name.name}
exploreName={resourceName}
{defaultExplorePreset}
{initExploreState}
{initUrlSearch}
{partialExploreState}
{loadedOutsideOfURL}
{urlSearchForPartial}
>
<DashboardThemeProvider>
<Dashboard
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,21 @@
import { getDashboardStateFromUrl } from "@rilldata/web-common/features/dashboards/proto-state/fromProto";
import { metricsExplorerStore } from "@rilldata/web-common/features/dashboards/stores/dashboard-stores";
import type { MetricsExplorerEntity } from "@rilldata/web-common/features/dashboards/stores/metrics-explorer-entity";
import { convertExploreStateToURLSearchParams } from "@rilldata/web-common/features/dashboards/url-state/convertExploreStateToURLSearchParams";
import { convertURLToExploreState } from "@rilldata/web-common/features/dashboards/url-state/convertPresetToExploreState";
import { redirect } from "@sveltejs/kit";
import { get } from "svelte/store";

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

// On the first dashboard load, if there are no URL params, append the token's state (in human-readable format) to the URL.
if (
token.state &&
![...url.searchParams.keys()].length &&
!(exploreName in get(metricsExplorerStore).entities)
) {
const exploreState = getDashboardStateFromUrl(
token.state,
metricsViewSpec,
exploreSpec,
{}, // TODO
);
const newUrl = new URL(url);
newUrl.search = convertExploreStateToURLSearchParams(
exploreState as MetricsExplorerEntity,
exploreSpec,
defaultExplorePreset,
);
throw redirect(307, `${newUrl.pathname}${newUrl.search}`);
}

// Get Explore state from URL params
let partialExploreState: Partial<MetricsExplorerEntity> = {};
let loadedOutsideOfURL = false;
let urlSearchForPartial = "";
const errors: Error[] = [];
if (metricsViewSpec && exploreSpec) {
const {
partialExploreState: partialExploreStateFromUrl,
loadedOutsideOfURL: partialLoadedOutsideOfURL,
urlSearchForPartial: _urlSearchForPartial,
errors: errorsFromConvert,
} = convertURLToExploreState(
exploreName,
Expand All @@ -53,12 +27,13 @@ export const load = async ({ url, parent, params }) => {
);
partialExploreState = partialExploreStateFromUrl;
errors.push(...errorsFromConvert);
loadedOutsideOfURL = partialLoadedOutsideOfURL;
urlSearchForPartial = _urlSearchForPartial;
}
console.log(partialExploreState, urlSearchForPartial);

return {
partialExploreState,
loadedOutsideOfURL,
urlSearchForPartial,
errors,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ export const load = async ({ params, depends, parent }) => {
let initExploreState: Partial<MetricsExplorerEntity> = {};
let initLoadedOutsideOfURL = false;
try {
const fetchedExploreSpecDetails = await fetchExploreSpec(
({
explore,
metricsView,
defaultExplorePreset,
initExploreState,
initLoadedOutsideOfURL,
} = await fetchExploreSpec(
runtime?.instanceId,
exploreName,
`__${organization}__${projectName}`,
);
explore = fetchedExploreSpecDetails.explore;
metricsView = fetchedExploreSpecDetails.metricsView;
defaultExplorePreset = fetchedExploreSpecDetails.defaultExplorePreset;
initExploreState = fetchedExploreSpecDetails.initExploreState;
initLoadedOutsideOfURL = fetchedExploreSpecDetails.initLoadedOutsideOfURL;
));
} catch {
// error handled in +page.svelte for now
// TODO: move it here
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { convertURLToExploreState } from "@rilldata/web-common/features/dashboards/url-state/convertPresetToExploreState";
import { getUpdatedUrlForExploreState } from "@rilldata/web-common/features/dashboards/url-state/getUpdatedUrlForExploreState";

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, loadedOutsideOfURL, errors } =
const { partialExploreState, urlSearchForPartial, errors } =
convertURLToExploreState(
exploreName,
`__${organization}__${project}`,
Expand All @@ -16,16 +15,8 @@ export const load = async ({ url, parent, params }) => {
exploreSpec,
defaultExplorePreset,
);
const urlSearchForPartial = loadedOutsideOfURL
? getUpdatedUrlForExploreState(
exploreSpec,
defaultExplorePreset,
partialExploreState,
url.searchParams,
)
: url.searchParams.toString();
console.log(partialExploreState, urlSearchForPartial);

console.log(exploreName, urlSearchForPartial);
return {
partialExploreState,
urlSearchForPartial,
Expand Down
4 changes: 3 additions & 1 deletion web-common/src/features/dashboards/proto-state/fromProto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,9 @@ export function getDashboardStateFromProto(
entity.dashboardSortType = dashboard.leaderboardSortType;
}

entity.pivot = fromPivotProto(dashboard, metricsView);
if (dashboard.pivotIsActive !== undefined) {
entity.pivot = fromPivotProto(dashboard, metricsView);
}

Object.assign(entity, fromActivePageProto(dashboard));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ export function createStateManagers({
query: {
queryClient,
enabled: !!validSpec?.data?.metricsView?.timeDimension,
staleTime: Infinity,
cacheTime: Infinity,
},
},
).subscribe(set),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
$: ({ error, data: timeRangeSummary } = $timeRangeSummaryStore);
$: timeRangeSummaryError = error as HTTPError;
$: console.log(exploreName, initUrlSearch, urlSearchForPartial);
let prevUrl = "";
function gotoNewState() {
if (!exploreSpec) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
import { page } from "$app/stores";
import { useMetricsViewTimeRange } from "@rilldata/web-common/features/dashboards/selectors";
import { getStateManagers } from "@rilldata/web-common/features/dashboards/state-managers/state-managers";
import { restorePersistedDashboardState } from "@rilldata/web-common/features/dashboards/stores/dashboard-store-defaults";
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 {
convertPresetToExploreState,
convertURLToExploreState,
} from "@rilldata/web-common/features/dashboards/url-state/convertPresetToExploreState";
import DashboardURLStateSync from "@rilldata/web-common/features/dashboards/url-state/DashboardURLStateSync.svelte";
import { getDefaultExplorePreset } from "@rilldata/web-common/features/dashboards/url-state/getDefaultExplorePreset";
import { getUpdatedUrlForExploreState } from "@rilldata/web-common/features/dashboards/url-state/getUpdatedUrlForExploreState";
import type { V1ExplorePreset } from "@rilldata/web-common/runtime-client";
import { runtime } from "@rilldata/web-common/runtime-client/runtime-store";
Expand All @@ -32,14 +37,42 @@
exploreSpec,
$metricsViewTimeRange.data,
);
let initExploreState: Partial<MetricsExplorerEntity> = {};
let initUrlSearch = "";
$: {
({ partialExploreState: initExploreState } = convertPresetToExploreState(
metricsViewSpec,
exploreSpec,
defaultExplorePreset,
));
let initLoadedOutsideOfURL = false;
const stateFromLocalStorage = restorePersistedDashboardState(
exploreSpec,
storeKeyPrefix + $exploreName,
);
if (stateFromLocalStorage) {
initLoadedOutsideOfURL = true;
Object.assign(initExploreState, stateFromLocalStorage);
}
initUrlSearch = initLoadedOutsideOfURL
? getUpdatedUrlForExploreState(
exploreSpec,
defaultExplorePreset,
initExploreState,
new URLSearchParams(),
)
: "";
}
let partialExploreState: Partial<MetricsExplorerEntity> = {};
let loaded = false;
let urlSearchForPartial = "";
function parseUrl(url: URL, defaultExplorePreset: V1ExplorePreset) {
// Get Explore state from URL params
const {
partialExploreState: partialExploreStateFromUrl,
loaded: loadedFromStorage,
urlSearchForPartial: _urlSearchForPartial,
} = convertURLToExploreState(
$exploreName,
storeKeyPrefix,
Expand All @@ -49,15 +82,23 @@
defaultExplorePreset,
);
partialExploreState = partialExploreStateFromUrl;
loaded = loadedFromStorage;
urlSearchForPartial = _urlSearchForPartial;
}
// only reactive to url and defaultExplorePreset
$: parseUrl($page.url, defaultExplorePreset);
</script>

{#if !$validSpecStore.isLoading && !$metricsViewTimeRange.isLoading}
<DashboardURLStateSync {defaultExplorePreset} {partialExploreState} {loaded}>
<DashboardURLStateSync
metricsViewName={$metricsViewName}
exploreName={$exploreName}
{initExploreState}
{defaultExplorePreset}
{initUrlSearch}
{partialExploreState}
{urlSearchForPartial}
>
<slot />
</DashboardURLStateSync>
{/if}
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ function shouldSetParam<T>(

// both preset and state value is non-truthy.
// EG: one is "" and another is undefined then we should not set param as empty string
if (!presetValue && !exploreStateValue) {
if ((!presetValue && !exploreStateValue) || exploreStateValue === undefined) {
return false;
}

Expand Down
Loading

0 comments on commit fbe200d

Please sign in to comment.