Fix get stakeholder order by publications and add cache in stats tool responses.

This commit is contained in:
Konstantinos Triantafyllou 2024-03-12 19:07:32 +02:00
parent 4b411ae0ed
commit 3681c4bce3
4 changed files with 83 additions and 36 deletions

View File

@ -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) {

View File

@ -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;
}
}
} }

View File

@ -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;
@RequestMapping(value = "/{type}", method = RequestMethod.GET) public List<List<String>> getResponse(String type) throws UnsupportedEncodingException {
public List<StakeholderPublications> getStakeholders(@PathVariable String type) throws UnsupportedEncodingException { if(type.equals("funder")) {
List<Stakeholder> stakeholders = this.service.getStakeholdersByTypeAndRole(type, null, false); return this.utils.getFunders();
String url = this.properties.getUrlByType(type); } else if(type.equals("organization")) {
if (url != null) { return this.utils.getOrganizations();
List<StakeholderPublications> stakeholderPublications = new ArrayList<>(); } else if(type.equals("datasource")) {
List<List<String>> data = this.utils.getData(url); return this.utils.getDataSources();
data.forEach(entity -> {
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)))));
});
return stakeholderPublications;
} else { } else {
return stakeholders.stream().map(stakeholder -> new StakeholderPublications(stakeholder, 0)).collect(Collectors.toList()); return null;
} }
} }
@RequestMapping(value = "/{type}", method = RequestMethod.GET)
public List<StakeholderPublications> getStakeholders(@PathVariable String type) throws UnsupportedEncodingException, InterruptedException {
List<Stakeholder> stakeholders = this.service.getStakeholdersByTypeAndRole(type, null, false);
List<StakeholderPublications> stakeholderPublications = new ArrayList<>();
List<List<String>> data = this.getResponse(type);
List<Stakeholder> remain = new ArrayList<>(stakeholders);
if (data != null) {
data.forEach(entity -> {
String id = entity.get(1);
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;
}
} }

View File

@ -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")
String service = url.substring(0, url.indexOf('=') + 1); public List<List<String>> getFunders() throws UnsupportedEncodingException {
String json = URLDecoder.decode(url.substring(url.indexOf('=') + 1), "UTF-8"); String url = this.properties.getRfo();
URI uri = UriComponentsBuilder.fromUriString(service + json).build().encode().toUri(); return this.getData(url);
ResponseEntity<Data> data = restTemplate.getForEntity(uri, Data.class); }
return data.getBody().getData();
@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 json = URLDecoder.decode(url.substring(url.indexOf('=') + 1), "UTF-8");
URI uri = UriComponentsBuilder.fromUriString(service + json).build().encode().toUri();
try {
ResponseEntity<Data> data = restTemplate.getForEntity(uri, Data.class);
if(data.getStatusCode() == HttpStatus.OK) {
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());
} }
} }