Skip to content

Commit

Permalink
Avoid using global runtime in loader funciton
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaHegde committed Dec 16, 2024
1 parent 61b8017 commit 677443c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 28 deletions.
8 changes: 1 addition & 7 deletions web-admin/src/routes/+layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
44 changes: 33 additions & 11 deletions web-common/src/features/explores/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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<ReturnType<typeof runtimeServiceGetExplore>>
> = ({ signal }) => runtimeServiceGetExplore(instanceId, queryParams, signal);
> = ({ signal }) =>
httpClient<V1GetExploreResponse>(
{
url: `/v1/instances/${runtime.instanceId}/resources/explore`,
method: "get",
params,
signal,
},
runtime,
);

const response = await queryClient.fetchQuery({
queryFn: queryFunction,
Expand All @@ -118,10 +131,19 @@ export async function fetchExploreSpec(
metricsViewName
) {
fullTimeRange = await queryClient.fetchQuery({
queryFn: () =>
queryServiceMetricsViewTimeRange(instanceId, metricsViewName, {}),
queryFn: ({ signal }) =>
httpClient<V1MetricsViewTimeRangeResponse>(
{
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,
{},
),
Expand Down
7 changes: 5 additions & 2 deletions web-common/src/runtime-client/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ export const httpRequestQueue = new HttpRequestQueue();

export const httpClient = async <T>(
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<T> => {
// 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 = {
Expand Down
4 changes: 1 addition & 3 deletions web-local/src/routes/(viz)/explore/[name]/+layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 677443c

Please sign in to comment.