Skip to content

Commit

Permalink
Merge pull request #115 from cuebook/lifetimetemplate
Browse files Browse the repository at this point in the history
Lifetime templates fixed
  • Loading branch information
ankitkpandey authored Aug 17, 2021
2 parents d21b34a + 4c4aa17 commit c540617
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
10 changes: 9 additions & 1 deletion api/ops/tasks/detectionTypes/lifetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
10 changes: 8 additions & 2 deletions api/ops/tasks/detectionTypes/tests/test_lifetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,15 @@ 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',
'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


8 changes: 4 additions & 4 deletions api/seeddata/anomalyCardTemplate.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"fields": {
"templateName": "Anomaly Daily Template Lifetime",
"title": "<b>{{metric}}</b>{% if dimensionVal %} for filter <span style=\"background:#eeeeee; padding: 0 4px; border-radius: 4px;\">{{dimension}} = {{dimensionVal}}</span>{% endif %} was at a lifetime {{highOrLow}} value on {{anomalyTimeISO|slice:\"0:10\"}}",
"bodyText": "{% if dimensionVal %}For <span style=\"background:#eeeeee; padding: 0 4px; border-radius: 4px;\">{{dimension}} = {{dimensionVal}}</span> 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 <span style=\"background:#eeeeee; padding: 0 4px; border-radius: 4px;\">{{dimension}} = {{dimensionVal}}</span> 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"
}
},
Expand All @@ -55,7 +55,7 @@
"fields": {
"templateName": "Anomaly Hourly Template Lifetime",
"title": "<b>{{metric}}</b>{% if dimensionVal %} for filter <span style=\"background:#eeeeee; padding: 0 4px; border-radius: 4px;\">{{dimension}} = {{dimensionVal}}</span>{% endif %} was at a lifetime {{highOrLow}} value on {{anomalyTimeISO|slice:\"0:10\"}} {{anomalyTimeISO|slice:\"11:16\"}}",
"bodyText": "{% if dimensionVal %}For <span style=\"background:#eeeeee; padding: 0 4px; border-radius: 4px;\">{{dimension}} = {{dimensionVal}}</span> 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 <span style=\"background:#eeeeee; padding: 0 4px; border-radius: 4px;\">{{dimension}} = {{dimensionVal}}</span> 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"
}
},
Expand All @@ -65,7 +65,7 @@
"fields": {
"templateName": "Anomaly Daily Template Value Threshold",
"title": "<b>{{metric}}</b>{% if dimensionVal %} for filter <span style=\"background:#eeeeee; padding: 0 4px; border-radius: 4px;\">{{dimension}} = {{dimensionVal}}</span>{% endif %} was {{operationCheck}}",
"bodyText": "{% if dimensionVal %}For <span style=\"background:#eeeeee; padding: 0 4px; border-radius: 4px;\">{{dimension}} = {{dimensionVal}}</span> 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 <span style=\"background:#eeeeee; padding: 0 4px; border-radius: 4px;\">{{dimension}} = {{dimensionVal}}</span> 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"
}
},
Expand All @@ -75,7 +75,7 @@
"fields": {
"templateName": "Anomaly Hourly Template Value Threshold",
"title": "<b>{{metric}}</b>{% if dimensionVal %} for filter <span style=\"background:#eeeeee; padding: 0 4px; border-radius: 4px;\">{{dimension}} = {{dimensionVal}}</span>{% endif %} was {{operationCheck}}",
"bodyText": "{% if dimensionVal %}For <span style=\"background:#eeeeee; padding: 0 4px; border-radius: 4px;\">{{dimension}} = {{dimensionVal}}</span> 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 <span style=\"background:#eeeeee; padding: 0 4px; border-radius: 4px;\">{{dimension}} = {{dimensionVal}}</span> 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"
}
}
Expand Down

0 comments on commit c540617

Please sign in to comment.