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.

72 lines
2.3 KiB

import config
import detectors
6 years ago
import json
import logging
import sys
import traceback
import time
6 years ago
logger = logging.getLogger('AnalyticUnitWorker')
6 years ago
class AnalyticUnitWorker:
def get_detector(self, analytic_unit_id, pattern_type):
if analytic_unit_id not in self.detectors_cache:
if pattern_type == 'GENERAL':
detector = detectors.GeneralDetector(analytic_unit_id)
else:
detector = detectors.PatternDetector(analytic_unit_id, pattern_type)
self.detectors_cache[analytic_unit_id] = detector
return self.detectors_cache[analytic_unit_id]
def __init__(self, detector: detectors.Detector):
pass
async def do_task(self, task):
6 years ago
try:
type = task['type']
analytic_unit_id = task['analyticUnitId']
payload = task['payload']
if type == "PREDICT":
result_payload = await self.do_predict(analytic_unit_id, payload)
elif type == "LEARN":
result_payload = await self.do_learn(analytic_unit_id, payload)
6 years ago
else:
raise ValueError('Unknown task type %s' % type)
6 years ago
except Exception as e:
#traceback.extract_stack()
error_text = traceback.format_exc()
logger.error("do_task Exception: '%s'" % error_text)
# TODO: move result to a class which renders to json for messaging to analytics
6 years ago
result = {
'status': "FAILED",
6 years ago
'error': str(e)
}
return {
'status': 'SUCCESS',
'payload': result_payload
}
6 years ago
async def do_learn(self, analytic_unit_id, payload) -> None:
pattern = payload['pattern']
segments = payload['segments']
data = payload['data'] # [time, value][]
detector = self.get_detector(analytic_unit_id, pattern)
await detector.learn(segments)
6 years ago
async def do_predict(self, analytic_unit_id, payload):
pattern = payload['pattern']
data = payload['data'] # [time, value][]
detector = self.get_detector(analytic_unit_id, pattern)
segments, last_prediction_time = await detector.predict(data)
6 years ago
return {
'segments': segments,
6 years ago
'lastPredictionTime': last_prediction_time
6 years ago
}