Skip to content

Commit

Permalink
fix: slow queries on some environments (#1524)
Browse files Browse the repository at this point in the history
* fix: slow queries on some environments

* test: fix
  • Loading branch information
tpluscode authored Jun 11, 2024
1 parent cfdb26d commit b82ebab
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 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'
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[]> {
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()
}

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'
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),
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ describe('@cube-creator/shared-dimensions-api/lib/domain/shared-dimensions @SPAR
describe('getSharedDimensions', () => {
it('returns from all graphs', async () => {
// when
const dataset = await $rdf.dataset()
.import(await getSharedDimensions(mdClients.streamClient))
const dataset = $rdf.dataset([
...await getSharedDimensions(mdClients.streamClient),
])

// then
const termSets = [...dataset.match(null, rdf.type, schema.DefinedTermSet)].map(({ subject }) => subject)
Expand All @@ -28,10 +29,11 @@ describe('@cube-creator/shared-dimensions-api/lib/domain/shared-dimensions @SPAR

it('returns filtered by name', async () => {
// when
const dataset = await $rdf.dataset()
.import(await getSharedDimensions(mdClients.streamClient, {
const dataset = $rdf.dataset([
...await getSharedDimensions(mdClients.streamClient, {
freetextQuery: 'colors',
}))
}),
])

// then
const termSets = [...dataset.match(null, rdf.type, schema.DefinedTermSet)].map(({ subject }) => subject)
Expand Down

0 comments on commit b82ebab

Please sign in to comment.