1
|
package eu.dnetlib.repo.manager.service;
|
2
|
|
3
|
import eu.dnetlib.repo.manager.controllers.RestTemplateResponseErrorHandler;
|
4
|
import eu.dnetlib.repo.manager.exception.EndPointException;
|
5
|
import org.apache.log4j.Logger;
|
6
|
import org.json.JSONArray;
|
7
|
import org.json.JSONException;
|
8
|
import org.json.JSONObject;
|
9
|
import org.springframework.beans.factory.annotation.Autowired;
|
10
|
import org.springframework.beans.factory.annotation.Value;
|
11
|
import org.springframework.http.HttpHeaders;
|
12
|
import org.springframework.http.HttpMethod;
|
13
|
import org.springframework.http.HttpStatus;
|
14
|
import org.springframework.http.ResponseEntity;
|
15
|
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
16
|
import org.springframework.stereotype.Service;
|
17
|
import org.springframework.web.client.RestTemplate;
|
18
|
import org.springframework.web.util.UriComponents;
|
19
|
import org.springframework.web.util.UriComponentsBuilder;
|
20
|
|
21
|
import javax.annotation.PostConstruct;
|
22
|
import java.util.HashMap;
|
23
|
import java.util.List;
|
24
|
import java.util.Map;
|
25
|
|
26
|
@Service("statsService")
|
27
|
public class StatsServiceImpl implements StatsService {
|
28
|
|
29
|
private RestTemplate restTemplate = null;
|
30
|
|
31
|
private HttpHeaders httpHeaders;
|
32
|
|
33
|
private static final Logger LOGGER = Logger.getLogger(RepositoryServiceImpl.class);
|
34
|
|
35
|
@Value("${search.api.baseAddress}")
|
36
|
private String baseAddress;
|
37
|
|
38
|
@Value("${search.api.usagestats}")
|
39
|
private String usagestatsBaseAddress;
|
40
|
|
41
|
@Value("${search.api.usageEvents}")
|
42
|
private String usagestatsEvents;
|
43
|
|
44
|
@Autowired
|
45
|
RestTemplateResponseErrorHandler restTemplateResponseErrorHandler;
|
46
|
|
47
|
@PostConstruct
|
48
|
private void init() {
|
49
|
LOGGER.debug("Initialization method of statistics api!");
|
50
|
restTemplate = new RestTemplate();
|
51
|
restTemplate.setErrorHandler(restTemplateResponseErrorHandler);
|
52
|
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
|
53
|
httpHeaders = new HttpHeaders();
|
54
|
httpHeaders.set("Content-Type", "application/json");
|
55
|
}
|
56
|
|
57
|
|
58
|
@Override
|
59
|
public Map<String, Object> getStatistics() throws JSONException {
|
60
|
|
61
|
String aggregators = getTotalByType("datasource",baseAddress+"/resources",
|
62
|
"?query= " +
|
63
|
" oaftype exact datasource and " +
|
64
|
" ( datasourcetypename exact Institutional Repository Aggregator " +
|
65
|
" or datasourcetypename exact Publication Repository Aggregator )");
|
66
|
|
67
|
String dataRepositories = getTotalByType("datasource",baseAddress+"/resources",
|
68
|
"?query= " +
|
69
|
" oaftype exact datasource and " +
|
70
|
" datasourcetypename exact Data Repository " );
|
71
|
|
72
|
String literature = getTotalByType("datasource",baseAddress+"/resources",
|
73
|
"?query= " +
|
74
|
" oaftype exact datasource and " +
|
75
|
" ( datasourcetypename exact Institutional Repository " +
|
76
|
" or datasourcetypename exact Publication Repository )");
|
77
|
|
78
|
String journal = getTotalByType("datasource",baseAddress+"/resources",
|
79
|
"?query= " +
|
80
|
" oaftype exact datasource and " +
|
81
|
" datasourcetypename exact Journal");
|
82
|
|
83
|
String publications = getTotalByType("other",baseAddress,"/publications/count");
|
84
|
String datasets = getTotalByType("other",baseAddress,"/datasets/count");
|
85
|
String software = getTotalByType("other",baseAddress,"/software/count");
|
86
|
|
87
|
|
88
|
Map lastYearUsagestats = getLastYearUsageStatsTotal();
|
89
|
Map<String,Object> stats = new HashMap<>();
|
90
|
stats.put("aggregators",aggregators);
|
91
|
stats.put("dataRepositories",dataRepositories);
|
92
|
stats.put("literature",literature);
|
93
|
stats.put("journal",journal);
|
94
|
stats.put("publications",publications);
|
95
|
stats.put("datasets",datasets);
|
96
|
stats.put("software",software);
|
97
|
stats.put("lastYearUsagestats", lastYearUsagestats != null ? lastYearUsagestats.toString() : null);
|
98
|
|
99
|
Integer usagestats = getUsageStatsTotal();
|
100
|
stats.put("usagestats",usagestats);
|
101
|
return stats;
|
102
|
}
|
103
|
|
104
|
|
105
|
private String getTotalByType(String type,String url,String query) {
|
106
|
UriComponents uriComponents = UriComponentsBuilder
|
107
|
.fromHttpUrl(url + query)
|
108
|
.queryParam("page",0)
|
109
|
.queryParam("size",0)
|
110
|
.queryParam("format","json")
|
111
|
.build().encode();
|
112
|
|
113
|
ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,null,Map.class);
|
114
|
|
115
|
// if(rs.getStatusCode().equals(HttpStatus.OK)){
|
116
|
if(type.equalsIgnoreCase("datasource")){
|
117
|
Map metadata = (Map) ((Map)rs.getBody()).get("meta");
|
118
|
return String.valueOf(metadata.get("total"));
|
119
|
}else {
|
120
|
Map metadata = (Map) (rs.getBody());
|
121
|
return String.valueOf(metadata.get("total"));
|
122
|
}
|
123
|
//}
|
124
|
// return null;
|
125
|
}
|
126
|
|
127
|
private Map getLastYearUsageStatsTotal() {
|
128
|
|
129
|
UriComponents uriComponents = UriComponentsBuilder
|
130
|
.fromHttpUrl(usagestatsBaseAddress + "/totals")
|
131
|
.build().encode();
|
132
|
|
133
|
ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,null,Map.class);
|
134
|
|
135
|
// if(rs.getStatusCode().equals(HttpStatus.OK)){
|
136
|
|
137
|
List yearly_stats = (List) ((Map)rs.getBody()).get("yearly_stats");
|
138
|
Map lastYear = (Map) yearly_stats.get(yearly_stats.size()-1);
|
139
|
Integer downloads = (Integer) lastYear.get("downloads");
|
140
|
Integer views = (Integer) lastYear.get("views");
|
141
|
Integer year = (Integer) lastYear.get("year");
|
142
|
|
143
|
Map<String,Object> usagestats = new HashMap<>();
|
144
|
usagestats.put("number",String.valueOf(downloads+views));
|
145
|
usagestats.put("year",year);
|
146
|
|
147
|
return usagestats;
|
148
|
//}
|
149
|
}
|
150
|
|
151
|
private Integer getUsageStatsTotal() throws JSONException {
|
152
|
UriComponents uriComponents = UriComponentsBuilder
|
153
|
.fromHttpUrl(usagestatsEvents)
|
154
|
.build().encode();
|
155
|
|
156
|
// ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,null,Map.class);
|
157
|
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
|
158
|
JSONObject resultSet = new JSONObject(rs);
|
159
|
JSONObject totals = resultSet.getJSONObject("totals");
|
160
|
return Integer.valueOf(totals.getString("events"));
|
161
|
}
|
162
|
}
|