Skip to content

Commit 587d9cb

Browse files
authored
Move Alerting data models over to common-utils (#242)
Signed-off-by: Subhobrata Dey <[email protected]>
1 parent 72a7261 commit 587d9cb

40 files changed

+5496
-2
lines changed

build.gradle

+5-1
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,16 @@ dependencies {
7575
compileOnly "org.jetbrains.kotlin:kotlin-stdlib:${kotlin_version}"
7676
compileOnly "org.jetbrains.kotlin:kotlin-stdlib-common:${kotlin_version}"
7777
compileOnly "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3" // ${kotlin_version} does not work for coroutines
78+
compileOnly "com.cronutils:cron-utils:9.1.6"
79+
compileOnly "commons-validator:commons-validator:1.7"
7880
testImplementation "org.opensearch.test:framework:${opensearch_version}"
7981
testImplementation "org.jetbrains.kotlin:kotlin-test:${kotlin_version}"
8082
testImplementation "org.mockito:mockito-core:3.10.0"
8183
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
8284
testImplementation 'org.mockito:mockito-junit-jupiter:3.10.0'
8385
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
86+
testImplementation "com.cronutils:cron-utils:9.1.6"
87+
testImplementation "commons-validator:commons-validator:1.7"
8488
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2'
8589

8690
ktlint "com.pinterest:ktlint:0.44.0"
@@ -213,4 +217,4 @@ task updateVersion {
213217
// Include the required files that needs to be updated with new Version
214218
ant.replaceregexp(file:'build.gradle', match: '"opensearch.version", "\\d.*"', replace: '"opensearch.version", "' + newVersion.tokenize('-')[0] + '-SNAPSHOT"', flags:'g', byline:true)
215219
}
216-
}
220+
}

detekt.yml

+30-1
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,48 @@
77
style:
88
ForbiddenComment:
99
active: false
10+
LoopWithTooManyJumpStatements:
11+
maxJumpCount: 4
1012
MaxLineLength:
11-
maxLineLength: 150
13+
maxLineLength: 200
1214
ThrowsCount:
1315
active: true
1416
max: 10
1517
ReturnCount:
1618
active: true
1719
max: 10
20+
UtilityClassWithPublicConstructor:
21+
active: false
22+
23+
empty-blocks:
24+
EmptyCatchBlock:
25+
excludes: ['**/test/**']
26+
27+
exceptions:
28+
SwallowedException:
29+
excludes: ['**/test/**']
30+
ignoredExceptionTypes:
31+
- 'ZoneRulesException'
32+
- 'DateTimeException'
1833

1934
complexity:
2035
LargeClass:
2136
excludes: ['**/test/**']
2237
LongMethod:
2338
excludes: ['**/test/**']
39+
threshold: 110
2440
LongParameterList:
2541
excludes: ['**/test/**']
42+
constructorThreshold: 8
43+
ComplexMethod:
44+
threshold: 27
45+
NestedBlockDepth:
46+
threshold: 10
47+
48+
naming:
49+
ObjectPropertyNaming:
50+
constantPattern: '[_A-Za-z][_A-Za-z0-9]*'
51+
52+
performance:
53+
SpreadOperator:
54+
active: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package org.opensearch.commons.alerting
6+
7+
import org.opensearch.action.ActionListener
8+
import org.opensearch.action.ActionResponse
9+
import org.opensearch.client.node.NodeClient
10+
import org.opensearch.common.io.stream.Writeable
11+
import org.opensearch.commons.alerting.action.AlertingActions
12+
import org.opensearch.commons.alerting.action.IndexMonitorRequest
13+
import org.opensearch.commons.alerting.action.IndexMonitorResponse
14+
import org.opensearch.commons.notifications.action.BaseResponse
15+
import org.opensearch.commons.utils.recreateObject
16+
17+
/**
18+
* All the transport action plugin interfaces for the Alerting plugin
19+
*/
20+
object AlertingPluginInterface {
21+
22+
/**
23+
* Index monitor interface.
24+
* @param client Node client for making transport action
25+
* @param request The request object
26+
* @param listener The listener for getting response
27+
*/
28+
fun indexMonitor(
29+
client: NodeClient,
30+
request: IndexMonitorRequest,
31+
listener: ActionListener<IndexMonitorResponse>
32+
) {
33+
client.execute(
34+
AlertingActions.INDEX_MONITOR_ACTION_TYPE,
35+
request,
36+
wrapActionListener(listener) { response ->
37+
recreateObject(response) {
38+
IndexMonitorResponse(
39+
it
40+
)
41+
}
42+
}
43+
)
44+
}
45+
46+
@Suppress("UNCHECKED_CAST")
47+
private fun <Response : BaseResponse> wrapActionListener(
48+
listener: ActionListener<Response>,
49+
recreate: (Writeable) -> Response
50+
): ActionListener<Response> {
51+
return object : ActionListener<ActionResponse> {
52+
override fun onResponse(response: ActionResponse) {
53+
val recreated = response as? Response ?: recreate(response)
54+
listener.onResponse(recreated)
55+
}
56+
57+
override fun onFailure(exception: java.lang.Exception) {
58+
listener.onFailure(exception)
59+
}
60+
} as ActionListener<Response>
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package org.opensearch.commons.alerting.action
6+
7+
import org.opensearch.action.ActionType
8+
9+
object AlertingActions {
10+
const val INDEX_MONITOR_ACTION_NAME = "cluster:admin/opendistro/alerting/monitor/write"
11+
12+
val INDEX_MONITOR_ACTION_TYPE =
13+
ActionType(INDEX_MONITOR_ACTION_NAME, ::IndexMonitorResponse)
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.opensearch.commons.alerting.action
2+
3+
import org.opensearch.action.ActionRequest
4+
import org.opensearch.action.ActionRequestValidationException
5+
import org.opensearch.action.support.WriteRequest
6+
import org.opensearch.common.io.stream.StreamInput
7+
import org.opensearch.common.io.stream.StreamOutput
8+
import org.opensearch.commons.alerting.model.Monitor
9+
import org.opensearch.rest.RestRequest
10+
import java.io.IOException
11+
12+
class IndexMonitorRequest : ActionRequest {
13+
val monitorId: String
14+
val seqNo: Long
15+
val primaryTerm: Long
16+
val refreshPolicy: WriteRequest.RefreshPolicy
17+
val method: RestRequest.Method
18+
var monitor: Monitor
19+
20+
constructor(
21+
monitorId: String,
22+
seqNo: Long,
23+
primaryTerm: Long,
24+
refreshPolicy: WriteRequest.RefreshPolicy,
25+
method: RestRequest.Method,
26+
monitor: Monitor
27+
) : super() {
28+
this.monitorId = monitorId
29+
this.seqNo = seqNo
30+
this.primaryTerm = primaryTerm
31+
this.refreshPolicy = refreshPolicy
32+
this.method = method
33+
this.monitor = monitor
34+
}
35+
36+
@Throws(IOException::class)
37+
constructor(sin: StreamInput) : this(
38+
monitorId = sin.readString(),
39+
seqNo = sin.readLong(),
40+
primaryTerm = sin.readLong(),
41+
refreshPolicy = WriteRequest.RefreshPolicy.readFrom(sin),
42+
method = sin.readEnum(RestRequest.Method::class.java),
43+
monitor = Monitor.readFrom(sin) as Monitor
44+
)
45+
46+
override fun validate(): ActionRequestValidationException? {
47+
return null
48+
}
49+
50+
@Throws(IOException::class)
51+
override fun writeTo(out: StreamOutput) {
52+
out.writeString(monitorId)
53+
out.writeLong(seqNo)
54+
out.writeLong(primaryTerm)
55+
refreshPolicy.writeTo(out)
56+
out.writeEnum(method)
57+
monitor.writeTo(out)
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.opensearch.commons.alerting.action
2+
3+
import org.opensearch.common.io.stream.StreamInput
4+
import org.opensearch.common.io.stream.StreamOutput
5+
import org.opensearch.common.xcontent.ToXContent
6+
import org.opensearch.common.xcontent.XContentBuilder
7+
import org.opensearch.commons.alerting.model.Monitor
8+
import org.opensearch.commons.alerting.util.IndexUtils.Companion._ID
9+
import org.opensearch.commons.alerting.util.IndexUtils.Companion._PRIMARY_TERM
10+
import org.opensearch.commons.alerting.util.IndexUtils.Companion._SEQ_NO
11+
import org.opensearch.commons.alerting.util.IndexUtils.Companion._VERSION
12+
import org.opensearch.commons.notifications.action.BaseResponse
13+
import java.io.IOException
14+
15+
class IndexMonitorResponse : BaseResponse {
16+
var id: String
17+
var version: Long
18+
var seqNo: Long
19+
var primaryTerm: Long
20+
var monitor: Monitor
21+
22+
constructor(
23+
id: String,
24+
version: Long,
25+
seqNo: Long,
26+
primaryTerm: Long,
27+
monitor: Monitor
28+
) : super() {
29+
this.id = id
30+
this.version = version
31+
this.seqNo = seqNo
32+
this.primaryTerm = primaryTerm
33+
this.monitor = monitor
34+
}
35+
36+
@Throws(IOException::class)
37+
constructor(sin: StreamInput) : this(
38+
sin.readString(), // id
39+
sin.readLong(), // version
40+
sin.readLong(), // seqNo
41+
sin.readLong(), // primaryTerm
42+
Monitor.readFrom(sin) as Monitor // monitor
43+
)
44+
45+
@Throws(IOException::class)
46+
override fun writeTo(out: StreamOutput) {
47+
out.writeString(id)
48+
out.writeLong(version)
49+
out.writeLong(seqNo)
50+
out.writeLong(primaryTerm)
51+
monitor.writeTo(out)
52+
}
53+
54+
@Throws(IOException::class)
55+
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder {
56+
return builder.startObject()
57+
.field(_ID, id)
58+
.field(_VERSION, version)
59+
.field(_SEQ_NO, seqNo)
60+
.field(_PRIMARY_TERM, primaryTerm)
61+
.field("monitor", monitor)
62+
.endObject()
63+
}
64+
}

0 commit comments

Comments
 (0)