import logging from collections import defaultdict from . import Result, LogSettings, Analyzer, ResultStore from .default import CategorizerStub logger = logging.getLogger(__name__) class BoardDurationAnalyzer(Analyzer): """ calculate display duration of boards """ __name__ = "BoardDuration" def render(self) -> str: return "\n".join(["{}\t{}".format(entry["active"], entry["id"]) for entry in self.result().get()]) def result(self, store: ResultStore) -> None: result = [] last_timestamp = None last_board = None for board in self.store: board_id, timestamp = board["id"], board["timestamp"] if not last_timestamp is None: result.append(self.save_entry(last_board, last_timestamp, timestamp - last_timestamp)) last_timestamp = timestamp last_board = board_id # TODO: last board? store.add(Result(type(self), result)) def process(self, entry: dict) -> bool: entry_type = entry[self.settings.type_field] if entry_type in self.settings.boards: self.store.append(self.save_entry(entry["board_id"], entry["timestamp"])) # TODO: make configurable return False def save_entry(self, board_id: str, timestamp: int, active: int = None): entry = {"id": board_id, "timestamp": timestamp} if not active is None: entry["active"] = active return entry def __init__(self, settings: LogSettings): super().__init__(settings) self.store = [] self.last = {} class SimulationRoundsAnalyzer(Analyzer): __name__ = "SimuRounds" def __init__(self, settings: LogSettings): super().__init__(settings) self.store = defaultdict(lambda: -1) # TODO verify def result(self, store: ResultStore) -> None: store.add(Result(type(self), dict(self.store))) def process(self, entry: dict) -> bool: entry_type = entry[self.settings.type_field] if entry_type in self.settings.custom['simulation_rounds']: if entry["answers"][self.settings.type_field] in self.settings.custom["simu_data"]: simu_id = entry['answers']["@id"] self.store[simu_id] += 1 return False class ActivationSequenceAnalyzer(Analyzer): __name__ = "ActivationSequence" def __init__(self, settings: LogSettings): super().__init__(settings) self.store = [] def result(self, store: ResultStore) -> None: store.add(Result(type(self), self.store)) def process(self, entry: dict) -> bool: if entry[self.settings.type_field] in self.settings.sequences['start']: if entry['cache']: self.store.append(entry['cache']['@id']) else: logger.error("null cache") return False class BiogamesCategorizer(CategorizerStub): def __init__(self, settings: LogSettings): super().__init__(settings) def process(self, entry: dict) -> bool: if self.key is "default": if entry[self.settings.type_field] in self.settings.custom['instance_start']: self.key = entry[self.settings.custom['instance_id']] return False