Skip to content

Commit

Permalink
Merge pull request #1016 from visualize-admin/perf/published-chart
Browse files Browse the repository at this point in the history
perf: Only load hierarchies where necessary
  • Loading branch information
bprusinowski authored Apr 5, 2023
2 parents 9f36514 + 9f01359 commit f7c8ee8
Show file tree
Hide file tree
Showing 7 changed files with 364 additions and 265 deletions.
3 changes: 2 additions & 1 deletion app/charts/column/columns-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ import {
} from "@/configurator/components/ui-helpers";
import { isTemporalDimension, Observation } from "@/domain/data";
import {
useFormatNumber,
formatNumberWithUnit,
useFormatNumber,
useTimeFormatUnit,
} from "@/formatters";
import {
Expand Down Expand Up @@ -224,6 +224,7 @@ const useColumnsState = (
fields.x.sorting,
fields.x.useAbbreviations,
xDimension,
chartConfig.filters,
]);

const yMeasure = measures.find((d) => d.iri === fields.y.componentIri);
Expand Down
13 changes: 9 additions & 4 deletions app/charts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ import {
NumericalMeasure,
OrdinalMeasure,
} from "../graphql/query-hooks";
import { DataCubeMetadata } from "../graphql/types";
import {
DataCubeMetadata,
DataCubeMetadataWithHierarchies,
} from "../graphql/types";
import { unreachableError } from "../utils/unreachable";

import {
Expand Down Expand Up @@ -133,7 +136,9 @@ export const DEFAULT_SORTING: SortingOption = {
/**
* Finds bottomost layer for the first hierarchy
*/
const findBottommostLayers = (dimension: DataCubeMetadata["dimensions"][0]) => {
const findBottommostLayers = (
dimension: DataCubeMetadataWithHierarchies["dimensions"][0]
) => {
const leaves = [] as HierarchyValue[];
let hasSeenMultiHierarchyNode = false;
bfs(dimension?.hierarchy as HierarchyValue[], (node) => {
Expand Down Expand Up @@ -222,8 +227,8 @@ export const getInitialConfig = ({
measures,
}: {
chartType: ChartType;
dimensions: DataCubeMetadata["dimensions"];
measures: DataCubeMetadata["measures"];
dimensions: DataCubeMetadataWithHierarchies["dimensions"];
measures: DataCubeMetadataWithHierarchies["measures"];
}): ChartConfig => {
const numericalMeasures = measures.filter(isNumericalMeasure);

Expand Down
5 changes: 2 additions & 3 deletions app/configurator/components/datatable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { Observation, isNumericalMeasure } from "@/domain/data";
import { useDimensionFormatters } from "@/formatters";
import {
DimensionMetadataFragment,
DimensionMetadataWithoutHierarchiesFragment,
useDataCubeObservationsQuery,
} from "@/graphql/query-hooks";
import SvgIcChevronDown from "@/icons/components/IcChevronDown";
Expand Down Expand Up @@ -281,8 +280,8 @@ export const DataSetTable = ({
};

export const getSortedColumns = (
dimensions: DimensionMetadataWithoutHierarchiesFragment[],
measures: DimensionMetadataWithoutHierarchiesFragment[]
dimensions: DimensionMetadataFragment[],
measures: DimensionMetadataFragment[]
) => {
const allDimensions = [...dimensions, ...measures];
allDimensions.sort((a, b) =>
Expand Down
20 changes: 12 additions & 8 deletions app/configurator/configurator-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ import {
import { DEFAULT_DATA_SOURCE } from "@/domain/datasource";
import { client } from "@/graphql/client";
import {
DataCubeMetadataWithComponentValuesAndHierarchiesQuery,
DataCubeMetadataWithComponentValuesAndHierarchiesQueryVariables,
DataCubeMetadataWithComponentValuesDocument,
DataCubeMetadataWithComponentValuesQuery,
DataCubeMetadataWithComponentValuesQueryVariables,
DimensionMetadataFragment,
DimensionMetadataWithHierarchiesFragment,
NumericalMeasure,
OrdinalMeasure,
} from "@/graphql/query-hooks";
Expand Down Expand Up @@ -299,8 +300,8 @@ const getCachedCubeMetadataWithComponentValues = (
locale: Locale
) => {
const query = client.readQuery<
DataCubeMetadataWithComponentValuesQuery,
DataCubeMetadataWithComponentValuesQueryVariables
DataCubeMetadataWithComponentValuesAndHierarchiesQuery,
DataCubeMetadataWithComponentValuesAndHierarchiesQueryVariables
>(DataCubeMetadataWithComponentValuesDocument, {
iri: draft.dataSet,
locale,
Expand Down Expand Up @@ -383,7 +384,10 @@ export const moveFilterField = produce(
);

export const deriveFiltersFromFields = produce(
(chartConfig: ChartConfig, dimensions: DimensionMetadataFragment[]) => {
(
chartConfig: ChartConfig,
dimensions: DimensionMetadataWithHierarchiesFragment[]
) => {
const { chartType, fields, filters } = chartConfig;

if (chartType === "table") {
Expand Down Expand Up @@ -484,7 +488,7 @@ export const applyNonTableDimensionToFilters = ({
isField,
}: {
filters: Filters;
dimension: DimensionMetadataFragment;
dimension: DimensionMetadataWithHierarchiesFragment;
isField: boolean;
}) => {
const currentFilter = filters[dimension.iri];
Expand Down Expand Up @@ -1434,8 +1438,8 @@ export const initChartStateFromCube = async (
): Promise<ConfiguratorState | undefined> => {
const { data } = await client
.query<
DataCubeMetadataWithComponentValuesQuery,
DataCubeMetadataWithComponentValuesQueryVariables
DataCubeMetadataWithComponentValuesAndHierarchiesQuery,
DataCubeMetadataWithComponentValuesAndHierarchiesQueryVariables
>(DataCubeMetadataWithComponentValuesDocument, {
iri: datasetIri,
sourceType: dataSource.type,
Expand Down
160 changes: 96 additions & 64 deletions app/graphql/queries/data-cubes.graphql
Original file line number Diff line number Diff line change
@@ -1,42 +1,3 @@
query DataCubes(
$sourceType: String!
$sourceUrl: String!
$locale: String!
$query: String
$order: DataCubeResultOrder
$includeDrafts: Boolean
$filters: [DataCubeSearchFilter!]
) {
dataCubes(
sourceType: $sourceType
sourceUrl: $sourceUrl
locale: $locale
query: $query
order: $order
includeDrafts: $includeDrafts
filters: $filters
) {
highlightedTitle
highlightedDescription
dataCube {
iri
title
workExamples
creator {
iri
label
}
description
publicationStatus
datePublished
themes {
iri
label
}
}
}
}

fragment dimensionMetadata on Dimension {
iri
label
Expand All @@ -51,7 +12,6 @@ fragment dimensionMetadata on Dimension {
iri
type
}
...hierarchyMetadata
... on TemporalDimension {
timeUnit
timeFormat
Expand All @@ -64,7 +24,7 @@ fragment dimensionMetadata on Dimension {
}
}

fragment dimensionMetadataWithoutHierarchies on Dimension {
fragment dimensionMetadataWithHierarchies on Dimension {
iri
label
description
Expand All @@ -88,6 +48,67 @@ fragment dimensionMetadataWithoutHierarchies on Dimension {
resolution
isDecimal
}
...hierarchyMetadata
}

fragment hierarchyMetadata on Dimension {
hierarchy(sourceType: $sourceType, sourceUrl: $sourceUrl) {
...hierarchyValueFields
children {
...hierarchyValueFields
children {
...hierarchyValueFields
children {
...hierarchyValueFields
children {
...hierarchyValueFields
children {
...hierarchyValueFields
}
}
}
}
}
}
}

query DataCubes(
$sourceType: String!
$sourceUrl: String!
$locale: String!
$query: String
$order: DataCubeResultOrder
$includeDrafts: Boolean
$filters: [DataCubeSearchFilter!]
) {
dataCubes(
sourceType: $sourceType
sourceUrl: $sourceUrl
locale: $locale
query: $query
order: $order
includeDrafts: $includeDrafts
filters: $filters
) {
highlightedTitle
highlightedDescription
dataCube {
iri
title
workExamples
creator {
iri
label
}
description
publicationStatus
datePublished
themes {
iri
label
}
}
}
}

query DataCubePreview(
Expand All @@ -110,10 +131,10 @@ query DataCubePreview(
description
publicationStatus
dimensions(sourceType: $sourceType, sourceUrl: $sourceUrl) {
...dimensionMetadataWithoutHierarchies
...dimensionMetadata
}
measures(sourceType: $sourceType, sourceUrl: $sourceUrl) {
...dimensionMetadataWithoutHierarchies
...dimensionMetadata
}
observations(sourceType: $sourceType, sourceUrl: $sourceUrl, limit: 10) {
data
Expand Down Expand Up @@ -190,32 +211,44 @@ query DataCubeMetadataWithComponentValues(
landingPage
dimensions(sourceType: $sourceType, sourceUrl: $sourceUrl) {
...dimensionMetadata
...hierarchyMetadata
}
measures(sourceType: $sourceType, sourceUrl: $sourceUrl) {
...dimensionMetadata
...hierarchyMetadata
}
}
}

fragment hierarchyMetadata on Dimension {
hierarchy(sourceType: $sourceType, sourceUrl: $sourceUrl) {
...hierarchyValueFields
children {
...hierarchyValueFields
children {
...hierarchyValueFields
children {
...hierarchyValueFields
children {
...hierarchyValueFields
children {
...hierarchyValueFields
}
}
}
}
query DataCubeMetadataWithComponentValuesAndHierarchies(
$iri: String!
$sourceType: String!
$sourceUrl: String!
$locale: String!
$latest: Boolean
$filters: Filters
) {
dataCubeByIri(
iri: $iri
sourceType: $sourceType
sourceUrl: $sourceUrl
locale: $locale
latest: $latest
) {
iri
title
publisher
publicationStatus
expires
identifier
workExamples
creator {
iri
}
landingPage
dimensions(sourceType: $sourceType, sourceUrl: $sourceUrl) {
...dimensionMetadataWithHierarchies
}
measures(sourceType: $sourceType, sourceUrl: $sourceUrl) {
...dimensionMetadataWithHierarchies
}
}
}
Expand All @@ -241,8 +274,7 @@ query DimensionValues(
sourceType: $sourceType
sourceUrl: $sourceUrl
) {
...dimensionMetadata
...hierarchyMetadata
...dimensionMetadataWithHierarchies
}
}
}
Expand Down
Loading

1 comment on commit f7c8ee8

@vercel
Copy link

@vercel vercel bot commented on f7c8ee8 Apr 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

visualization-tool – ./

visualization-tool-git-main-ixt1.vercel.app
visualization-tool-ixt1.vercel.app
visualization-tool-alpha.vercel.app

Please sign in to comment.