package eu.dnetlib.repo.manager.service.customHystrixCommands; import com.netflix.hystrix.HystrixCommand; import com.netflix.hystrix.HystrixCommandGroupKey; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponentsBuilder; import java.util.HashMap; import java.util.List; import java.util.Map; public class LastYearUsageStatsHystrixCommand extends HystrixCommand { RestTemplate restTemplate; String usagestatsBaseAddress; public LastYearUsageStatsHystrixCommand(String usagestatsBaseAddress,RestTemplate restTemplate) { super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup")); this.usagestatsBaseAddress = usagestatsBaseAddress; this.restTemplate = restTemplate; } @Override protected Map run() throws Exception { UriComponents uriComponents = UriComponentsBuilder .fromHttpUrl(usagestatsBaseAddress + "/totals") .build().encode(); ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,null,Map.class); List yearly_stats = (List) ((Map)rs.getBody()).get("yearly_stats"); Map lastYear = (Map) yearly_stats.get(yearly_stats.size()-1); Integer downloads = (Integer) lastYear.get("downloads"); Integer views = (Integer) lastYear.get("views"); Integer year = (Integer) lastYear.get("year"); Map usagestats = new HashMap<>(); usagestats.put("number",String.valueOf(downloads+views)); usagestats.put("year",year); return usagestats; } @Override protected Map getFallback() { return null; } }