153 lines
5.5 KiB
Java
153 lines
5.5 KiB
Java
package eu.dnetlib.repo.manager.service.controllers;
|
|
|
|
import com.mongodb.util.JSON;
|
|
import org.apache.log4j.Logger;
|
|
import org.json.JSONArray;
|
|
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;
|
|
|
|
@Value("${search.api.usagestats}")
|
|
private String usagestatsBaseAddress;
|
|
|
|
@Value("${search.api.usageEvents}")
|
|
private String usagestatsEvents;
|
|
|
|
@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, Object> 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");
|
|
JSONObject lastYearUsagestats = getLastYearUsageStatsTotal();
|
|
Integer usagestats = getUsageStatsTotal();
|
|
|
|
|
|
HashMap<String,Object> 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);
|
|
|
|
stats.put("lastYearUsagestats",lastYearUsagestats.toString());
|
|
stats.put("usagestats",usagestats.toString());
|
|
|
|
|
|
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"));
|
|
|
|
}
|
|
|
|
private JSONObject getLastYearUsageStatsTotal() throws JSONException {
|
|
|
|
UriComponents uriComponents = UriComponentsBuilder
|
|
.fromHttpUrl(usagestatsBaseAddress + "/totals")
|
|
.build().encode();
|
|
|
|
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
|
|
|
JSONArray resultSet = (JSONArray) new JSONObject(rs).getJSONArray("yearly_stats");
|
|
JSONObject lastYear = resultSet.getJSONObject(resultSet.length()-1);
|
|
|
|
Integer downloads = lastYear.getInt("downloads");
|
|
Integer views = lastYear.getInt("views");
|
|
String year = lastYear.getString("year");
|
|
|
|
JSONObject usagestats = new JSONObject();
|
|
usagestats.put("number",String.valueOf(downloads+views));
|
|
usagestats.put("year",year);
|
|
|
|
return usagestats;
|
|
|
|
}
|
|
|
|
private Integer getUsageStatsTotal() throws JSONException {
|
|
UriComponents uriComponents = UriComponentsBuilder
|
|
.fromHttpUrl(usagestatsEvents)
|
|
.build().encode();
|
|
|
|
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
|
JSONObject resultSet = new JSONObject(rs);
|
|
JSONObject totals = resultSet.getJSONObject("totals");
|
|
return Integer.valueOf(totals.getString("events"));
|
|
}
|
|
}
|