Skip to content

Commit

Permalink
Merge pull request #35 from skohub-io/34-all-vocabs-endpoint
Browse files Browse the repository at this point in the history
34 all vocabs endpoint
  • Loading branch information
sroertgen committed Feb 15, 2024
2 parents 09f85c2 + 5fee730 commit eb891a6
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ The service currently suppports the following reconciliation services of the abo

Be aware that this is still in proof-of-concept phase.

- Additionally there is a `/ping` endpoint to check if the service is up.
## Additional Endpoints

- `/ping` to check if the service is up
- `/vocabs` to see all vocabularies and their service manifest urls

## Setup

Expand Down
3 changes: 2 additions & 1 deletion src/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import preview from "./preview/index.js";
import suggest from "./suggest/index.js";
import reconcile from "./reconcile/index.js";
import ping from "./ping/index.js";
import vocabs from "./vocabs/index.js"

export default { reconcile, query, preview, suggest, flyout, manifest, ping };
export default { reconcile, query, preview, suggest, flyout, manifest, ping, vocabs };
22 changes: 22 additions & 0 deletions src/controller/vocabs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import esQueries from "../../queries/index.js";
import { config } from "../../config.js";

const buildReconcileUrl = (serviceUrl, account, dataset, language) => `${serviceUrl}/reconcile?language=${language}&account=${account}&dataset=${dataset}`

export default async function vocabs(req, res) {
const combinations = await esQueries.getDatasetAccountCombination()
const manifestUrls = combinations.map(c => {
return {
account: c.account,
dataset: c.dataset,
manifestUrl: Object.fromEntries(
c.languages.map(l => ([
l, buildReconcileUrl(config.app_baseurl, c.account, c.dataset, l)
]))
)

}
})
res.json(manifestUrls)
return res
}
60 changes: 59 additions & 1 deletion src/queries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,62 @@ async function getDatasets() {
}
}

export default { query, queryID, suggest, getAccounts, getDatasets };
/**
* @returns {[{
* dataset: string,
* account: string,
* languages: [string]
* }]} Array of account dataset combination
*/
async function getDatasetAccountCombination() {
const combinations = []
const aggregation =
{
"size": 0,
"aggs": {
"dataset_account_combinations": {
"terms": {
"field": "dataset",
},
"aggs": {
"accounts": {
"terms": {
"field": "account",
},
"aggs": {
"languages": {
"scripted_metric": {
"init_script": "state.languages = [:]",
"map_script": "if (params._source.containsKey('prefLabel')) { for (entry in params._source.prefLabel.entrySet()) { state.languages.put(entry.getKey(), true) } }",
"combine_script": "return state",
"reduce_script": "def languages = [:]; for (state in states) { for (entry in state.languages.entrySet()) { languages.put(entry.getKey(), true) } } return languages.keySet()"
}
}
}
}
}
}
}
}
try {
const resp = await esClient.search({
index: index,
...aggregation,
});
resp.aggregations.dataset_account_combinations.buckets.forEach((dataset, _) => {

dataset.accounts.buckets.forEach(account => combinations.push({
dataset: dataset.key,
account: account.key,
languages: account.languages.value
}))
});
return combinations;
} catch (error) {
console.trace(error);
return [];
}

}

export default { query, queryID, suggest, getAccounts, getDatasets, getDatasetAccountCombination };
3 changes: 3 additions & 0 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ routes.route('/suggest').get(controller.suggest)
// GET: flyout
routes.route('/suggest/flyout').get(controller.flyout)

// GET: vocabs
routes.route('/vocabs').get(controller.vocabs)

export { routes }

0 comments on commit eb891a6

Please sign in to comment.