1
|
package eu.dnetlib.repo.manager.service.controllers;
|
2
|
|
3
|
import org.apache.log4j.Logger;
|
4
|
import org.json.JSONException;
|
5
|
import org.json.JSONObject;
|
6
|
import org.springframework.beans.factory.annotation.Value;
|
7
|
import org.springframework.http.HttpHeaders;
|
8
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
9
|
import org.springframework.stereotype.Component;
|
10
|
import org.springframework.web.client.RestTemplate;
|
11
|
import org.springframework.web.util.UriComponents;
|
12
|
import org.springframework.web.util.UriComponentsBuilder;
|
13
|
|
14
|
import javax.annotation.PostConstruct;
|
15
|
import java.util.HashMap;
|
16
|
import java.util.Map;
|
17
|
|
18
|
@Component
|
19
|
public class StatsApiImpl implements StatsApi {
|
20
|
|
21
|
private RestTemplate restTemplate = null;
|
22
|
|
23
|
private HttpHeaders httpHeaders;
|
24
|
|
25
|
private static final Logger LOGGER = Logger.getLogger(RepositoryApiImpl.class);
|
26
|
|
27
|
@Value("${search.api.baseAddress}")
|
28
|
private String baseAddress;
|
29
|
|
30
|
@PostConstruct
|
31
|
private void init() {
|
32
|
LOGGER.debug("Initialization method of statistics api!");
|
33
|
|
34
|
restTemplate = new RestTemplate();
|
35
|
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
36
|
|
37
|
httpHeaders = new HttpHeaders();
|
38
|
httpHeaders.set("Content-Type", "application/json");
|
39
|
}
|
40
|
|
41
|
|
42
|
@Override
|
43
|
public Map<String, String> getStatistics() throws JSONException {
|
44
|
|
45
|
|
46
|
String aggregators = getTotalByType("datasource",baseAddress+"/resources",
|
47
|
"?query= " +
|
48
|
" oaftype exact datasource and " +
|
49
|
" ( datasourcetypename exact Institutional Repository Aggregator " +
|
50
|
" or datasourcetypename exact Publication Repository Aggregator )");
|
51
|
|
52
|
String dataRepositories = getTotalByType("datasource",baseAddress+"/resources",
|
53
|
"?query= " +
|
54
|
" oaftype exact datasource and " +
|
55
|
" datasourcetypename exact Data Repository " );
|
56
|
|
57
|
String literature = getTotalByType("datasource",baseAddress+"/resources",
|
58
|
"?query= " +
|
59
|
" oaftype exact datasource and " +
|
60
|
" ( datasourcetypename exact Institutional Repository " +
|
61
|
" or datasourcetypename exact Publication Repository )");
|
62
|
|
63
|
String journal = getTotalByType("datasource",baseAddress+"/resources",
|
64
|
"?query= " +
|
65
|
" oaftype exact datasource and " +
|
66
|
" datasourcetypename exact Journal");
|
67
|
|
68
|
String publications = getTotalByType("other",baseAddress,"/publications/count");
|
69
|
String datasets = getTotalByType("other",baseAddress,"/datasets/count");
|
70
|
String software = getTotalByType("other",baseAddress,"/software/count");
|
71
|
|
72
|
|
73
|
HashMap<String,String> stats = new HashMap<>();
|
74
|
stats.put("aggregators",aggregators);
|
75
|
stats.put("dataRepositories",dataRepositories);
|
76
|
stats.put("literature",literature);
|
77
|
stats.put("journal",journal);
|
78
|
|
79
|
stats.put("publications",publications);
|
80
|
stats.put("datasets",datasets);
|
81
|
stats.put("software",software);
|
82
|
|
83
|
|
84
|
return stats;
|
85
|
}
|
86
|
|
87
|
|
88
|
private String getTotalByType(String type,String url,String query) throws JSONException {
|
89
|
UriComponents uriComponents = UriComponentsBuilder
|
90
|
.fromHttpUrl(url + query)
|
91
|
.queryParam("page",0)
|
92
|
.queryParam("size",0)
|
93
|
.queryParam("format","json")
|
94
|
.build().encode();
|
95
|
|
96
|
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
97
|
|
98
|
if(type.equalsIgnoreCase("datasource")){
|
99
|
JSONObject metadata = (JSONObject) new JSONObject(rs).get("meta");
|
100
|
return String.valueOf(metadata.get("total"));
|
101
|
}else
|
102
|
return String.valueOf(new JSONObject(rs).get("total"));
|
103
|
|
104
|
}
|
105
|
}
|