From d80dd0c3f2eb7e4833562d18f4e5aeb756a68452 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=B6ren=20Henning?= <soeren.henning@email.uni-kiel.de>
Date: Thu, 8 Dec 2022 11:18:33 +0100
Subject: [PATCH] Support overriding threshold

---
 .../kubernetes/slo/SloCheckerFactory.kt       | 14 +++---
 .../kubernetes/slo/SloCheckerFactoryTest.kt   | 45 +++++++++++++++++++
 2 files changed, 52 insertions(+), 7 deletions(-)

diff --git a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/SloCheckerFactory.kt b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/SloCheckerFactory.kt
index 1f37142ad..c26643648 100644
--- a/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/SloCheckerFactory.kt
+++ b/theodolite/src/main/kotlin/rocks/theodolite/kubernetes/slo/SloCheckerFactory.kt
@@ -60,9 +60,9 @@ class SloCheckerFactory {
                     "repetitionAggregation" to (properties["repetitionAggregation"]
                         ?: throw IllegalArgumentException("repetitionAggregation expected")),
                     "operator" to (properties["operator"] ?: throw IllegalArgumentException("operator expected")),
-                    "threshold" to (properties["threshold"]?.toDouble()
-                        ?: properties["thresholdRelToLoad"]?.toDouble()?.times(load)
-                        ?: properties["thresholdRelToResources"]?.toDouble()?.times(resources)
+                    "threshold" to (properties["threshold"]?.toDoubleOrNull()
+                        ?: properties["thresholdRelToLoad"]?.toDoubleOrNull()?.times(load)
+                        ?: properties["thresholdRelToResources"]?.toDoubleOrNull()?.times(resources)
                         ?: throw IllegalArgumentException("'threshold', 'thresholdRelToLoad' or 'thresholdRelToResources' expected"))
                 )
             )
@@ -71,10 +71,10 @@ class SloCheckerFactory {
                     ?: throw IllegalArgumentException("externalSloUrl expected"),
                 metadata = mapOf(
                     "warmup" to (properties["warmup"]?.toInt() ?: throw IllegalArgumentException("warmup expected")),
-                    "threshold" to (properties["threshold"]?.toDouble()
-                        ?: properties["thresholdRelToLoad"]?.toDouble()?.times(load)
-                        ?: properties["thresholdRelToResources"]?.toDouble()?.times(resources)
-                        ?: throw IllegalArgumentException("'threshold', 'thresholdRelToLoad' or 'thresholdRelToResources' expected"))
+                    "threshold" to (properties["threshold"]?.toDoubleOrNull()
+                        ?: properties["thresholdRelToLoad"]?.toDoubleOrNull()?.times(load)
+                        ?: properties["thresholdRelToResources"]?.toDoubleOrNull()?.times(resources)
+                        ?: throw IllegalArgumentException("Valid 'threshold', 'thresholdRelToLoad' or 'thresholdRelToResources' expected"))
                 )
             )
             SloTypes.LAG_TREND_RATIO, SloTypes.DROPPED_RECORDS_RATIO -> {
diff --git a/theodolite/src/test/kotlin/rocks/theodolite/kubernetes/slo/SloCheckerFactoryTest.kt b/theodolite/src/test/kotlin/rocks/theodolite/kubernetes/slo/SloCheckerFactoryTest.kt
index ef018f1ec..5f1a85005 100644
--- a/theodolite/src/test/kotlin/rocks/theodolite/kubernetes/slo/SloCheckerFactoryTest.kt
+++ b/theodolite/src/test/kotlin/rocks/theodolite/kubernetes/slo/SloCheckerFactoryTest.kt
@@ -152,6 +152,30 @@ internal class SloCheckerFactoryTest {
         assertEquals(10.0, computedThreshold as Double, 0.001)
     }
 
+    @Test
+    fun testCreateGenericSloWithThresholdRelToLoadAndInvalidThreshold() {
+        val factory = SloCheckerFactory()
+        val sloChecker = factory.create(
+            SloTypes.GENERIC.value,
+            mapOf(
+                "externalSloUrl" to "http://localhost:1234",
+                "warmup" to "60",
+                "queryAggregation" to "median",
+                "repetitionAggregation" to "median",
+                "operator" to "lte",
+                "threshold" to "",
+                "thresholdRelToLoad" to "0.1"
+            ),
+            100,
+            5,
+            Metric.DEMAND
+        )
+        assertTrue(sloChecker is ExternalSloChecker)
+        val computedThreshold = (sloChecker as ExternalSloChecker).metadata["threshold"]
+        assertTrue(computedThreshold is Double)
+        assertEquals(10.0, computedThreshold as Double, 0.001)
+    }
+
     @Test
     fun testCreateGenericSloWithThresholdRelToResources() {
         val factory = SloCheckerFactory()
@@ -290,6 +314,27 @@ internal class SloCheckerFactoryTest {
         assertEquals(10.0, computedThreshold as Double, 0.001)
     }
 
+    @Test
+    fun testCreateLagTrendSloWithThresholdRelToLoadAndInvalidThreshold() {
+        val factory = SloCheckerFactory()
+        val sloChecker = factory.create(
+            SloTypes.LAG_TREND.value,
+            mapOf(
+                "externalSloUrl" to "http://localhost:1234",
+                "warmup" to "60",
+                "threshold" to "",
+                "thresholdRelToLoad" to "0.1"
+            ),
+            100,
+            5,
+            Metric.DEMAND
+        )
+        assertTrue(sloChecker is ExternalSloChecker)
+        val computedThreshold = (sloChecker as ExternalSloChecker).metadata["threshold"]
+        assertTrue(computedThreshold is Double)
+        assertEquals(10.0, computedThreshold as Double, 0.001)
+    }
+
     @Test
     fun testCreateLagTrendSloWithThresholdRelToResources() {
         val factory = SloCheckerFactory()
-- 
GitLab