implemented health and kpi on API

This commit is contained in:
Sandro La Bruzzo 2024-10-14 10:20:03 +02:00
parent 0a9b13c016
commit 76a130d09b
6 changed files with 171 additions and 4 deletions

View File

@ -66,6 +66,10 @@
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>eu.dnetlib.dhp</groupId>
<artifactId>dhp-schemas</artifactId>

View File

@ -82,6 +82,14 @@ public class ScholexplorerApiApplication {
.build();
}
@Bean
public GroupedOpenApi publicApiKPI() {
return GroupedOpenApi.builder()
.group("Scholexplorer KPI")
.pathsToMatch("/kpi/**")
.build();
}
@Bean
public OpenAPI newSwaggerDocket() {
@ -95,6 +103,7 @@ public class ScholexplorerApiApplication {
return new OpenAPI()
.servers(servers)
.info(getSwaggerInfo())
.tags(new ArrayList<>());
}

View File

@ -0,0 +1,34 @@
package eu.dnetlib.scholexplorer.api.controller;
import eu.dnetlib.dhp.schema.sx.api.model.v1.LinkPublisher;
import eu.dnetlib.scholexplorer.api.ScholixException;
import eu.dnetlib.scholexplorer.api.index.ScholixIndexManager;
import eu.dnetlib.scholexplorer.api.model.KPIMetric;
import io.micrometer.core.annotation.Timed;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/kpi")
@Tag(name = "kpi")
public class KPIController {
@Autowired
private ScholixIndexManager manager;
@Operation(summary = "Get Scholix KPI", description = "returns the current KPI")
@GetMapping("/getKPI")
public KPIMetric getKPI() throws ScholixException {
return manager.getKPI();
}
}

View File

@ -10,6 +10,7 @@ import eu.dnetlib.dhp.schema.sx.scholix.ScholixResource;
import eu.dnetlib.dhp.schema.sx.scholix.flat.ScholixFlat;
import eu.dnetlib.scholexplorer.api.ScholixException;
import eu.dnetlib.scholexplorer.api.TaggedCounter;
import eu.dnetlib.scholexplorer.api.model.KPIMetric;
import eu.dnetlib.scholexplorer.api.model.Summary;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
@ -19,6 +20,10 @@ import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder;
import org.elasticsearch.search.aggregations.bucket.composite.ParsedComposite;
import org.elasticsearch.search.aggregations.bucket.composite.TermsValuesSourceBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
@ -31,10 +36,7 @@ import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.elasticsearch.index.query.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -197,6 +199,66 @@ public class ScholixIndexManager {
}
public KPIMetric getKPI() throws ScholixException{
List<CompositeValuesSourceBuilder<?>> sources = new ArrayList<>();
sources.add(new TermsValuesSourceBuilder("relationTypeSource")
.field("sourceType"));
sources.add(new TermsValuesSourceBuilder("relationTypeTarget")
.field("targetType"));
CompositeAggregationBuilder compositeAggregationBuilder = new CompositeAggregationBuilder(
"Composite_relationTypes", sources)
.size(10000);
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
.withSearchType(SearchType.DEFAULT)
.withPageable(PageRequest.of(0, 10))
.addAggregation(
compositeAggregationBuilder)
.build();
Pair<RestHighLevelClient, ElasticsearchRestTemplate> resource = null;
try {
resource = connectionPool.getResource();
ElasticsearchRestTemplate client = resource.getValue();
final long total = client.count(new NativeSearchQueryBuilder().withSearchType(SearchType.DEFAULT).build(), ScholixFlat.class, IndexCoordinates.of(elasticSearchProperties.getIndexName()));
final long totalItem = client.count(new NativeSearchQueryBuilder().withSearchType(SearchType.DEFAULT).build(), ScholixFlat.class, IndexCoordinates.of(elasticSearchProperties.getIndexResourceName()));
final KPIMetric metric = new KPIMetric();
metric.setNumberOfRelationships(total);
metric.setNumerOfEntity(totalItem);
final SearchHits<ScholixFlat> hits = client.search(searchQuery, ScholixFlat.class, IndexCoordinates.of(elasticSearchProperties.getIndexName()));
final Aggregations aggregations = hits.getAggregations();
if (aggregations == null)
return null;
metric.setRelationMetrics(
((ParsedComposite) aggregations.get("Composite_relationTypes"))
.getBuckets()
.stream()
.map(b -> new KPIMetric.ScholixRelMetric(
b.getKey().get("relationTypeSource").toString(),
b.getKey().get("relationTypeTarget").toString(),
b.getDocCount())).toList());
return metric;
} catch (ScholixException e) {
throw e;
} finally {
if (connectionPool != null) {
connectionPool.returnResource(resource);
}
}
}
public List<Pair<String, Long>> totalLinksPublisher(final RelationPrefix prefix, final String filterName) throws ScholixException {

View File

@ -0,0 +1,57 @@
package eu.dnetlib.scholexplorer.api.model;
import java.util.List;
public class KPIMetric {
public static class ScholixRelMetric {
public String sourceType;
public String targetType;
public long numberOfrelationships;
public ScholixRelMetric(String sourceType, String targetType, long numberOfrelationships) {
this.sourceType = sourceType;
this.targetType = targetType;
this.numberOfrelationships = numberOfrelationships;
}
}
private Long numberOfRelationships;
private List<ScholixRelMetric> relationMetrics;
private Long numerOfEntity;
public Long getNumberOfRelationships() {
return numberOfRelationships;
}
public KPIMetric setNumberOfRelationships(Long numberOfRelationships) {
this.numberOfRelationships = numberOfRelationships;
return this;
}
public Long getNumerOfEntity() {
return numerOfEntity;
}
public KPIMetric setNumerOfEntity(Long numerOfEntity) {
this.numerOfEntity = numerOfEntity;
return this;
}
public List<ScholixRelMetric> getRelationMetrics() {
return relationMetrics;
}
public KPIMetric setRelationMetrics(List<ScholixRelMetric> relationMetrics) {
this.relationMetrics = relationMetrics;
return this;
}
}

View File

@ -10,6 +10,7 @@ dhp.swagger.api.host = localhost:8080
#dhp.swagger.api.host = api.scholexplorer.openaire.eu
dhp.swagger.api.basePath = /
management.endpoint.health.show-details=always
management.endpoints.web.exposure.include = health, metrics, prometheus
management.metrics.tags.application=${spring.application.name}