-
Notifications
You must be signed in to change notification settings - Fork 94
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
support list of monitor ids in Chained Monitor Findings #514
Merged
Merged
Changes from all commits
Commits
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 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
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 |
---|---|---|
|
@@ -9,63 +9,89 @@ import org.opensearch.core.xcontent.XContentBuilder | |
import org.opensearch.core.xcontent.XContentParser | ||
import org.opensearch.core.xcontent.XContentParserUtils | ||
import java.io.IOException | ||
import java.util.Collections | ||
|
||
/** | ||
* Context passed in delegate monitor to filter data queried by a monitor based on the findings of the given monitor id. | ||
* Context passed in delegate monitor to filter data matched by a list of monitors based on the findings of the given monitor ids. | ||
*/ | ||
// TODO - Remove the class and move the monitorId to Delegate (as a chainedMonitorId property) if this class won't be updated by adding new properties | ||
data class ChainedMonitorFindings( | ||
val monitorId: String | ||
val monitorId: String? = null, | ||
val monitorIds: List<String> = emptyList(), // if monitorId field is non-null it would be given precendence for BWC | ||
) : BaseModel { | ||
|
||
init { | ||
validateId(monitorId) | ||
require(!(monitorId.isNullOrBlank() && monitorIds.isEmpty())) { | ||
"at least one of fields, 'monitorIds' and 'monitorId' should be provided" | ||
} | ||
if (monitorId != null && monitorId.isBlank()) { | ||
validateId(monitorId) | ||
} else { | ||
monitorIds.forEach { validateId(it) } | ||
} | ||
} | ||
|
||
@Throws(IOException::class) | ||
constructor(sin: StreamInput) : this( | ||
sin.readString(), // monitorId | ||
sin.readOptionalString(), // monitorId | ||
Collections.unmodifiableList(sin.readStringList()) | ||
) | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun asTemplateArg(): Map<String, Any> { | ||
return mapOf( | ||
MONITOR_ID_FIELD to monitorId, | ||
) | ||
MONITOR_IDS_FIELD to monitorIds | ||
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. is it possible we merge these 2 fields to 1? it looks little complex that we have both
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. we need to have both for backward compatibility. |
||
) as Map<String, Any> | ||
} | ||
|
||
@Throws(IOException::class) | ||
override fun writeTo(out: StreamOutput) { | ||
out.writeString(monitorId) | ||
out.writeOptionalString(monitorId) | ||
out.writeStringCollection(monitorIds) | ||
} | ||
|
||
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { | ||
builder.startObject() | ||
.field(MONITOR_ID_FIELD, monitorId) | ||
.field(MONITOR_IDS_FIELD, monitorIds) | ||
.endObject() | ||
return builder | ||
} | ||
|
||
companion object { | ||
const val MONITOR_ID_FIELD = "monitor_id" | ||
const val MONITOR_IDS_FIELD = "monitor_ids" | ||
|
||
@JvmStatic | ||
@Throws(IOException::class) | ||
fun parse(xcp: XContentParser): ChainedMonitorFindings { | ||
lateinit var monitorId: String | ||
|
||
var monitorId: String? = null | ||
val monitorIds = mutableListOf<String>() | ||
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.currentToken(), xcp) | ||
while (xcp.nextToken() != XContentParser.Token.END_OBJECT) { | ||
val fieldName = xcp.currentName() | ||
xcp.nextToken() | ||
|
||
when (fieldName) { | ||
MONITOR_ID_FIELD -> { | ||
monitorId = xcp.text() | ||
validateId(monitorId) | ||
if (!xcp.currentToken().equals(XContentParser.Token.VALUE_NULL)) | ||
monitorId = xcp.text() | ||
} | ||
|
||
MONITOR_IDS_FIELD -> { | ||
XContentParserUtils.ensureExpectedToken( | ||
XContentParser.Token.START_ARRAY, | ||
xcp.currentToken(), | ||
xcp | ||
) | ||
while (xcp.nextToken() != XContentParser.Token.END_ARRAY) { | ||
monitorIds.add(xcp.text()) | ||
} | ||
} | ||
} | ||
} | ||
return ChainedMonitorFindings(monitorId) | ||
return ChainedMonitorFindings(monitorId, monitorIds) | ||
} | ||
|
||
@JvmStatic | ||
|
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
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
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
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
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.
can we use kotlin
toList
here?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.
Any specific reason to use
toList
?Collections.unmodifiableList
has been used across the code base to wrap string lists read from stream input