Fix get stakeholder order by publications and add cache in stats tool responses.
This commit is contained in:
parent
4b411ae0ed
commit
3681c4bce3
|
@ -8,9 +8,11 @@ import eu.dnetlib.uoamonitorservice.UoaMonitorServiceConfiguration;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
import org.springframework.context.annotation.PropertySources;
|
import org.springframework.context.annotation.PropertySources;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
@SpringBootApplication(scanBasePackages = {"eu.dnetlib.irishmonitorservice"})
|
@SpringBootApplication(scanBasePackages = {"eu.dnetlib.irishmonitorservice"})
|
||||||
@PropertySources({
|
@PropertySources({
|
||||||
|
@ -21,6 +23,8 @@ import org.springframework.context.annotation.PropertySources;
|
||||||
@PropertySource(value = "classpath:dnet-override.properties", ignoreResourceNotFound = true)
|
@PropertySource(value = "classpath:dnet-override.properties", ignoreResourceNotFound = true)
|
||||||
})
|
})
|
||||||
@EnableConfigurationProperties({GlobalVars.class, APIProperties.class, StatsToolProperties.class})
|
@EnableConfigurationProperties({GlobalVars.class, APIProperties.class, StatsToolProperties.class})
|
||||||
|
@EnableCaching
|
||||||
|
@EnableScheduling
|
||||||
@Import({UoaMonitorServiceConfiguration.class, AuthenticationConfiguration.class})
|
@Import({UoaMonitorServiceConfiguration.class, AuthenticationConfiguration.class})
|
||||||
public class IrishMonitorServiceApplication {
|
public class IrishMonitorServiceApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
|
@ -34,17 +34,4 @@ public class StatsToolProperties {
|
||||||
public void setRepository(String repository) {
|
public void setRepository(String repository) {
|
||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getUrlByType(String type) {
|
|
||||||
switch (type) {
|
|
||||||
case "funder":
|
|
||||||
return this.rfo;
|
|
||||||
case "organization":
|
|
||||||
return this.rpo;
|
|
||||||
case "datasource":
|
|
||||||
return this.repository;
|
|
||||||
default:
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,29 +18,40 @@ import java.util.stream.Collectors;
|
||||||
@RequestMapping("publications")
|
@RequestMapping("publications")
|
||||||
public class StakeholderPublicationsController {
|
public class StakeholderPublicationsController {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private StatsToolProperties properties;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private StakeholderService service;
|
private StakeholderService service;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private StatsToolUtils utils;
|
private StatsToolUtils utils;
|
||||||
|
|
||||||
|
public List<List<String>> getResponse(String type) throws UnsupportedEncodingException {
|
||||||
|
if(type.equals("funder")) {
|
||||||
|
return this.utils.getFunders();
|
||||||
|
} else if(type.equals("organization")) {
|
||||||
|
return this.utils.getOrganizations();
|
||||||
|
} else if(type.equals("datasource")) {
|
||||||
|
return this.utils.getDataSources();
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/{type}", method = RequestMethod.GET)
|
@RequestMapping(value = "/{type}", method = RequestMethod.GET)
|
||||||
public List<StakeholderPublications> getStakeholders(@PathVariable String type) throws UnsupportedEncodingException {
|
public List<StakeholderPublications> getStakeholders(@PathVariable String type) throws UnsupportedEncodingException, InterruptedException {
|
||||||
List<Stakeholder> stakeholders = this.service.getStakeholdersByTypeAndRole(type, null, false);
|
List<Stakeholder> stakeholders = this.service.getStakeholdersByTypeAndRole(type, null, false);
|
||||||
String url = this.properties.getUrlByType(type);
|
|
||||||
if (url != null) {
|
|
||||||
List<StakeholderPublications> stakeholderPublications = new ArrayList<>();
|
List<StakeholderPublications> stakeholderPublications = new ArrayList<>();
|
||||||
List<List<String>> data = this.utils.getData(url);
|
List<List<String>> data = this.getResponse(type);
|
||||||
|
List<Stakeholder> remain = new ArrayList<>(stakeholders);
|
||||||
|
if (data != null) {
|
||||||
data.forEach(entity -> {
|
data.forEach(entity -> {
|
||||||
String id = entity.get(1);
|
String id = entity.get(1);
|
||||||
stakeholders.stream().filter(stakeholder -> stakeholder.getIndex_id().equals(id)).findFirst().ifPresent(stakeholder -> stakeholderPublications.add(new StakeholderPublications(stakeholder, Integer.parseInt(entity.get(0)))));
|
stakeholders.stream().filter(stakeholder -> stakeholder.getIndex_id().equals(id)).findFirst().ifPresent(stakeholder -> {
|
||||||
|
remain.remove(stakeholder);
|
||||||
|
stakeholderPublications.add(new StakeholderPublications(stakeholder, Integer.parseInt(entity.get(0))));
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
remain.forEach(stakeholder -> stakeholderPublications.add(new StakeholderPublications(stakeholder, 0)));
|
||||||
return stakeholderPublications;
|
return stakeholderPublications;
|
||||||
} else {
|
|
||||||
return stakeholders.stream().map(stakeholder -> new StakeholderPublications(stakeholder, 0)).collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,28 +1,73 @@
|
||||||
package eu.dnetlib.irishmonitorservice.utils;
|
package eu.dnetlib.irishmonitorservice.utils;
|
||||||
|
|
||||||
|
import eu.dnetlib.irishmonitorservice.configuration.properties.StatsToolProperties;
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.cache.annotation.CacheEvict;
|
||||||
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.client.RestClientException;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
import org.springframework.web.util.UriComponentsBuilder;
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URLDecoder;
|
import java.net.URLDecoder;
|
||||||
import java.net.URLEncoder;
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class StatsToolUtils {
|
public class StatsToolUtils {
|
||||||
|
private final Logger logger = LogManager.getLogger(StatsToolUtils.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private StatsToolProperties properties;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private RestTemplate restTemplate;
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
public List<List<String>> getData(String url) throws UnsupportedEncodingException {
|
@Cacheable(value = "funders")
|
||||||
|
public List<List<String>> getFunders() throws UnsupportedEncodingException {
|
||||||
|
String url = this.properties.getRfo();
|
||||||
|
return this.getData(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Cacheable(value = "organizations")
|
||||||
|
public List<List<String>> getOrganizations() throws UnsupportedEncodingException {
|
||||||
|
String url = this.properties.getRpo();
|
||||||
|
return this.getData(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Cacheable(value = "datasources")
|
||||||
|
public List<List<String>> getDataSources() throws UnsupportedEncodingException {
|
||||||
|
String url = this.properties.getRepository();
|
||||||
|
return this.getData(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<List<String>> getData(String url) throws UnsupportedEncodingException {
|
||||||
|
if(url != null) {
|
||||||
String service = url.substring(0, url.indexOf('=') + 1);
|
String service = url.substring(0, url.indexOf('=') + 1);
|
||||||
String json = URLDecoder.decode(url.substring(url.indexOf('=') + 1), "UTF-8");
|
String json = URLDecoder.decode(url.substring(url.indexOf('=') + 1), "UTF-8");
|
||||||
URI uri = UriComponentsBuilder.fromUriString(service + json).build().encode().toUri();
|
URI uri = UriComponentsBuilder.fromUriString(service + json).build().encode().toUri();
|
||||||
|
try {
|
||||||
ResponseEntity<Data> data = restTemplate.getForEntity(uri, Data.class);
|
ResponseEntity<Data> data = restTemplate.getForEntity(uri, Data.class);
|
||||||
|
if(data.getStatusCode() == HttpStatus.OK) {
|
||||||
return data.getBody().getData();
|
return data.getBody().getData();
|
||||||
}
|
}
|
||||||
|
} catch (RestClientException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@CacheEvict(allEntries = true, value = {"funders", "organizations", "datasources"})
|
||||||
|
@Scheduled(cron = "0 0 0 * * *") // Reset cache every day at 00:00
|
||||||
|
public void clearCacheAtMidnight() {
|
||||||
|
logger.info("Cache cleared at " + LocalDateTime.now());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue