Skip to content

Commit

Permalink
fix: Support legacy hierarchies described with hasHierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Aug 23, 2022
1 parent c17b27f commit ac4db2e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
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

0 comments on commit ac4db2e

Please sign in to comment.