|
|
@ -1,5 +1,6 @@ |
|
|
|
from data_provider import DataProvider |
|
|
|
from data_provider import DataProvider |
|
|
|
import logging |
|
|
|
import logging |
|
|
|
|
|
|
|
from urllib.parse import urlparse |
|
|
|
import os.path |
|
|
|
import os.path |
|
|
|
import json |
|
|
|
import json |
|
|
|
import config |
|
|
|
import config |
|
|
@ -23,32 +24,32 @@ def segments_box(segments): |
|
|
|
|
|
|
|
|
|
|
|
class PatternDetectionModel: |
|
|
|
class PatternDetectionModel: |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, pattern_name, preset=None): |
|
|
|
def __init__(self, anomaly_id, pattern): |
|
|
|
self.pattern_name = pattern_name |
|
|
|
self.anomaly_id = anomaly_id |
|
|
|
self.preset = preset |
|
|
|
self.pattern = pattern |
|
|
|
|
|
|
|
|
|
|
|
self.__load_anomaly_config() |
|
|
|
self.__load_anomaly_config() |
|
|
|
datasource = self.anomaly_config['metric']['datasource'] |
|
|
|
|
|
|
|
|
|
|
|
parsedUrl = urlparse(self.anomaly_config['panelUrl']) |
|
|
|
|
|
|
|
origin = parsedUrl.scheme + '://' + parsedUrl.netloc |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
datasource = self.anomaly_config['datasource'] |
|
|
|
metric_name = self.anomaly_config['metric']['targets'][0] |
|
|
|
metric_name = self.anomaly_config['metric']['targets'][0] |
|
|
|
|
|
|
|
|
|
|
|
dbconfig_filename = os.path.join(config.DATASOURCE_FOLDER, datasource + ".json") |
|
|
|
|
|
|
|
target_filename = os.path.join(config.METRICS_FOLDER, metric_name + ".json") |
|
|
|
target_filename = os.path.join(config.METRICS_FOLDER, metric_name + ".json") |
|
|
|
|
|
|
|
datasource['origin'] = origin |
|
|
|
dataset_filename = os.path.join(config.DATASET_FOLDER, metric_name + ".csv") |
|
|
|
dataset_filename = os.path.join(config.DATASET_FOLDER, metric_name + ".csv") |
|
|
|
|
|
|
|
|
|
|
|
with open(dbconfig_filename, 'r') as config_file: |
|
|
|
|
|
|
|
dbconfig = json.load(config_file) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with open(target_filename, 'r') as file: |
|
|
|
with open(target_filename, 'r') as file: |
|
|
|
target = json.load(file) |
|
|
|
target = json.load(file) |
|
|
|
|
|
|
|
|
|
|
|
self.data_prov = DataProvider(dbconfig, target, dataset_filename) |
|
|
|
self.data_prov = DataProvider(datasource, target, dataset_filename) |
|
|
|
|
|
|
|
|
|
|
|
self.model = None |
|
|
|
self.model = None |
|
|
|
self.__load_model(preset) |
|
|
|
self.__load_model(pattern) |
|
|
|
|
|
|
|
|
|
|
|
def learn(self, segments): |
|
|
|
def learn(self, segments): |
|
|
|
self.model = self.__create_model(self.preset) |
|
|
|
self.model = self.__create_model(self.pattern) |
|
|
|
window_size = 200 |
|
|
|
window_size = 200 |
|
|
|
|
|
|
|
|
|
|
|
dataframe = self.data_prov.get_dataframe() |
|
|
|
dataframe = self.data_prov.get_dataframe() |
|
|
@ -100,26 +101,26 @@ class PatternDetectionModel: |
|
|
|
def synchronize_data(self): |
|
|
|
def synchronize_data(self): |
|
|
|
self.data_prov.synchronize() |
|
|
|
self.data_prov.synchronize() |
|
|
|
|
|
|
|
|
|
|
|
def __create_model(self, preset): |
|
|
|
def __create_model(self, pattern): |
|
|
|
if preset == "peaks": |
|
|
|
if pattern == "peaks": |
|
|
|
from peaks_detector import PeaksDetector |
|
|
|
from peaks_detector import PeaksDetector |
|
|
|
return PeaksDetector() |
|
|
|
return PeaksDetector() |
|
|
|
if preset == "steps" or preset == "cliffs": |
|
|
|
if pattern == "jumps" or pattern == "drops": |
|
|
|
from step_detector import StepDetector |
|
|
|
from step_detector import StepDetector |
|
|
|
return StepDetector(preset) |
|
|
|
return StepDetector(pattern) |
|
|
|
|
|
|
|
|
|
|
|
def __load_anomaly_config(self): |
|
|
|
def __load_anomaly_config(self): |
|
|
|
with open(os.path.join(config.ANOMALIES_FOLDER, self.pattern_name + ".json"), 'r') as config_file: |
|
|
|
with open(os.path.join(config.ANOMALIES_FOLDER, self.anomaly_id + ".json"), 'r') as config_file: |
|
|
|
self.anomaly_config = json.load(config_file) |
|
|
|
self.anomaly_config = json.load(config_file) |
|
|
|
|
|
|
|
|
|
|
|
def __save_model(self): |
|
|
|
def __save_model(self): |
|
|
|
logger.info("Save model '%s'" % self.pattern_name) |
|
|
|
logger.info("Save model '%s'" % self.anomaly_id) |
|
|
|
model_filename = os.path.join(config.MODELS_FOLDER, self.pattern_name + ".m") |
|
|
|
model_filename = os.path.join(config.MODELS_FOLDER, self.anomaly_id + ".m") |
|
|
|
self.model.save(model_filename) |
|
|
|
self.model.save(model_filename) |
|
|
|
|
|
|
|
|
|
|
|
def __load_model(self, preset): |
|
|
|
def __load_model(self, pattern): |
|
|
|
logger.info("Load model '%s'" % self.pattern_name) |
|
|
|
logger.info("Load model '%s'" % self.anomaly_id) |
|
|
|
model_filename = os.path.join(config.MODELS_FOLDER, self.pattern_name + ".m") |
|
|
|
model_filename = os.path.join(config.MODELS_FOLDER, self.pattern + ".m") |
|
|
|
if os.path.exists(model_filename): |
|
|
|
if os.path.exists(model_filename): |
|
|
|
self.model = self.__create_model(preset) |
|
|
|
self.model = self.__create_model(pattern) |
|
|
|
self.model.load(model_filename) |
|
|
|
self.model.load(model_filename) |