Skip to content

Commit

Permalink
Merge pull request #1265 from visualize-admin/perf/optimize-dataset-p…
Browse files Browse the repository at this point in the history
…review-performance

perf: Optimize dataset previews performance
  • Loading branch information
bprusinowski authored Nov 13, 2023
2 parents 8ad1cff + 1cc1134 commit 5453000
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 17 additions & 23 deletions app/rdf/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,8 @@ const buildFilters = ({
});
};

type ObservationRaw = Record<string, Literal | NamedNode>;

async function fetchViewObservations({
limit,
observationsView,
Expand All @@ -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<ObservationRaw[]> | undefined;

let observationsRaw:
| PromiseValue<ReturnType<typeof observationsView.observations>>
| 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<string, Literal | NamedNode<string>>;
Expand All @@ -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;

Expand Down
5 changes: 5 additions & 0 deletions app/typings/rdf.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,18 @@ 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({
disableDistinct,
}: {
disableDistinct?: boolean;
}): Promise<Record<string, Literal | NamedNode>[]>;
async preview(options: {
limit?: number;
offset?: number;
}): Promise<Record<string, Literal | NamedNode>[]>;
addDimension(dimension: Dimension): View;
createDimension(options: $FixMe): Dimension;
setDefaultColumns(): void;
Expand Down

1 comment on commit 5453000

@vercel
Copy link

@vercel vercel bot commented on 5453000 Nov 13, 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-alpha.vercel.app
visualization-tool-git-main-ixt1.vercel.app
visualization-tool-ixt1.vercel.app

Please sign in to comment.