refactor by deploy

This commit is contained in:
Anatoly Karlov 2023-08-14 15:43:51 +03:00
parent 39c452860f
commit 3b8634840b
6 changed files with 81 additions and 51 deletions

View File

@ -15,5 +15,6 @@ public class OpenSearchProperties {
private String hostname;
private Integer port;
private Resource certificate;
private String index;
}

View File

@ -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;

View File

@ -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<LimitsData> 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<Query> 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)));
}
}

View File

@ -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);
}
}

View File

@ -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<LimitsData> 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());
}
}

View File

@ -61,3 +61,4 @@ opensearch:
hostname: changeit
port: 9200
certificate: changeit
index: changeit