-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mohammad Qureshi <[email protected]> Co-authored-by: Rishabh Maurya <[email protected]>
- Loading branch information
1 parent
25194d6
commit dd8b281
Showing
2 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
alerting/src/main/kotlin/org/opensearch/alerting/model/AggregationResultBucket.kt
This file contains 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,93 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.alerting.model | ||
|
||
import org.opensearch.common.ParsingException | ||
import org.opensearch.common.io.stream.StreamInput | ||
import org.opensearch.common.io.stream.StreamOutput | ||
import org.opensearch.common.io.stream.Writeable | ||
import org.opensearch.common.xcontent.ToXContent | ||
import org.opensearch.common.xcontent.ToXContentObject | ||
import org.opensearch.common.xcontent.XContentBuilder | ||
import org.opensearch.common.xcontent.XContentParser | ||
import org.opensearch.common.xcontent.XContentParser.Token | ||
import org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken | ||
import java.io.IOException | ||
import java.util.Locale | ||
|
||
data class AggregationResultBucket( | ||
val parentBucketPath: String?, | ||
val bucketKeys: List<String>, | ||
val bucket: Map<String, Any>? // TODO: Should reduce contents to only top-level to not include sub-aggs here | ||
) : Writeable, ToXContentObject { | ||
|
||
@Throws(IOException::class) | ||
constructor(sin: StreamInput) : this(sin.readString(), sin.readStringList(), sin.readMap()) | ||
|
||
override fun writeTo(out: StreamOutput) { | ||
out.writeString(parentBucketPath) | ||
out.writeStringCollection(bucketKeys) | ||
out.writeMap(bucket) | ||
} | ||
|
||
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { | ||
builder.startObject() | ||
innerXContent(builder) | ||
return builder.endObject() | ||
} | ||
|
||
fun innerXContent(builder: XContentBuilder): XContentBuilder { | ||
builder.startObject(CONFIG_NAME) | ||
.field(PARENTS_BUCKET_PATH, parentBucketPath) | ||
.field(BUCKET_KEYS, bucketKeys.toTypedArray()) | ||
.field(BUCKET, bucket) | ||
.endObject() | ||
return builder | ||
} | ||
|
||
companion object { | ||
const val CONFIG_NAME = "agg_alert_content" | ||
const val PARENTS_BUCKET_PATH = "parent_bucket_path" | ||
const val BUCKET_KEYS = "bucket_keys" | ||
private const val BUCKET = "bucket" | ||
|
||
fun parse(xcp: XContentParser): AggregationResultBucket { | ||
var parentBucketPath: String? = null | ||
var bucketKeys = mutableListOf<String>() | ||
var bucket: MutableMap<String, Any>? = null | ||
ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.currentToken(), xcp) | ||
|
||
if (CONFIG_NAME != xcp.currentName()) { | ||
throw ParsingException(xcp.tokenLocation, | ||
String.format( | ||
Locale.ROOT, "Failed to parse object: expecting token with name [%s] but found [%s]", | ||
CONFIG_NAME, xcp.currentName()) | ||
) | ||
} | ||
while (xcp.nextToken() != Token.END_OBJECT) { | ||
val fieldName = xcp.currentName() | ||
xcp.nextToken() | ||
when (fieldName) { | ||
PARENTS_BUCKET_PATH -> parentBucketPath = xcp.text() | ||
BUCKET_KEYS -> { | ||
ensureExpectedToken(Token.START_ARRAY, xcp.currentToken(), xcp) | ||
while (xcp.nextToken() != Token.END_ARRAY) { | ||
bucketKeys.add(xcp.text()) | ||
} | ||
} | ||
BUCKET -> bucket = xcp.map() | ||
} | ||
} | ||
return AggregationResultBucket(parentBucketPath, bucketKeys, bucket) | ||
} | ||
} | ||
} |
This file contains 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