From bb14c557443a4e2d3d557b23761042904e98877f Mon Sep 17 00:00:00 2001 From: Katerina Date: Fri, 5 Nov 2021 12:23:41 +0200 Subject: [PATCH] adding api url property --- .../service/MetricsService.java | 29 +++++++++++-------- .../utils/PrometheusMetricsFormatter.java | 8 ++--- src/main/resources/application.properties | 3 +- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/main/java/eu/dnetlib/dnetexploremetrics/service/MetricsService.java b/src/main/java/eu/dnetlib/dnetexploremetrics/service/MetricsService.java index b421176..d6b49c2 100644 --- a/src/main/java/eu/dnetlib/dnetexploremetrics/service/MetricsService.java +++ b/src/main/java/eu/dnetlib/dnetexploremetrics/service/MetricsService.java @@ -7,6 +7,7 @@ import eu.dnetlib.dnetexploremetrics.model.Metrics; import eu.dnetlib.dnetexploremetrics.utils.PrometheusMetricsFormatter; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.ResponseEntity; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; @@ -20,6 +21,9 @@ public class MetricsService { @Autowired private MetricsDAO metricsDAO; + @Value("${search.service.api.url:https://services.openaire.eu/search/v2/api/}") + private String searchUrl; + private final String recordsRequest = "results?size=0&format=json"; private final String publicationsRequest = "publications?size=0&format=json"; private final String datasetsRequest = "datasets?size=0&format=json"; @@ -35,55 +39,56 @@ public class MetricsService { " or reldatasourcecompatibilityid exact hostedBy or relproject=*))&type=organizations&size=0&format=json"; - @Scheduled(cron = "0 0 0 * * ?") + @Scheduled(cron = "0 0/1 * * * *") + //@Scheduled(cron = "0 0 0 * * ?") private void calculateMetrics() { - System.out.println("Calculating metrics..."); Metrics metrics = new Metrics(); RestTemplate restTemplate = new RestTemplate(); + + System.out.println(searchUrl); ResponseEntity response - = restTemplate.getForEntity("https://services.openaire.eu/search/v2/api/" + recordsRequest, String.class); + = restTemplate.getForEntity(searchUrl + recordsRequest, String.class); JSONObject jsonObject = new JSONObject(response.getBody()); metrics.records= jsonObject.getJSONObject("meta").get("total").toString(); response = restTemplate. - getForEntity("https://services.openaire.eu/search/v2/api/" + publicationsRequest, String.class); + getForEntity(searchUrl + publicationsRequest, String.class); jsonObject = new JSONObject(response.getBody()); metrics.publications = jsonObject.getJSONObject("meta").get("total").toString(); response = restTemplate. - getForEntity("https://services.openaire.eu/search/v2/api/" + datasetsRequest, String.class); + getForEntity(searchUrl + datasetsRequest, String.class); jsonObject = new JSONObject(response.getBody()); metrics.datasets = jsonObject.getJSONObject("meta").get("total").toString(); response = restTemplate. - getForEntity("https://services.openaire.eu/search/v2/api/" + softwareRequest, String.class); + getForEntity(searchUrl + softwareRequest, String.class); jsonObject = new JSONObject(response.getBody()); metrics.software = jsonObject.getJSONObject("meta").get("total").toString(); response = restTemplate. - getForEntity("https://services.openaire.eu/search/v2/api/" + orpRequest, String.class); + getForEntity(searchUrl + orpRequest, String.class); jsonObject = new JSONObject(response.getBody()); metrics.orp = jsonObject.getJSONObject("meta").get("total").toString(); response = restTemplate. - getForEntity("https://services.openaire.eu/search/v2/api/" + fundersRequest, String.class); + getForEntity(searchUrl + fundersRequest, String.class); jsonObject = new JSONObject(response.getBody()); metrics.funders = jsonObject.getJSONObject("refineResults").getJSONArray("relfunder").length()+""; response = restTemplate. - getForEntity("https://services.openaire.eu/search/v2/api/" + projectsRequest, String.class); + getForEntity(searchUrl + projectsRequest, String.class); jsonObject = new JSONObject(response.getBody()); metrics.projects = jsonObject.getJSONObject("meta").get("total").toString(); response = restTemplate. - getForEntity("https://services.openaire.eu/search/v2/api/" + contentProvidersRequest, String.class); + getForEntity(searchUrl + contentProvidersRequest, String.class); jsonObject = new JSONObject(response.getBody()); metrics.contentProviders = jsonObject.getJSONObject("meta").get("total").toString(); - System.out.println("https://services.openaire.eu/search/v2/api/" + organizationsRequest); response = restTemplate. - getForEntity("https://services.openaire.eu/search/v2/api/" + organizationsRequest, String.class); + getForEntity(searchUrl + organizationsRequest, String.class); jsonObject = new JSONObject(response.getBody()); metrics.organizations = jsonObject.getJSONObject("meta").get("total").toString(); diff --git a/src/main/java/eu/dnetlib/dnetexploremetrics/utils/PrometheusMetricsFormatter.java b/src/main/java/eu/dnetlib/dnetexploremetrics/utils/PrometheusMetricsFormatter.java index 1b13abf..73cd54e 100644 --- a/src/main/java/eu/dnetlib/dnetexploremetrics/utils/PrometheusMetricsFormatter.java +++ b/src/main/java/eu/dnetlib/dnetexploremetrics/utils/PrometheusMetricsFormatter.java @@ -7,10 +7,10 @@ public class PrometheusMetricsFormatter { private enum PrometheusMetrics { RECORDS("# TYPE explore_total_records gauge\nexplore_total_records %s"), - PUBLICATIONS("# TYPE explore_total_publications gauge\nexplore_total_publications %s"), - DATASETS("# TYPE explore_total_datasets gauge\nexplore_total_datasets %s"), - SOFTWARE("# TYPE explore_total_software gauge\nexplore_total_software %s"), - ORP("# TYPE explore_total_otherresearchproducts gauge\nexplore_total_otherresearchproducts %s"), + PUBLICATIONS("# TYPE explore_records gauge\nexplore_records{type=\"publications\"} %s"), + DATASETS("# TYPE explore_total_datasets gauge\nexplore_records{type=\"datasets\"} %s"), + SOFTWARE("# TYPE explore_total_software gauge\nexplore_records{type=\"explore_total_software\"} %s"), + ORP("# TYPE explore_total_otherresearchproducts gauge\nexplore_records{type=\"otherresearchproducts\"} %s"), FUNDERS("# TYPE explore_total_funders gauge\nexplore_total_funders %s"), PROJECTS("#TYPE explore_total_projects gauge\nexplore_total_projects %s"), CONTENT_PROVIDERS("#TYPE explore_total_contentproviders gauge\nexplore_total_contentproviders %s"), diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index a6f09c4..87d4b37 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,2 @@ -search.service.url = http://services.openaire.eu/search/ +search.service.api.url = https://services.openaire.eu/search/v2/api/ +server.port=8180