|
|
@ -1,8 +1,6 @@ |
|
|
|
import config |
|
|
|
import config |
|
|
|
from detectors.general_detector import GeneralDetector |
|
|
|
from detectors.general_detector import GeneralDetector |
|
|
|
from detectors.pattern_detection_model import PatternDetectionModel |
|
|
|
from detectors.pattern_detection_model import PatternDetectionModel |
|
|
|
import queue |
|
|
|
|
|
|
|
import threading |
|
|
|
|
|
|
|
import json |
|
|
|
import json |
|
|
|
import logging |
|
|
|
import logging |
|
|
|
import sys |
|
|
|
import sys |
|
|
@ -15,42 +13,20 @@ logger = logging.getLogger('WORKER') |
|
|
|
|
|
|
|
|
|
|
|
class AnalyticUnitWorker(object): |
|
|
|
class AnalyticUnitWorker(object): |
|
|
|
models_cache = {} |
|
|
|
models_cache = {} |
|
|
|
thread = None |
|
|
|
|
|
|
|
queue = queue.Queue() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def start(self): |
|
|
|
|
|
|
|
self.thread = threading.Thread(target=self.run) |
|
|
|
|
|
|
|
self.thread.start() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def stop(self): |
|
|
|
|
|
|
|
if self.thread: |
|
|
|
|
|
|
|
self.queue.put(None) |
|
|
|
|
|
|
|
self.thread.join() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run(self): |
|
|
|
|
|
|
|
while True: |
|
|
|
|
|
|
|
task = self.queue.get() |
|
|
|
|
|
|
|
if task['type'] == "stop": |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
self.do_task(task) |
|
|
|
|
|
|
|
self.queue.task_done() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_task(self, task): |
|
|
|
|
|
|
|
self.queue.put(task) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: get task as an object built from json |
|
|
|
# TODO: get task as an object built from json |
|
|
|
def do_task(self, task): |
|
|
|
async def do_task(self, task): |
|
|
|
try: |
|
|
|
try: |
|
|
|
type = task['type'] |
|
|
|
type = task['type'] |
|
|
|
analytic_unit_id = task['analyticUnitId'] |
|
|
|
analytic_unit_id = task['analyticUnitId'] |
|
|
|
if type == "predict": |
|
|
|
if type == "predict": |
|
|
|
last_prediction_time = task['lastPredictionTime'] |
|
|
|
last_prediction_time = task['lastPredictionTime'] |
|
|
|
pattern = task['pattern'] |
|
|
|
pattern = task['pattern'] |
|
|
|
result = self.do_predict(analytic_unit_id, last_prediction_time, pattern) |
|
|
|
result = await self.do_predict(analytic_unit_id, last_prediction_time, pattern) |
|
|
|
elif type == "learn": |
|
|
|
elif type == "learn": |
|
|
|
segments = task['segments'] |
|
|
|
segments = task['segments'] |
|
|
|
pattern = task['pattern'] |
|
|
|
pattern = task['pattern'] |
|
|
|
result = self.do_learn(analytic_unit_id, segments, pattern) |
|
|
|
result = await self.do_learn(analytic_unit_id, segments, pattern) |
|
|
|
else: |
|
|
|
else: |
|
|
|
result = { |
|
|
|
result = { |
|
|
|
'status': "failed", |
|
|
|
'status': "failed", |
|
|
@ -69,10 +45,10 @@ class AnalyticUnitWorker(object): |
|
|
|
} |
|
|
|
} |
|
|
|
return result |
|
|
|
return result |
|
|
|
|
|
|
|
|
|
|
|
def do_learn(self, analytic_unit_id, segments, pattern): |
|
|
|
async def do_learn(self, analytic_unit_id, segments, pattern): |
|
|
|
model = self.get_model(analytic_unit_id, pattern) |
|
|
|
model = self.get_model(analytic_unit_id, pattern) |
|
|
|
model.synchronize_data() |
|
|
|
model.synchronize_data() |
|
|
|
last_prediction_time = model.learn(segments) |
|
|
|
last_prediction_time = await model.learn(segments) |
|
|
|
# TODO: we should not do predict before labeling in all models, not just in drops |
|
|
|
# TODO: we should not do predict before labeling in all models, not just in drops |
|
|
|
|
|
|
|
|
|
|
|
if pattern == 'drop' and len(segments) == 0: |
|
|
|
if pattern == 'drop' and len(segments) == 0: |
|
|
@ -84,15 +60,15 @@ class AnalyticUnitWorker(object): |
|
|
|
'lastPredictionTime': last_prediction_time |
|
|
|
'lastPredictionTime': last_prediction_time |
|
|
|
} |
|
|
|
} |
|
|
|
else: |
|
|
|
else: |
|
|
|
result = self.do_predict(analytic_unit_id, last_prediction_time, pattern) |
|
|
|
result = await self.do_predict(analytic_unit_id, last_prediction_time, pattern) |
|
|
|
|
|
|
|
|
|
|
|
result['task'] = 'learn' |
|
|
|
result['task'] = 'learn' |
|
|
|
return result |
|
|
|
return result |
|
|
|
|
|
|
|
|
|
|
|
def do_predict(self, analytic_unit_id, last_prediction_time, pattern): |
|
|
|
async def do_predict(self, analytic_unit_id, last_prediction_time, pattern): |
|
|
|
model = self.get_model(analytic_unit_id, pattern) |
|
|
|
model = self.get_model(analytic_unit_id, pattern) |
|
|
|
model.synchronize_data() |
|
|
|
model.synchronize_data() |
|
|
|
segments, last_prediction_time = model.predict(last_prediction_time) |
|
|
|
segments, last_prediction_time = await model.predict(last_prediction_time) |
|
|
|
return { |
|
|
|
return { |
|
|
|
'task': "predict", |
|
|
|
'task': "predict", |
|
|
|
'status': "success", |
|
|
|
'status': "success", |
|
|
@ -109,5 +85,3 @@ class AnalyticUnitWorker(object): |
|
|
|
model = PatternDetectionModel(analytic_unit_id, pattern_type) |
|
|
|
model = PatternDetectionModel(analytic_unit_id, pattern_type) |
|
|
|
self.models_cache[analytic_unit_id] = model |
|
|
|
self.models_cache[analytic_unit_id] = model |
|
|
|
return self.models_cache[analytic_unit_id] |
|
|
|
return self.models_cache[analytic_unit_id] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|