|
|
@ -2,34 +2,56 @@ from typing import Dict |
|
|
|
import pandas as pd |
|
|
|
import pandas as pd |
|
|
|
import numpy as np |
|
|
|
import numpy as np |
|
|
|
import logging, traceback |
|
|
|
import logging, traceback |
|
|
|
|
|
|
|
from concurrent.futures import Executor, ThreadPoolExecutor |
|
|
|
|
|
|
|
|
|
|
|
import detectors |
|
|
|
import detectors |
|
|
|
from analytic_unit_worker import AnalyticUnitWorker |
|
|
|
from analytic_unit_worker import AnalyticUnitWorker |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger('AnalyticUnitManager') |
|
|
|
logger = logging.getLogger('AnalyticUnitManager') |
|
|
|
|
|
|
|
WORKERS_EXECUTORS = 20 |
|
|
|
|
|
|
|
|
|
|
|
AnalyticUnitId = str |
|
|
|
AnalyticUnitId = str |
|
|
|
analytic_workers: Dict[AnalyticUnitId, AnalyticUnitWorker] = dict() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_detector_by_type(analytic_unit_type) -> detectors.Detector: |
|
|
|
def get_detector_by_type(analytic_unit_type) -> detectors.Detector: |
|
|
|
return detectors.PatternDetector(analytic_unit_type) |
|
|
|
return detectors.PatternDetector(analytic_unit_type) |
|
|
|
|
|
|
|
|
|
|
|
def ensure_worker(analytic_unit_id, analytic_unit_type) -> AnalyticUnitWorker: |
|
|
|
def prepare_data(data: list): |
|
|
|
if analytic_unit_id in analytic_workers: |
|
|
|
""" |
|
|
|
|
|
|
|
Takes list |
|
|
|
|
|
|
|
- converts it into pd.DataFrame, |
|
|
|
|
|
|
|
- converts 'timestamp' column to pd.Datetime, |
|
|
|
|
|
|
|
- subtracts min value from dataset |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
data = pd.DataFrame(data, columns=['timestamp', 'value']) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data['timestamp'] = pd.to_datetime(data['timestamp'], unit='ms') |
|
|
|
|
|
|
|
if not np.isnan(min(data['value'])): |
|
|
|
|
|
|
|
data['value'] = data['value'] - min(data['value']) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return data |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AnalyticUnitManager: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self): |
|
|
|
|
|
|
|
self.analytic_workers: Dict[AnalyticUnitId, AnalyticUnitWorker] = dict() |
|
|
|
|
|
|
|
self.workers_executor = ThreadPoolExecutor(max_workers=WORKERS_EXECUTORS) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __ensure_worker(self, analytic_unit_id, analytic_unit_type) -> AnalyticUnitWorker: |
|
|
|
|
|
|
|
if analytic_unit_id in self.analytic_workers: |
|
|
|
# TODO: check that type is the same |
|
|
|
# TODO: check that type is the same |
|
|
|
return analytic_workers[analytic_unit_id] |
|
|
|
return self.analytic_workers[analytic_unit_id] |
|
|
|
detector = get_detector_by_type(analytic_unit_type) |
|
|
|
detector = get_detector_by_type(analytic_unit_type) |
|
|
|
worker = AnalyticUnitWorker(analytic_unit_id, detector) |
|
|
|
worker = AnalyticUnitWorker(analytic_unit_id, detector, self.workers_executor) |
|
|
|
analytic_workers[analytic_unit_id] = worker |
|
|
|
self.analytic_workers[analytic_unit_id] = worker |
|
|
|
return worker |
|
|
|
return worker |
|
|
|
|
|
|
|
|
|
|
|
async def handle_analytic_task(task): |
|
|
|
async def handle_analytic_task(self, task): |
|
|
|
try: |
|
|
|
try: |
|
|
|
payload = task['payload'] |
|
|
|
payload = task['payload'] |
|
|
|
|
|
|
|
worker = self.__ensure_worker(task['analyticUnitId'], payload['pattern']) |
|
|
|
worker = ensure_worker(task['analyticUnitId'], payload['pattern']) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data = prepare_data(payload['data']) |
|
|
|
data = prepare_data(payload['data']) |
|
|
|
result_payload = {} |
|
|
|
result_payload = {} |
|
|
|
if task['type'] == 'LEARN': |
|
|
|
if task['type'] == 'LEARN': |
|
|
@ -52,17 +74,4 @@ async def handle_analytic_task(task): |
|
|
|
'error': str(e) |
|
|
|
'error': str(e) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
def prepare_data(data: list): |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
Takes list |
|
|
|
|
|
|
|
- converts it into pd.DataFrame, |
|
|
|
|
|
|
|
- converts 'timestamp' column to pd.Datetime, |
|
|
|
|
|
|
|
- subtracts min value from dataset |
|
|
|
|
|
|
|
""" |
|
|
|
|
|
|
|
data = pd.DataFrame(data, columns=['timestamp', 'value']) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data['timestamp'] = pd.to_datetime(data['timestamp'], unit='ms') |
|
|
|
|
|
|
|
if not np.isnan(min(data['value'])): |
|
|
|
|
|
|
|
data['value'] = data['value'] - min(data['value']) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return data |
|
|
|
|
|
|
|