From a9d06e61fab65851a7b40c539c2ebaacf8581b5f Mon Sep 17 00:00:00 2001 From: argirok Date: Mon, 5 Feb 2024 12:07:34 +0200 Subject: [PATCH] [ develop | DONE | ADDED] Utils services: group common requests in a single request --- services/utils-service/beta-properties.file | 1 + services/utils-service/package.json | 1 + .../utils-service/production-properties.file | 1 + services/utils-service/properties.file | 1 + services/utils-service/uploadService.js | 93 +++++++++++++++++++ 5 files changed, 97 insertions(+) diff --git a/services/utils-service/beta-properties.file b/services/utils-service/beta-properties.file index 7e37b386..1a473047 100644 --- a/services/utils-service/beta-properties.file +++ b/services/utils-service/beta-properties.file @@ -1,4 +1,5 @@ userInfoUrl = https://beta.services.openaire.eu/login-service/userInfo +searchServiceAPIUrl = https://beta.services.openaire.eu/search/v2/api/ ssl = true localPath = false # photo size in KB diff --git a/services/utils-service/package.json b/services/utils-service/package.json index 5ef016df..b816d04a 100644 --- a/services/utils-service/package.json +++ b/services/utils-service/package.json @@ -13,6 +13,7 @@ "author": "", "license": "ISC", "dependencies": { + "axios": "^1.6.7", "body-parser": "^1.20.2", "cookie-parser": "^1.4.6", "cors": "^2.8.5", diff --git a/services/utils-service/production-properties.file b/services/utils-service/production-properties.file index 93f676c1..c2f029a3 100644 --- a/services/utils-service/production-properties.file +++ b/services/utils-service/production-properties.file @@ -1,4 +1,5 @@ userInfoUrl = https://services.openaire.eu/login-service/userInfo +searchServiceAPIUrl = https://services.openaire.eu/search/v2/api/ ssl = true localPath = false # photo size in KB diff --git a/services/utils-service/properties.file b/services/utils-service/properties.file index 4f43d8fc..fd1565a4 100644 --- a/services/utils-service/properties.file +++ b/services/utils-service/properties.file @@ -1,4 +1,5 @@ userInfoUrl = http://mpagasas.di.uoa.gr:19080/login-service/userInfo +searchServiceAPIUrl = https://beta.services.openaire.eu/search/v2/api/ ssl = false localPath = true # photo size in KB diff --git a/services/utils-service/uploadService.js b/services/utils-service/uploadService.js index c929c2e2..ab5e7de6 100644 --- a/services/utils-service/uploadService.js +++ b/services/utils-service/uploadService.js @@ -1,3 +1,4 @@ +const axios = require('axios'); var express = require("express"); var bodyParser = require("body-parser"); var cookieParser = require('cookie-parser'); @@ -12,6 +13,7 @@ if (properties.get('ssl')) { } else { http = require("http"); } +var searchServiceAPIUrl = properties.get('searchServiceAPIUrl'); var auth = properties.get('userInfoUrl'); /** @deprecated*/ var authDeprecated = auth.includes("accessToken"); @@ -97,6 +99,97 @@ app.delete(['/delete/:filename', '/delete/stakeholder/:filename', '/delete/:type }); }); +app.get('/explore/home', async function (req, res) { + try { + // Make requests to multiple APIs + const publicationsRES = await axios.get(searchServiceAPIUrl + 'publications/count?format=json'); + const datasetsRES = await axios.get(searchServiceAPIUrl + 'datasets/count?format=json'); + const softwareRES = await axios.get(searchServiceAPIUrl + 'software/count?format=json'); + const otherRES = await axios.get(searchServiceAPIUrl + 'other/count?format=json'); + const resultRES = await axios.get(searchServiceAPIUrl +'results/?fields=relfunder&sf=relfunder&format=json&size=0'); + const datasourcesRES = await axios.get(searchServiceAPIUrl + 'datasources/count?format=json'); + const organizationsRES = await axios.get(searchServiceAPIUrl + 'resources2/?format=json&size=0&type=organizations&fq=(reldatasourcecompatibilityid exact driver or reldatasourcecompatibilityid exact driver-openaire2.0 or reldatasourcecompatibilityid exact openaire2.0 or reldatasourcecompatibilityid exact openaire3.0 or reldatasourcecompatibilityid exact openaire4.0 or reldatasourcecompatibilityid exact openaire-cris_1.1 or reldatasourcecompatibilityid exact openaire2.0_data or reldatasourcecompatibilityid exact hostedBy or relproject=*)'); + const projectsRES = await axios.get(searchServiceAPIUrl + 'projects/?fields=funder&sf=funder&format=json&size=0'); + const datasetsInterlinkedRES = await axios.get(searchServiceAPIUrl + 'resources?query=(%20(oaftype%20exact%20result)%20and%20(resulttypeid%20exact%20dataset)%20and%20(relresulttype%3Dpublication)%20%20)&page=0&size=0&format=json'); + const softwareInterlinkedRES = await axios.get(searchServiceAPIUrl + 'resources?query=(%20(oaftype%20exact%20result)%20and%20(resulttypeid%20exact%20software)%20and%20(relresulttype%3Dpublication)%20%20)&page=0&size=0&format=json'); + + // Combine the data + const aggregatedData = { + publications: publicationsRES.data.total, + datasets: datasetsRES.data.total, + software: softwareRES.data.total, + other: otherRES.data.total, + results: resultRES.data.meta.total, + /*resultFunders:resultRES.data.refineResults.relfunder.length,*/ + datasources: datasourcesRES.data.total, + organizations: organizationsRES.data.meta.total?organizationsRES.data.meta.total:'--', + projects:projectsRES.data.meta.total, + /*projectFunders:projectsRES.data.refineResults.funder.length,*/ + funders: parseNoOfFunders(resultRES, projectsRES), + datasetsInterlinked:datasetsInterlinkedRES.data.meta.total, + softwareInterlinked:softwareInterlinkedRES.data.meta.total, + + }; + + // Send the aggregated data as the response + res.json(aggregatedData); + } catch (error) { + console.error('Error fetching data:', error.message); + res.status(500).send('Internal Server Error'); + } +}); + +app.get('/explore/search', async function (req, res) { + try { + // Make requests to multiple APIs + const resultRES = await axios.get(searchServiceAPIUrl +'resources2/?format=json&size=0&type=results&fq=resultbestaccessright%20exact%20%22Open%20Access%22'); + const datasourcesRES = await axios.get(searchServiceAPIUrl + 'datasources/count?format=json'); + const organizationsRES = await axios.get(searchServiceAPIUrl + 'resources2/?format=json&size=0&type=organizations&fq=(reldatasourcecompatibilityid exact driver or reldatasourcecompatibilityid exact driver-openaire2.0 or reldatasourcecompatibilityid exact openaire2.0 or reldatasourcecompatibilityid exact openaire3.0 or reldatasourcecompatibilityid exact openaire4.0 or reldatasourcecompatibilityid exact openaire-cris_1.1 or reldatasourcecompatibilityid exact openaire2.0_data or reldatasourcecompatibilityid exact hostedBy or relproject=*)'); + const projectsRES = await axios.get(searchServiceAPIUrl + 'projects/count?format=json'); + + // Combine the data + const aggregatedData = { + results: resultRES.data.meta.total, + datasources: datasourcesRES.data.total, + organizations: organizationsRES.data.meta.total?organizationsRES.data.meta.total:'--', + projects:projectsRES.data.total + + }; + + // Send the aggregated data as the response + res.json(aggregatedData); + } catch (error) { + console.error('Error fetching data:', error.message); + res.status(500).send('Internal Server Error'); + } +}); +app.get('/grouped-requests', async function (req, res) { + res.json([ + "/explore/search", + "/explore/home" + ]); +}); +function parseNoOfFunders(resultRES, projectsRES){ + // combines the refines qeries on funders field, the funders with results and the funders that have at least one project + let mergedFundersSet = new Set(); + let queriedFunders = resultRES.data.refineResults.relfunder; + queriedFunders.forEach(queriedFunder => { + if (!mergedFundersSet.has(queriedFunder.id)) { + mergedFundersSet.add(queriedFunder.id); + } + }); + queriedFunders = projectsRES.data.refineResults.funder; + queriedFunders.forEach(queriedFunder => { + if(+queriedFunder.count > 1) { + if (!mergedFundersSet.has(queriedFunder.id)) { + mergedFundersSet.add(queriedFunder.id); + } + } + }); + return mergedFundersSet.size; +} + + const server = app.listen(properties.get('port'), function () { console.log("Listening on port %s...", server.address().port); });