Browse Source

No data threshold #370 (#396)

"No data" threshold support
pull/1/head
rozetko 6 years ago committed by Evgeny Smyshlyaev
parent
commit
390fa4c037
  1. 19
      analytics/analytics/detectors/threshold_detector.py
  2. 9
      analytics/analytics/utils/common.py
  3. 3
      server/src/models/threshold_model.ts

19
analytics/analytics/detectors/threshold_detector.py

@ -6,6 +6,7 @@ from typing import Optional
from detectors import Detector from detectors import Detector
from models import ModelCache from models import ModelCache
from time import time from time import time
from utils import convert_sec_to_ms
logger = log.getLogger('THRESHOLD_DETECTOR') logger = log.getLogger('THRESHOLD_DETECTOR')
@ -28,15 +29,25 @@ class ThresholdDetector(Detector):
value = cache['value'] value = cache['value']
condition = cache['condition'] condition = cache['condition']
now = convert_sec_to_ms(time())
segment = ({'from': now, 'to': now})
segments = []
dataframe_without_nans = dataframe.dropna() dataframe_without_nans = dataframe.dropna()
if len(dataframe_without_nans) == 0: 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_entry = dataframe_without_nans.iloc[-1]
last_value = last_entry['value'] last_value = last_entry['value']
now = int(time()) * 1000
segment = ({ 'from': now, 'to': now })
segments = []
if condition == '>': if condition == '>':
if last_value > value: if last_value > value:
segments.append(segment) segments.append(segment)

9
analytics/analytics/utils/common.py

@ -22,12 +22,6 @@ def exponential_smoothing(series, alpha):
result.append(alpha * series[n] + (1 - alpha) * result[n - 1]) result.append(alpha * series[n] + (1 - alpha) * result[n - 1])
return result 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): def segments_box(segments):
max_time = 0 max_time = 0
min_time = float("inf") min_time = float("inf")
@ -326,3 +320,6 @@ def cut_dataframe(data: pd.DataFrame) -> pd.DataFrame:
def get_min_max(array, default): def get_min_max(array, default):
return float(min(array, default=default)), float(max(array, default=default)) return float(min(array, default=default)), float(max(array, default=default))
def convert_sec_to_ms(sec):
return int(sec) * 1000

3
server/src/models/threshold_model.ts

@ -12,7 +12,8 @@ export enum Condition {
ABOVE_OR_EQUAL = '>=', ABOVE_OR_EQUAL = '>=',
EQUAL = '=', EQUAL = '=',
LESS_OR_EQUAL = '<=', LESS_OR_EQUAL = '<=',
LESS = '<' LESS = '<',
NO_DATA = 'NO_DATA'
}; };
export class Threshold { export class Threshold {

Loading…
Cancel
Save