From 0492f2d3d9a927a81dbc80ba2ed9ee92c33a46e4 Mon Sep 17 00:00:00 2001 From: ankit Date: Tue, 17 Aug 2021 19:25:55 +0530 Subject: [PATCH 1/2] Lifetime templates fixed --- api/ops/tasks/detectionTypes/lifetime.py | 10 +++++++++- api/ops/tasks/detectionTypes/tests/test_lifetime.py | 6 +++++- api/seeddata/anomalyCardTemplate.json | 8 ++++---- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/api/ops/tasks/detectionTypes/lifetime.py b/api/ops/tasks/detectionTypes/lifetime.py index 5a44dc2..bc1b7d0 100644 --- a/api/ops/tasks/detectionTypes/lifetime.py +++ b/api/ops/tasks/detectionTypes/lifetime.py @@ -12,9 +12,17 @@ def checkLatestAnomaly(df): lastAnomalyRow = anomalies.iloc[-1] anomalyTime = lastAnomalyRow["ds"] + maxRow = anomalies[anomalies["y"] == anomalies.y.max()].iloc[-1] + minRow = anomalies[anomalies["y"] == anomalies.y.min()].iloc[-1] + return { - "highOrLow": "high" if lastAnomalyRow["y"] == df.y.max() else "low", + "highOrLow": "high" if lastAnomalyRow["y"] == anomalies.y.max() else "low", "value": float(lastAnomalyRow["y"]), + "highVal": float(maxRow["y"]), + "highDate": dp.parse(maxRow["ds"]).isoformat(), + "lowVal": float(minRow["y"]), + "lowDate": dp.parse(minRow["ds"]).isoformat(), + "firstDate": dp.parse(df.iloc[0]["ds"]).isoformat(), "anomalyTimeISO": dp.parse(anomalyTime).isoformat(), "anomalyTime": dp.parse(anomalyTime).timestamp() * 1000, } diff --git a/api/ops/tasks/detectionTypes/tests/test_lifetime.py b/api/ops/tasks/detectionTypes/tests/test_lifetime.py index b0c3f27..55db3e0 100644 --- a/api/ops/tasks/detectionTypes/tests/test_lifetime.py +++ b/api/ops/tasks/detectionTypes/tests/test_lifetime.py @@ -67,9 +67,13 @@ def testPercentChange(): {'ds': '2020-08-10T00:00:00', 'y': 725, 'anomaly': 1}]}, 'anomalyLatest': {'highOrLow': 'high', 'value': 1324.0, + 'highVal': 1324.0, + 'highDate': '2020-07-30T00:00:00', + 'lowVal': 340.0, + 'lowDate': '2020-07-26T00:00:00', 'anomalyTimeISO': '2020-07-30T00:00:00', 'anomalyTime': 1596067200000.0}} - + assert lifetimeDetect(df, "day") == output diff --git a/api/seeddata/anomalyCardTemplate.json b/api/seeddata/anomalyCardTemplate.json index e1b8de0..90b7ff4 100644 --- a/api/seeddata/anomalyCardTemplate.json +++ b/api/seeddata/anomalyCardTemplate.json @@ -45,7 +45,7 @@ "fields": { "templateName": "Anomaly Daily Template Lifetime", "title": "{{metric}}{% if dimensionVal %} for filter {{dimension}} = {{dimensionVal}}{% endif %} was at a lifetime {{highOrLow}} value on {{anomalyTimeISO|slice:\"0:10\"}}", - "bodyText": "{% if dimensionVal %}For {{dimension}} = {{dimensionVal}} filter, {% endif %}{{datasetName}}.{{metric}} was at lifetime {{highOrLow}} value of {{value}} on {{anomalyTimeISO|slice:\"0:10\"}}.{% if dimensionVal and contribution %} This filter's contribution to {{metric}} was {{contribution|floatformat:\"1\"}}%.{% endif %}", + "bodyText": "{% if dimensionVal %}For {{dimension}} = {{dimensionVal}} filter, {% endif %}{{datasetName}}.{{metric}} was at lifetime high value of {{highVal}} on {{highDate|slice:\"0:10\"}} and lifetime low value of {{lowVal}} on {{lowDate|slice:\"0:10\"}}.{% if dimensionVal and contribution %} This filter's contribution to {{metric}} was {{contribution|floatformat:\"1\"}}%.{% endif %} The lifetime period started on {{firstDate|slice:\"0:10\"}}.", "supportedVariables": "granularity, datasetName, metric, dimension, dimensionVal, percent, contribution, anomalyTimeISO, highOrLow" } }, @@ -55,7 +55,7 @@ "fields": { "templateName": "Anomaly Hourly Template Lifetime", "title": "{{metric}}{% if dimensionVal %} for filter {{dimension}} = {{dimensionVal}}{% endif %} was at a lifetime {{highOrLow}} value on {{anomalyTimeISO|slice:\"0:10\"}} {{anomalyTimeISO|slice:\"11:16\"}}", - "bodyText": "{% if dimensionVal %}For {{dimension}} = {{dimensionVal}} filter, {% endif %}{{datasetName}}.{{metric}} was at lifetime {{highOrLow}} value of {{value}} on {{anomalyTimeISO|slice:\"0:10\"}} {{anomalyTimeISO|slice:\"11:16\"}}.{% if dimensionVal and contribution %} This filter's contribution to {{metric}} was {{contribution|floatformat:\"1\"}}%.{% endif %}", + "bodyText": "{% if dimensionVal %}For {{dimension}} = {{dimensionVal}} filter, {% endif %}{{datasetName}}.{{metric}} was at lifetime high value of {{highVal}} on {{highDate|slice:\"0:10\"}} {{highDate|slice:\"11:16\"}} and lifetime low value of {{lowVal}} on {{lowDate|slice:\"0:10\"}} {{lowDate|slice:\"11:16\"}}.{% if dimensionVal and contribution %} This filter's contribution to {{metric}} was {{contribution|floatformat:\"1\"}}%.{% endif %} The lifetime period started on {{firstDate|slice:\"0:10\"}} {{firstDate|slice:\"11:16\"}}.", "supportedVariables": "granularity, datasetName, metric, dimension, dimensionVal, percent, contribution, anomalyTimeISO, highOrLow" } }, @@ -65,7 +65,7 @@ "fields": { "templateName": "Anomaly Daily Template Value Threshold", "title": "{{metric}}{% if dimensionVal %} for filter {{dimension}} = {{dimensionVal}}{% endif %} was {{operationCheck}}", - "bodyText": "{% if dimensionVal %}For {{dimension}} = {{dimensionVal}} filter, {% endif %}{{datasetName}}.{{metric}} was {{{operationCheck}} on {{anomalyTimeISO|slice:\"0:10\"}}.{% if dimensionVal and contribution %} This filter's contribution to {{metric}} was {{contribution|floatformat:\"1\"}}%.{% endif %}", + "bodyText": "{% if dimensionVal %}For {{dimension}} = {{dimensionVal}} filter, {% endif %}{{datasetName}}.{{metric}} was {{operationCheck}} on {{anomalyTimeISO|slice:\"0:10\"}}.{% if dimensionVal and contribution %} This filter's contribution to {{metric}} was {{contribution|floatformat:\"1\"}}%.{% endif %}", "supportedVariables": "granularity, datasetName, metric, dimension, dimensionVal, percent, contribution, anomalyTimeISO, operationCheck" } }, @@ -75,7 +75,7 @@ "fields": { "templateName": "Anomaly Hourly Template Value Threshold", "title": "{{metric}}{% if dimensionVal %} for filter {{dimension}} = {{dimensionVal}}{% endif %} was {{operationCheck}}", - "bodyText": "{% if dimensionVal %}For {{dimension}} = {{dimensionVal}} filter, {% endif %}{{datasetName}}.{{metric}} was {{{operationCheck}} on {{anomalyTimeISO|slice:\"0:10\"}} {{anomalyTimeISO|slice:\"11:16\"}}.{% if dimensionVal and contribution %} This filter's contribution to {{metric}} was {{contribution|floatformat:\"1\"}}%.{% endif %}", + "bodyText": "{% if dimensionVal %}For {{dimension}} = {{dimensionVal}} filter, {% endif %}{{datasetName}}.{{metric}} was {{operationCheck}} on {{anomalyTimeISO|slice:\"0:10\"}} {{anomalyTimeISO|slice:\"11:16\"}}.{% if dimensionVal and contribution %} This filter's contribution to {{metric}} was {{contribution|floatformat:\"1\"}}%.{% endif %}", "supportedVariables": "granularity, datasetName, metric, dimension, dimensionVal, contribution, anomalyTimeISO, operationCheck" } } From 4c4aa17b54076139be2e9b76f615c5ca5668615f Mon Sep 17 00:00:00 2001 From: ankit Date: Tue, 17 Aug 2021 19:38:45 +0530 Subject: [PATCH 2/2] Test case fix --- api/ops/tasks/detectionTypes/tests/test_lifetime.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/api/ops/tasks/detectionTypes/tests/test_lifetime.py b/api/ops/tasks/detectionTypes/tests/test_lifetime.py index 55db3e0..cf0c93e 100644 --- a/api/ops/tasks/detectionTypes/tests/test_lifetime.py +++ b/api/ops/tasks/detectionTypes/tests/test_lifetime.py @@ -71,9 +71,11 @@ def testPercentChange(): 'highDate': '2020-07-30T00:00:00', 'lowVal': 340.0, 'lowDate': '2020-07-26T00:00:00', + 'firstDate': '2020-07-12T00:00:00', 'anomalyTimeISO': '2020-07-30T00:00:00', 'anomalyTime': 1596067200000.0}} - assert lifetimeDetect(df, "day") == output + + assert lifetimeDetect(df, "day") == output