@ -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,8 @@ if (properties.get('ssl')) {
} else {
http = require ( "http" ) ;
}
var searchServiceAPIUrl = properties . get ( 'searchServiceAPIUrl' ) ;
var monitorServiceAPIUrl = properties . get ( 'monitorAPIUrl' ) ;
var auth = properties . get ( 'userInfoUrl' ) ;
/** @deprecated*/
var authDeprecated = auth . includes ( "accessToken" ) ;
@ -97,6 +100,181 @@ app.delete(['/delete/:filename', '/delete/stakeholder/:filename', '/delete/:type
} ) ;
} ) ;
app . get ( '/explore/home' , async function ( req , res ) {
try {
// Make requests to multiple APIs
let requests = [
searchServiceAPIUrl + 'publications/count?format=json' ,
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 + '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 + 'projects/?fields=funder&sf=funder&format=json&size=0' ,
searchServiceAPIUrl + 'resources?query=(%20(oaftype%20exact%20result)%20and%20(resulttypeid%20exact%20dataset)%20and%20(relresulttype%3Dpublication)%20%20)&page=0&size=0&format=json' ,
searchServiceAPIUrl + 'resources?query=(%20(oaftype%20exact%20result)%20and%20(resulttypeid%20exact%20software)%20and%20(relresulttype%3Dpublication)%20%20)&page=0&size=0&format=json'
] ;
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
const aggregatedData = {
publications : dataResponses [ 0 ] . data . total ,
datasets : dataResponses [ 1 ] . data . total ,
software : dataResponses [ 2 ] . data . total ,
other : dataResponses [ 3 ] . data . total ,
results : dataResponses [ 4 ] . data . meta . total ,
/*resultFunders:resultRES.data.refineResults.relfunder.length,*/
datasources : dataResponses [ 5 ] . data . total ,
organizations : dataResponses [ 6 ] . data . meta . total ,
projects : dataResponses [ 7 ] . data . meta . total ,
/*projectFunders:projectsRES.data.refineResults.funder.length,*/
funders : parseNoOfFunders ( dataResponses [ 4 ] . data , dataResponses [ 7 ] . data ) ,
datasetsInterlinked : dataResponses [ 8 ] . data . meta . total ,
softwareInterlinked : dataResponses [ 9 ] . data . meta . total ,
success : allRequestsSuccessful
} ;
// Send the aggregated data as the response
res . status ( allRequestsSuccessful ? 200 : 207 ) . 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 ) {
let aggregatedData = { } ;
try {
// Make requests to multiple APIs
let requests = [
searchServiceAPIUrl + 'resources2/?format=json&size=0&type=results' ,
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 + 'projects/count?format=json'
]
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 = {
results : dataResponses [ 0 ] . data . meta . total ,
datasources : dataResponses [ 1 ] . data . total ,
organizations : dataResponses [ 2 ] . data . meta . total ,
projects : 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 ) {
let aggregatedData = { } ;
try {
// Make requests to multiple APIs
let requests = [
searchServiceAPIUrl + 'resources2/?format=json&type=results&fq=relfunder=*&refine=true&fields=relfunder&sf=relfunder&page=0&size=0' ,
searchServiceAPIUrl + 'resources2/?format=json&type=results&fq=relfunder=*&refine=true&fields=relfunder&sf=relfunder&page=0&size=0&fq=resultbestaccessright%20exact%20%22Open%20Access%22' ,
searchServiceAPIUrl + 'resources2/?format=json&type=projects&fq=funder=*&fq=projecttitle<>"unidentified"&refine=true&fields=funder&sf=funder&page=0&size=0' ,
monitorServiceAPIUrl + 'stakeholder?type=funder' ,
]
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 ) ;
let fundersMap = new Map ( ) ;
let resultsFunders = dataResponses [ 0 ] . data . refineResults . relfunder ;
resultsFunders . forEach ( queriedFunder => {
if ( ! fundersMap . has ( queriedFunder . id ) ) {
fundersMap . set ( queriedFunder . id , { name : queriedFunder . name . split ( "||" ) [ 0 ] , id : queriedFunder . id , results : queriedFunder . count , openResults : null , projects : null , stakeholder : null } ) ;
}
} ) ;
let openResultsFunders = dataResponses [ 1 ] . data . refineResults . relfunder ;
openResultsFunders . forEach ( queriedFunder => {
if ( ! fundersMap . has ( queriedFunder . id ) ) {
fundersMap . set ( queriedFunder . id , { name : queriedFunder . name . split ( "||" ) [ 0 ] , id : queriedFunder . id , results : null , openResults : queriedFunder . count , projects : null , stakeholder : null } ) ;
} else {
fundersMap . get ( queriedFunder . id ) . openResults = queriedFunder . count ;
}
} ) ;
let projectFunders = dataResponses [ 2 ] . data . refineResults . funder ;
projectFunders . forEach ( queriedFunder => {
if ( ! fundersMap . has ( queriedFunder . id ) ) {
fundersMap . set ( queriedFunder . id , { name : queriedFunder . name . split ( "||" ) [ 0 ] , id : queriedFunder . id , results : null , openResults : null , projects : queriedFunder . count , stakeholder : null } ) ;
} else {
fundersMap . get ( queriedFunder . id ) . projects = queriedFunder . count ;
}
} ) ;
let stakeholders = dataResponses [ 3 ] . data ;
stakeholders . forEach ( stakeholder => {
let id = stakeholder . index _id + "||" + stakeholder . index _name + "||" + stakeholder . index _shortName ;
// console.log(id);
if ( fundersMap . has ( id ) ) {
let ministakeholder = { id : id , name : stakeholder . name , alias : stakeholder . alias , visibility : stakeholder . visibility ,
logoUrl : stakeholder . logoUrl , isUpload : stakeholder . isUpload }
fundersMap . get ( id ) . stakeholder = ministakeholder ;
}
} ) ;
// Combine the data
// Send the aggregated data as the response
// console.log(fundersMap)
aggregatedData = {
count : fundersMap . size ,
results : dataResponses [ 0 ] . data . meta . total ,
projects : dataResponses [ 2 ] . data . meta . total ,
funders : Array . from ( fundersMap . values ( ) ) ,
success : allRequestsSuccessful
} ;
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 ( '/grouped-requests' , async function ( req , res ) {
res . json ( [
"/explore/search" ,
"/explore/home" ,
"/explore/funders"
] ) ;
} ) ;
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 . refineResults . relfunder ;
queriedFunders . forEach ( queriedFunder => {
if ( ! mergedFundersSet . has ( queriedFunder . id ) ) {
mergedFundersSet . add ( queriedFunder . id ) ;
}
} ) ;
queriedFunders = projectsRES . 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 ) ;
} ) ;