Skip to content

Commit

Permalink
Fix/734/external dataset loading (#1071)
Browse files Browse the repository at this point in the history
* Fix external resource fetching sim dataset id argument.

* Added a query just to get external dataset names to populate view modification

* Remove duplicate resource names

* Added a type for our external dataset query and modified the query to use its own flag

* Removed fetchingResourcesExternalNames for now

---------

Co-authored-by: Aaron Plave <[email protected]>
  • Loading branch information
cohansen and AaronPlave authored Jan 4, 2024
1 parent c44edfa commit d1c5e3f
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/components/timeline/form/TimelineEditorPanel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import { ViewConstants } from '../../../enums/view';
import { activityTypes, maxTimeRange, viewTimeRange } from '../../../stores/plan';
import {
externalResources,
externalResourceNames,
resourceTypes,
resourcesByViewLayerId,
simulationDataset,
Expand Down Expand Up @@ -436,8 +436,8 @@
return $activityTypes.map(t => t.name);
} else if (layer.chartType === 'line' || layer.chartType === 'x-range') {
return $resourceTypes
.concat($externalResources)
.map(t => t.name)
.concat($externalResourceNames)
.sort();
}
return [];
Expand Down
10 changes: 9 additions & 1 deletion src/routes/plans/[id]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
} from '../../../stores/scheduling';
import {
enableSimulation,
externalResourceNames,
externalResources,
fetchingResources,
resetSimulationStores,
Expand Down Expand Up @@ -156,6 +157,7 @@
let windowWidth = 0;
let simulationDataAbortController: AbortController;
let resourcesExternalAbortController: AbortController;
let externalDatasetNamesAbortController: AbortController;
$: ({ invalidActivityCount, ...activityErrorCounts } = $activityErrorRollups.reduce(
(prevCounts, activityErrorRollup) => {
Expand Down Expand Up @@ -289,12 +291,18 @@
}
$: if ($plan) {
externalDatasetNamesAbortController?.abort();
externalDatasetNamesAbortController = new AbortController();
effects
.getExternalDatasetNames($plan.id, data.user, externalDatasetNamesAbortController.signal)
.then(names => ($externalResourceNames = names));
resourcesExternalAbortController?.abort();
resourcesExternalAbortController = new AbortController();
effects
.getResourcesExternal(
$plan.id,
$simulationDatasetId ?? null,
$simulationDatasetId > -1 ? $simulationDatasetId : null,
$plan.start_time,
data.user,
resourcesExternalAbortController.signal,
Expand Down
2 changes: 2 additions & 0 deletions src/stores/simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export const simulationDatasetId: Writable<number> = writable(-1);

export const externalResources: Writable<Resource[]> = writable([]);

export const externalResourceNames: Writable<string[]> = writable([]);

export const resources: Writable<Resource[]> = writable([]);

export const fetchingResources: Writable<boolean> = writable(false);
Expand Down
4 changes: 4 additions & 0 deletions src/types/simulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export type PlanDataset = {
offset_from_plan_start: string;
};

export type PlanDatasetNames = {
dataset: { profiles: { name: string }[] };
};

export type Profile = {
dataset_id: number;
duration: string;
Expand Down
55 changes: 49 additions & 6 deletions src/utilities/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ import type {
} from '../types/sequencing';
import type {
PlanDataset,
PlanDatasetNames,
Profile,
Resource,
ResourceType,
Expand Down Expand Up @@ -2580,6 +2581,44 @@ const effects = {
}
},

async getExternalDatasetNames(
planId: number,
user: User | null,
signal: AbortSignal | undefined = undefined,
): Promise<string[]> {
try {
const data = await reqHasura<PlanDatasetNames[]>(
gql.GET_PROFILES_EXTERNAL_NAMES,
{
planId,
},
user,
signal,
);
const { plan_dataset: plan_datasets } = data;

if (plan_datasets != null) {
const resourceNames: string[] = [];

for (const dataset of plan_datasets) {
for (const profile of dataset.dataset.profiles) {
resourceNames.push(profile.name);
}
}

return [...new Set(resourceNames)];
} else {
throw Error('Unable to get external resource names');
}
} catch (e) {
const error = e as Error;
if (error.name !== 'AbortError') {
catchError(error);
}
return [];
}
},

async getModels(user: User | null): Promise<ModelSlim[]> {
try {
const data = await reqHasura<ModelSlim[]>(gql.GET_MODELS, {}, user);
Expand Down Expand Up @@ -2885,16 +2924,20 @@ const effects = {
): Promise<Resource[]> {
try {
fetchingResourcesExternal.set(true);

// Always fetch external resources that aren't tied to a simulation, optionally get the resources tied to one if we have a dataset ID.
const clauses: { simulation_dataset_id: { _is_null: boolean } | { _eq: number } }[] = [
{ simulation_dataset_id: { _is_null: true } },
];
if (simulationDatasetId !== null) {
clauses.push({ simulation_dataset_id: { _eq: simulationDatasetId } });
}

const data = await reqHasura<PlanDataset[]>(
gql.GET_PROFILES_EXTERNAL,
{
planId,
simulationDatasetFilter:
simulationDatasetId === null
? {
_is_null: true,
}
: { _eq: simulationDatasetId },
simulationDatasetFilter: clauses,
},
user,
signal,
Expand Down
16 changes: 14 additions & 2 deletions src/utilities/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1051,8 +1051,8 @@ const gql = {
`,

GET_PROFILES_EXTERNAL: `#graphql
query GetProfilesExternal($planId: Int!, $simulationDatasetFilter: Int_comparison_exp) {
plan_dataset(where: { plan_id: { _eq: $planId }, simulation_dataset_id: $simulationDatasetFilter }) {
query GetProfilesExternal($planId: Int!, $simulationDatasetFilter: [plan_dataset_bool_exp!]) {
plan_dataset(where: { plan_id: { _eq: $planId }, _or: $simulationDatasetFilter }) {
dataset {
profiles {
dataset_id
Expand All @@ -1074,6 +1074,18 @@ const gql = {
}
`,

GET_PROFILES_EXTERNAL_NAMES: `#graphql
query GetProfilesExternalNames($planId: Int!) {
plan_dataset(where: { plan_id: { _eq: $planId }}) {
dataset {
profiles {
name
}
}
}
}
`,

GET_RESOURCE_TYPES: `#graphql
query GetResourceTypes($model_id: Int!, $limit: Int) {
resource_types: resource_type(where: { model_id: { _eq: $model_id } }, order_by: { name: asc }, limit: $limit) {
Expand Down

0 comments on commit d1c5e3f

Please sign in to comment.