Browse Source

Pattern type handler (#6)

pull/1/head
rozetko 7 years ago committed by GitHub
parent
commit
a0942e46ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 47
      analytics/pattern_detection_model.py
  2. 4
      analytics/step_detector.py
  3. 32
      analytics/worker.py
  4. 1
      server/src/routes/anomalies.ts
  5. 36
      server/src/services/analytics.ts
  6. 1
      server/src/services/anomalyType.ts

47
analytics/pattern_detection_model.py

@ -1,5 +1,6 @@
from data_provider import DataProvider
import logging
from urllib.parse import urlparse
import os.path
import json
import config
@ -23,32 +24,32 @@ def segments_box(segments):
class PatternDetectionModel:
def __init__(self, pattern_name, preset=None):
self.pattern_name = pattern_name
self.preset = preset
def __init__(self, anomaly_id, pattern):
self.anomaly_id = anomaly_id
self.pattern = pattern
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]
dbconfig_filename = os.path.join(config.DATASOURCE_FOLDER, datasource + ".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")
with open(dbconfig_filename, 'r') as config_file:
dbconfig = json.load(config_file)
with open(target_filename, 'r') as 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.__load_model(preset)
self.__load_model(pattern)
def learn(self, segments):
self.model = self.__create_model(self.preset)
self.model = self.__create_model(self.pattern)
window_size = 200
dataframe = self.data_prov.get_dataframe()
@ -100,26 +101,26 @@ class PatternDetectionModel:
def synchronize_data(self):
self.data_prov.synchronize()
def __create_model(self, preset):
if preset == "peaks":
def __create_model(self, pattern):
if pattern == "peaks":
from peaks_detector import PeaksDetector
return PeaksDetector()
if preset == "steps" or preset == "cliffs":
if pattern == "jumps" or pattern == "drops":
from step_detector import StepDetector
return StepDetector(preset)
return StepDetector(pattern)
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)
def __save_model(self):
logger.info("Save model '%s'" % self.pattern_name)
model_filename = os.path.join(config.MODELS_FOLDER, self.pattern_name + ".m")
logger.info("Save model '%s'" % self.anomaly_id)
model_filename = os.path.join(config.MODELS_FOLDER, self.anomaly_id + ".m")
self.model.save(model_filename)
def __load_model(self, preset):
logger.info("Load model '%s'" % self.pattern_name)
model_filename = os.path.join(config.MODELS_FOLDER, self.pattern_name + ".m")
def __load_model(self, pattern):
logger.info("Load model '%s'" % self.anomaly_id)
model_filename = os.path.join(config.MODELS_FOLDER, self.pattern + ".m")
if os.path.exists(model_filename):
self.model = self.__create_model(preset)
self.model = self.__create_model(pattern)
self.model.load(model_filename)

4
analytics/step_detector.py

@ -58,8 +58,8 @@ def compress_segments(segments):
class StepDetector:
def __init__(self, preset):
self.preset = preset
def __init__(self, pattern):
self.pattern = pattern
self.mean = None
self.window_size = None
self.corr_max = None

32
analytics/worker.py

@ -45,18 +45,12 @@ class worker(object):
anomaly_id = task['anomaly_id']
if type == "predict":
last_prediction_time = task['last_prediction_time']
analytics_type = task['analytics_type']
preset = None
if "preset" in task:
preset = task['preset']
result = self.do_predict(anomaly_id, last_prediction_time, analytics_type, preset)
pattern = task['pattern']
result = self.do_predict(anomaly_id, last_prediction_time, pattern)
elif type == "learn":
segments = task['segments']
analytics_type = task['analytics_type']
preset = None
if "preset" in task:
preset = task['preset']
result = self.do_learn(anomaly_id, segments, analytics_type, preset)
pattern = task['pattern']
result = self.do_learn(anomaly_id, segments, pattern)
else:
result = {
'status': "failed",
@ -74,16 +68,16 @@ class worker(object):
}
return result
def do_learn(self, anomaly_id, segments, analytics_type, preset=None):
model = self.get_model(anomaly_id, analytics_type, preset)
def do_learn(self, anomaly_id, segments, pattern):
model = self.get_model(anomaly_id, pattern)
model.synchronize_data()
last_prediction_time = model.learn(segments)
result = self.do_predict(anomaly_id, last_prediction_time, analytics_type, preset)
result = self.do_predict(anomaly_id, last_prediction_time, pattern)
result['task'] = 'learn'
return result
def do_predict(self, anomaly_id, last_prediction_time, analytics_type, preset=None):
model = self.get_model(anomaly_id, analytics_type, preset)
def do_predict(self, anomaly_id, last_prediction_time, pattern):
model = self.get_model(anomaly_id, pattern)
model.synchronize_data()
segments, last_prediction_time = model.predict(last_prediction_time)
return {
@ -94,12 +88,12 @@ class worker(object):
'last_prediction_time': last_prediction_time
}
def get_model(self, anomaly_id, analytics_type, preset=None):
def get_model(self, anomaly_id, pattern):
if anomaly_id not in self.models_cache:
if analytics_type == "anomalies":
if pattern == "general approach":
model = AnomalyModel(anomaly_id)
elif analytics_type == "patterns":
model = PatternDetectionModel(anomaly_id, preset)
else:
model = PatternDetectionModel(anomaly_id, pattern)
self.models_cache[anomaly_id] = model
return self.models_cache[anomaly_id]

1
server/src/routes/anomalies.ts

@ -77,6 +77,7 @@ async function createAnomaly(ctx: Router.IRouterContext) {
const anomaly:Anomaly = {
name: body.name,
panelUrl: body.panelUrl,
pattern: body.pattern.toLowerCase(),
metric: metric,
datasource: body.datasource,
status: 'learning',

36
server/src/services/analytics.ts

@ -55,25 +55,11 @@ async function runLearning(anomalyId:AnomalyId) {
let segments = getLabeledSegments(anomalyId);
setAnomalyStatus(anomalyId, 'learning');
let anomaly:Anomaly = loadAnomalyById(anomalyId);
let analyticsType = "anomalies";
let preset = undefined;
if (anomaly.name.includes("jumps")) {
analyticsType = "patterns";
preset = "steps"
}
if (anomaly.name.includes("cliffs") || anomaly.name.includes("drops")) {
analyticsType = "patterns";
preset = "cliffs"
}
if (anomaly.name.includes("peaks")) {
analyticsType = "patterns";
preset = "peaks"
}
let pattern = anomaly.pattern;
let task = {
type: 'learn',
anomaly_id: anomalyId,
analytics_type: analyticsType,
preset,
pattern,
segments: segments
};
@ -90,25 +76,11 @@ async function runLearning(anomalyId:AnomalyId) {
async function runPredict(anomalyId:AnomalyId) {
let anomaly:Anomaly = loadAnomalyById(anomalyId);
let analyticsType = "anomalies";
let preset = undefined;
if (anomaly.name.includes("jump")) {
analyticsType = "patterns";
preset = "steps"
}
if (anomaly.name.includes("cliffs") || anomaly.name.includes("drops")) {
analyticsType = "patterns";
preset = "cliffs"
}
if (anomaly.name.includes("peaks")) {
analyticsType = "patterns";
preset = "peaks"
}
let pattern = anomaly.pattern;
let task = {
type: 'predict',
anomaly_id: anomalyId,
analytics_type: analyticsType,
preset,
pattern,
last_prediction_time: anomaly.last_prediction_time
};
let result = await runTask(task);

1
server/src/services/anomalyType.ts

@ -22,6 +22,7 @@ export type Anomaly = {
panelUrl: string,
pattern: string,
metric: Metric,
datasource: Datasource
status: string,

Loading…
Cancel
Save