Skip to content

Commit

Permalink
Run ktlintFormat
Browse files Browse the repository at this point in the history
Signed-off-by: Chase Engelbrecht <[email protected]>
  • Loading branch information
engechas committed Mar 13, 2024
1 parent 11250f8 commit 479d989
Show file tree
Hide file tree
Showing 35 changed files with 147 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class AcknowledgeAlertResponse : BaseResponse {

@Throws(IOException::class)
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder {

builder.startObject().startArray("success")
acknowledged.forEach { builder.value(it.id) }
builder.endArray().startArray("failed")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ object AlertingActions {
@JvmField
val INDEX_MONITOR_ACTION_TYPE =
ActionType(INDEX_MONITOR_ACTION_NAME, ::IndexMonitorResponse)

@JvmField
val GET_ALERTS_ACTION_TYPE =
ActionType(GET_ALERTS_ACTION_NAME, ::GetAlertsResponse)

@JvmField
val DELETE_MONITOR_ACTION_TYPE =
ActionType(DELETE_MONITOR_ACTION_NAME, ::DeleteMonitorResponse)

@JvmField
val GET_FINDINGS_ACTION_TYPE =
ActionType(GET_FINDINGS_ACTION_NAME, ::GetFindingsResponse)

@JvmField
val ACKNOWLEDGE_ALERTS_ACTION_TYPE =
ActionType(ACKNOWLEDGE_ALERTS_ACTION_NAME, ::AcknowledgeAlertResponse)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ class BucketSelectorExtAggregator : SiblingPipelineAggregator {
}

return BucketSelectorIndices(
name(), parentBucketPath, selectedBucketsIndex, originalAgg.metadata
name(),
parentBucketPath,
selectedBucketsIndex,
originalAgg.metadata
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import java.io.IOException
class BucketSelectorExtFilter : BaseModel {
// used for composite aggregations
val filtersMap: HashMap<String, IncludeExclude>?

// used for filtering string term aggregation
val filters: IncludeExclude?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ data class AlertError(val timestamp: Instant, val message: String) : Writeable,
@JvmStatic
@Throws(IOException::class)
fun parse(xcp: XContentParser): AlertError {

lateinit var timestamp: Instant
lateinit var message: String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ data class AggregationResultBucket(
throw ParsingException(
xcp.tokenLocation,
String.format(
Locale.ROOT, "Failed to parse object: expecting token with name [%s] but found [%s]",
CONFIG_NAME, xcp.currentName()
Locale.ROOT,
"Failed to parse object: expecting token with name [%s] but found [%s]",
CONFIG_NAME,
xcp.currentName()
)
)
}
Expand Down
14 changes: 9 additions & 5 deletions src/main/kotlin/org/opensearch/commons/alerting/model/Alert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ data class Alert(
) : Writeable, ToXContent {

init {
if (errorMessage != null) require(state == State.DELETED || state == State.ERROR) {
"Attempt to create an alert with an error in state: $state"
if (errorMessage != null) {
require(state == State.DELETED || state == State.ERROR) {
"Attempt to create an alert with an error in state: $state"
}
}
}

Expand Down Expand Up @@ -139,7 +141,9 @@ data class Alert(
monitorVersion = sin.readLong(),
monitorUser = if (sin.readBoolean()) {
User(sin)
} else null,
} else {
null
},
triggerId = sin.readString(),
triggerName = sin.readString(),
findingIds = sin.readStringList(),
Expand Down Expand Up @@ -216,10 +220,10 @@ data class Alert(
const val NO_ID = ""
const val NO_VERSION = Versions.NOT_FOUND

@JvmStatic @JvmOverloads
@JvmStatic
@JvmOverloads
@Throws(IOException::class)
fun parse(xcp: XContentParser, id: String = NO_ID, version: Long = NO_VERSION): Alert {

lateinit var monitorId: String
var schemaVersion = NO_SCHEMA_VERSION
lateinit var monitorName: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ data class BucketLevelTrigger(
const val PARENT_BUCKET_PATH = "parentBucketPath"

val XCONTENT_REGISTRY = NamedXContentRegistry.Entry(
Trigger::class.java, ParseField(BUCKET_LEVEL_TRIGGER_FIELD),
Trigger::class.java,
ParseField(BUCKET_LEVEL_TRIGGER_FIELD),
CheckedFunction { parseInner(it) }
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ data class ClusterMetricsInput(
"Invalid URI constructed from the path and path_params inputs, or the url input."
}

if (url.isNotEmpty() && validateFieldsNotEmpty())
if (url.isNotEmpty() && validateFieldsNotEmpty()) {
require(constructedUri == constructUrlFromInputs()) {
"The provided URL and URI fields form different URLs."
}
}

require(constructedUri.host.lowercase() == SUPPORTED_HOST) {
"Only host '$SUPPORTED_HOST' is supported."
Expand Down Expand Up @@ -104,7 +105,8 @@ data class ClusterMetricsInput(
/**
* This parse function uses [XContentParser] to parse JSON input and store corresponding fields to create a [ClusterMetricsInput] object
*/
@JvmStatic @Throws(IOException::class)
@JvmStatic
@Throws(IOException::class)
fun parseInner(xcp: XContentParser): ClusterMetricsInput {
var path = ""
var pathParams = ""
Expand Down Expand Up @@ -161,17 +163,20 @@ data class ClusterMetricsInput(
if (pathParams.isNotEmpty()) {
pathParams = pathParams.trim('/')
ILLEGAL_PATH_PARAMETER_CHARACTERS.forEach { character ->
if (pathParams.contains(character))
if (pathParams.contains(character)) {
throw IllegalArgumentException(
"The provided path parameters contain invalid characters or spaces. Please omit: " + "${ILLEGAL_PATH_PARAMETER_CHARACTERS.joinToString(" ")}"
)
}
}
}

if (apiType.requiresPathParams && pathParams.isEmpty())
if (apiType.requiresPathParams && pathParams.isEmpty()) {
throw IllegalArgumentException("The API requires path parameters.")
if (!apiType.supportsPathParams && pathParams.isNotEmpty())
}
if (!apiType.supportsPathParams && pathParams.isNotEmpty()) {
throw IllegalArgumentException("The API does not use path parameters.")
}

return pathParams
}
Expand All @@ -187,11 +192,13 @@ data class ClusterMetricsInput(
ClusterMetricType.values()
.filter { option -> option != ClusterMetricType.BLANK }
.forEach { option ->
if (uriPath.startsWith(option.prependPath) || uriPath.startsWith(option.defaultPath))
if (uriPath.startsWith(option.prependPath) || uriPath.startsWith(option.defaultPath)) {
apiType = option
}
}
if (apiType.isBlank())
if (apiType.isBlank()) {
throw IllegalArgumentException("The API could not be determined from the provided URI.")
}
return apiType
}

Expand All @@ -213,12 +220,15 @@ data class ClusterMetricsInput(
* If [path] and [pathParams] are empty, populates them with values from [url].
*/
private fun parseEmptyFields() {
if (pathParams.isEmpty())
if (pathParams.isEmpty()) {
pathParams = this.parsePathParams()
if (path.isEmpty())
}
if (path.isEmpty()) {
path = if (pathParams.isEmpty()) clusterMetricType.defaultPath else clusterMetricType.prependPath
if (url.isEmpty())
}
if (url.isEmpty()) {
url = constructedUri.toString()
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ data class DataSources(
ALERTS_HISTORY_INDEX_FIELD to alertsHistoryIndex,
ALERTS_HISTORY_INDEX_PATTERN_FIELD to alertsHistoryIndexPattern,
QUERY_INDEX_MAPPINGS_BY_TYPE to queryIndexMappingsByType,
FINDINGS_ENABLED_FIELD to findingsEnabled,
FINDINGS_ENABLED_FIELD to findingsEnabled
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ data class DocLevelMonitorInput(

val XCONTENT_REGISTRY = NamedXContentRegistry.Entry(
Input::class.java,
ParseField(DOC_LEVEL_INPUT_FIELD), CheckedFunction { parse(it) }
ParseField(DOC_LEVEL_INPUT_FIELD),
CheckedFunction { parse(it) }
)

@JvmStatic @Throws(IOException::class)
@JvmStatic
@Throws(IOException::class)
fun parse(xcp: XContentParser): DocLevelMonitorInput {
var description: String = NO_DESCRIPTION
val indices: MutableList<String> = mutableListOf()
Expand Down Expand Up @@ -106,7 +108,8 @@ data class DocLevelMonitorInput(
return DocLevelMonitorInput(description = description, indices = indices, queries = docLevelQueries)
}

@JvmStatic @Throws(IOException::class)
@JvmStatic
@Throws(IOException::class)
fun readFrom(sin: StreamInput): DocLevelMonitorInput {
return DocLevelMonitorInput(sin)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ data class DocLevelQuery(
const val NO_ID = ""
val INVALID_CHARACTERS: List<String> = listOf(" ", "[", "]", "{", "}", "(", ")")

@JvmStatic @Throws(IOException::class)
@JvmStatic
@Throws(IOException::class)
fun parse(xcp: XContentParser): DocLevelQuery {
var id: String = UUID.randomUUID().toString()
lateinit var query: String
Expand Down Expand Up @@ -111,7 +112,8 @@ data class DocLevelQuery(
)
}

@JvmStatic @Throws(IOException::class)
@JvmStatic
@Throws(IOException::class)
fun readFrom(sin: StreamInput): DocLevelQuery {
return DocLevelQuery(sin)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ data class DocumentLevelTrigger(
const val QUERY_IDS_FIELD = "query_ids"

val XCONTENT_REGISTRY = NamedXContentRegistry.Entry(
Trigger::class.java, ParseField(DOCUMENT_LEVEL_TRIGGER_FIELD),
Trigger::class.java,
ParseField(DOCUMENT_LEVEL_TRIGGER_FIELD),
CheckedFunction { parseInner(it) }
)

@JvmStatic @Throws(IOException::class)
@JvmStatic
@Throws(IOException::class)
fun parseInner(xcp: XContentParser): DocumentLevelTrigger {
var id = UUIDs.base64UUID() // assign a default triggerId if one is not specified
lateinit var name: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ class Finding(
const val TIMESTAMP_FIELD = "timestamp"
const val NO_ID = ""

@JvmStatic @JvmOverloads
@JvmStatic
@JvmOverloads
@Throws(IOException::class)
fun parse(xcp: XContentParser): Finding {
var id: String = NO_ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class FindingDocument(
const val NO_ID = ""
const val NO_INDEX = ""

@JvmStatic @JvmOverloads
@JvmStatic
@JvmOverloads
@Throws(IOException::class)
fun parse(xcp: XContentParser, id: String = NO_ID, index: String = NO_INDEX): FindingDocument {
var found = false
Expand Down
21 changes: 15 additions & 6 deletions src/main/kotlin/org/opensearch/commons/alerting/model/Monitor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ data class Monitor(
monitorType = sin.readEnum(MonitorType::class.java),
user = if (sin.readBoolean()) {
User(sin)
} else null,
} else {
null
},
schemaVersion = sin.readInt(),
inputs = sin.readList((Input)::readFrom),
triggers = sin.readList((Trigger)::readFrom),
Expand Down Expand Up @@ -181,8 +183,11 @@ data class Monitor(
// Outputting type with each Input so that the generic Input.readFrom() can read it
out.writeVInt(inputs.size)
inputs.forEach {
if (it is SearchInput) out.writeEnum(Input.Type.SEARCH_INPUT)
else out.writeEnum(Input.Type.DOCUMENT_LEVEL_INPUT)
if (it is SearchInput) {
out.writeEnum(Input.Type.SEARCH_INPUT)
} else {
out.writeEnum(Input.Type.DOCUMENT_LEVEL_INPUT)
}
it.writeTo(out)
}
// Outputting type with each Trigger so that the generic Trigger.readFrom() can read it
Expand Down Expand Up @@ -273,8 +278,9 @@ data class Monitor(
)
while (xcp.nextToken() != XContentParser.Token.END_ARRAY) {
val input = Input.parse(xcp)
if (input is ClusterMetricsInput)
if (input is ClusterMetricsInput) {
supportedClusterMetricsSettings?.validateApiType(input)
}
inputs.add(input)
}
}
Expand All @@ -291,8 +297,11 @@ data class Monitor(
ENABLED_TIME_FIELD -> enabledTime = xcp.instant()
LAST_UPDATE_TIME_FIELD -> lastUpdateTime = xcp.instant()
UI_METADATA_FIELD -> uiMetadata = xcp.map()
DATA_SOURCES_FIELD -> dataSources = if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) DataSources()
else DataSources.parse(xcp)
DATA_SOURCES_FIELD -> dataSources = if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) {
DataSources()
} else {
DataSources.parse(xcp)
}
OWNER_FIELD -> owner = if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) owner else xcp.text()
else -> {
xcp.skipChildren()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ data class QueryLevelTrigger(
/** Returns a representation of the trigger suitable for passing into painless and mustache scripts. */
fun asTemplateArg(): Map<String, Any> {
return mapOf(
ID_FIELD to id, NAME_FIELD to name, SEVERITY_FIELD to severity,
ID_FIELD to id,
NAME_FIELD to name,
SEVERITY_FIELD to severity,
ACTIONS_FIELD to actions.map { it.asTemplateArg() }
)
}
Expand All @@ -77,7 +79,8 @@ data class QueryLevelTrigger(
const val SCRIPT_FIELD = "script"

val XCONTENT_REGISTRY = NamedXContentRegistry.Entry(
Trigger::class.java, ParseField(QUERY_LEVEL_TRIGGER_FIELD),
Trigger::class.java,
ParseField(QUERY_LEVEL_TRIGGER_FIELD),
CheckedFunction { parseInner(it) }
)

Expand Down Expand Up @@ -113,7 +116,8 @@ data class QueryLevelTrigger(
* It isn't typically conventional but this parse method will account for both START_OBJECT
* and FIELD_NAME as the starting token to cover both cases.
*/
@JvmStatic @Throws(IOException::class)
@JvmStatic
@Throws(IOException::class)
fun parseInner(xcp: XContentParser): QueryLevelTrigger {
var id = UUIDs.base64UUID() // assign a default triggerId if one is not specified
lateinit var name: String
Expand Down
Loading

0 comments on commit 479d989

Please sign in to comment.