from collections import defaultdict from log_analyzer import LogSettings from .analyzer import Analyzer from util import combinate def init_filter(settings: LogSettings, state: str) -> callable: # this implies OR for lists; AND for dicts if type(settings.sequences[state]) in (str, list): return lambda entry: entry[settings.entry_type] in settings.sequences[state] else: return lambda entry: combinate(settings.sequences[state], entry) class LocomotionActionAnalyzer(Analyzer): # TODO """ calculate locomation/action times and ratio Anything between LogEntryCache and CacheEnableAction is counted as ActionTime, the rest as LocomotionTime. """ __name__ = "LocomotionAction" def process(self, entry: dict) -> bool: self.last_timestamp = entry["timestamp"] if self.instance_start is None: self.instance_start = self.last_timestamp self.last = self.last_timestamp if self.cache_time is None: self.cache_time = self.last_timestamp offset = self.last_timestamp - self.cache_time if self.current_cache is None and self.filter_start(entry): if entry['cache'] is None: return False self.current_cache = entry['cache']['@id'] self.cache_time = self.last_timestamp self.locomotion.append(offset) self.last = None elif self.current_cache and self.filter_end(entry): self.actions.append(offset) self.cache_time = self.last_timestamp self.current_cache = None self.last = None def result(self) -> dict: if self.last is not None: if self.current_cache is None: self.locomotion.append(self.last - self.cache_time) else: self.actions.append(self.last - self.cache_time) locomotion = sum(self.locomotion) action = sum(self.actions) return { 'loco_sum': locomotion, 'action_sum': action, 'loco': self.locomotion, 'act': self.actions, 'dur': (self.last_timestamp - self.instance_start) } def __init__(self, settings: LogSettings): super().__init__(settings) self.filter_start = init_filter(settings, "start") self.filter_end = init_filter(settings, "end") self.current_cache = None self.cache_time = None self.locomotion = [] self.actions = [] self.instance_start = None self.last_timestamp = None self.last = None class CacheSequenceAnalyzer(Analyzer): # TODO __name__ = "CacheSequence" def process(self, entry: dict) -> bool: if self.filter(entry): if entry['cache']: self.store.append((entry['timestamp'],entry['cache']['@id'])) else: self.store.append((entry['timestamp'],entry['cache'])) def result(self) -> list: return self.store def __init__(self, settings: LogSettings): super().__init__(settings) self.store = [] self.filter = init_filter(settings, "start")