Skip to content
Snippets Groups Projects
Commit 2895cadc authored by Mathieu Massaviol's avatar Mathieu Massaviol
Browse files

Improve getDistribution query #68

parent 2ead4efd
Branches
No related tags found
1 merge request!79Resolve "Investiguer / Optimiser les temps de réponse des requêtes Virtuoso"
...@@ -14,6 +14,7 @@ import { ...@@ -14,6 +14,7 @@ import {
executeSparqlConstruct executeSparqlConstruct
} from './sparql' } from './sparql'
import { resourceContext } from './resource' import { resourceContext } from './resource'
import { ContextDefinition } from 'jsonld'
export async function insertDistribution( export async function insertDistribution(
distribution: Partial<OcDistribution>, distribution: Partial<OcDistribution>,
...@@ -124,104 +125,74 @@ export async function getDistribution( ...@@ -124,104 +125,74 @@ export async function getDistribution(
identifier: string, identifier: string,
auth?: Credentials auth?: Credentials
): Promise<OcDistribution> { ): Promise<OcDistribution> {
/**
* First, get the @id of the catalog
* to better filtering requests.
*/
const distributionIdResponse = await executeSparqlConstruct<OcDistribution>(
`
CONSTRUCT {
?distribution ?p ?o.
?distribution oct:graph ?g.
}
WHERE {
?distribution ?p ?o;
a dcat:Distribution.
GRAPH ?g { const context: ContextDefinition = {
?distribution dct:identifier ?identifier. ...resourceContext,
} accessURL: { '@id': 'http://www.w3.org/ns/dcat#accessURL', '@type': '@id' },
FILTER (str(?identifier) = "${identifier}"). license: { '@id': 'http://purl.org/dc/terms/license', '@type': '@id' },
type: { '@id': 'http://purl.org/dc/terms/type', '@type': '@id' },
accessRights: { '@id': 'http://purl.org/dc/terms/accessRights', '@type': '@id' },
format: { '@id': 'http://purl.org/dc/terms/format', '@type': '@id' },
label: {
'@id': 'http://www.w3.org/2004/02/skos/core#label',
'@container': '@language'
},
prefLabel: {
'@id': 'http://www.w3.org/2004/02/skos/core#prefLabel',
'@container': '@language'
} }
}
`,
{ const frame = {
auth, '@context': context,
context: { '@type': [
...resourceContext, 'http://www.w3.org/ns/dcat#Distribution',
accessURL: { '@id': 'http://www.w3.org/ns/dcat#accessURL', '@type': '@id' }, ],
license: { '@id': 'http://purl.org/dc/terms/license', '@type': '@id' }, contains: {
type: { '@id': 'http://purl.org/dc/terms/type', '@type': '@id' }, accessRights: {
accessRights: { '@id': 'http://purl.org/dc/terms/accessRights', '@type': '@id' }, '@embed': '@always',
format: { '@id': 'http://purl.org/dc/terms/format', '@type': '@id' } },
type: {
'@embed': '@always'
},
format: {
'@embed': '@always'
},
license: {
'@embed': '@always'
} }
} }
) }
const distribution = distributionIdResponse?.[0]
const distributionUri = distribution['@id']
/** const distributionResponse = await executeSparqlConstruct<OcDistribution>(
* access rights, License, type, access URL and format
*/
const distributionRelatedResponse = await executeSparqlConstruct<OcConcept>(
` `
CONSTRUCT { CONSTRUCT {
?accessRights ?p1 ?o1. ?distribution ?p ?o.
?license ?p2 ?o2. ?distribution oct:graph ?g.
?type ?p3 ?o3. ?o2 ?p3 ?o3.
?format ?p5 ?o5.
} }
WHERE { WHERE {
OPTIONAL { GRAPH ?g {
<${distributionUri}> dct:accessRights ?accessRights. ?distribution a dcat:Distribution;
?accessRights ?p1 ?o1. dct:identifier ?identifier.
} FILTER (str(?identifier) = "${identifier}")
OPTIONAL {
<${distributionUri}> dct:license ?license.
?license ?p2 ?o2.
}
OPTIONAL {
<${distributionUri}> dct:type ?type.
?type ?p3 ?o3.
}
OPTIONAL {
<${distributionUri}> dct:format ?format.
?format ?p5 ?o5.
} }
?distribution ?p ?o.
VALUES ?p2 { dct:accessRights dct:license dct:type dct:format }
?distribution ?p2 ?o2.
?o2 ?p3 ?o3.
} }
`, `,
{ {
auth, auth,
context: { context: context,
...resourceContext, frame: frame
label: {
'@id': 'http://www.w3.org/2004/02/skos/core#label',
'@container': '@language'
},
prefLabel: {
'@id': 'http://www.w3.org/2004/02/skos/core#prefLabel',
'@container': '@language'
}
}
} }
) )
const distribution = distributionResponse?.[0]
const distributionResponse = await Promise.all([
distributionRelatedResponse // 0
])
distribution.license = distributionResponse[0].find(
(d) => d['@id'] === (distribution.license as unknown as string)
)
distribution.type = distributionResponse[0].find(
(d) => d['@id'] === (distribution.type as unknown as string)
)
distribution.accessRights = distributionResponse[0].find(
(d) => d['@id'] === (distribution.accessRights as unknown as string)
)
distribution.format = distributionResponse[0].find(
(d) => d['@id'] === (distribution.format as unknown as string)
)
return distribution return distribution
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment