uoa-repository-manager-service/src/main/java/eu/dnetlib/repo/manager/service/StatsServiceImpl.java

322 lines
11 KiB
Java

package eu.dnetlib.repo.manager.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
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;
import java.util.Objects;
@Service("statsService")
public class StatsServiceImpl implements StatsService {
private static final Logger logger = LoggerFactory.getLogger(StatsServiceImpl.class);
@Autowired
RestTemplate restTemplate;
@Value("${services.provide.clients.search}")
private String baseAddress;
@Value("${services.provide.clients.usagestats}")
private String usagestatsBaseAddress;
@Value("${services.provide.clients.usageEvents}")
private String usagestatsEvents;
@Override
public Map getStatistics() {
Map<String,Object> stats = new HashMap<>();
stats.put("aggregators", this.getAggregatorsStats());
stats.put("dataRepositories", this.getDataRepositoriesMetadata());
stats.put("literature", this.getLiteratureStats());
stats.put("journal", this.getJournalsStats());
stats.put("publications", this.getPublicationStats());
stats.put("datasets", this.getDatasetsStats());
stats.put("software", this.getSoftwareStats());
stats.put("lastYearUsagestats", this.getLastYearUsageStats());
stats.put("usagestats", this.getUsageStatsTotal());
return stats;
}
private String getAggregatorsStats()
{
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();
try {
ResponseEntity<Map> rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map) ((Map<?, ?>) Objects.requireNonNull(rs.getBody())).get("meta");
return String.valueOf(metadata.get("total"));
} catch ( RestClientException rce ) {
logger.error(rce.getMessage());
return null;
} catch ( Exception e ) {
logger.error("", e);
return null;
}
}
private String getDataRepositoriesMetadata()
{
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();
try {
ResponseEntity<Map> rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map) ((Map<?, ?>) Objects.requireNonNull(rs.getBody())).get("meta");
if ( metadata == null ) {
logger.error("The metadata was null!");
return null;
}
return String.valueOf(metadata.get("total"));
} catch ( RestClientException rce ) {
logger.error(rce.getMessage());
return null;
} catch ( Exception e ) {
logger.error("", e);
return null;
}
}
private String getLiteratureStats()
{
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();
try {
ResponseEntity<Map> rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map) ((Map<?, ?>) Objects.requireNonNull(rs.getBody())).get("meta");
if ( metadata == null ) {
logger.error("The metadata was null!");
return null;
}
return String.valueOf(metadata.get("total"));
} catch ( RestClientException rce ) {
logger.error(rce.getMessage());
return null;
} catch ( Exception e ) {
logger.error("", e);
return null;
}
}
private String getJournalsStats()
{
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();
try {
ResponseEntity<Map> rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map) ((Map<?, ?>) Objects.requireNonNull(rs.getBody())).get("meta");
if ( metadata == null ) {
logger.error("The metadata was null!");
return null;
}
return String.valueOf(metadata.get("total"));
} catch ( RestClientException rce ) {
logger.error(rce.getMessage());
return null;
} catch ( Exception e ) {
logger.error("", e);
return null;
}
}
private String getPublicationStats()
{
String url = baseAddress + "/publications/count";
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(url)
.queryParam("page", 0)
.queryParam("size", 0)
.queryParam("format", "json")
.build().encode();
try {
ResponseEntity<Map> rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map<?, ?>) rs.getBody();
if ( metadata == null ) {
logger.error("The metadata was null!");
return null;
}
return String.valueOf(metadata.get("total"));
} catch ( RestClientException rce ) {
logger.error(rce.getMessage());
return null;
} catch ( Exception e ) {
logger.error("", e);
return null;
}
}
private String getDatasetsStats()
{
String url = baseAddress + "/datasets/count";
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(url)
.queryParam("page", 0)
.queryParam("size", 0)
.queryParam("format", "json")
.build().encode();
try {
ResponseEntity<Map> rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map<?, ?>) rs.getBody();
if ( metadata == null ) {
logger.error("The metadata was null!");
return null;
}
return String.valueOf(metadata.get("total"));
} catch ( RestClientException rce ) {
logger.error(rce.getMessage());
return null;
} catch ( Exception e ) {
logger.error("", e);
return null;
}
}
private String getSoftwareStats()
{
String url = baseAddress + "/software/count";
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(url)
.queryParam("page", 0)
.queryParam("size", 0)
.queryParam("format", "json")
.build().encode();
try {
ResponseEntity<Map> rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map<?, ?>) rs.getBody();
if ( metadata == null ) {
logger.error("The metadata was null!");
return null;
}
return String.valueOf(metadata.get("total"));
} catch ( RestClientException rce ) {
logger.error(rce.getMessage());
return null;
} catch ( Exception e ) {
logger.error("", e);
return null;
}
}
private Map<String,Object> getLastYearUsageStats()
{
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(usagestatsBaseAddress + "/totals")
.build().encode();
try {
ResponseEntity<Map> rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
List yearly_stats = (List) ((Map<?, ?>) Objects.requireNonNull(rs.getBody())).get("yearly_stats");
Map lastYear = (Map) yearly_stats.get(yearly_stats.size() - 2);
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;
} catch ( RestClientException rce ) {
logger.error(rce.getMessage());
return null;
} catch ( Exception e ) {
logger.error("", e);
return null;
}
}
private Integer getUsageStatsTotal()
{
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(usagestatsEvents)
.build().encode();
try {
ResponseEntity<Map> rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map) ((Map<?, ?>) Objects.requireNonNull(rs.getBody())).get("totals");
if ( metadata == null ) {
logger.error("The metadata was null!");
return null;
}
// String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
// JSONObject resultSet = new JSONObject(rs);
// JSONObject totals = resultSet.getJSONObject("totals");
return (Integer) metadata.get("events");
} catch ( RestClientException rce ) {
logger.error(rce.getMessage());
return null;
} catch ( Exception e ) {
logger.error("", e);
return null;
}
}
}