From 390fa4c03736aebaf41a17fe8750984d9ab96bce Mon Sep 17 00:00:00 2001 From: rozetko Date: Wed, 6 Feb 2019 11:13:37 +0300 Subject: [PATCH] No data threshold #370 (#396) "No data" threshold support --- .../analytics/detectors/threshold_detector.py | 19 +++++++++++++++---- analytics/analytics/utils/common.py | 9 +++------ server/src/models/threshold_model.ts | 3 ++- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/analytics/analytics/detectors/threshold_detector.py b/analytics/analytics/detectors/threshold_detector.py index b3aee5a..fc733e7 100644 --- a/analytics/analytics/detectors/threshold_detector.py +++ b/analytics/analytics/detectors/threshold_detector.py @@ -6,6 +6,7 @@ from typing import Optional from detectors import Detector from models import ModelCache from time import time +from utils import convert_sec_to_ms logger = log.getLogger('THRESHOLD_DETECTOR') @@ -28,15 +29,25 @@ class ThresholdDetector(Detector): value = cache['value'] condition = cache['condition'] + now = convert_sec_to_ms(time()) + segment = ({'from': now, 'to': now}) + segments = [] + dataframe_without_nans = dataframe.dropna() if len(dataframe_without_nans) == 0: - return dict() + if condition == 'NO_DATA': + segments.append(segment) + return { + 'cache': cache, + 'segments': segments, + 'lastDetectionTime': now + } + else: + return None + last_entry = dataframe_without_nans.iloc[-1] last_value = last_entry['value'] - now = int(time()) * 1000 - segment = ({ 'from': now, 'to': now }) - segments = [] if condition == '>': if last_value > value: segments.append(segment) diff --git a/analytics/analytics/utils/common.py b/analytics/analytics/utils/common.py index 259e263..8e7002e 100644 --- a/analytics/analytics/utils/common.py +++ b/analytics/analytics/utils/common.py @@ -22,12 +22,6 @@ def exponential_smoothing(series, alpha): result.append(alpha * series[n] + (1 - alpha) * result[n - 1]) return result -def anomalies_to_timestamp(anomalies): - for anomaly in anomalies: - anomaly['from'] = int(anomaly['from'].timestamp() * 1000) - anomaly['to'] = int(anomaly['to'].timestamp() * 1000) - return anomalies - def segments_box(segments): max_time = 0 min_time = float("inf") @@ -326,3 +320,6 @@ def cut_dataframe(data: pd.DataFrame) -> pd.DataFrame: def get_min_max(array, default): return float(min(array, default=default)), float(max(array, default=default)) + +def convert_sec_to_ms(sec): + return int(sec) * 1000 diff --git a/server/src/models/threshold_model.ts b/server/src/models/threshold_model.ts index 1649d2a..13217e3 100644 --- a/server/src/models/threshold_model.ts +++ b/server/src/models/threshold_model.ts @@ -12,7 +12,8 @@ export enum Condition { ABOVE_OR_EQUAL = '>=', EQUAL = '=', LESS_OR_EQUAL = '<=', - LESS = '<' + LESS = '<', + NO_DATA = 'NO_DATA' }; export class Threshold {