-
Notifications
You must be signed in to change notification settings - Fork 75
feat: implement histogram analytics use case #12364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dev-marek
wants to merge
5
commits into
master
Choose a base branch
from
apim-10031-return-histogram-data
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+976
−37
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
32533f6
feat: implement histogram analytics use case
dev-marek 72bd377
feat: add histogram analytics support with new data models and query …
dev-marek 7782e87
feat: implement histogram search functionality in analytics repository
dev-marek 0a47bc0
feat: update bucket mapping
dev-marek 0692ffc
feat: add histogram analytics support in API analytics resource and m…
dev-marek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...pository-api/src/main/java/io/gravitee/repository/log/v4/model/analytics/Aggregation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright © 2015 The Gravitee team (http://gravitee.io) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.gravitee.repository.log.v4.model.analytics; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
public class Aggregation { | ||
|
||
private String field; | ||
private AggregationType type; | ||
} |
23 changes: 23 additions & 0 deletions
23
...tory-api/src/main/java/io/gravitee/repository/log/v4/model/analytics/AggregationType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright © 2015 The Gravitee team (http://gravitee.io) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.gravitee.repository.log.v4.model.analytics; | ||
|
||
public enum AggregationType { | ||
FIELD, | ||
AVG, | ||
MIN, | ||
MAX, | ||
} |
30 changes: 30 additions & 0 deletions
30
...y-api/src/main/java/io/gravitee/repository/log/v4/model/analytics/HistogramAggregate.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright © 2015 The Gravitee team (http://gravitee.io) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.gravitee.repository.log.v4.model.analytics; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
public class HistogramAggregate<T extends Number> { | ||
|
||
private String field; | ||
private String name; | ||
private Map<String, List<T>> buckets; | ||
} |
35 changes: 35 additions & 0 deletions
35
...itory-api/src/main/java/io/gravitee/repository/log/v4/model/analytics/HistogramQuery.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright © 2015 The Gravitee team (http://gravitee.io) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.gravitee.repository.log.v4.model.analytics; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Builder | ||
@AllArgsConstructor | ||
public class HistogramQuery { | ||
|
||
private String apiId; | ||
private Instant from; | ||
private Instant to; | ||
private Duration interval; | ||
private List<Aggregation> aggregations; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
import io.gravitee.repository.elasticsearch.v4.analytics.adapter.SearchAverageConnectionDurationResponseAdapter; | ||
import io.gravitee.repository.elasticsearch.v4.analytics.adapter.SearchAverageMessagesPerRequestQueryAdapter; | ||
import io.gravitee.repository.elasticsearch.v4.analytics.adapter.SearchAverageMessagesPerRequestResponseAdapter; | ||
import io.gravitee.repository.elasticsearch.v4.analytics.adapter.SearchHistogramQueryAdapter; | ||
import io.gravitee.repository.elasticsearch.v4.analytics.adapter.SearchRequestResponseTimeAdapter; | ||
import io.gravitee.repository.elasticsearch.v4.analytics.adapter.SearchRequestsCountQueryAdapter; | ||
import io.gravitee.repository.elasticsearch.v4.analytics.adapter.SearchRequestsCountResponseAdapter; | ||
|
@@ -38,6 +39,8 @@ | |
import io.gravitee.repository.log.v4.model.analytics.AverageConnectionDurationQuery; | ||
import io.gravitee.repository.log.v4.model.analytics.AverageMessagesPerRequestQuery; | ||
import io.gravitee.repository.log.v4.model.analytics.CountAggregate; | ||
import io.gravitee.repository.log.v4.model.analytics.HistogramAggregate; | ||
import io.gravitee.repository.log.v4.model.analytics.HistogramQuery; | ||
import io.gravitee.repository.log.v4.model.analytics.RequestResponseTimeAggregate; | ||
import io.gravitee.repository.log.v4.model.analytics.RequestResponseTimeQueryCriteria; | ||
import io.gravitee.repository.log.v4.model.analytics.RequestsCountQuery; | ||
|
@@ -191,6 +194,17 @@ public Optional<TopFailedAggregate> searchTopFailedApis(QueryContext queryContex | |
return this.client.search(indexes, null, esQuery).map(SearchTopFailedApisAdapter::adaptResponse).blockingGet(); | ||
} | ||
|
||
@Override | ||
public List<HistogramAggregate<?>> searchHistogram(QueryContext queryContext, HistogramQuery query) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WDYT of including a Collection definitionVersions and call the appropriate index? This could help in the future if we start creating histograms from both indexes. |
||
var index = this.indexNameGenerator.getWildcardIndexName(queryContext.placeholder(), Type.V4_METRICS, clusters); | ||
var adapter = new SearchHistogramQueryAdapter(); | ||
var esQuery = adapter.adapt(query); | ||
var response = client.search(index, null, esQuery).blockingGet(); | ||
|
||
log.debug("Search histogram query: {}", esQuery); | ||
return adapter.adaptResponse(response); | ||
} | ||
|
||
private String getIndices(QueryContext queryContext, Collection<DefinitionVersion> definitionVersions) { | ||
var indexByVersion = Map.of(DefinitionVersion.V4, Type.V4_METRICS, DefinitionVersion.V2, Type.REQUEST); | ||
return definitionVersions | ||
|
193 changes: 193 additions & 0 deletions
193
...o/gravitee/repository/elasticsearch/v4/analytics/adapter/SearchHistogramQueryAdapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
/* | ||
* Copyright © 2015 The Gravitee team (http://gravitee.io) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.gravitee.repository.elasticsearch.v4.analytics.adapter; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import io.gravitee.elasticsearch.model.Aggregation; | ||
import io.gravitee.elasticsearch.model.SearchResponse; | ||
import io.gravitee.repository.log.v4.model.analytics.AggregationType; | ||
import io.gravitee.repository.log.v4.model.analytics.HistogramAggregate; | ||
import io.gravitee.repository.log.v4.model.analytics.HistogramQuery; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class SearchHistogramQueryAdapter { | ||
|
||
private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
private static final List<String> ENTRYPOINT_IDS = List.of("http-post", "http-get", "http-proxy"); | ||
private static final Map<AggregationType, String> AGGREGATION_PREFIX = Map.of( | ||
AggregationType.FIELD, | ||
"by_", | ||
AggregationType.AVG, | ||
"avg_", | ||
AggregationType.MAX, | ||
"max_", | ||
AggregationType.MIN, | ||
"min" | ||
); | ||
private static final Map<AggregationType, String> AGGREGATION_NAME = Map.of( | ||
AggregationType.FIELD, | ||
"terms", | ||
AggregationType.AVG, | ||
"avg", | ||
AggregationType.MAX, | ||
"max", | ||
AggregationType.MIN, | ||
"min" | ||
); | ||
private final Map<String, String> aggFieldMap = new LinkedHashMap<>(); | ||
private final Map<String, AggregationType> aggTypeMap = new LinkedHashMap<>(); | ||
|
||
public String adapt(HistogramQuery query) { | ||
aggFieldMap.clear(); | ||
|
||
ObjectNode root = MAPPER.createObjectNode(); | ||
root.put("size", 0); | ||
|
||
var filterArray = MAPPER.createArrayNode(); | ||
|
||
var apiIds = List.of(query.getApiId()); | ||
|
||
var mustArray = MAPPER.createArrayNode(); | ||
var apiTerms = MAPPER.createObjectNode(); | ||
apiTerms.set("terms", MAPPER.createObjectNode().set("api-id", MAPPER.valueToTree(apiIds))); | ||
mustArray.add(apiTerms); | ||
|
||
var entrypointIdTerms = MAPPER.createObjectNode(); | ||
entrypointIdTerms.set("terms", MAPPER.createObjectNode().set("entrypoint-id", MAPPER.valueToTree(ENTRYPOINT_IDS))); | ||
mustArray.add(entrypointIdTerms); | ||
|
||
var boolMust = MAPPER.createObjectNode(); | ||
boolMust.set("must", mustArray); | ||
|
||
var shouldArray = MAPPER.createArrayNode(); | ||
shouldArray.add(MAPPER.createObjectNode().set("bool", boolMust)); | ||
|
||
var boolShould = MAPPER.createObjectNode(); | ||
boolShould.put("minimum_should_match", 1); | ||
boolShould.set("should", shouldArray); | ||
|
||
filterArray.add(MAPPER.createObjectNode().set("bool", boolShould)); | ||
|
||
ObjectNode range = MAPPER.createObjectNode(); | ||
range.put("from", query.getFrom().toEpochMilli()); | ||
range.put("to", query.getTo().toEpochMilli()); | ||
range.put("include_lower", true); | ||
range.put("include_upper", true); | ||
|
||
ObjectNode rangeQuery = MAPPER.createObjectNode(); | ||
rangeQuery.set("@timestamp", range); | ||
|
||
filterArray.add(MAPPER.createObjectNode().set("range", rangeQuery)); | ||
|
||
ObjectNode bool = MAPPER.createObjectNode(); | ||
bool.set("filter", filterArray); | ||
|
||
ObjectNode queryNode = MAPPER.createObjectNode(); | ||
queryNode.set("bool", bool); | ||
|
||
root.set("query", queryNode); | ||
|
||
ObjectNode aggs = MAPPER.createObjectNode(); | ||
ObjectNode byDate = MAPPER.createObjectNode(); | ||
ObjectNode dateHistogram = MAPPER.createObjectNode(); | ||
dateHistogram.put("field", "@timestamp"); | ||
dateHistogram.put("fixed_interval", query.getInterval().toMillis() + "ms"); | ||
dateHistogram.put("min_doc_count", 0); | ||
ObjectNode extendedBounds = MAPPER.createObjectNode(); | ||
extendedBounds.put("min", query.getFrom().toEpochMilli()); | ||
extendedBounds.put("max", query.getTo().toEpochMilli()); | ||
dateHistogram.set("extended_bounds", extendedBounds); | ||
byDate.set("date_histogram", dateHistogram); | ||
|
||
aggFieldMap.put("by_date", "@timestamp"); | ||
|
||
ObjectNode subAggs = MAPPER.createObjectNode(); | ||
for (io.gravitee.repository.log.v4.model.analytics.Aggregation agg : query.getAggregations()) { | ||
String aggName = AGGREGATION_PREFIX.get(agg.getType()) + agg.getField(); | ||
String aggType = AGGREGATION_NAME.get(agg.getType()); | ||
ObjectNode subAgg = MAPPER.createObjectNode(); | ||
ObjectNode subAggAgg = MAPPER.createObjectNode(); | ||
subAggAgg.put("field", agg.getField()); | ||
subAgg.set(aggType, subAggAgg); | ||
subAggs.set(aggName, subAgg); | ||
aggFieldMap.put(aggName, agg.getField()); | ||
aggTypeMap.put(aggName, agg.getType()); | ||
} | ||
|
||
byDate.set("aggregations", subAggs); | ||
|
||
aggs.set("by_date", byDate); | ||
root.set("aggregations", aggs); | ||
|
||
return root.toString(); | ||
} | ||
|
||
public List<HistogramAggregate<?>> adaptResponse(SearchResponse response) { | ||
if (response == null || response.getAggregations() == null) { | ||
return Collections.emptyList(); | ||
} | ||
Aggregation histogramAgg = response.getAggregations().get("by_date"); | ||
if (histogramAgg == null || histogramAgg.getBuckets() == null) { | ||
return Collections.emptyList(); | ||
} | ||
List<HistogramAggregate<?>> result = new ArrayList<>(); | ||
var histogramSize = histogramAgg.getBuckets().size(); | ||
for (var aggType : aggTypeMap.entrySet()) { | ||
if (aggType.getValue() == AggregationType.FIELD) { | ||
var aggName = aggType.getKey(); | ||
var fieldName = aggFieldMap.get(aggName); | ||
var values = new HashMap<String, List<Long>>(); | ||
List<JsonNode> buckets = histogramAgg.getBuckets(); | ||
for (int i = 0; i < buckets.size(); i++) { | ||
var bucket = buckets.get(i); | ||
if (bucket.has(aggName)) { | ||
var aggBuckets = bucket.get(aggName).get("buckets"); | ||
for (var aggBucket : aggBuckets) { | ||
var key = aggBucket.get("key").asText(); | ||
if (!values.containsKey(key)) { | ||
values.put(key, new ArrayList<>(Collections.nCopies(histogramSize, 0L))); | ||
} | ||
values.get(key).set(i, aggBucket.get("doc_count").asLong()); | ||
} | ||
} | ||
} | ||
result.add(new HistogramAggregate<>(aggName, fieldName, values)); | ||
} else { | ||
var aggName = aggType.getKey(); | ||
var fieldName = aggFieldMap.get(aggName); | ||
var values = new HashMap<String, List<Double>>(); | ||
values.put(aggName, new ArrayList<>(Collections.nCopies(histogramSize, 0.0d))); | ||
List<JsonNode> buckets = histogramAgg.getBuckets(); | ||
for (int i = 0; i < buckets.size(); i++) { | ||
var bucket = buckets.get(i); | ||
if (!bucket.has(aggName)) { | ||
values.get(aggName).set(i, bucket.get("value").asDouble()); | ||
} | ||
} | ||
result.add(new HistogramAggregate<>(aggName, fieldName, values)); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you use
?
instead of reflecting more what is accepted by a HistogramAggregateT extends Number
?