You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.8 KiB

import detectors
import utils
from grafana_data_provider import GrafanaDataProvider
7 years ago
import logging
from urllib.parse import urlparse
7 years ago
import os.path
import json
7 years ago
import config
7 years ago
import pandas as pd
7 years ago
7 years ago
logger = logging.getLogger('analytic_toolset')
def resolve_detector_by_pattern(pattern):
if pattern == 'peak':
return detectors.PeaksDetector()
if pattern == 'drop':
return detectors.StepDetector()
if pattern == 'jump':
return detectors.JumpDetector()
raise ValueError('Unknown pattern "%s"' % pattern)
7 years ago
class PatternDetector:
7 years ago
def __init__(self, analytic_unit_id, pattern_type):
self.analytic_unit_id = analytic_unit_id
self.pattern_type = pattern_type
7 years ago
self.__load_anomaly_config()
parsedUrl = urlparse(self.anomaly_config['panelUrl'])
origin = parsedUrl.scheme + '://' + parsedUrl.netloc
datasource = self.anomaly_config['datasource']
7 years ago
metric_name = self.anomaly_config['metric']['targets'][0]
7 years ago
target_filename = os.path.join(config.METRICS_FOLDER, metric_name + ".json")
datasource['origin'] = origin
7 years ago
dataset_filename = os.path.join(config.DATASET_FOLDER, metric_name + ".csv")
7 years ago
with open(target_filename, 'r') as file:
target = json.load(file)
self.data_prov = GrafanaDataProvider(datasource, target, dataset_filename)
7 years ago
self.model = None
self.__load_model(pattern_type)
7 years ago
async def learn(self, segments):
self.model = resolve_detector_by_pattern(self.pattern_type)
7 years ago
window_size = 200
dataframe = self.data_prov.get_dataframe()
segments = self.data_prov.transform_anomalies(segments)
# TODO: pass only part of dataframe that has segments
await self.model.fit(dataframe, segments)
7 years ago
self.__save_model()
return 0
async def predict(self, last_prediction_time):
7 years ago
if self.model is None:
return [], last_prediction_time
window_size = 100
last_prediction_time = pd.to_datetime(last_prediction_time, unit='ms')
start_index = self.data_prov.get_upper_bound(last_prediction_time)
start_index = max(0, start_index - window_size)
dataframe = self.data_prov.get_data_range(start_index)
predicted_indexes = await self.model.predict(dataframe)
7 years ago
predicted_indexes = [(x, y) for (x, y) in predicted_indexes if x >= start_index and y >= start_index]
predicted_times = self.data_prov.inverse_transform_indexes(predicted_indexes)
segments = []
for time_value in predicted_times:
ts1 = int(time_value[0].timestamp() * 1000)
ts2 = int(time_value[1].timestamp() * 1000)
segments.append({
'start': min(ts1, ts2),
'finish': max(ts1, ts2)
7 years ago
})
last_dataframe_time = dataframe.iloc[-1]['timestamp']
7 years ago
last_prediction_time = int(last_dataframe_time.timestamp() * 1000)
return segments, last_prediction_time
# return predicted_anomalies, last_prediction_time
def synchronize_data(self):
self.data_prov.synchronize()
def __load_anomaly_config(self):
with open(os.path.join(config.ANALYTIC_UNITS_FOLDER, self.analytic_unit_id + ".json"), 'r') as config_file:
7 years ago
self.anomaly_config = json.load(config_file)
def __save_model(self):
logger.info("Save model '%s'" % self.analytic_unit_id)
model_filename = os.path.join(config.MODELS_FOLDER, self.analytic_unit_id + ".m")
7 years ago
self.model.save(model_filename)
def __load_model(self, pattern):
logger.info("Load model '%s'" % self.analytic_unit_id)
model_filename = os.path.join(config.MODELS_FOLDER, self.pattern_type + ".m")
7 years ago
if os.path.exists(model_filename):
self.model = resolve_detector_by_pattern(pattern)
self.model.load(model_filename)