project/analyzers/analyzer/biogames.py

43 lines
1.2 KiB
Python

from . import Result, LogSettings, Analyzer
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) -> Result:
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?
return 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 = {}