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 unique values hierarchy #832

Merged
merged 3 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 22 additions & 0 deletions app/configurator/components/use-hierarchy-parents.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import hierarchy from "../../test/__fixtures/data/tarrifs-hierarchy.json";

import { groupByParents } from "./use-hierarchy-parents";

describe("grouping hierarchy by parents", () => {
it("should work", () => {
const archy = hierarchy as Parameters<typeof groupByParents>[0];
const groups = groupByParents(archy);
const iri =
"https://ld.admin.ch/cube/dimension/aussenhandel_warenkoerbe/1_listZT";
const ztGroups = groups.filter(
([parents, values]) =>
parents.length > 0 && parents[parents.length - 1].value === iri
)!;

expect(ztGroups.length).toBe(1);
const ztGroup = ztGroups[0]!;
const [parents, children] = ztGroup;
expect(parents.length).toBe(2);
expect(children.length).toBe(41);
});
});
73 changes: 56 additions & 17 deletions app/configurator/components/use-hierarchy-parents.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import { groups } from "d3-array";
import uniqBy from "lodash/uniqBy";
import { useMemo } from "react";

import { DataSource } from "@/configurator/config-types";
import { useDimensionHierarchyQuery } from "@/graphql/query-hooks";
import {
DimensionHierarchyQuery,
useDimensionHierarchyQuery,
} from "@/graphql/query-hooks";
import { DataCubeMetadata } from "@/graphql/types";
import { dfs } from "@/utils/dfs";

type NN<T> = NonNullable<T>;
export type DimensionHierarchyQueryHierarchy = NN<
NN<
NN<DimensionHierarchyQuery["dataCubeByIri"]>["dimensionByIri"]
>["hierarchy"]
>;

export const groupByParents = (hierarchy: DimensionHierarchyQueryHierarchy) => {
const allHierarchyValues = [
...dfs(hierarchy, (node, { depth, parents }) => ({
node,
parents,
depth,
})),
];

return groups(allHierarchyValues, (v) => v.parents);
};

const useHierarchyParents = (
dataSet: string,
dataSource: DataSource,
Expand All @@ -30,22 +51,40 @@ const useHierarchyParents = (
if (!hierarchy) {
return;
}
const values = dimension.values;
const valueSet = new Set(values.map((v) => v.value));
const dimensionValues = uniqBy(
[
...dfs(hierarchy, (node, { depth, parents }) => ({
node,
parents,
depth,
})),
].filter(({ node }) => {
return valueSet.has(node.value);
}),
(x) => x.node.value
);
return groups(dimensionValues, (v) => v.parents);
const valueSet = new Set(dimension.values.map((x) => x.value));
return groupByParents(hierarchy)
.map(
([parents, values]) =>
[parents, values.filter((x) => valueSet.has(x.node.value))] as const
)
.filter(([_parents, values]) => values.length > 0);
}, [dimension.values, hierarchy]);
};

/**
* Can be used for debugging, pass a hierarchy, and copy the output
* to graphviz.
*
* @see https://dreampuf.github.io/GraphvizOnline/
*/
export const hierarchyToGraphviz = (
hierarchy: DimensionHierarchyQueryHierarchy
) => {
const lines = [] as string[];
dfs(hierarchy, (node, { depth, parents }) => {
lines.push(`"${node.value}"[label="${node.label.replace(/"/g, "")}"]`);
if (parents.length > 0) {
const parent = parents[parents.length - 1];
lines.push(`"${parent.value}" -> "${node.value}"`);
}
});
return `
digraph G {
rankdir=LR

${lines.join("\n")}
}
`;
};

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

Copy link

@FabianCretton FabianCretton Nov 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can give an input here, and this might be an "special" situation in my data: most of the nodes are related to values in the Data, except the "list of Zolltarif" node itself -> this is kind of a "fake" node, just to regroup and display the list of Zolltarifs that are included in a basket (whithout having to look at all tarifs in the children, and also given that a tarif could be directly at that level and not coming from a child).
As discussed with Patrick, there could be a better way to show this, we can talk about it. This was just a "possible" way that I used for that POC.
Thus, for me it makes sense if the node "list of Zolltarfis" can not be selected. It is a nice handling of the situation by Visualize.

And it is the same for the node "Zolltarifs" itself: there is no value in the data for it, but its children do have a value.

export default useHierarchyParents;
Loading