Skip to content

Commit

Permalink
perf: Use cube loader in hierarchy fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Nov 2, 2022
1 parent c80a645 commit fdcb09c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 26 deletions.
18 changes: 13 additions & 5 deletions app/graphql/resolvers/rdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,18 +279,26 @@ const getDimensionValuesLoader = (

export const hierarchy: NonNullable<DimensionResolvers["hierarchy"]> = async (
{ data: { iri } },
{ sourceUrl },
_unused,
{ setup },
info
) => {
const { sparqlClient, sparqlClientStream } = await setup(info);
return queryHierarchy(
const { locale } = info.variableValues;
const cubeIri = info.variableValues.iri || info.variableValues.cubeIri;
const { sparqlClient, sparqlClientStream, loaders } = await setup(info);
const cube = await loaders.cube.load(cubeIri);

if (!cube) {
throw new Error("Could not find cube");
}
const res = await queryHierarchy(
cube,
iri,
sourceUrl,
info.variableValues.locale,
locale,
sparqlClient,
sparqlClientStream
);
return res;
};

export const dimensionValues: NonNullable<
Expand Down
23 changes: 2 additions & 21 deletions app/rdf/query-hierarchies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { StreamClient } from "sparql-http-client";
import { ParsingClient } from "sparql-http-client/ParsingClient";

import { HierarchyValue } from "@/graphql/resolver-types";
import { createSource, pragmas } from "@/rdf/create-source";
import { pragmas } from "@/rdf/create-source";

import * as ns from "./namespace";
import { pruneTree, mapTree } from "./tree-utils";
Expand Down Expand Up @@ -82,31 +82,12 @@ const findHierarchyForDimension = (cube: Cube, dimensionIri: string) => {
};

export const queryHierarchy = async (
cube: Cube,
dimensionIri: string,
sourceUrl: string,
locale: string,
sparqlClient: ParsingClient,
sparqlClientStream: StreamClient
): Promise<HierarchyValue[] | null> => {
const source = createSource({ endpointUrl: sourceUrl });

const cubeQuery = SELECT`?cube`.WHERE`
?cube ${ns.cube.observationConstraint} ?shape.
?shape ?prop ?dimension.
?dimension ${ns.sh.path} <${dimensionIri}>.
FILTER NOT EXISTS { ?cube ${ns.schema.expires} ?any . }
`.prologue`${pragmas}`;
const cubeResults = await cubeQuery.execute(sparqlClient.query);
if (cubeResults.length === 0) {
throw new Error("Could not find cube");
}

const cubeIri = cubeResults[0].cube.value;
const cube = await source.cube(cubeIri);
if (!cube) {
throw new Error("Could not find cube");
}
const hierarchy = findHierarchyForDimension(cube, dimensionIri);

// @ts-ignore
Expand Down

0 comments on commit fdcb09c

Please sign in to comment.