1
|
package eu.dnetlib.repo.manager.service.customHystrixCommands;
|
2
|
|
3
|
import com.netflix.hystrix.HystrixCommand;
|
4
|
import com.netflix.hystrix.HystrixCommandGroupKey;
|
5
|
import org.springframework.http.HttpMethod;
|
6
|
import org.springframework.http.ResponseEntity;
|
7
|
import org.springframework.web.client.RestTemplate;
|
8
|
import org.springframework.web.util.UriComponents;
|
9
|
import org.springframework.web.util.UriComponentsBuilder;
|
10
|
|
11
|
import java.util.HashMap;
|
12
|
import java.util.List;
|
13
|
import java.util.Map;
|
14
|
|
15
|
public class LastYearUsageStatsHystrixCommand extends HystrixCommand<Map> {
|
16
|
|
17
|
RestTemplate restTemplate;
|
18
|
String usagestatsBaseAddress;
|
19
|
|
20
|
public LastYearUsageStatsHystrixCommand(String usagestatsBaseAddress,RestTemplate restTemplate) {
|
21
|
super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
|
22
|
this.usagestatsBaseAddress = usagestatsBaseAddress;
|
23
|
this.restTemplate = restTemplate;
|
24
|
}
|
25
|
|
26
|
@Override
|
27
|
protected Map run() throws Exception {
|
28
|
UriComponents uriComponents = UriComponentsBuilder
|
29
|
.fromHttpUrl(usagestatsBaseAddress + "/totals")
|
30
|
.build().encode();
|
31
|
|
32
|
ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,null,Map.class);
|
33
|
|
34
|
List yearly_stats = (List) ((Map)rs.getBody()).get("yearly_stats");
|
35
|
Map lastYear = (Map) yearly_stats.get(yearly_stats.size()-1);
|
36
|
Integer downloads = (Integer) lastYear.get("downloads");
|
37
|
Integer views = (Integer) lastYear.get("views");
|
38
|
Integer year = (Integer) lastYear.get("year");
|
39
|
|
40
|
Map<String,Object> usagestats = new HashMap<>();
|
41
|
usagestats.put("number",String.valueOf(downloads+views));
|
42
|
usagestats.put("year",year);
|
43
|
|
44
|
return usagestats;
|
45
|
}
|
46
|
|
47
|
@Override
|
48
|
protected Map getFallback() {
|
49
|
return null;
|
50
|
}
|
51
|
|
52
|
}
|