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

chore: Update hierarchy query library #1223

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@deck.gl/react": "^8.9.19",
"@emotion/react": "^11.8.2",
"@emotion/styled": "^11.8.1",
"@hydrofoil/shape-to-query": "^0.9.3",
"@juggle/resize-observer": "^3.2.0",
"@lingui/core": "^3.17.2",
"@lingui/react": "^3.17.2",
Expand All @@ -50,7 +51,7 @@
"@visx/group": "^2.10.0",
"@visx/text": "^2.12.2",
"@visx/zoom": "^2.10.0",
"@zazuko/cube-hierarchy-query": "^2.0.0",
"@zazuko/cube-hierarchy-query": "^2.2.0",
"apollo-server-micro": "^3.0.0",
"autosuggest-highlight": "^3.3.4",
"catalog": "^4.0.1-canary.2",
Expand Down
101 changes: 69 additions & 32 deletions app/rdf/query-hierarchies.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import {
getHierarchy,
PropertyShape,
ShapePatterns,
sparql,
} from "@hydrofoil/shape-to-query";
import {
HierarchyNode,
getHierarchy,
} from "@zazuko/cube-hierarchy-query/index";
import { AnyPointer } from "clownface";
import orderBy from "lodash/orderBy";
import uniqBy from "lodash/uniqBy";
import { Cube } from "rdf-cube-view-query";
import rdf from "rdf-ext";
import { StreamClient } from "sparql-http-client";
import { Quad } from "rdf-js";
import { ParsingClient } from "sparql-http-client/ParsingClient";
import { StreamClient } from "sparql-http-client/StreamClient";
import { LRUCache } from "typescript-lru-cache";

import { parseTerm } from "@/domain/data";
import { truthy } from "@/domain/types";
import { HierarchyValue } from "@/graphql/resolver-types";
import { ResolvedDimension } from "@/graphql/shared-types";
Expand All @@ -35,36 +40,34 @@ const getName = (pointer: AnyPointer, language: string) => {

const toTree = (
results: HierarchyNode[],
dimensionIri: string,
locale: string
dimensionIri: string
): HierarchyValue[] => {
const sortChildren = (children: HierarchyValue[]) =>
orderBy(children, ["position", "identifier"]);
const serializeNode = (
node: HierarchyNode,
depth: number
): HierarchyValue | undefined => {
const name = getName(node.resource, locale);
const resource = node.resource;
// TODO Find out why some hierachy nodes have no label. We filter
// them out at the moment
// @see https://zulip.zazuko.com/#narrow/stream/40-bafu-ext/topic/labels.20for.20each.20hierarchy.20level/near/312845

const identifier = parseTerm(node.resource.out(ns.schema.identifier)?.term);
const name = resource.out(ns.schema.name).value;
const res: HierarchyValue | undefined = name
? {
dimensionIri,
depth,
label: name || "-",
alternateName: node.resource.out(ns.schema.alternateName).term?.value,
value: node.resource.value,
alternateName: resource.out(ns.schema.alternateName).value,
value: resource.value,
children: sortChildren(
node.nextInHierarchy
.map((childNode) => serializeNode(childNode, depth + 1))
.filter(truthy)
.filter((d) => d.label)
),
position: parseTerm(node.resource.out(ns.schema.position).term),
identifier: identifier,
depth,
dimensionIri: dimensionIri,
position: resource.out(ns.schema.position).value,
identifier: resource.out(ns.schema.identifier).value,
}
: undefined;
return res;
Expand All @@ -87,26 +90,45 @@ const findHierarchiesForDimension = (
.toArray(),
(x) => getName(x, locale)
);

if (newHierarchies) {
return newHierarchies;
}

const legacyHierarchies = cube.ptr
.any()
.has(ns.sh.path, rdf.namedNode(dimensionIri))
.has(ns.cubeMeta.hasHierarchy)
.out(ns.cubeMeta.hasHierarchy)
.toArray();

if (legacyHierarchies) {
return legacyHierarchies;
}

return [];
};

class PropertyShapeEx extends PropertyShape {
nodeConstraintPatterns({
constructClause,
whereClause,
}: ShapePatterns): ShapePatterns {
return {
constructClause,
whereClause: sparql`
#pragma group.joins
${whereClause}
`,
};
}
}

export const queryHierarchy = async (
rdimension: ResolvedDimension,
locale: string,
sparqlClient: ParsingClient,
sparqlClientStream: StreamClient,
sparqlClientStream: StreamClient<Quad>,
cache: LRUCache | undefined
): Promise<HierarchyValue[] | null> => {
const hierarchies = findHierarchiesForDimension(
Expand All @@ -119,27 +141,42 @@ export const queryHierarchy = async (
return null;
}

const dimensionValuesWithLabels = await getCubeDimensionValuesWithMetadata({
cube: rdimension.cube,
dimension: rdimension.dimension,
sparqlClient,
locale,
cache,
});

const allHierarchies = await Promise.all(
hierarchies?.map(async (h) => ({
// @ts-ignore
nodes: (await getHierarchy(h).execute(sparqlClientStream, rdf)) || [],
hierarchyName: getName(h.out(ns.cubeMeta.nextInHierarchy), locale),
}))
);
const [dimensionValuesWithLabels, ...allHierarchies] = await Promise.all([
getCubeDimensionValuesWithMetadata({
cube: rdimension.cube,
dimension: rdimension.dimension,
sparqlClient,
locale,
cache,
}),
...hierarchies?.map(async (hierarchy) => ({
nodes:
// @ts-ignore
(await getHierarchy(hierarchy, {
properties: [
ns.schema.identifier,
[ns.schema.name, { language: locale }],
[ns.schema.description, { language: locale }],
ns.schema.position,
[ns.schema.alternateName, { language: locale }],
],
// See https://gitlab.ldbar.ch/bafu/visualize/-/issues/548#note_13760.
shapeToQueryOptions: {
PropertyShape: PropertyShapeEx,
},
// @ts-ignore
}).execute(sparqlClientStream, rdf)) || [],
hierarchyName: getName(
hierarchy.out(ns.cubeMeta.nextInHierarchy),
locale
),
})),
]);

const trees = allHierarchies.map((h) => {
const tree: (HierarchyValue & { hierarchyName?: string })[] = toTree(
h.nodes,
rdimension.data.iri,
locale
rdimension.data.iri
);

if (tree.length > 0) {
Expand Down
Loading
Loading