Skip to content

Commit

Permalink
Fix redundant resource and span fetches during simulation progress up…
Browse files Browse the repository at this point in the history
…dates and abort stale requests.
  • Loading branch information
AaronPlave committed Dec 11, 2023
1 parent aac1370 commit f3087ae
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 deletions.
42 changes: 31 additions & 11 deletions src/routes/plans/[id]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import {
enableSimulation,
externalResources,
fetchingResources,
resetSimulationStores,
resourceTypes,
resources,
Expand Down Expand Up @@ -152,6 +153,8 @@
let simulationExtent: string | null;
let selectedSimulationStatus: string | null;
let windowWidth = 0;
let simulationDataAbortController: AbortController;
let resourcesExternalAbortController: AbortController;
$: activityErrorCounts = $activityErrorRollups.reduce(
(prevCounts, activityErrorRollup) => {
Expand Down Expand Up @@ -275,8 +278,16 @@
}
$: if ($plan) {
resourcesExternalAbortController?.abort();
resourcesExternalAbortController = new AbortController();
effects
.getResourcesExternal($plan.id, $simulationDatasetId ?? null, $plan.start_time, data.user)
.getResourcesExternal(
$plan.id,
$simulationDatasetId ?? null,
$plan.start_time,
data.user,
resourcesExternalAbortController.signal,
)
.then(newResources => ($externalResources = newResources));
}
Expand All @@ -285,16 +296,25 @@
selectActivity(null, null);
}
$: if ($plan && $simulationDataset !== undefined) {
if ($simulationDataset !== null && $simulationDatasetId !== -1) {
const datasetId = $simulationDataset.dataset_id;
const startTimeYmd = $simulationDataset?.simulation_start_time ?? $plan.start_time;
effects.getResources(datasetId, startTimeYmd, data.user).then(newResources => ($resources = newResources));
effects.getSpans(datasetId, data.user).then(newSpans => ($spans = newSpans));
} else {
$resources = [];
$spans = [];
}
$: if (
$plan &&
$simulationDatasetId !== -1 &&
$simulationDataset?.id === $simulationDatasetId &&
getSimulationStatus($simulationDataset) === Status.Complete
) {
const datasetId = $simulationDatasetId;
const startTimeYmd = $simulationDataset?.simulation_start_time ?? $plan.start_time;
simulationDataAbortController?.abort();
simulationDataAbortController = new AbortController();
effects
.getResources(datasetId, startTimeYmd, data.user, simulationDataAbortController.signal)
.then(newResources => ($resources = newResources));
effects.getSpans(datasetId, data.user, simulationDataAbortController.signal).then(newSpans => ($spans = newSpans));
} else {
simulationDataAbortController?.abort();
fetchingResources.set(false);
$resources = [];
$spans = [];
}
$: {
Expand Down
2 changes: 2 additions & 0 deletions src/stores/simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export const resources: Writable<Resource[]> = writable([]);

export const fetchingResources: Writable<boolean> = writable(false);

export const fetchingResourcesExternal: Writable<boolean> = writable(false);

export const resourceTypes: Writable<ResourceType[]> = writable([]);

export const spans: Writable<Span[]> = writable([]);
Expand Down
38 changes: 29 additions & 9 deletions src/utilities/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import {
import { createModelError, createPlanError, creatingModel, creatingPlan, models } from '../stores/plan';
import { schedulingStatus, selectedSpecId } from '../stores/scheduling';
import { commandDictionaries } from '../stores/sequencing';
import { fetchingResources, selectedSpanId, simulationDatasetId } from '../stores/simulation';
import {
fetchingResources,
fetchingResourcesExternal,
selectedSpanId,
simulationDatasetId,
} from '../stores/simulation';
import { createTagError } from '../stores/tags';
import { applyViewUpdate, view, viewUpdateTimeline } from '../stores/views';
import type {
Expand Down Expand Up @@ -2839,17 +2844,25 @@ const effects = {
}
},

async getResources(datasetId: number, startTimeYmd: string, user: User | null): Promise<Resource[]> {
async getResources(
datasetId: number,
startTimeYmd: string,
user: User | null,
signal: AbortSignal | undefined = undefined,
): Promise<Resource[]> {
try {
fetchingResources.set(true);
const data = await reqHasura<Profile[]>(gql.GET_PROFILES, { datasetId }, user);
const data = await reqHasura<Profile[]>(gql.GET_PROFILES, { datasetId }, user, signal);
const { profile: profiles } = data;
const sampledProfiles = sampleProfiles(profiles, startTimeYmd);
fetchingResources.set(false);
return sampledProfiles;
} catch (e) {
catchError(e as Error);
fetchingResources.set(false);
const error = e as Error;
if (error.name !== 'AbortError') {
catchError(error);
fetchingResources.set(false);
}
return [];
}
},
Expand All @@ -2859,8 +2872,10 @@ const effects = {
simulationDatasetId: number | null,
startTimeYmd: string,
user: User | null,
signal: AbortSignal | undefined = undefined,
): Promise<Resource[]> {
try {
fetchingResourcesExternal.set(true);
const data = await reqHasura<PlanDataset[]>(
gql.GET_PROFILES_EXTERNAL,
{
Expand All @@ -2873,6 +2888,7 @@ const effects = {
: { _eq: simulationDatasetId },
},
user,
signal,
);
const { plan_dataset: plan_datasets } = data;
if (plan_datasets != null) {
Expand All @@ -2885,13 +2901,17 @@ const effects = {
const sampledResources: Resource[] = sampleProfiles(profiles, startTimeYmd, offset_from_plan_start);
resources = [...resources, ...sampledResources];
}

fetchingResourcesExternal.set(false);
return resources;
} else {
throw Error('Unable to get external resources');
}
} catch (e) {
catchError(e as Error);
const error = e as Error;
if (error.name !== 'AbortError') {
catchError(error);
fetchingResourcesExternal.set(false);
}
return [];
}
},
Expand Down Expand Up @@ -2995,9 +3015,9 @@ const effects = {
}
},

async getSpans(datasetId: number, user: User | null): Promise<Span[]> {
async getSpans(datasetId: number, user: User | null, signal: AbortSignal | undefined = undefined): Promise<Span[]> {
try {
const data = await reqHasura<Span[]>(gql.GET_SPANS, { datasetId }, user);
const data = await reqHasura<Span[]>(gql.GET_SPANS, { datasetId }, user, signal);
const { span: spans } = data;
if (spans != null) {
return spans;
Expand Down

0 comments on commit f3087ae

Please sign in to comment.