106 lines
3.8 KiB
Java
106 lines
3.8 KiB
Java
package eu.dnetlib.repo.manager.service.controllers;
|
|
|
|
import org.apache.log4j.Logger;
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.client.RestTemplate;
|
|
import org.springframework.web.util.UriComponents;
|
|
import org.springframework.web.util.UriComponentsBuilder;
|
|
|
|
import javax.annotation.PostConstruct;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
@Component
|
|
public class StatsApiImpl implements StatsApi {
|
|
|
|
private RestTemplate restTemplate = null;
|
|
|
|
private HttpHeaders httpHeaders;
|
|
|
|
private static final Logger LOGGER = Logger.getLogger(RepositoryApiImpl.class);
|
|
|
|
@Value("${search.api.baseAddress}")
|
|
private String baseAddress;
|
|
|
|
@PostConstruct
|
|
private void init() {
|
|
LOGGER.debug("Initialization method of statistics api!");
|
|
|
|
restTemplate = new RestTemplate();
|
|
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
|
|
|
httpHeaders = new HttpHeaders();
|
|
httpHeaders.set("Content-Type", "application/json");
|
|
}
|
|
|
|
|
|
@Override
|
|
public Map<String, String> getStatistics() throws JSONException {
|
|
|
|
|
|
String aggregators = getTotalByType("datasource",baseAddress+"/resources",
|
|
"?query= " +
|
|
" oaftype exact datasource and " +
|
|
" ( datasourcetypename exact Institutional Repository Aggregator " +
|
|
" or datasourcetypename exact Publication Repository Aggregator )");
|
|
|
|
String dataRepositories = getTotalByType("datasource",baseAddress+"/resources",
|
|
"?query= " +
|
|
" oaftype exact datasource and " +
|
|
" datasourcetypename exact Data Repository " );
|
|
|
|
String literature = getTotalByType("datasource",baseAddress+"/resources",
|
|
"?query= " +
|
|
" oaftype exact datasource and " +
|
|
" ( datasourcetypename exact Institutional Repository " +
|
|
" or datasourcetypename exact Publication Repository )");
|
|
|
|
String journal = getTotalByType("datasource",baseAddress+"/resources",
|
|
"?query= " +
|
|
" oaftype exact datasource and " +
|
|
" datasourcetypename exact Journal");
|
|
|
|
String publications = getTotalByType("other",baseAddress,"/publications/count");
|
|
String datasets = getTotalByType("other",baseAddress,"/datasets/count");
|
|
String software = getTotalByType("other",baseAddress,"/software/count");
|
|
|
|
|
|
HashMap<String,String> stats = new HashMap<>();
|
|
stats.put("aggregators",aggregators);
|
|
stats.put("dataRepositories",dataRepositories);
|
|
stats.put("literature",literature);
|
|
stats.put("journal",journal);
|
|
|
|
stats.put("publications",publications);
|
|
stats.put("datasets",datasets);
|
|
stats.put("software",software);
|
|
|
|
|
|
return stats;
|
|
}
|
|
|
|
|
|
private String getTotalByType(String type,String url,String query) throws JSONException {
|
|
UriComponents uriComponents = UriComponentsBuilder
|
|
.fromHttpUrl(url + query)
|
|
.queryParam("page",0)
|
|
.queryParam("size",0)
|
|
.queryParam("format","json")
|
|
.build().encode();
|
|
|
|
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
|
|
|
if(type.equalsIgnoreCase("datasource")){
|
|
JSONObject metadata = (JSONObject) new JSONObject(rs).get("meta");
|
|
return String.valueOf(metadata.get("total"));
|
|
}else
|
|
return String.valueOf(new JSONObject(rs).get("total"));
|
|
|
|
}
|
|
}
|