Skip to content
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

[Backport for PR #227] Move Alerting data models over to common-utils #242

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,16 @@ dependencies {
compileOnly "org.jetbrains.kotlin:kotlin-stdlib:${kotlin_version}"
compileOnly "org.jetbrains.kotlin:kotlin-stdlib-common:${kotlin_version}"
compileOnly "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3" // ${kotlin_version} does not work for coroutines
compileOnly "com.cronutils:cron-utils:9.1.6"
compileOnly "commons-validator:commons-validator:1.7"
testImplementation "org.opensearch.test:framework:${opensearch_version}"
testImplementation "org.jetbrains.kotlin:kotlin-test:${kotlin_version}"
testImplementation "org.mockito:mockito-core:3.10.0"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
testImplementation 'org.mockito:mockito-junit-jupiter:3.10.0'
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
testImplementation "com.cronutils:cron-utils:9.1.6"
testImplementation "commons-validator:commons-validator:1.7"
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'

ktlint "com.pinterest:ktlint:0.44.0"
Expand Down Expand Up @@ -213,4 +217,4 @@ task updateVersion {
// Include the required files that needs to be updated with new Version
ant.replaceregexp(file:'build.gradle', match: '"opensearch.version", "\\d.*"', replace: '"opensearch.version", "' + newVersion.tokenize('-')[0] + '-SNAPSHOT"', flags:'g', byline:true)
}
}
}
31 changes: 30 additions & 1 deletion detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,48 @@
style:
ForbiddenComment:
active: false
LoopWithTooManyJumpStatements:
maxJumpCount: 4
MaxLineLength:
maxLineLength: 150
maxLineLength: 200
ThrowsCount:
active: true
max: 10
ReturnCount:
active: true
max: 10
UtilityClassWithPublicConstructor:
active: false

empty-blocks:
EmptyCatchBlock:
excludes: ['**/test/**']

exceptions:
SwallowedException:
excludes: ['**/test/**']
ignoredExceptionTypes:
- 'ZoneRulesException'
- 'DateTimeException'

complexity:
LargeClass:
excludes: ['**/test/**']
LongMethod:
excludes: ['**/test/**']
threshold: 110
LongParameterList:
excludes: ['**/test/**']
constructorThreshold: 8
ComplexMethod:
threshold: 27
NestedBlockDepth:
threshold: 10

naming:
ObjectPropertyNaming:
constantPattern: '[_A-Za-z][_A-Za-z0-9]*'

performance:
SpreadOperator:
active: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.commons.alerting

import org.opensearch.action.ActionListener
import org.opensearch.action.ActionResponse
import org.opensearch.client.node.NodeClient
import org.opensearch.common.io.stream.Writeable
import org.opensearch.commons.alerting.action.AlertingActions
import org.opensearch.commons.alerting.action.IndexMonitorRequest
import org.opensearch.commons.alerting.action.IndexMonitorResponse
import org.opensearch.commons.notifications.action.BaseResponse
import org.opensearch.commons.utils.recreateObject

/**
* All the transport action plugin interfaces for the Alerting plugin
*/
object AlertingPluginInterface {

/**
* Index monitor interface.
* @param client Node client for making transport action
* @param request The request object
* @param listener The listener for getting response
*/
fun indexMonitor(
client: NodeClient,
request: IndexMonitorRequest,
listener: ActionListener<IndexMonitorResponse>
) {
client.execute(
AlertingActions.INDEX_MONITOR_ACTION_TYPE,
request,
wrapActionListener(listener) { response ->
recreateObject(response) {
IndexMonitorResponse(
it
)
}
}
)
}

@Suppress("UNCHECKED_CAST")
private fun <Response : BaseResponse> wrapActionListener(
listener: ActionListener<Response>,
recreate: (Writeable) -> Response
): ActionListener<Response> {
return object : ActionListener<ActionResponse> {
override fun onResponse(response: ActionResponse) {
val recreated = response as? Response ?: recreate(response)
listener.onResponse(recreated)
}

override fun onFailure(exception: java.lang.Exception) {
listener.onFailure(exception)
}
} as ActionListener<Response>
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.commons.alerting.action

import org.opensearch.action.ActionType

object AlertingActions {
const val INDEX_MONITOR_ACTION_NAME = "cluster:admin/opendistro/alerting/monitor/write"

val INDEX_MONITOR_ACTION_TYPE =
ActionType(INDEX_MONITOR_ACTION_NAME, ::IndexMonitorResponse)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.opensearch.commons.alerting.action

import org.opensearch.action.ActionRequest
import org.opensearch.action.ActionRequestValidationException
import org.opensearch.action.support.WriteRequest
import org.opensearch.common.io.stream.StreamInput
import org.opensearch.common.io.stream.StreamOutput
import org.opensearch.commons.alerting.model.Monitor
import org.opensearch.rest.RestRequest
import java.io.IOException

class IndexMonitorRequest : ActionRequest {
val monitorId: String
val seqNo: Long
val primaryTerm: Long
val refreshPolicy: WriteRequest.RefreshPolicy
val method: RestRequest.Method
var monitor: Monitor

constructor(
monitorId: String,
seqNo: Long,
primaryTerm: Long,
refreshPolicy: WriteRequest.RefreshPolicy,
method: RestRequest.Method,
monitor: Monitor
) : super() {
this.monitorId = monitorId
this.seqNo = seqNo
this.primaryTerm = primaryTerm
this.refreshPolicy = refreshPolicy
this.method = method
this.monitor = monitor
}

@Throws(IOException::class)
constructor(sin: StreamInput) : this(
monitorId = sin.readString(),
seqNo = sin.readLong(),
primaryTerm = sin.readLong(),
refreshPolicy = WriteRequest.RefreshPolicy.readFrom(sin),
method = sin.readEnum(RestRequest.Method::class.java),
monitor = Monitor.readFrom(sin) as Monitor
)

override fun validate(): ActionRequestValidationException? {
return null
}

@Throws(IOException::class)
override fun writeTo(out: StreamOutput) {
out.writeString(monitorId)
out.writeLong(seqNo)
out.writeLong(primaryTerm)
refreshPolicy.writeTo(out)
out.writeEnum(method)
monitor.writeTo(out)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.opensearch.commons.alerting.action

import org.opensearch.common.io.stream.StreamInput
import org.opensearch.common.io.stream.StreamOutput
import org.opensearch.common.xcontent.ToXContent
import org.opensearch.common.xcontent.XContentBuilder
import org.opensearch.commons.alerting.model.Monitor
import org.opensearch.commons.alerting.util.IndexUtils.Companion._ID
import org.opensearch.commons.alerting.util.IndexUtils.Companion._PRIMARY_TERM
import org.opensearch.commons.alerting.util.IndexUtils.Companion._SEQ_NO
import org.opensearch.commons.alerting.util.IndexUtils.Companion._VERSION
import org.opensearch.commons.notifications.action.BaseResponse
import java.io.IOException

class IndexMonitorResponse : BaseResponse {
var id: String
var version: Long
var seqNo: Long
var primaryTerm: Long
var monitor: Monitor

constructor(
id: String,
version: Long,
seqNo: Long,
primaryTerm: Long,
monitor: Monitor
) : super() {
this.id = id
this.version = version
this.seqNo = seqNo
this.primaryTerm = primaryTerm
this.monitor = monitor
}

@Throws(IOException::class)
constructor(sin: StreamInput) : this(
sin.readString(), // id
sin.readLong(), // version
sin.readLong(), // seqNo
sin.readLong(), // primaryTerm
Monitor.readFrom(sin) as Monitor // monitor
)

@Throws(IOException::class)
override fun writeTo(out: StreamOutput) {
out.writeString(id)
out.writeLong(version)
out.writeLong(seqNo)
out.writeLong(primaryTerm)
monitor.writeTo(out)
}

@Throws(IOException::class)
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder {
return builder.startObject()
.field(_ID, id)
.field(_VERSION, version)
.field(_SEQ_NO, seqNo)
.field(_PRIMARY_TERM, primaryTerm)
.field("monitor", monitor)
.endObject()
}
}
Loading