diff --git a/api/ops/tasks/detection/core/detectionTypes/lifetime.py b/api/ops/tasks/detection/core/detectionTypes/lifetime.py index bc1b7d0..e1b1c66 100644 --- a/api/ops/tasks/detection/core/detectionTypes/lifetime.py +++ b/api/ops/tasks/detection/core/detectionTypes/lifetime.py @@ -32,11 +32,13 @@ def lifetimeDetect(df, granularity): """ Method to perform anomaly detection on given dataframe """ - today = dt.datetime.now() + today = dt.datetime.now().replace(minute=0, second=0, microsecond=0, tzinfo=None) df["ds"] = pd.to_datetime(df["ds"]) df = df.sort_values("ds") df["ds"] = df["ds"].apply(lambda date: date.isoformat()[:19]) - todayISO = today.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=None).isoformat()[:19] + if granularity == "day": + today = today.replace(hour=0) + todayISO = today.isoformat()[:19] df = df[df["ds"] < todayISO] maxVal = df.y.max() minVal = df.y.min() diff --git a/api/ops/tasks/detection/core/detectionTypes/percentageChange.py b/api/ops/tasks/detection/core/detectionTypes/percentageChange.py index 1cc2bb5..f1e2492 100644 --- a/api/ops/tasks/detection/core/detectionTypes/percentageChange.py +++ b/api/ops/tasks/detection/core/detectionTypes/percentageChange.py @@ -27,11 +27,13 @@ def percentChangeDetect(df, granularity, threshold): Method to perform anomaly detection on given dataframe using fbProphet """ threshold = float(threshold) - today = dt.datetime.now() + today = dt.datetime.now().replace(minute=0, second=0, microsecond=0, tzinfo=None) df["ds"] = pd.to_datetime(df["ds"]) df = df.sort_values("ds") df["ds"] = df["ds"].apply(lambda date: date.isoformat()[:19]) - todayISO = today.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=None).isoformat()[:19] + if granularity == "day": + today = today.replace(hour=0) + todayISO = today.isoformat()[:19] df = df[df["ds"] < todayISO] df["percentageChange"] = 100 * (df.y - df.y.shift(1)) / df.y.shift(1) df["anomaly"] = (abs(df["percentageChange"]) > threshold) * 14 + 1 diff --git a/api/ops/tasks/detection/core/detectionTypes/prophet.py b/api/ops/tasks/detection/core/detectionTypes/prophet.py index 57b01a0..95ca856 100644 --- a/api/ops/tasks/detection/core/detectionTypes/prophet.py +++ b/api/ops/tasks/detection/core/detectionTypes/prophet.py @@ -40,12 +40,12 @@ def prophetDetect(df, granularity, iterations=None): """ Method to perform anomaly detection on given dataframe using fbProphet """ - today = dt.datetime.now() + today = dt.datetime.now().replace(minute=0, second=0, microsecond=0, tzinfo=None) df["ds"] = pd.to_datetime(df["ds"]) df["ds"] = df["ds"].apply(lambda date: date.isoformat()[:19]) - todayISO = today.replace( - hour=0, minute=0, second=0, microsecond=0, tzinfo=None - ).isoformat()[:19] + if granularity == "day": + today = today.replace(hour=0) + todayISO = today.isoformat()[:19] df = df[df["ds"] < todayISO] lastActualRow = df[-1:] lastISO = df.iloc[-1]["ds"] diff --git a/api/ops/tasks/detection/core/detectionTypes/valueThreshold.py b/api/ops/tasks/detection/core/detectionTypes/valueThreshold.py index fed896e..97ef9b5 100644 --- a/api/ops/tasks/detection/core/detectionTypes/valueThreshold.py +++ b/api/ops/tasks/detection/core/detectionTypes/valueThreshold.py @@ -49,11 +49,13 @@ def valueThresholdDetect(df, granularity, operator, value1, value2): "between": '((df["y"] >= lowerVal) & (df["y"] <= upperVal)) * 14 + 1', "!between": '((df["y"] < lowerVal) | (df["y"] > upperVal)) * 14 + 1' } - today = dt.datetime.now() + today = dt.datetime.now().replace(minute=0, second=0, microsecond=0, tzinfo=None) df["ds"] = pd.to_datetime(df["ds"]) df = df.sort_values("ds") df["ds"] = df["ds"].apply(lambda date: date.isoformat()[:19]) - todayISO = today.replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=None).isoformat()[:19] + if granularity == "day": + today = today.replace(hour=0) + todayISO = today.isoformat()[:19] df = df[df["ds"] < todayISO] df["anomaly"] = eval(operationDict[operator]) anomalyLatest = checkLatestAnomaly(df, operationStrDict[operator])