Skip to content
Snippets Groups Projects
Commit 77651473 authored by Sören Henning's avatar Sören Henning
Browse files

Refactor some null checks

parent 37da0555
No related branches found
No related tags found
No related merge requests found
Pipeline #6004 passed
......@@ -8,8 +8,7 @@ import java.net.ConnectException
/**
* [SloChecker] that uses an external source for the concrete evaluation.
* @param externalSlopeURL The url under which the external evaluation can be reached.
* @param threshold threshold that should not be exceeded to evaluate to true.
* @param warmup time that is not taken into consideration for the evaluation.
* @param metadata metadata passed to the external SLO checker.
*/
class ExternalSloChecker(
private val externalSlopeURL: String,
......@@ -26,19 +25,16 @@ class ExternalSloChecker(
* Will try to reach the external service until success or [RETRIES] times.
* Each request will timeout after [TIMEOUT].
*
* @param start point of the experiment.
* @param end point of the experiment.
* @param fetchedData that should be evaluated
* @return true if the experiment was successful(the threshold was not exceeded.
* @return true if the experiment was successful (the threshold was not exceeded).
* @throws ConnectException if the external service could not be reached.
*/
override fun evaluate(fetchedData: List<PrometheusResponse>): Boolean {
var counter = 0
val data = SloJson.Builder()
.results(fetchedData.map { it.data?.result })
.addMetadata(metadata)
.build()
.toJson()
val data = SloJson(
results = fetchedData.map { it.data?.result ?: listOf() },
metadata = metadata
).toJson()
while (counter < RETRIES) {
val result = post(externalSlopeURL, data = data, timeout = TIMEOUT)
......
......@@ -3,58 +3,17 @@ package theodolite.evaluation
import com.google.gson.Gson
import theodolite.util.PromResult
class SloJson private constructor(
val results: List<List<PromResult>?>? = null,
var metadata: MutableMap<String, Any>? = null
class SloJson constructor(
val results: List<List<PromResult>>,
var metadata: Map<String, Any>
) {
data class Builder(
var results:List<List<PromResult>?>? = null,
var metadata: MutableMap<String, Any>? = null
) {
/**
* Set the results
*
* @param results list of prometheus results
*/
fun results(results: List<List<PromResult>?>) = apply { this.results = results }
/**
* Add metadata as key value pairs
*
* @param key key of the metadata to be added
* @param value value of the metadata to be added
*/
fun addMetadata(key: String, value: Any) = apply {
if (this.metadata.isNullOrEmpty()) {
this.metadata = mutableMapOf(key to value)
} else {
this.metadata!![key] = value
}
}
/**
* Add metadata as map of key value pairs.
*
* @param metadata map of key-value pairs to be added to be added
*/
fun addMetadata(metadata: Map<String, Any>) = apply {
for (entry in metadata) {
this.addMetadata(entry.key, entry.value)
}
}
fun build() = SloJson(
results = results,
metadata = metadata
fun toJson(): String {
return Gson().toJson(
mapOf(
"results" to this.results,
"metadata" to this.metadata
)
)
}
fun toJson(): String {
return Gson().toJson(mapOf(
"results" to this.results,
"metadata" to this.metadata
))
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment