|
| 1 | +package org.opensearch.commons.alerting.model |
| 2 | + |
| 3 | +import org.opensearch.common.lucene.uid.Versions |
| 4 | +import org.opensearch.commons.alerting.model.Alert.Companion.ALERT_ID_FIELD |
| 5 | +import org.opensearch.commons.alerting.model.Alert.Companion.ALERT_VERSION_FIELD |
| 6 | +import org.opensearch.commons.alerting.model.Alert.Companion.ERROR_MESSAGE_FIELD |
| 7 | +import org.opensearch.commons.alerting.model.Alert.Companion.EXECUTION_ID_FIELD |
| 8 | +import org.opensearch.commons.alerting.model.Alert.Companion.MONITOR_ID_FIELD |
| 9 | +import org.opensearch.commons.alerting.model.Alert.Companion.MONITOR_NAME_FIELD |
| 10 | +import org.opensearch.commons.alerting.model.Alert.Companion.MONITOR_VERSION_FIELD |
| 11 | +import org.opensearch.commons.alerting.model.Alert.Companion.NO_ID |
| 12 | +import org.opensearch.commons.alerting.model.Alert.Companion.NO_VERSION |
| 13 | +import org.opensearch.commons.alerting.model.Alert.Companion.SCHEMA_VERSION_FIELD |
| 14 | +import org.opensearch.commons.alerting.model.Alert.Companion.SEVERITY_FIELD |
| 15 | +import org.opensearch.commons.alerting.model.Alert.Companion.TRIGGER_ID_FIELD |
| 16 | +import org.opensearch.commons.alerting.model.Alert.Companion.TRIGGER_NAME_FIELD |
| 17 | +import org.opensearch.commons.alerting.util.IndexUtils.Companion.NO_SCHEMA_VERSION |
| 18 | +import org.opensearch.commons.alerting.util.instant |
| 19 | +import org.opensearch.commons.alerting.util.nonOptionalTimeField |
| 20 | +import org.opensearch.commons.alerting.util.optionalTimeField |
| 21 | +import org.opensearch.core.common.io.stream.StreamInput |
| 22 | +import org.opensearch.core.common.io.stream.StreamOutput |
| 23 | +import org.opensearch.core.common.io.stream.Writeable |
| 24 | +import org.opensearch.core.xcontent.ToXContent |
| 25 | +import org.opensearch.core.xcontent.XContentBuilder |
| 26 | +import org.opensearch.core.xcontent.XContentParser |
| 27 | +import org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken |
| 28 | +import java.io.IOException |
| 29 | +import java.time.Instant |
| 30 | + |
| 31 | +data class AlertV2( |
| 32 | + val id: String = NO_ID, |
| 33 | + val version: Long = NO_VERSION, |
| 34 | + val schemaVersion: Int = NO_SCHEMA_VERSION, |
| 35 | + val monitorId: String, |
| 36 | + val monitorName: String, |
| 37 | + val monitorVersion: Long, |
| 38 | +// val monitorUser: User?, |
| 39 | + val triggerId: String, |
| 40 | + val triggerName: String, |
| 41 | + val queryResults: Map<String, Any>, |
| 42 | + val triggeredTime: Instant, |
| 43 | + val expirationTime: Instant?, |
| 44 | + val errorMessage: String? = null, |
| 45 | + val severity: String, |
| 46 | + val executionId: String? = null |
| 47 | +) : Writeable, ToXContent { |
| 48 | + @Throws(IOException::class) |
| 49 | + constructor(sin: StreamInput) : this( |
| 50 | + id = sin.readString(), |
| 51 | + version = sin.readLong(), |
| 52 | + schemaVersion = sin.readInt(), |
| 53 | + monitorId = sin.readString(), |
| 54 | + monitorName = sin.readString(), |
| 55 | + monitorVersion = sin.readLong(), |
| 56 | +// monitorUser = if (sin.readBoolean()) { |
| 57 | +// User(sin) |
| 58 | +// } else { |
| 59 | +// null |
| 60 | +// }, |
| 61 | + triggerId = sin.readString(), |
| 62 | + triggerName = sin.readString(), |
| 63 | + queryResults = sin.readMap()!!.toMap(), |
| 64 | + triggeredTime = sin.readInstant(), |
| 65 | + expirationTime = sin.readOptionalInstant(), |
| 66 | + errorMessage = sin.readOptionalString(), |
| 67 | + severity = sin.readString(), |
| 68 | + executionId = sin.readOptionalString() |
| 69 | + ) |
| 70 | + |
| 71 | + @Throws(IOException::class) |
| 72 | + override fun writeTo(out: StreamOutput) { |
| 73 | + out.writeString(id) |
| 74 | + out.writeLong(version) |
| 75 | + out.writeInt(schemaVersion) |
| 76 | + out.writeString(monitorId) |
| 77 | + out.writeString(monitorName) |
| 78 | + out.writeLong(monitorVersion) |
| 79 | +// out.writeBoolean(monitorUser != null) |
| 80 | +// monitorUser?.writeTo(out) |
| 81 | + out.writeString(triggerId) |
| 82 | + out.writeString(triggerName) |
| 83 | + out.writeMap(queryResults) |
| 84 | + out.writeInstant(triggeredTime) |
| 85 | + out.writeOptionalInstant(expirationTime) |
| 86 | + out.writeOptionalString(errorMessage) |
| 87 | + out.writeString(severity) |
| 88 | + out.writeOptionalString(executionId) |
| 89 | + } |
| 90 | + |
| 91 | + override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { |
| 92 | + builder.startObject() |
| 93 | + .field(ALERT_ID_FIELD, id) |
| 94 | + .field(ALERT_VERSION_FIELD, version) |
| 95 | + .field(MONITOR_ID_FIELD, monitorId) |
| 96 | + .field(SCHEMA_VERSION_FIELD, schemaVersion) |
| 97 | + .field(MONITOR_VERSION_FIELD, monitorVersion) |
| 98 | + .field(MONITOR_NAME_FIELD, monitorName) |
| 99 | + .field(EXECUTION_ID_FIELD, executionId) |
| 100 | + .field(TRIGGER_ID_FIELD, triggerId) |
| 101 | + .field(TRIGGER_NAME_FIELD, triggerName) |
| 102 | + .field(QUERY_RESULTS_FIELD, queryResults) |
| 103 | + .field(ERROR_MESSAGE_FIELD, errorMessage) |
| 104 | + .field(SEVERITY_FIELD, severity) |
| 105 | + .nonOptionalTimeField(TRIGGERED_TIME_FIELD, triggeredTime) |
| 106 | + .optionalTimeField(EXPIRATION_TIME_FIELD, expirationTime) |
| 107 | + .endObject() |
| 108 | + |
| 109 | +// if (!secure) { |
| 110 | +// builder.optionalUserField(MONITOR_USER_FIELD, monitorUser) |
| 111 | +// } |
| 112 | + |
| 113 | + return builder |
| 114 | + } |
| 115 | + |
| 116 | + fun asTemplateArg(): Map<String, Any?> { |
| 117 | + return mapOf( |
| 118 | + ALERT_ID_FIELD to id, |
| 119 | + ALERT_VERSION_FIELD to version, |
| 120 | + ERROR_MESSAGE_FIELD to errorMessage, |
| 121 | + EXECUTION_ID_FIELD to executionId, |
| 122 | + EXPIRATION_TIME_FIELD to expirationTime?.toEpochMilli(), |
| 123 | + SEVERITY_FIELD to severity |
| 124 | + ) |
| 125 | + } |
| 126 | + |
| 127 | + companion object { |
| 128 | + const val TRIGGERED_TIME_FIELD = "triggered_time" |
| 129 | + const val EXPIRATION_TIME_FIELD = "expiration_time" |
| 130 | + const val QUERY_RESULTS_FIELD = "query_results" |
| 131 | + |
| 132 | + @JvmStatic |
| 133 | + @JvmOverloads |
| 134 | + @Throws(IOException::class) |
| 135 | + fun parse(xcp: XContentParser, id: String = NO_ID, version: Long = NO_VERSION): AlertV2 { |
| 136 | + var schemaVersion = NO_SCHEMA_VERSION |
| 137 | + lateinit var monitorId: String |
| 138 | + lateinit var monitorName: String |
| 139 | + var monitorVersion: Long = Versions.NOT_FOUND |
| 140 | +// var monitorUser: User? = null |
| 141 | + lateinit var triggerId: String |
| 142 | + lateinit var triggerName: String |
| 143 | + var queryResults: Map<String, Any> = mapOf() |
| 144 | + lateinit var severity: String |
| 145 | + var triggeredTime: Instant? = null |
| 146 | + var expirationTime: Instant? = null |
| 147 | + var errorMessage: String? = null |
| 148 | + var executionId: String? = null |
| 149 | + |
| 150 | + ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.currentToken(), xcp) |
| 151 | + while (xcp.nextToken() != XContentParser.Token.END_OBJECT) { |
| 152 | + val fieldName = xcp.currentName() |
| 153 | + xcp.nextToken() |
| 154 | + |
| 155 | + when (fieldName) { |
| 156 | + MONITOR_ID_FIELD -> monitorId = xcp.text() |
| 157 | + SCHEMA_VERSION_FIELD -> schemaVersion = xcp.intValue() |
| 158 | + MONITOR_NAME_FIELD -> monitorName = xcp.text() |
| 159 | + MONITOR_VERSION_FIELD -> monitorVersion = xcp.longValue() |
| 160 | +// MONITOR_USER_FIELD -> |
| 161 | +// monitorUser = if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) { |
| 162 | +// null |
| 163 | +// } else { |
| 164 | +// User.parse(xcp) |
| 165 | +// } |
| 166 | + TRIGGER_ID_FIELD -> triggerId = xcp.text() |
| 167 | + TRIGGER_NAME_FIELD -> triggerName = xcp.text() |
| 168 | + QUERY_RESULTS_FIELD -> queryResults = xcp.map() |
| 169 | + TRIGGERED_TIME_FIELD -> triggeredTime = xcp.instant() |
| 170 | + EXPIRATION_TIME_FIELD -> expirationTime = xcp.instant() |
| 171 | + ERROR_MESSAGE_FIELD -> errorMessage = xcp.textOrNull() |
| 172 | + EXECUTION_ID_FIELD -> executionId = xcp.textOrNull() |
| 173 | + SEVERITY_FIELD -> severity = xcp.text() |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return AlertV2( |
| 178 | + id = id, |
| 179 | + version = version, |
| 180 | + schemaVersion = schemaVersion, |
| 181 | + monitorId = requireNotNull(monitorId), |
| 182 | + monitorName = requireNotNull(monitorName), |
| 183 | + monitorVersion = monitorVersion, |
| 184 | +// monitorUser = monitorUser, |
| 185 | + triggerId = requireNotNull(triggerId), |
| 186 | + triggerName = requireNotNull(triggerName), |
| 187 | + queryResults = requireNotNull(queryResults), |
| 188 | + triggeredTime = requireNotNull(triggeredTime), |
| 189 | + expirationTime = expirationTime, |
| 190 | + errorMessage = errorMessage, |
| 191 | + severity = severity, |
| 192 | + executionId = executionId |
| 193 | + ) |
| 194 | + } |
| 195 | + |
| 196 | + @JvmStatic |
| 197 | + @Throws(IOException::class) |
| 198 | + fun readFrom(sin: StreamInput): AlertV2 { |
| 199 | + return AlertV2(sin) |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments