Compare commits
5 Commits
9719_curso
...
master
Author | SHA1 | Date |
---|---|---|
Alexios Symeonidis | 1caeb46a86 | |
Alexios Symeonidis | af13405065 | |
Alexios Symeonidis | c1baa3a631 | |
Alexios Symeonidis | 60cc376d40 | |
Alexios Symeonidis | f23acfff89 |
15
pom.xml
15
pom.xml
|
@ -163,6 +163,21 @@
|
|||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.micrometer</groupId>
|
||||
<artifactId>micrometer-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.micrometer</groupId>
|
||||
<artifactId>micrometer-registry-prometheus</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Add dump schema dependency -->
|
||||
<dependency>
|
||||
<groupId>eu.dnetlib.dhp</groupId>
|
||||
|
|
|
@ -2,8 +2,10 @@ package eu.openaire.api;
|
|||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableAspectJAutoProxy
|
||||
public class OpenaireRestApiApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
package eu.openaire.api.config.metrics;
|
||||
|
||||
import io.micrometer.core.annotation.Timed;
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
import io.micrometer.core.instrument.Timer;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
public class MetricsAspect {
|
||||
|
||||
private final MeterRegistry meterRegistry;
|
||||
|
||||
@Autowired
|
||||
public MetricsAspect(MeterRegistry meterRegistry) {
|
||||
this.meterRegistry = meterRegistry;
|
||||
}
|
||||
|
||||
/***
|
||||
* Advice for timing methods annotated with {@link Timed}.
|
||||
*/
|
||||
@Around("@annotation(io.micrometer.core.annotation.Timed)")
|
||||
public Object timeAnnotatedMethods(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
|
||||
|
||||
String className = joinPoint.getTarget().getClass().getSimpleName();
|
||||
String methodName = method.getName();
|
||||
String timerName = className + "." + methodName;
|
||||
|
||||
Timer.Sample sample = Timer.start(meterRegistry);
|
||||
|
||||
try {
|
||||
return joinPoint.proceed();
|
||||
} finally {
|
||||
sample.stop(Timer.builder(timerName)
|
||||
.tags("application", "openaire-search-api")
|
||||
.tags("class", className)
|
||||
.tags("method", methodName)
|
||||
.publishPercentiles(0.5, 0.95)
|
||||
.register(meterRegistry));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,9 +9,6 @@ import jakarta.validation.constraints.Pattern;
|
|||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static eu.openaire.api.mappers.Utils.API_CURSOR_DESC;
|
||||
import static eu.openaire.api.mappers.Utils.API_PAGE_DESC;
|
||||
|
@ -77,32 +74,28 @@ public class ProjectRequest implements PaginatedRequest {
|
|||
private String[] fundingStreamId;
|
||||
|
||||
@Parameter(
|
||||
description = "Gets the projects with start date greater than or equal to the given date. Please provide a date formatted as YYYY-MM-DD",
|
||||
schema = @Schema(type = "string", format = "date")
|
||||
description = "Gets the projects with start date greater than or equal to the given date. Provide a date in YYYY or YYYY-MM-DD format",
|
||||
schema = @Schema(type = "string")
|
||||
)
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||
private LocalDate fromStartDate;
|
||||
private String fromStartDate;
|
||||
|
||||
@Parameter(
|
||||
description = "Gets the projects with start date less than or equal to the given date. Please provide a date formatted as YYYY-MM-DD",
|
||||
schema = @Schema(type = "string", format = "date")
|
||||
description = "Gets the projects with start date less than or equal to the given date. Provide a date in YYYY or YYYY-MM-DD format",
|
||||
schema = @Schema(type = "string")
|
||||
)
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||
private LocalDate toStartDate;
|
||||
private String toStartDate;
|
||||
|
||||
@Parameter(
|
||||
description = "Gets the projects with end date greater than or equal to the given date. Please provide a date formatted as YYYY-MM-DD",
|
||||
schema = @Schema(type = "string", format = "date")
|
||||
description = "Gets the projects with end date greater than or equal to the given date. Provide a date in YYYY or YYYY-MM-DD format",
|
||||
schema = @Schema(type = "string")
|
||||
)
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||
private LocalDate fromEndDate;
|
||||
private String fromEndDate;
|
||||
|
||||
@Parameter(
|
||||
description = "Gets the projects with end date less than or equal to the given date. Please provide a date formatted as YYYY-MM-DD",
|
||||
schema = @Schema(type = "string", format = "date")
|
||||
description = "Gets the projects with end date less than or equal to the given date. Provide a date in YYYY or YYYY-MM-DD format",
|
||||
schema = @Schema(type = "string")
|
||||
)
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||
private LocalDate toEndDate;
|
||||
private String toEndDate;
|
||||
|
||||
@Parameter(
|
||||
description = "The name or short name of the related organization",
|
||||
|
|
|
@ -9,9 +9,6 @@ import jakarta.validation.constraints.Pattern;
|
|||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static eu.openaire.api.mappers.Utils.API_CURSOR_DESC;
|
||||
import static eu.openaire.api.mappers.Utils.API_PAGE_DESC;
|
||||
|
@ -76,18 +73,14 @@ public class ResearchProductsRequest implements PaginatedRequest {
|
|||
private String[] type;
|
||||
|
||||
@Parameter(
|
||||
description = "Gets the research products whose publication date is greater than or equal to he given date. Please provide a date formatted as YYYY-MM-DD",
|
||||
schema = @Schema(type = "string", format = "date")
|
||||
)
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||
private LocalDate fromPublicationDate;
|
||||
description = "Gets the research products whose publication date is greater than or equal to he given date. Provide a date in YYYY or YYYY-MM-DD format",
|
||||
schema = @Schema(type = "string"))
|
||||
private String fromPublicationDate;
|
||||
|
||||
@Parameter(
|
||||
description = "Gets the research products whose publication date is less than or equal to the given date. Please provide a date formatted as YYYY-MM-DD",
|
||||
schema = @Schema(type = "string", format = "date")
|
||||
)
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||
private LocalDate toPublicationDate;
|
||||
description = "Gets the research products whose publication date is less than or equal to the given date. Provide a date in YYYY or YYYY-MM-DD format",
|
||||
schema = @Schema(type = "string"))
|
||||
private String toPublicationDate;
|
||||
|
||||
@Parameter(
|
||||
description = "List of subjects associated to the research product",
|
||||
|
|
|
@ -9,6 +9,7 @@ import java.time.LocalDateTime;
|
|||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.*;
|
||||
|
||||
public class Utils {
|
||||
|
@ -38,7 +39,7 @@ public class Utils {
|
|||
indicating that there are no more results.
|
||||
""";
|
||||
|
||||
static public String escapeAndJoin(String[] tokens, String predicate, boolean addQuotes, String suffix) {
|
||||
public static String escapeAndJoin(String[] tokens, String predicate, boolean addQuotes, String suffix) {
|
||||
|
||||
tokens = Arrays.stream(tokens)
|
||||
// remove empty tokens
|
||||
|
@ -51,7 +52,7 @@ public class Utils {
|
|||
return String.join(" " + predicate + " ", tokens);
|
||||
}
|
||||
|
||||
static public String handleInput(String token, boolean addQuotes, String suffix) {
|
||||
public static String handleInput(String token, boolean addQuotes, String suffix) {
|
||||
|
||||
boolean hasLogicalOperator = containsLogicalOperator(token);
|
||||
|
||||
|
@ -73,11 +74,11 @@ public class Utils {
|
|||
return sb.toString();
|
||||
}
|
||||
|
||||
static private boolean containsLogicalOperator(String input) {
|
||||
private static boolean containsLogicalOperator(String input) {
|
||||
return input.contains("AND") || input.contains("OR") || input.contains("NOT");
|
||||
}
|
||||
|
||||
static public String escapeInput(String input, String suffix) {
|
||||
public static String escapeInput(String input, String suffix) {
|
||||
|
||||
// Split the input into tokens at whitespace or parentheses or quotes
|
||||
String[] tokens = input.split("(\\s+|(?=[()\\\"]|(?<=[()\\\"])))");
|
||||
|
@ -163,24 +164,66 @@ public class Utils {
|
|||
}
|
||||
|
||||
return value;
|
||||
|
||||
}
|
||||
|
||||
static private void removeLastElementIfSpace(List<String> list) {
|
||||
private static void removeLastElementIfSpace(List<String> list) {
|
||||
if (list.get(list.size() - 1).equals(" ")) {
|
||||
list.remove(list.size() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
static public boolean isNullOrEmpty(String str) {
|
||||
public static boolean isNullOrEmpty(String str) {
|
||||
return str == null || str.isBlank();
|
||||
}
|
||||
|
||||
static public boolean isNullOrEmpty(String[] str) {
|
||||
public static boolean isNullOrEmpty(String[] str) {
|
||||
return str == null || str.length == 0;
|
||||
}
|
||||
|
||||
static private String formatDate(LocalDate date) {
|
||||
public static String formatSolrDateRange(String fieldName, String fromDate, String toDate) {
|
||||
|
||||
if (fromDate != null && toDate != null) {
|
||||
return String.format(fieldName,
|
||||
appendHHMMSS(appendMMDD(fromDate, "-01-01")),
|
||||
appendHHMMSS(appendMMDD(toDate, "-12-31")));
|
||||
} else {
|
||||
if (fromDate != null) {
|
||||
return String.format(fieldName,
|
||||
appendHHMMSS(appendMMDD(fromDate, "-01-01")), "*");
|
||||
}
|
||||
|
||||
if (toDate != null) {
|
||||
return String.format(fieldName, "*",
|
||||
appendHHMMSS(appendMMDD(toDate, "-12-31")));
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/***
|
||||
* Append -mm-dd if date came in yyyy format
|
||||
* @return LocalDate in yyyy-mm-dd format
|
||||
*/
|
||||
private static LocalDate appendMMDD(String date, String monthDaySuffix) {
|
||||
try {
|
||||
if (date.length() == 10) { //yyyy-mm-dd
|
||||
return LocalDate.parse(date);
|
||||
} else if (date.length() == 4) { //yyyy
|
||||
return LocalDate.parse(date + monthDaySuffix);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid date format");
|
||||
}
|
||||
} catch (DateTimeParseException e) {
|
||||
throw new IllegalArgumentException("Failed to parse date: " + date, e);
|
||||
}
|
||||
}
|
||||
|
||||
/***
|
||||
* The incoming date comes in yyyy-mm-dd, so we have to append hh-mm-ss, also.
|
||||
* @return String in yyyy-MM-dd'T'HH:mm:ss'Z' format
|
||||
*/
|
||||
private static String appendHHMMSS(LocalDate date) {
|
||||
|
||||
// IMPORTANT: all dates are indexed in 12:00:00 in the index (not sure why)
|
||||
// so we need to set this time to the date (this should change if dates are indexed in a different way)
|
||||
|
@ -200,23 +243,7 @@ public class Utils {
|
|||
return zdt.format(formatter);
|
||||
}
|
||||
|
||||
static public String formatSolrDateRange(String fieldName, LocalDate fromDate, LocalDate toDate) {
|
||||
|
||||
if (fromDate != null && toDate != null) {
|
||||
return String.format(fieldName, Utils.formatDate(fromDate), Utils.formatDate(toDate));
|
||||
} else {
|
||||
if (fromDate != null) {
|
||||
return String.format(fieldName, Utils.formatDate(fromDate), "*");
|
||||
}
|
||||
|
||||
if (toDate != null) {
|
||||
return String.format(fieldName, "*", Utils.formatDate(toDate));
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static public List<SolrQuery.SortClause> formatSortByParam(String sortBy, Map<String, String> fieldMapping) {
|
||||
public static List<SolrQuery.SortClause> formatSortByParam(String sortBy, Map<String, String> fieldMapping) {
|
||||
|
||||
if (Utils.isNullOrEmpty(sortBy)) {
|
||||
return null;
|
||||
|
|
|
@ -139,11 +139,8 @@ public interface ResearchProductMapper {
|
|||
return null;
|
||||
List<Pid> orcid = authorPidList
|
||||
.stream()
|
||||
.filter(
|
||||
ap -> ap
|
||||
.getTypeCode()
|
||||
.equals(ModelConstants.ORCID))
|
||||
.collect(Collectors.toList());
|
||||
.filter(ap -> ModelConstants.ORCID.equals(ap.getTypeCode()))
|
||||
.toList();
|
||||
if (orcid.size() == 1) {
|
||||
return getAuthorPid(orcid.get(0));
|
||||
}
|
||||
|
@ -151,11 +148,8 @@ public interface ResearchProductMapper {
|
|||
return null;
|
||||
orcid = authorPidList
|
||||
.stream()
|
||||
.filter(
|
||||
ap -> ap
|
||||
.getTypeCode()
|
||||
.equals(ModelConstants.ORCID_PENDING))
|
||||
.collect(Collectors.toList());
|
||||
.filter(ap -> ModelConstants.ORCID_PENDING.equals(ap.getTypeCode()))
|
||||
.toList();
|
||||
if (orcid.size() == 1) {
|
||||
return getAuthorPid(orcid.get(0));
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import eu.openaire.api.mappers.response.ResponseResultsMapper;
|
|||
import eu.openaire.api.mappers.response.entities.DatasourceMapper;
|
||||
import eu.openaire.api.repositories.SolrRepository;
|
||||
import eu.openaire.api.solr.SolrQueryParams;
|
||||
import io.micrometer.core.annotation.Timed;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
@ -37,6 +38,7 @@ public class DataSourceService {
|
|||
private final Logger log = LogManager.getLogger(this.getClass());
|
||||
|
||||
@SneakyThrows
|
||||
@Timed
|
||||
public Datasource getById(String id) {
|
||||
|
||||
var doc = solrRepository.getById(id);
|
||||
|
@ -48,6 +50,7 @@ public class DataSourceService {
|
|||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Timed
|
||||
public SearchResponse<Datasource> search(DataSourceRequest request) {
|
||||
|
||||
SolrQueryParams solrQueryParams = dataSourceRequestMapper.toSolrQuery(request);
|
||||
|
|
|
@ -11,6 +11,7 @@ import eu.openaire.api.mappers.response.ResponseResultsMapper;
|
|||
import eu.openaire.api.mappers.response.entities.OrganizationMapper;
|
||||
import eu.openaire.api.repositories.SolrRepository;
|
||||
import eu.openaire.api.solr.SolrQueryParams;
|
||||
import io.micrometer.core.annotation.Timed;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
@ -37,6 +38,7 @@ public class OrganizationService {
|
|||
private final Logger log = LogManager.getLogger(this.getClass());
|
||||
|
||||
@SneakyThrows
|
||||
@Timed
|
||||
public Organization getById(String id) {
|
||||
|
||||
var doc = solrRepository.getById(id);
|
||||
|
@ -48,6 +50,7 @@ public class OrganizationService {
|
|||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Timed
|
||||
public SearchResponse<Organization> search(OrganizationRequest request) {
|
||||
|
||||
SolrQueryParams solrQueryParams = organizationRequestMapper.toSolrQuery(request);
|
||||
|
|
|
@ -11,6 +11,7 @@ import eu.openaire.api.mappers.response.ResponseResultsMapper;
|
|||
import eu.openaire.api.mappers.response.entities.ProjectMapper;
|
||||
import eu.openaire.api.repositories.SolrRepository;
|
||||
import eu.openaire.api.solr.SolrQueryParams;
|
||||
import io.micrometer.core.annotation.Timed;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
@ -37,6 +38,7 @@ public class ProjectService {
|
|||
private final Logger log = LogManager.getLogger(this.getClass());
|
||||
|
||||
@SneakyThrows
|
||||
@Timed
|
||||
public Project getById(String id) {
|
||||
|
||||
var doc = solrRepository.getById(id);
|
||||
|
@ -48,6 +50,7 @@ public class ProjectService {
|
|||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Timed
|
||||
public SearchResponse<Project> search(ProjectRequest request) {
|
||||
|
||||
SolrQueryParams solrQueryParams = projectRequestMapper.toSolrQuery(request);
|
||||
|
|
|
@ -11,6 +11,7 @@ import eu.openaire.api.mappers.response.ResponseResultsMapper;
|
|||
import eu.openaire.api.mappers.response.entities.ResearchProductMapper;
|
||||
import eu.openaire.api.repositories.SolrRepository;
|
||||
import eu.openaire.api.solr.SolrQueryParams;
|
||||
import io.micrometer.core.annotation.Timed;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
|
@ -37,6 +38,7 @@ public class ResearchProductService {
|
|||
private final Logger log = LogManager.getLogger(this.getClass());
|
||||
|
||||
@SneakyThrows
|
||||
@Timed
|
||||
public GraphResult getById(String id) {
|
||||
|
||||
var doc = solrRepository.getById(id);
|
||||
|
@ -49,6 +51,7 @@ public class ResearchProductService {
|
|||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Timed
|
||||
public SearchResponse<GraphResult> search(ResearchProductsRequest request) {
|
||||
|
||||
SolrQueryParams solrQueryParams = researchProductsRequestMapper.toSolrQuery(request);
|
||||
|
|
|
@ -15,8 +15,10 @@ server.error.include-stacktrace=never
|
|||
# Enable the health endpoint
|
||||
management.endpoint.health.enabled=true
|
||||
|
||||
# Expose the health endpoint
|
||||
management.endpoints.web.exposure.include=health,info
|
||||
# Expose health, info and metrics endpoint
|
||||
management.endpoints.web.exposure.include=health,info,prometheus
|
||||
management.endpoint.prometheus.enabled=true
|
||||
management.metrics.export.prometheus.enabled=true
|
||||
|
||||
# Customize health endpoint settings
|
||||
management.endpoint.health.show-details=always
|
||||
|
|
Loading…
Reference in New Issue