-
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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()) | ||
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. can we use kotlin 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. Any specific reason to use |
||
) | ||
|
||
@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 | ||
|
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.
is the
,
required?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.
reverted