Skip to content

Commit

Permalink
refactor: Deduplicate helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Sep 28, 2022
1 parent 1374200 commit 5d171fd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 39 deletions.
15 changes: 1 addition & 14 deletions app/rdf/query-cube-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { schema, dcat, dcterms, cube } from "../../app/rdf/namespace";
import { DataCubeOrganization, DataCubeTheme } from "../graphql/query-hooks";

import { makeLocalesFilter } from "./query-labels";
import { makeVisualizeDatasetFilter } from "./query-utils";

type RawDataCubeTheme = Omit<DataCubeTheme, "__typename">;
type RawDataCubeOrganization = Omit<DataCubeOrganization, "__typename">;
Expand Down Expand Up @@ -129,20 +130,6 @@ export const queryDatasetCountByOrganization = async ({
.filter((r) => r.iri);
};

const makeVisualizeDatasetFilter = (options?: { includeDrafts?: boolean }) => {
const includeDrafts = options?.includeDrafts || false;
return sparql`
?iri ${schema.workExample} <https://ld.admin.ch/application/visualize>.
${
includeDrafts
? ""
: sparql`?iri ${schema.creativeWorkStatus} <https://ld.admin.ch/vocabulary/CreativeWorkStatus/Published>.`
}
FILTER NOT EXISTS {?iri ${schema.expires} ?expiryDate }
FILTER NOT EXISTS {?iri ${schema.validThrough} ?validThrough }
`;
};

export const queryDatasetCountByTheme = async ({
sparqlClient,
organization,
Expand Down
31 changes: 6 additions & 25 deletions app/rdf/query-search.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DESCRIBE, SELECT, sparql } from "@tpluscode/sparql-builder";
import { DESCRIBE, SELECT } from "@tpluscode/sparql-builder";
import clownface from "clownface";
import { descending } from "d3";
import { Cube } from "rdf-cube-view-query";
Expand All @@ -14,6 +14,7 @@ import { parseCube, parseIri, parseVersionHistory } from "@/rdf/parse";
import { fromStream } from "@/rdf/sparql-client";

import { computeScores, highlight } from "./query-search-score-utils";
import { makeVisualizeDatasetFilter } from "./query-utils";

const toNamedNode = (x: string) => {
return `<${x}>`;
Expand Down Expand Up @@ -59,29 +60,6 @@ const executeAndMeasure = async <T extends SelectQuery | DescribeQuery>(
};
};

const makeVisualizeFilter = (includeDrafts: boolean) => {
return sparql`
?cube ${ns.schema.workExample} <https://ld.admin.ch/application/visualize>.
?cube ${ns.schema.creativeWorkStatus} ?workStatus.
${
!includeDrafts
? `
FILTER (
?workStatus IN (<https://ld.admin.ch/vocabulary/CreativeWorkStatus/Published>)
)`
: ""
}
FILTER (
NOT EXISTS { ?cube <http://schema.org/validThrough> ?validThrough . }
)
FILTER (
NOT EXISTS { ?cube <http://schema.org/expires> ?expires . }
)
`;
};

const enhanceQuery = (rawQuery: string) => {
const enhancedQuery = rawQuery
.toLowerCase()
Expand Down Expand Up @@ -150,7 +128,10 @@ export const searchCubes = async ({
?versionHistory ${ns.schema.hasPart} ?cube.
}
${makeVisualizeFilter(!!includeDrafts)}
${makeVisualizeDatasetFilter({
includeDrafts: !!includeDrafts,
cubeIriVar: "?cube",
})}
${makeInFilter("about", aboutValues)}
${makeInFilter("theme", themeValues)}
Expand Down
23 changes: 23 additions & 0 deletions app/rdf/query-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { sparql } from "@tpluscode/sparql-builder";

import { schema } from "../../app/rdf/namespace";

export const makeVisualizeDatasetFilter = (options?: {
includeDrafts?: boolean;
cubeIriVar?: string;
}) => {
const cubeIriVar = options?.cubeIriVar || "?iri";
const includeDrafts = options?.includeDrafts || false;
return sparql`
${cubeIriVar} ${
schema.workExample
} <https://ld.admin.ch/application/visualize>.
${
includeDrafts
? ""
: sparql`${cubeIriVar} ${schema.creativeWorkStatus} <https://ld.admin.ch/vocabulary/CreativeWorkStatus/Published>.`
}
FILTER NOT EXISTS {${cubeIriVar} ${schema.expires} ?expiryDate }
FILTER NOT EXISTS {${cubeIriVar} ${schema.validThrough} ?validThrough }
`;
};

0 comments on commit 5d171fd

Please sign in to comment.