From 3b8634840bb88b1287b21366a04652bf7a9d890d Mon Sep 17 00:00:00 2001 From: Anatoly Karlov Date: Mon, 14 Aug 2023 15:43:51 +0300 Subject: [PATCH] refactor by deploy --- .../limits/config/OpenSearchProperties.java | 1 + .../{opensearch => model}/LimitsData.java | 2 +- .../opensearch/OpenSearchCustomClient.java | 47 ------------ .../limits/service/LimitsService.java | 5 +- .../limits/service/OpenSearchService.java | 76 +++++++++++++++++++ src/main/resources/application.yml | 1 + 6 files changed, 81 insertions(+), 51 deletions(-) rename src/main/java/dev/vality/exporter/limits/{opensearch => model}/LimitsData.java (96%) delete mode 100644 src/main/java/dev/vality/exporter/limits/opensearch/OpenSearchCustomClient.java create mode 100644 src/main/java/dev/vality/exporter/limits/service/OpenSearchService.java diff --git a/src/main/java/dev/vality/exporter/limits/config/OpenSearchProperties.java b/src/main/java/dev/vality/exporter/limits/config/OpenSearchProperties.java index f528d43..47b2fa5 100644 --- a/src/main/java/dev/vality/exporter/limits/config/OpenSearchProperties.java +++ b/src/main/java/dev/vality/exporter/limits/config/OpenSearchProperties.java @@ -15,5 +15,6 @@ public class OpenSearchProperties { private String hostname; private Integer port; private Resource certificate; + private String index; } diff --git a/src/main/java/dev/vality/exporter/limits/opensearch/LimitsData.java b/src/main/java/dev/vality/exporter/limits/model/LimitsData.java similarity index 96% rename from src/main/java/dev/vality/exporter/limits/opensearch/LimitsData.java rename to src/main/java/dev/vality/exporter/limits/model/LimitsData.java index b37afc0..f52dbfc 100644 --- a/src/main/java/dev/vality/exporter/limits/opensearch/LimitsData.java +++ b/src/main/java/dev/vality/exporter/limits/model/LimitsData.java @@ -1,4 +1,4 @@ -package dev.vality.exporter.limits.opensearch; +package dev.vality.exporter.limits.model; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.AllArgsConstructor; diff --git a/src/main/java/dev/vality/exporter/limits/opensearch/OpenSearchCustomClient.java b/src/main/java/dev/vality/exporter/limits/opensearch/OpenSearchCustomClient.java deleted file mode 100644 index 958ba48..0000000 --- a/src/main/java/dev/vality/exporter/limits/opensearch/OpenSearchCustomClient.java +++ /dev/null @@ -1,47 +0,0 @@ -package dev.vality.exporter.limits.opensearch; - -import lombok.RequiredArgsConstructor; -import lombok.SneakyThrows; -import org.opensearch.client.json.JsonData; -import org.opensearch.client.opensearch.OpenSearchClient; -import org.opensearch.client.opensearch._types.query_dsl.Query; -import org.opensearch.client.opensearch._types.query_dsl.RangeQuery; -import org.opensearch.client.opensearch.core.SearchRequest; -import org.opensearch.client.opensearch.core.search.Hit; -import org.opensearch.client.util.ObjectBuilder; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Service; - -import java.util.List; -import java.util.stream.Collectors; - -@Service -@RequiredArgsConstructor -public class OpenSearchCustomClient { - - private final OpenSearchClient openSearchClient; - - @Value("${interval.time}") - private String intervalTime; - - @SneakyThrows - public List getLimitsData() { - var searchRequest = new SearchRequest.Builder() - .query(q -> q.match(builder -> builder.field("message") - .query(builder1 -> builder1.stringValue("Limit change commited")))) - .query(q -> q.bool(builder -> builder.filter(this::range))) - .build(); - return openSearchClient.search(searchRequest, LimitsData.class).hits().hits() - .stream() - .map(Hit::source) - .collect(Collectors.toList()); - } - - private ObjectBuilder range(Query.Builder builder1) { - return builder1.range(this::timestamp); - } - - private RangeQuery.Builder timestamp(RangeQuery.Builder builder2) { - return builder2.field("@timestamp").gte(JsonData.of(String.format("now-%ss", intervalTime))); - } -} diff --git a/src/main/java/dev/vality/exporter/limits/service/LimitsService.java b/src/main/java/dev/vality/exporter/limits/service/LimitsService.java index f3d7c06..ea48032 100644 --- a/src/main/java/dev/vality/exporter/limits/service/LimitsService.java +++ b/src/main/java/dev/vality/exporter/limits/service/LimitsService.java @@ -1,6 +1,5 @@ package dev.vality.exporter.limits.service; -import dev.vality.exporter.limits.opensearch.OpenSearchCustomClient; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -10,10 +9,10 @@ import org.springframework.stereotype.Service; @Slf4j public class LimitsService { - private final OpenSearchCustomClient openSearchCustomClient; + private final OpenSearchService openSearchService; public void registerMetrics() { - var limitsData = openSearchCustomClient.getLimitsData(); + var limitsData = openSearchService.getLimitsDataByInterval(); log.info("limitsData {}", limitsData); } } diff --git a/src/main/java/dev/vality/exporter/limits/service/OpenSearchService.java b/src/main/java/dev/vality/exporter/limits/service/OpenSearchService.java new file mode 100644 index 0000000..29f0540 --- /dev/null +++ b/src/main/java/dev/vality/exporter/limits/service/OpenSearchService.java @@ -0,0 +1,76 @@ +package dev.vality.exporter.limits.service; + +import dev.vality.exporter.limits.config.OpenSearchProperties; +import dev.vality.exporter.limits.model.LimitsData; +import lombok.RequiredArgsConstructor; +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import org.opensearch.client.json.JsonData; +import org.opensearch.client.opensearch.OpenSearchClient; +import org.opensearch.client.opensearch._types.SortOrder; +import org.opensearch.client.opensearch._types.mapping.FieldType; +import org.opensearch.client.opensearch._types.query_dsl.MatchPhraseQuery; +import org.opensearch.client.opensearch._types.query_dsl.RangeQuery; +import org.opensearch.client.opensearch.core.search.Hit; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.stream.Collectors; + +@Service +@RequiredArgsConstructor +@Slf4j +public class OpenSearchService { + + private static final String KUBERNETES_CONTAINER_NAME = "kubernetes.container_name"; + private static final String TIMESTAMP = "@timestamp"; + private static final String DATE_TIME = "date_time"; + private static final String STRICT_DATE_OPTIONAL_TIME = "strict_date_optional_time"; + private static final String HELLGATE = "hellgate"; + private static final String LIMITS = "\"Limit change commited\""; + + private final OpenSearchProperties openSearchProperties; + private final OpenSearchClient openSearchClient; + + @Value("${interval.time}") + private String intervalTime; + + @SneakyThrows + public List getLimitsDataByInterval() { + return openSearchClient.search(s -> s + .index(openSearchProperties.getIndex()) + .sort(builder -> builder + .field(builder1 -> builder1 + .field(TIMESTAMP) + .order(SortOrder.Desc) + .unmappedType(FieldType.Boolean))) + .docvalueFields(builder -> builder + .field(TIMESTAMP) + .format(DATE_TIME)) + .query(builder -> builder + .bool(builder1 -> builder1 + .must(builder2 -> builder2 + .queryString(builder3 -> builder3 + .query(LIMITS) + .analyzeWildcard(true))) + .filter(new RangeQuery.Builder() + .field(TIMESTAMP) + .gte(JsonData.of( + String.format("now-%ss", intervalTime))) + .format(STRICT_DATE_OPTIONAL_TIME) + .build() + ._toQuery(), + new MatchPhraseQuery.Builder() + .field(KUBERNETES_CONTAINER_NAME) + .query(HELLGATE) + .build() + ._toQuery()))), + LimitsData.class) + .hits() + .hits() + .stream() + .map(Hit::source) + .collect(Collectors.toList()); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index c64a1f7..4fe9e47 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -61,3 +61,4 @@ opensearch: hostname: changeit port: 9200 certificate: changeit + index: changeit