Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Dec 23, 2024
1 parent b9db92b commit 683ef80
Show file tree
Hide file tree
Showing 7 changed files with 1,047 additions and 38 deletions.
10 changes: 7 additions & 3 deletions apis/shared-dimensions/lib/domain/shared-dimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ParsingClient } from 'sparql-http-client/ParsingClient'
import { md } from '@cube-creator/core/namespace'
import env from '../env'
import shapeToQuery, { rewriteTemplates } from '../shapeToQuery'
import { CollectionData } from '../handlers/collection'
import { getDynamicProperties } from './shared-dimension'

interface GetSharedDimensions {
Expand All @@ -20,7 +21,7 @@ interface GetSharedDimensions {
includeDeprecated?: Literal
}

export async function getSharedDimensions(client: StreamClient, { freetextQuery = '', limit = 10, offset = 0, includeDeprecated }: GetSharedDimensions = {}): Promise<Quad[]> {
export async function getSharedDimensions(client: StreamClient, { freetextQuery = '', limit = 10, offset = 0, includeDeprecated }: GetSharedDimensions = {}): Promise<CollectionData<Quad[]>> {
const { constructQuery } = await shapeToQuery()

const shape = await loadShape('dimensions-query-shape')
Expand All @@ -44,7 +45,10 @@ export async function getSharedDimensions(client: StreamClient, { freetextQuery
termSet.addOut(md.terms, $rdf.namedNode(`${MANAGED_DIMENSIONS_BASE}dimension/_terms?dimension=${termSet.value}`))
})

return dataset.toArray()
return {
members: dataset,
totalItems: dataset.match(null, rdf.type, schema.DefinedTermSet).length,
}
}

interface GetSharedTerms {
Expand All @@ -55,7 +59,7 @@ interface GetSharedTerms {
validThrough?: Date
}

export async function getSharedTerms<C extends StreamClient | ParsingClient>({ sharedDimensions, freetextQuery, validThrough, limit = 10, offset = 0 }: GetSharedTerms, client: C): Promise<C extends StreamClient ? Stream : Quad[]> {
export async function getSharedTerms<C extends StreamClient | ParsingClient>({ sharedDimensions, freetextQuery, validThrough, limit = 10, offset = 0 }: GetSharedTerms, client: C): Promise<CollectionData<C extends StreamClient ? Stream : Quad[]>> {
const shape = await loadShape('terms-query-shape')

shape.addOut(sh.targetNode, sharedDimensions)
Expand Down
15 changes: 10 additions & 5 deletions apis/shared-dimensions/lib/handlers/collection.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import type { NamedNode, Quad } from '@rdfjs/types'
import type { NamedNode, Quad, Stream } from '@rdfjs/types'
import $rdf from 'rdf-ext'
import clownface, { GraphPointer } from 'clownface'
import { hydra, rdf } from '@tpluscode/rdf-ns-builders'

export interface CollectionData<M extends Stream | Iterable<Quad> = Stream | Iterable<Quad>> {
members: M
totalItems: number
}

interface CollectionHandler {
memberType: NamedNode
collectionType: NamedNode
view?: NamedNode
memberQuads: Quad[]
data: CollectionData<Iterable<Quad>>
collection: NamedNode
}

export function getCollection({ collection, view, memberQuads, memberType, collectionType }: CollectionHandler): GraphPointer<NamedNode> {
const dataset = $rdf.dataset(memberQuads)
export function getCollection({ collection, view, data: { members: memberQuads, totalItems }, memberType, collectionType }: CollectionHandler): GraphPointer<NamedNode> {
const dataset = $rdf.dataset([...memberQuads])

const graph = clownface({ dataset })
const members = graph.has(rdf.type, memberType)

graph.node(collection)
.addOut(rdf.type, [hydra.Collection, collectionType])
.addOut(hydra.member, members)
.addOut(hydra.totalItems, members.terms.length)
.addOut(hydra.totalItems, totalItems)

if (view) {
graph.node(view)
Expand Down
4 changes: 2 additions & 2 deletions apis/shared-dimensions/lib/handlers/shared-dimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const get = asyncMiddleware(async (req, res, next) => {

const collection = getCollection({
view: $rdf.namedNode(req.absoluteUrl()),
memberQuads: await getSharedDimensions(streamClient, queryParams),
data: await getSharedDimensions(streamClient, queryParams),
collectionType: ns.md.SharedDimensions,
memberType: schema.DefinedTermSet,
collection: req.hydra.resource.term,
Expand Down Expand Up @@ -96,7 +96,7 @@ export const getTerms = asyncMiddleware(async (req, res, next) => {
}

const collection = getCollection({
memberQuads: await getSharedTerms(queryParams, parsingClient),
data: await getSharedTerms(queryParams, parsingClient),
memberType: schema.DefinedTerm,
collectionType: ns.md.SharedDimensionTerms,
collection: termsCollectionId(sharedDimensions, queryParams.freetextQuery),
Expand Down
57 changes: 31 additions & 26 deletions apis/shared-dimensions/lib/shapes/dimensions-query-shape.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,7 @@ prefix md: <https://cube-creator.zazuko.com/shared-dimensions/vocab#>
sh:orderBy [ sh:path [ s2q:variable "orderBy" ] ] ;
sh:nodes
[
sh:filterShape
[
sh:property
[
sh:path schema:name ;
sh:pattern
[
s2q:template "^${freetextQuery}" ;
] ;
sh:flags "i" ;
] ;
sh:property
[
sh:path rdf:type ;
sh:hasValue schema:DefinedTermSet, meta:SharedDimension ;
] ;
sh:expression
[
sh:deactivated [ s2q:variable "includeDeprecated" ; sh:defaultValue false ] ;
sparql:or
(
[ sparql:not ( [ sparql:bound ( _:validThrough ) ] ) ]
[ sparql:gt ( _:validThrough [ sparql:now () ] ) ]
) ;
] ;
] ;
sh:filterShape _:FilterShape ;
] ;
] ;
] ;
Expand Down Expand Up @@ -117,3 +92,33 @@ _:validThrough
sh:path schema:validThrough ;
] ;
.

_:FilterShape
sh:property
[
sh:path schema:name ;
sh:pattern
[
s2q:template "^${freetextQuery}" ;
] ;
sh:flags "i" ;
] ;
sh:property
[
sh:path rdf:type ;
sh:hasValue schema:DefinedTermSet, meta:SharedDimension ;
] ;
sh:expression
[
sh:deactivated
[
s2q:variable "includeDeprecated" ;
sh:defaultValue false
] ;
sparql:or
(
[ sparql:not ( [ sparql:bound ( _:validThrough ) ] ) ]
[ sparql:gt ( _:validThrough [ sparql:now () ] ) ]
) ;
] ;
.
Loading

0 comments on commit 683ef80

Please sign in to comment.