Add hystrix configuration for rest statistics calls

This commit is contained in:
Panagiotis Kanakakis 2019-02-16 00:01:58 +00:00
parent 76cd57d035
commit 0a57ede298
18 changed files with 478 additions and 151 deletions

View File

@ -240,6 +240,12 @@
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.1.5.RELEASE</version>
</dependency>
</dependencies>
<build>

View File

@ -2,20 +2,22 @@ package eu.dnetlib.repo.manager.config;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.context.annotation.*;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;
import org.springframework.web.client.RestTemplate;
import javax.annotation.PostConstruct;
@Configuration
@EnableRedisHttpSession
@EnableAspectJAutoProxy
@EnableCircuitBreaker
@PropertySource(value = {"classpath:application.properties"} )
@ComponentScan(basePackages = "eu.dnetlib.repo.manager.*")
public class Config {
@ -61,4 +63,13 @@ public class Config {
return serializer;
}
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("Content-Type", "application/json");
return restTemplate;
}
}

View File

@ -5,6 +5,8 @@ import eu.dnetlib.repo.manager.service.MonitorService;
import eu.dnetlib.repo.manager.service.PiWikService;
import eu.dnetlib.repo.manager.service.RepositoryService;
import eu.dnetlib.repo.manager.service.ValidatorService;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@ -27,6 +29,7 @@ import java.util.ArrayList;
@Configuration
@EnableSwagger2
@EnableWebMvc
@EnableCircuitBreaker
@ComponentScan(basePackageClasses = {
RepositoryController.class,
MonitorController.class,

View File

@ -23,7 +23,7 @@ public class StatsController {
@RequestMapping(value = "/getStatistics" , method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Map<String, Object> getStatistics() throws JSONException {
public Map getStatistics() throws JSONException {
return statsService.getStatistics();
}
}

View File

@ -42,7 +42,8 @@ public class BrokerServiceImpl implements BrokerService {
private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
.getLogger(BrokerServiceImpl.class);
private RestTemplate restTemplate = null;
@Autowired
RestTemplate restTemplate ;
private HttpHeaders httpHeaders;
@ -54,9 +55,6 @@ public class BrokerServiceImpl implements BrokerService {
@PostConstruct
private void initDnetTopicsMap() {
restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
httpHeaders = new HttpHeaders();
httpHeaders.set("Content-Type", "application/json");

View File

@ -46,7 +46,8 @@ public class RepositoryServiceImpl implements RepositoryService {
@Value("${api.baseAddress}")
private String baseAddress;
private RestTemplate restTemplate = null;
@Autowired
RestTemplate restTemplate;
private HttpHeaders httpHeaders;
@ -116,11 +117,6 @@ public class RepositoryServiceImpl implements RepositoryService {
LOGGER.debug("Initialization method of repository api!");
LOGGER.debug("Updated version!");
restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
httpHeaders = new HttpHeaders();
httpHeaders.set("Content-Type", "application/json;charset=UTF-8");
for (String vocName : vocabularyNames) {
vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT));

View File

@ -1,5 +1,6 @@
package eu.dnetlib.repo.manager.service;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import eu.dnetlib.repo.manager.exception.EndPointException;
import org.json.JSONException;
@ -8,5 +9,5 @@ import java.util.Map;
public interface StatsService {
Map<String, Object> getStatistics() throws JSONException, EndPointException;
Map getStatistics() ;
}

View File

@ -1,162 +1,50 @@
package eu.dnetlib.repo.manager.service;
import eu.dnetlib.repo.manager.controllers.RestTemplateResponseErrorHandler;
import eu.dnetlib.repo.manager.exception.EndPointException;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import eu.dnetlib.repo.manager.service.customHystrixCommands.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service("statsService")
public class StatsServiceImpl implements StatsService {
private RestTemplate restTemplate = null;
private HttpHeaders httpHeaders;
private static final Logger LOGGER = Logger.getLogger(RepositoryServiceImpl.class);
@Autowired
RestTemplate restTemplate;
@Value("${search.api.baseAddress}")
private String baseAddress;
@Value("${search.api.usagestats}")
private String usagestatsBaseAddress;
@Value("${search.api.usageEvents}")
private String usagestatsEvents;
@Autowired
RestTemplateResponseErrorHandler restTemplateResponseErrorHandler;
@PostConstruct
private void init() {
LOGGER.debug("Initialization method of statistics api!");
restTemplate = new RestTemplate();
restTemplate.setErrorHandler(restTemplateResponseErrorHandler);
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
httpHeaders = new HttpHeaders();
httpHeaders.set("Content-Type", "application/json");
}
@Override
public Map<String, Object> getStatistics() throws JSONException {
public Map getStatistics() {
String aggregators = getTotalByType("datasource",baseAddress+"/resources",
"?query= " +
" oaftype exact datasource and " +
" ( datasourcetypename exact Institutional Repository Aggregator " +
" or datasourcetypename exact Publication Repository Aggregator )");
UsageStatsTotalHystrixCommand usageStatsTotalHystrixCommand = new UsageStatsTotalHystrixCommand(usagestatsEvents,restTemplate);
DataRepositoriesHystrixCommand dataRepositoriesHystrixCommand = new DataRepositoriesHystrixCommand(baseAddress,restTemplate);
AggregatorsHystrixCommand aggregatorsHystrixCommand = new AggregatorsHystrixCommand(baseAddress,restTemplate);
LiteratureHystrixCommand literatureHystrixCommand = new LiteratureHystrixCommand(baseAddress,restTemplate);
JournalHystrixCommand journalHystrixCommand = new JournalHystrixCommand(baseAddress,restTemplate);
PublicationHystrixCommand publicationHystrixCommand = new PublicationHystrixCommand(baseAddress,restTemplate);
DatasetsHystrixCommand datasetsHystrixCommand = new DatasetsHystrixCommand(baseAddress,restTemplate);
SoftwareHystrixCommand softwareHystrixCommand = new SoftwareHystrixCommand(baseAddress,restTemplate);
LastYearUsageStatsHystrixCommand lastYearUsageStatsHystrixCommand = new LastYearUsageStatsHystrixCommand(usagestatsBaseAddress,restTemplate);
String dataRepositories = getTotalByType("datasource",baseAddress+"/resources",
"?query= " +
" oaftype exact datasource and " +
" datasourcetypename exact Data Repository " );
String literature = getTotalByType("datasource",baseAddress+"/resources",
"?query= " +
" oaftype exact datasource and " +
" ( datasourcetypename exact Institutional Repository " +
" or datasourcetypename exact Publication Repository )");
String journal = getTotalByType("datasource",baseAddress+"/resources",
"?query= " +
" oaftype exact datasource and " +
" datasourcetypename exact Journal");
String publications = getTotalByType("other",baseAddress,"/publications/count");
String datasets = getTotalByType("other",baseAddress,"/datasets/count");
String software = getTotalByType("other",baseAddress,"/software/count");
Map lastYearUsagestats = getLastYearUsageStatsTotal();
Map<String,Object> stats = new HashMap<>();
stats.put("aggregators",aggregators);
stats.put("dataRepositories",dataRepositories);
stats.put("literature",literature);
stats.put("journal",journal);
stats.put("publications",publications);
stats.put("datasets",datasets);
stats.put("software",software);
stats.put("lastYearUsagestats", lastYearUsagestats);
// Integer usagestats = getUsageStatsTotal();
stats.put("usagestats",null);
stats.put("aggregators",aggregatorsHystrixCommand.execute());
stats.put("dataRepositories",dataRepositoriesHystrixCommand.execute());
stats.put("literature",literatureHystrixCommand.execute());
stats.put("journal",journalHystrixCommand.execute());
stats.put("publications",publicationHystrixCommand.execute());
stats.put("datasets",datasetsHystrixCommand.execute());
stats.put("software",softwareHystrixCommand.execute());
stats.put("lastYearUsagestats", lastYearUsageStatsHystrixCommand.execute());
stats.put("usagestats",usageStatsTotalHystrixCommand.execute());
return stats;
}
private String getTotalByType(String type,String url,String query) {
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(url + query)
.queryParam("page",0)
.queryParam("size",0)
.queryParam("format","json")
.build().encode();
ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,null,Map.class);
// if(rs.getStatusCode().equals(HttpStatus.OK)){
if(type.equalsIgnoreCase("datasource")){
Map metadata = (Map) ((Map)rs.getBody()).get("meta");
return String.valueOf(metadata.get("total"));
}else {
Map metadata = (Map) (rs.getBody());
return String.valueOf(metadata.get("total"));
}
//}
// return null;
}
private Map getLastYearUsageStatsTotal() {
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(usagestatsBaseAddress + "/totals")
.build().encode();
ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,null,Map.class);
// if(rs.getStatusCode().equals(HttpStatus.OK)){
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<String,Object> usagestats = new HashMap<>();
usagestats.put("number",String.valueOf(downloads+views));
usagestats.put("year",year);
return usagestats;
//}
}
private Integer getUsageStatsTotal() throws JSONException {
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(usagestatsEvents)
.build().encode();
// ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,null,Map.class);
String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
JSONObject resultSet = new JSONObject(rs);
JSONObject totals = resultSet.getJSONObject("totals");
return Integer.valueOf(totals.getString("events"));
}
}

View File

@ -0,0 +1,48 @@
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.Map;
public class AggregatorsHystrixCommand extends HystrixCommand<String> {
RestTemplate restTemplate;
String baseAddress;
public AggregatorsHystrixCommand(String baseAddress, RestTemplate restTemplate) {
super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
this.baseAddress = baseAddress;
this.restTemplate = restTemplate;
}
@Override
protected String run() throws Exception {
String url = baseAddress + "/resources" +
"?query= " +
" oaftype exact datasource and " +
" ( datasourcetypename exact Institutional Repository Aggregator " +
" or datasourcetypename exact Publication Repository Aggregator )";
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(url)
.queryParam("page", 0)
.queryParam("size", 0)
.queryParam("format", "json")
.build().encode();
ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map) ((Map) rs.getBody()).get("meta");
return String.valueOf(metadata.get("total"));
}
@Override
protected String getFallback() {
return null;
}
}

View File

@ -0,0 +1,47 @@
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.Map;
public class DataRepositoriesHystrixCommand extends HystrixCommand<String> {
RestTemplate restTemplate ;
private String baseAddress;
public DataRepositoriesHystrixCommand(String baseAddress,RestTemplate restTemplate) {
super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
this.baseAddress = baseAddress;
this.restTemplate = restTemplate;
}
@Override
protected String run() {
String url = baseAddress+"/resources" +
"?query= " +
" oaftype exact datasource and " +
" datasourcetypename exact Data Repository ";
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(url)
.queryParam("page",0)
.queryParam("size",0)
.queryParam("format","json")
.build().encode();
ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,null,Map.class);
Map metadata = (Map) ((Map)rs.getBody()).get("meta");
return String.valueOf(metadata.get("total"));
}
@Override
protected String getFallback() {
return null;
}
}

View File

@ -0,0 +1,44 @@
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.Map;
public class DatasetsHystrixCommand extends HystrixCommand<String> {
RestTemplate restTemplate;
String baseAddress;
public DatasetsHystrixCommand(String baseAddress,RestTemplate restTemplate) {
super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
this.restTemplate = restTemplate;
this.baseAddress = baseAddress;
}
@Override
protected String run() throws Exception {
String url = baseAddress+"/datasets/count";
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(url)
.queryParam("page", 0)
.queryParam("size", 0)
.queryParam("format", "json")
.build().encode();
ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map) (rs.getBody());
return String.valueOf(metadata.get("total"));
}
@Override
protected String getFallback() {
return null;
}
}

View File

@ -0,0 +1,48 @@
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.Map;
public class JournalHystrixCommand extends HystrixCommand<String> {
RestTemplate restTemplate;
private String baseAddress;
public JournalHystrixCommand(String baseAddress, RestTemplate restTemplate) {
super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
this.baseAddress = baseAddress;
this.restTemplate = restTemplate;
}
@Override
protected String run() throws Exception {
String url = baseAddress+"/resources"+
"?query= " +
" oaftype exact datasource and " +
" datasourcetypename exact Journal";
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(url)
.queryParam("page", 0)
.queryParam("size", 0)
.queryParam("format", "json")
.build().encode();
ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map) ((Map) rs.getBody()).get("meta");
return String.valueOf(metadata.get("total"));
}
@Override
protected String getFallback() {
return null;
}
}

View File

@ -0,0 +1,52 @@
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<Map> {
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<String,Object> usagestats = new HashMap<>();
usagestats.put("number",String.valueOf(downloads+views));
usagestats.put("year",year);
return usagestats;
}
@Override
protected Map getFallback() {
return null;
}
}

View File

@ -0,0 +1,49 @@
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.Map;
public class LiteratureHystrixCommand extends HystrixCommand<String> {
RestTemplate restTemplate;
private String baseAddress;
public LiteratureHystrixCommand(String baseAddress , RestTemplate restTemplate) {
super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
this.baseAddress = baseAddress;
this.restTemplate = restTemplate;
}
@Override
protected String run() throws Exception {
String url = baseAddress+"/resources"+
"?query= " +
" oaftype exact datasource and " +
" ( datasourcetypename exact Institutional Repository " +
" or datasourcetypename exact Publication Repository )";
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(url)
.queryParam("page", 0)
.queryParam("size", 0)
.queryParam("format", "json")
.build().encode();
ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map) ((Map) rs.getBody()).get("meta");
return String.valueOf(metadata.get("total"));
}
@Override
protected String getFallback() {
return null;
}
}

View File

@ -0,0 +1,45 @@
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.Map;
public class PublicationHystrixCommand extends HystrixCommand<String> {
RestTemplate restTemplate;
private String baseAddress;
public PublicationHystrixCommand(String baseAddress, RestTemplate restTemplate) {
super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
this.baseAddress = baseAddress;
this.restTemplate = restTemplate;
}
@Override
protected String run() throws Exception {
String url = baseAddress + "/publications/count";
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(url)
.queryParam("page", 0)
.queryParam("size", 0)
.queryParam("format", "json")
.build().encode();
ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map) (rs.getBody());
return String.valueOf(metadata.get("total"));
}
@Override
protected String getFallback() {
return null;
}
}

View File

@ -0,0 +1,44 @@
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.Map;
public class SoftwareHystrixCommand extends HystrixCommand<String> {
String baseAddress;
RestTemplate restTemplate;
public SoftwareHystrixCommand(String baseAddress, RestTemplate restTemplate) {
super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
this.baseAddress = baseAddress;
this.restTemplate = restTemplate;
}
@Override
protected String run() {
String url = baseAddress + "/software/count";
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(url)
.queryParam("page", 0)
.queryParam("size", 0)
.queryParam("format", "json")
.build().encode();
ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map) (rs.getBody());
return String.valueOf(metadata.get("total"));
}
@Override
protected String getFallback() {
return null;
}
}

View File

@ -0,0 +1,43 @@
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.Map;
public class UsageStatsTotalHystrixCommand extends HystrixCommand<Integer> {
RestTemplate restTemplate ;
String usagestatsEvents;
public UsageStatsTotalHystrixCommand(String usagestatsEvents,RestTemplate restTemplate) {
super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup"));
this.usagestatsEvents = usagestatsEvents;
this.restTemplate = restTemplate;
}
@Override
protected Integer run() {
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(usagestatsEvents)
.build().encode();
ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,null,Map.class);
Map metadata = (Map) ((Map)rs.getBody()).get("totals");
// String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
// JSONObject resultSet = new JSONObject(rs);
// JSONObject totals = resultSet.getJSONObject("totals");
return (Integer) metadata.get("events");
}
@Override
protected Integer getFallback() {
return null;
}
}

4
svn-commit.tmp~ Normal file
View File

@ -0,0 +1,4 @@
--This line, and those below, will be ignored--
M src/main/java/eu/dnetlib/repo/manager/service/StatsServiceImpl.java