From 776514734e19130652bf3f6b1b8517673ca7cfbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Henning?= <soeren.henning@email.uni-kiel.de> Date: Sun, 16 Jan 2022 15:31:23 +0100 Subject: [PATCH] Refactor some null checks --- .../evaluation/ExternalSloChecker.kt | 16 ++--- .../kotlin/theodolite/evaluation/SloJson.kt | 59 +++---------------- 2 files changed, 15 insertions(+), 60 deletions(-) diff --git a/theodolite/src/main/kotlin/theodolite/evaluation/ExternalSloChecker.kt b/theodolite/src/main/kotlin/theodolite/evaluation/ExternalSloChecker.kt index bdbdbc53e..7fb5417e2 100644 --- a/theodolite/src/main/kotlin/theodolite/evaluation/ExternalSloChecker.kt +++ b/theodolite/src/main/kotlin/theodolite/evaluation/ExternalSloChecker.kt @@ -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) diff --git a/theodolite/src/main/kotlin/theodolite/evaluation/SloJson.kt b/theodolite/src/main/kotlin/theodolite/evaluation/SloJson.kt index 0c643c009..205389276 100644 --- a/theodolite/src/main/kotlin/theodolite/evaluation/SloJson.kt +++ b/theodolite/src/main/kotlin/theodolite/evaluation/SloJson.kt @@ -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 -- GitLab