From 677443cf4cfe004f3bed6210b0c4a504a5219533 Mon Sep 17 00:00:00 2001 From: Aditya Hegde Date: Mon, 16 Dec 2024 12:43:23 +0530 Subject: [PATCH] Avoid using global runtime in loader funciton --- web-admin/src/routes/+layout.ts | 8 +--- .../[project]/-/share/[token]/+layout.ts | 5 +-- .../[project]/explore/[dashboard]/+layout.ts | 2 +- web-common/src/features/explores/selectors.ts | 44 ++++++++++++++----- web-common/src/runtime-client/http-client.ts | 7 ++- .../routes/(viz)/explore/[name]/+layout.ts | 4 +- 6 files changed, 42 insertions(+), 28 deletions(-) diff --git a/web-admin/src/routes/+layout.ts b/web-admin/src/routes/+layout.ts index e692c242926..2a1b62e2b39 100644 --- a/web-admin/src/routes/+layout.ts +++ b/web-admin/src/routes/+layout.ts @@ -84,13 +84,7 @@ export const load = async ({ params, url, route }) => { runtime: runtimeData, } = await fetchProjectDeploymentDetails(organization, project, token); - await runtime.setRuntime( - queryClient, - fixLocalhostRuntimePort(runtimeData.host ?? ""), - runtimeData.instanceId, - runtimeData.jwt?.token, - runtimeData.jwt?.authContext, - ); + runtimeData.host = fixLocalhostRuntimePort(runtimeData.host ?? ""); return { organizationPermissions, diff --git a/web-admin/src/routes/[organization]/[project]/-/share/[token]/+layout.ts b/web-admin/src/routes/[organization]/[project]/-/share/[token]/+layout.ts index 86058fe4b0f..7079fd36e81 100644 --- a/web-admin/src/routes/[organization]/[project]/-/share/[token]/+layout.ts +++ b/web-admin/src/routes/[organization]/[project]/-/share/[token]/+layout.ts @@ -13,10 +13,7 @@ export const load = async ({ params: { token }, parent }) => { } const { explore, metricsView, defaultExplorePreset } = - await fetchExploreSpec( - runtime.instanceId as string, - tokenData.token.resourceName, - ); + await fetchExploreSpec(runtime, tokenData.token.resourceName); return { explore, diff --git a/web-admin/src/routes/[organization]/[project]/explore/[dashboard]/+layout.ts b/web-admin/src/routes/[organization]/[project]/explore/[dashboard]/+layout.ts index 914f498f0b8..efd73fd31c9 100644 --- a/web-admin/src/routes/[organization]/[project]/explore/[dashboard]/+layout.ts +++ b/web-admin/src/routes/[organization]/[project]/explore/[dashboard]/+layout.ts @@ -17,7 +17,7 @@ export const load = async ({ params, depends, parent }) => { try { const { explore, metricsView, defaultExplorePreset } = - await fetchExploreSpec(runtime?.instanceId, exploreName); + await fetchExploreSpec(runtime, exploreName); // used to merge home bookmark to url state const bookmarks = await fetchBookmarks(project.id, exploreName); diff --git a/web-common/src/features/explores/selectors.ts b/web-common/src/features/explores/selectors.ts index 3bd4f9333f1..326098cfc56 100644 --- a/web-common/src/features/explores/selectors.ts +++ b/web-common/src/features/explores/selectors.ts @@ -24,7 +24,11 @@ import { type V1ExplorePreset, V1ExploreWebView, } from "@rilldata/web-common/runtime-client"; -import type { ErrorType } from "@rilldata/web-common/runtime-client/http-client"; +import { + type ErrorType, + httpClient, +} from "@rilldata/web-common/runtime-client/http-client"; +import type { Runtime } from "@rilldata/web-common/runtime-client/runtime-store"; import { error, redirect } from "@sveltejs/kit"; export function useExplore( @@ -83,17 +87,26 @@ export function useExploreValidSpec( ); } -export async function fetchExploreSpec( - instanceId: string, - exploreName: string, -) { - const queryParams = { +export async function fetchExploreSpec(runtime: Runtime, exploreName: string) { + const params = { name: exploreName, }; - const queryKey = getRuntimeServiceGetExploreQueryKey(instanceId, queryParams); + const queryKey = getRuntimeServiceGetExploreQueryKey( + runtime.instanceId, + params, + ); const queryFunction: QueryFunction< Awaited> - > = ({ signal }) => runtimeServiceGetExplore(instanceId, queryParams, signal); + > = ({ signal }) => + httpClient( + { + url: `/v1/instances/${runtime.instanceId}/resources/explore`, + method: "get", + params, + signal, + }, + runtime, + ); const response = await queryClient.fetchQuery({ queryFn: queryFunction, @@ -118,10 +131,19 @@ export async function fetchExploreSpec( metricsViewName ) { fullTimeRange = await queryClient.fetchQuery({ - queryFn: () => - queryServiceMetricsViewTimeRange(instanceId, metricsViewName, {}), + queryFn: ({ signal }) => + httpClient( + { + url: `/v1/instances/${runtime.instanceId}/queries/metrics-views/${metricsViewName}/time-range-summary`, + method: "post", + headers: { "Content-Type": "application/json" }, + data: {}, + signal, + }, + runtime, + ), queryKey: getQueryServiceMetricsViewTimeRangeQueryKey( - instanceId, + runtime.instanceId, metricsViewName, {}, ), diff --git a/web-common/src/runtime-client/http-client.ts b/web-common/src/runtime-client/http-client.ts index b51c75331ea..3d7319593d5 100644 --- a/web-common/src/runtime-client/http-client.ts +++ b/web-common/src/runtime-client/http-client.ts @@ -20,15 +20,18 @@ export const httpRequestQueue = new HttpRequestQueue(); export const httpClient = async ( config: FetchWrapperOptions, + // used to override the global runtime in cases where it is either not initialised or is stale + // this could happen when switching projects with different runtimes + runtimeData = get(runtime), ): Promise => { // Naive request interceptors // Set host - const host = get(runtime).host; + const host = runtimeData.host; const interceptedConfig = { ...config, baseUrl: host }; // Set JWT - let jwt = get(runtime).jwt; + let jwt = runtimeData.jwt; if (jwt && jwt.token) { jwt = await maybeWaitForFreshJWT(jwt); interceptedConfig.headers = { diff --git a/web-local/src/routes/(viz)/explore/[name]/+layout.ts b/web-local/src/routes/(viz)/explore/[name]/+layout.ts index f391f8a192e..72375be69f5 100644 --- a/web-local/src/routes/(viz)/explore/[name]/+layout.ts +++ b/web-local/src/routes/(viz)/explore/[name]/+layout.ts @@ -4,15 +4,13 @@ import { error } from "@sveltejs/kit"; import { get } from "svelte/store"; export const load = async ({ params, depends }) => { - const { instanceId } = get(runtime); - const exploreName = params.name; depends(exploreName, "explore"); try { const { explore, metricsView, defaultExplorePreset } = - await fetchExploreSpec(instanceId, exploreName); + await fetchExploreSpec(get(runtime), exploreName); return { explore,