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

fix: Support legacy hierarchies described with hasHierarchy #649

Merged
merged 1 commit into from
Aug 23, 2022
Merged
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
1 change: 1 addition & 0 deletions app/rdf/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ const buildFilters = ({
}

const hasHierarchy =
cubeDimension.out(ns.cubeMeta.inHierarchy).values.length > 0 ||
cubeDimension.out(ns.cubeMeta.hasHierarchy).values.length > 0;
const toRDFValue = (value: string): NamedNode | Literal => {
return dataType && !hasHierarchy
Expand Down
31 changes: 24 additions & 7 deletions app/rdf/query-hierarchies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from "@zazuko/cube-hierarchy-query/index";
import { AnyPointer } from "clownface";
import { isGraphPointer } from "is-graph-pointer";
import { Cube } from "rdf-cube-view-query";
import rdf from "rdf-ext";
import { StreamClient } from "sparql-http-client";
import { ParsingClient } from "sparql-http-client/ParsingClient";
Expand Down Expand Up @@ -59,6 +60,27 @@ const toTree = (
return results.map((r) => serializeNode(r, 0));
};

const findHierarchyForDimension = (cube: Cube, dimensionIri: string) => {
const newHierarchy = cube.ptr
.any()
.has(ns.sh.path, rdf.namedNode(dimensionIri))
.has(ns.cubeMeta.inHierarchy)
.out(ns.cubeMeta.inHierarchy)
.toArray()[0];
if (newHierarchy) {
return newHierarchy;
}
const legacyHierarchy = cube.ptr
.any()
.has(ns.sh.path, rdf.namedNode(dimensionIri))
.has(ns.cubeMeta.hasHierarchy)
.out(ns.cubeMeta.hasHierarchy)
.toArray()[0];
if (legacyHierarchy) {
return legacyHierarchy;
}
};

export const queryHierarchy = async (
dimensionIri: string,
sourceUrl: string,
Expand All @@ -79,18 +101,13 @@ export const queryHierarchy = async (
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 = cube.ptr
.any()
.has(ns.sh.path, rdf.namedNode(dimensionIri))
.has(ns.cubeMeta.inHierarchy)
.out(ns.cubeMeta.inHierarchy)
.toArray()
.shift();
const hierarchy = findHierarchyForDimension(cube, dimensionIri);

// @ts-ignore
if (!isGraphPointer(hierarchy)) {
Expand Down