Skip to content

Commit

Permalink
fix: slow queries on some environments
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Jun 7, 2024
1 parent cfdb26d commit aaa978e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
13 changes: 11 additions & 2 deletions apis/shared-dimensions/lib/domain/shared-dimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import clownface from 'clownface'
import { isGraphPointer } from 'is-graph-pointer'
import { StreamClient } from 'sparql-http-client/StreamClient'
import { ParsingClient } from 'sparql-http-client/ParsingClient'
import { md } from '@cube-creator/core/namespace'

Check warning on line 11 in apis/shared-dimensions/lib/domain/shared-dimensions.ts

View check run for this annotation

Codecov / codecov/patch

apis/shared-dimensions/lib/domain/shared-dimensions.ts#L11

Added line #L11 was not covered by tests
import env from '../env'
import shapeToQuery, { rewriteTemplates } from '../shapeToQuery'
import { getDynamicProperties } from './shared-dimension'
Expand All @@ -18,7 +19,7 @@ interface GetSharedDimensions {
offset?: number
}

export async function getSharedDimensions<C extends StreamClient | ParsingClient>(client: C, { freetextQuery = '', limit = 10, offset = 0 }: GetSharedDimensions = {}): Promise<C extends StreamClient ? Stream : Quad[]> {
export async function getSharedDimensions(client: StreamClient, { freetextQuery = '', limit = 10, offset = 0 }: GetSharedDimensions = {}): Promise<Quad[]> {

Check warning on line 22 in apis/shared-dimensions/lib/domain/shared-dimensions.ts

View check run for this annotation

Codecov / codecov/patch

apis/shared-dimensions/lib/domain/shared-dimensions.ts#L22

Added line #L22 was not covered by tests
const { constructQuery } = await shapeToQuery()

const shape = await loadShape('dimensions-query-shape')
Expand All @@ -33,7 +34,15 @@ export async function getSharedDimensions<C extends StreamClient | ParsingClient
}))
await rewriteTemplates(shape, variables)

return constructQuery(shape).execute(client) as any
const dataset = await $rdf.dataset().import(await constructQuery(shape).execute(client))
clownface({ dataset })
.has(rdf.type, schema.DefinedTermSet)
.forEach(termSet => {
termSet.addOut(md.export, $rdf.namedNode(`${MANAGED_DIMENSIONS_BASE}dimension/_export?dimension=${termSet.value}`))
termSet.addOut(md.terms, $rdf.namedNode(`${MANAGED_DIMENSIONS_BASE}dimension/_terms?dimension=${termSet.value}`))
})

return dataset.toArray()

Check warning on line 45 in apis/shared-dimensions/lib/domain/shared-dimensions.ts

View check run for this annotation

Codecov / codecov/patch

apis/shared-dimensions/lib/domain/shared-dimensions.ts#L37-L45

Added lines #L37 - L45 were not covered by tests
}

interface GetSharedTerms {
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 @@ -13,7 +13,7 @@ import { shaclValidate } from '../middleware/shacl'
import { getSharedDimensions, getSharedTerms } from '../domain/shared-dimensions'
import { create } from '../domain/shared-dimension'
import { store } from '../store'
import { parsingClient } from '../sparql'
import { parsingClient, streamClient } from '../sparql'

Check warning on line 16 in apis/shared-dimensions/lib/handlers/shared-dimensions.ts

View check run for this annotation

Codecov / codecov/patch

apis/shared-dimensions/lib/handlers/shared-dimensions.ts#L16

Added line #L16 was not covered by tests
import env from '../env'
import { rewrite, rewriteTerm } from '../rewrite'
import { postImportedDimension } from './shared-dimension/import'
Expand All @@ -36,7 +36,7 @@ export const get = asyncMiddleware(async (req, res, next) => {

const collection = getCollection({
view: $rdf.namedNode(req.absoluteUrl()),
memberQuads: await getSharedDimensions(parsingClient, queryParams),
memberQuads: await getSharedDimensions(streamClient, queryParams),

Check warning on line 39 in apis/shared-dimensions/lib/handlers/shared-dimensions.ts

View check run for this annotation

Codecov / codecov/patch

apis/shared-dimensions/lib/handlers/shared-dimensions.ts#L39

Added line #L39 was not covered by tests
collectionType: md.SharedDimensions,
memberType: schema.DefinedTermSet,
collection: req.hydra.resource.term,
Expand Down
2 changes: 2 additions & 0 deletions apis/shared-dimensions/lib/shapes/dimensions-query-shape.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ prefix md: <https://cube-creator.zazuko.com/shared-dimensions/vocab#>
] ;
sh:property
[
sh:deactivated true ;
sh:path md:terms ;
sh:values
[
Expand All @@ -83,6 +84,7 @@ prefix md: <https://cube-creator.zazuko.com/shared-dimensions/vocab#>
] ;
sh:property
[
sh:deactivated true ;
sh:path md:export ;
sh:values
[
Expand Down

0 comments on commit aaa978e

Please sign in to comment.