|
|
@ -29,14 +29,16 @@ def resolve_model_by_pattern(pattern: str) -> models.Model: |
|
|
|
return models.CustomModel() |
|
|
|
return models.CustomModel() |
|
|
|
raise ValueError('Unknown pattern "%s"' % pattern) |
|
|
|
raise ValueError('Unknown pattern "%s"' % pattern) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AnalyticUnitId = str |
|
|
|
class PatternDetector(Detector): |
|
|
|
class PatternDetector(Detector): |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, pattern_type): |
|
|
|
def __init__(self, pattern_type: str, analytic_unit_id: AnalyticUnitId): |
|
|
|
|
|
|
|
self.analytic_unit_id = analytic_unit_id |
|
|
|
self.pattern_type = pattern_type |
|
|
|
self.pattern_type = pattern_type |
|
|
|
self.model = resolve_model_by_pattern(self.pattern_type) |
|
|
|
self.model = resolve_model_by_pattern(self.pattern_type) |
|
|
|
self.window_size = 100 |
|
|
|
self.window_size = 150 |
|
|
|
self.bucket = DataBucket() |
|
|
|
self.bucket = DataBucket() |
|
|
|
|
|
|
|
self.bucket_full_reported = False |
|
|
|
|
|
|
|
|
|
|
|
def train(self, dataframe: pd.DataFrame, segments: list, cache: Optional[models.ModelCache]) -> models.ModelCache: |
|
|
|
def train(self, dataframe: pd.DataFrame, segments: list, cache: Optional[models.ModelCache]) -> models.ModelCache: |
|
|
|
# TODO: pass only part of dataframe that has segments |
|
|
|
# TODO: pass only part of dataframe that has segments |
|
|
@ -46,6 +48,7 @@ class PatternDetector(Detector): |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
def detect(self, dataframe: pd.DataFrame, cache: Optional[models.ModelCache]) -> dict: |
|
|
|
def detect(self, dataframe: pd.DataFrame, cache: Optional[models.ModelCache]) -> dict: |
|
|
|
|
|
|
|
logger.debug('Unit {} got {} data points for detection'.format(self.analytic_unit_id, len(dataframe))) |
|
|
|
# TODO: split and sleep (https://github.com/hastic/hastic-server/pull/124#discussion_r214085643) |
|
|
|
# TODO: split and sleep (https://github.com/hastic/hastic-server/pull/124#discussion_r214085643) |
|
|
|
detected = self.model.detect(dataframe, cache) |
|
|
|
detected = self.model.detect(dataframe, cache) |
|
|
|
|
|
|
|
|
|
|
@ -63,14 +66,19 @@ class PatternDetector(Detector): |
|
|
|
|
|
|
|
|
|
|
|
def recieve_data(self, data: pd.DataFrame, cache: Optional[ModelCache]) -> Optional[dict]: |
|
|
|
def recieve_data(self, data: pd.DataFrame, cache: Optional[ModelCache]) -> Optional[dict]: |
|
|
|
self.bucket.receive_data(data.dropna()) |
|
|
|
self.bucket.receive_data(data.dropna()) |
|
|
|
if cache != None: |
|
|
|
|
|
|
|
self.window_size = cache['WINDOW_SIZE'] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(self.bucket.data) >= self.window_size and cache != None: |
|
|
|
if len(self.bucket.data) >= self.window_size and cache != None: |
|
|
|
|
|
|
|
if not self.bucket_full_reported: |
|
|
|
|
|
|
|
logging.debug('{} unit`s bucket full, run detect'.format(self.analytic_unit_id)) |
|
|
|
|
|
|
|
self.bucket_full_reported = True |
|
|
|
|
|
|
|
|
|
|
|
res = self.detect(self.bucket.data, cache) |
|
|
|
res = self.detect(self.bucket.data, cache) |
|
|
|
|
|
|
|
|
|
|
|
excess_data = len(self.bucket.data) - self.window_size |
|
|
|
excess_data = len(self.bucket.data) - self.window_size |
|
|
|
self.bucket.drop_data(excess_data) |
|
|
|
self.bucket.drop_data(excess_data) |
|
|
|
return res |
|
|
|
return res |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
filling = len(self.bucket.data)*100 / self.window_size |
|
|
|
|
|
|
|
logging.debug('bucket for {} {}% full'.format(self.analytic_unit_id, filling)) |
|
|
|
|
|
|
|
|
|
|
|
return None |
|
|
|
return None |
|
|
|