Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Optimize initializing chart from cube #1697

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 49 additions & 44 deletions app/configurator/configurator-state/init.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import {
ConfiguratorStateConfiguringChart,
DataSource,
decodeConfiguratorState,
SingleFilters,
} from "@/config-types";
import { SELECTING_DATASET_STATE } from "@/configurator/configurator-state/initial";
import { executeDataCubesComponentsQuery } from "@/graphql/hooks";
import {
DataCubePreviewDocument,
DataCubePreviewQuery,
DataCubePreviewQueryVariables,
PossibleFiltersDocument,
PossibleFiltersQuery,
PossibleFiltersQueryVariables,
Expand All @@ -25,10 +28,7 @@ import { migrateConfiguratorState } from "@/utils/chart-config/versioning";
import { getLocalStorageKey } from "./localstorage";
import { deriveFiltersFromFields, transitionStepNext } from "./reducer";

import {
getFiltersByMappingStatus,
getStateWithCurrentDataSource,
} from "./index";
import { getStateWithCurrentDataSource } from "./index";

export const initChartStateFromCube = async (
client: Client,
Expand All @@ -43,56 +43,61 @@ export const initChartStateFromCube = async (
dataSource,
});

const { data: components } = await executeDataCubesComponentsQuery(client, {
sourceType: dataSource.type,
sourceUrl: dataSource.url,
locale,
cubeFilters: [{ iri: cubeIri, loadValues: true }],
});
const { data: dataCubePreview } = await client
.query<DataCubePreviewQuery, DataCubePreviewQueryVariables>(
DataCubePreviewDocument,
{
sourceType: dataSource.type,
sourceUrl: dataSource.url,
locale,
cubeFilter: { iri: cubeIri },
}
)
.toPromise();

if (!components?.dataCubesComponents) {
throw new Error(`Could not fetch components for ${cubeIri}!`);
if (!dataCubePreview?.dataCubePreview) {
throw new Error(`Could not fetch preview for ${cubeIri}!`);
}

const { dimensions, measures } = components.dataCubesComponents;
const possibleChartTypes = getPossibleChartTypes({
dimensions,
measures,
cubeCount: 1,
});
const initialChartConfig = getInitialConfig({
chartType: possibleChartTypes[0],
iris: [{ iri: cubeIri, publishIri: cubePublishIri }],
dimensions,
measures,
});
const temporaryChartConfig = deriveFiltersFromFields(initialChartConfig, {
dimensions,
});
const { unmappedFilters } = getFiltersByMappingStatus(temporaryChartConfig, {
cubeIri,
});
const shouldFetchPossibleFilters = Object.keys(unmappedFilters).length > 0;
const variables = getPossibleFiltersQueryVariables({
cubeIri,
dataSource,
unmappedFilters,
});
const { dimensions, measures } = dataCubePreview.dataCubePreview;
const { data: possibleFilters } = await client
.query<
PossibleFiltersQuery,
PossibleFiltersQueryVariables
>(PossibleFiltersDocument, variables, { pause: !shouldFetchPossibleFilters })
.query<PossibleFiltersQuery, PossibleFiltersQueryVariables>(
PossibleFiltersDocument,
getPossibleFiltersQueryVariables({
cubeIri,
dataSource,
unmappedFilters: dataCubePreview.dataCubePreview.dimensions.reduce(
(acc, { iri, values }) => ({
...acc,
[iri]: {
type: "single",
value: values[0].value,
},
}),
{} as SingleFilters
),
})
)
.toPromise();

if (!possibleFilters?.possibleFilters && shouldFetchPossibleFilters) {
if (!possibleFilters?.possibleFilters) {
throw new Error(`Could not fetch possible filters for ${cubeIri}!`);
}

const chartConfig = deriveFiltersFromFields(initialChartConfig, {
const possibleChartTypes = getPossibleChartTypes({
dimensions,
possibleFilters: possibleFilters?.possibleFilters,
measures,
cubeCount: 1,
});
const chartConfig = deriveFiltersFromFields(
getInitialConfig({
chartType: possibleChartTypes[0],
iris: [{ iri: cubeIri, publishIri: cubePublishIri }],
dimensions,
measures,
}),
{ dimensions, possibleFilters: possibleFilters.possibleFilters }
);

return transitionStepNext(
getStateWithCurrentDataSource(SELECTING_DATASET_STATE),
Expand Down
Loading