[ develop | DONE | ADDED] Utils servise: add a method to get counts for results, use the same method in /explore/home

This commit is contained in:
argirok 2024-02-20 11:39:49 +02:00
parent 178846353c
commit c81c6196c0
1 changed files with 54 additions and 18 deletions

View File

@ -104,10 +104,7 @@ app.get('/explore/home', async function (req, res) {
try { try {
// Make requests to multiple APIs // Make requests to multiple APIs
let requests= [ let requests= [
searchServiceAPIUrl + 'publications/count?format=json', "http://localhost:" + properties.get('port') + "/portals/countResults",
searchServiceAPIUrl + 'datasets/count?format=json',
searchServiceAPIUrl + 'software/count?format=json',
searchServiceAPIUrl + 'other/count?format=json',
searchServiceAPIUrl +'results/?fields=relfunder&sf=relfunder&format=json&size=0', searchServiceAPIUrl +'results/?fields=relfunder&sf=relfunder&format=json&size=0',
searchServiceAPIUrl + 'datasources/count?format=json', searchServiceAPIUrl + 'datasources/count?format=json',
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=*)', 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=*)',
@ -121,26 +118,24 @@ app.get('/explore/home', async function (req, res) {
const allRequestsSuccessful = dataResponses.every((response) => response.status === 200); const allRequestsSuccessful = dataResponses.every((response) => response.status === 200);
// Combine the data // Combine the data
const aggregatedData = { const aggregatedData = {
publications: dataResponses[0].data.total, publications: dataResponses[0].data.publications,
datasets: dataResponses[1].data.total, datasets: dataResponses[0].data.datasets,
software: dataResponses[2].data.total, software: dataResponses[0].data.software,
other: dataResponses[3].data.total, other: dataResponses[0].data.other,
results: dataResponses[4].data.meta.total, results: dataResponses[1].data.meta.total,
/*resultFunders:resultRES.data.refineResults.relfunder.length,*/ datasources: dataResponses[2].data.total,
datasources: dataResponses[5].data.total, organizations: dataResponses[3].data.meta.total,
organizations: dataResponses[6].data.meta.total, projects:dataResponses[4].data.meta.total,
projects:dataResponses[7].data.meta.total, funders: parseNoOfFunders(dataResponses[1].data, dataResponses[4].data),
/*projectFunders:projectsRES.data.refineResults.funder.length,*/ datasetsInterlinked:dataResponses[5].data.meta.total,
funders: parseNoOfFunders(dataResponses[4].data, dataResponses[7].data), softwareInterlinked:dataResponses[6].data.meta.total,
datasetsInterlinked:dataResponses[8].data.meta.total,
softwareInterlinked:dataResponses[9].data.meta.total,
success:allRequestsSuccessful success:allRequestsSuccessful
}; };
// Send the aggregated data as the response // Send the aggregated data as the response
res.status(allRequestsSuccessful?200:207).json(aggregatedData); res.status(allRequestsSuccessful?200:207).json(aggregatedData);
} catch (error) { } catch (error) {
console.error('Error fetching data:', error.message); console.error('Error fetching data:', error);
res.status(500).send('Internal Server Error'); res.status(500).send('Internal Server Error');
} }
}); });
@ -178,6 +173,47 @@ app.get('/explore/search', async function (req, res) {
} }
}); });
app.get('/portals/countResults', async function (req, res) {
const field = req.query.field;
let allowedFields = ["communityid"];
if(field && allowedFields.indexOf(field) ==-1){
res.status(405).send('Not allowed request');
return;
}
const value = req.query.value;
const params = field && value?(encodeURI("&fq=" + field + "=" + value)):"";
let aggregatedData = {};
try {
// Make requests to multiple APIs
let requests= [
searchServiceAPIUrl +'publications/count?format=json' + params,
searchServiceAPIUrl +'datasets/count?format=json' + params,
searchServiceAPIUrl +'software/count?format=json' + params,
searchServiceAPIUrl +'other/count?format=json' + params
]
const dataPromises = requests.map((url) => axios.get( url));
const dataResponses = await Promise.all(dataPromises);
// Determine if all additional requests were successful
const allRequestsSuccessful = dataResponses.every((response) => response.status === 200);
// Combine the data
aggregatedData = {
publications: dataResponses[0].data.total,
datasets: dataResponses[1].data.total,
software: dataResponses[2].data.total,
other:dataResponses[3].data.total,
success:allRequestsSuccessful
};
// Send the aggregated data as the response
res.status(allRequestsSuccessful?200:207).json(aggregatedData);
} catch (error) {
console.log(aggregatedData)
console.error('Error fetching data:', error);
res.status(500).send('Internal Server Error');
}
});
app.get('/explore/funders', async function (req, res) { app.get('/explore/funders', async function (req, res) {
let aggregatedData = {}; let aggregatedData = {};
try { try {