From 1cc1134bb7bea82a221ec5d7025333045c093150 Mon Sep 17 00:00:00 2001 From: Bartosz Prusinowski Date: Fri, 10 Nov 2023 17:46:05 +0100 Subject: [PATCH] perf: Use View's preview method to fetch dataset previews --- CHANGELOG.md | 1 + app/rdf/queries.ts | 40 +++++++++++++++++----------------------- app/typings/rdf.d.ts | 5 +++++ 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64b13ffb8..587c6174d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ You can also check the [release page](https://github.com/visualize-admin/visuali - Hierarchy names are now correctly retrieved - Performance - We no longer fetch shape when initalizing the cube, as we might need to re-fetch it again if a newer cube is required + - Vastly improved performance of dataset preview by using a new version of `cube-view-query` library (`View.preview`) # [3.24.0] - 2023-11-08 diff --git a/app/rdf/queries.ts b/app/rdf/queries.ts index b6728d0d1..c79eafb3d 100644 --- a/app/rdf/queries.ts +++ b/app/rdf/queries.ts @@ -842,6 +842,8 @@ const buildFilters = ({ }); }; +type ObservationRaw = Record; + async function fetchViewObservations({ limit, observationsView, @@ -851,37 +853,28 @@ async function fetchViewObservations({ observationsView: View; disableDistinct: boolean; }) { - /** - * Add LIMIT to query - */ - if (limit !== undefined) { - // From https://github.com/zazuko/cube-creator/blob/a32a90ff93b2c6c1c5ab8fd110a9032a8d179670/apis/core/lib/domain/observations/lib/index.ts#L41 - observationsView.ptr.addOut(ns.cubeView.projection, (projection: $FixMe) => - projection.addOut(ns.cubeView.limit, limit) - ); - } - - const queryOptions = { - disableDistinct, - }; + const fullQuery = observationsView.observationsQuery({ disableDistinct }); + const query = ( + limit ? fullQuery.previewQuery({ limit }) : fullQuery.query + ).toString(); - const query = observationsView - .observationsQuery(queryOptions) - .query.toString(); + let observationsRaw: PromiseValue | undefined; - let observationsRaw: - | PromiseValue> - | undefined; try { - observationsRaw = await observationsView.observations(queryOptions); + observationsRaw = await (limit + ? observationsView.preview({ limit }) + : observationsView.observations({ disableDistinct })); } catch (e) { - console.warn("Query failed", query); + console.warn("Observations query failed!", query); throw new Error( `Could not retrieve data: ${e instanceof Error ? e.message : e}` ); } - return { query, observationsRaw }; + return { + query, + observationsRaw, + }; } type RDFObservation = Record>; @@ -892,7 +885,8 @@ function parseObservation( ): (value: RDFObservation) => Observation { return (obs) => { const res = {} as Observation; - for (let d of cubeDimensions) { + + for (const d of cubeDimensions) { const label = obs[labelDimensionIri(d.data.iri)]?.value; const termType = obs[d.data.iri]?.termType; diff --git a/app/typings/rdf.d.ts b/app/typings/rdf.d.ts index 37a2cc5fc..d62eaa34b 100644 --- a/app/typings/rdf.d.ts +++ b/app/typings/rdf.d.ts @@ -101,6 +101,7 @@ declare module "rdf-cube-view-query" { dimension(options: { cubeDimension: NamedNode | string }): Dimension | null; observationsQuery({ disableDistinct }: { disableDistinct?: boolean }): { query: $FixMe; + previewQuery: $FixMe; dimensionMap: Map; }; async observations({ @@ -108,6 +109,10 @@ declare module "rdf-cube-view-query" { }: { disableDistinct?: boolean; }): Promise[]>; + async preview(options: { + limit?: number; + offset?: number; + }): Promise[]>; addDimension(dimension: Dimension): View; createDimension(options: $FixMe): Dimension; setDefaultColumns(): void;