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