2022-06-22 17:17:20 +00:00
|
|
|
import time
|
2022-07-24 20:02:25 +00:00
|
|
|
|
|
|
|
import aiohttp.web_request
|
|
|
|
|
2021-06-05 11:12:23 +00:00
|
|
|
from src.config import settings
|
|
|
|
from typing import Union, Optional
|
2022-06-22 17:17:20 +00:00
|
|
|
from enum import Enum
|
|
|
|
|
|
|
|
|
|
|
|
class LogType(Enum):
|
|
|
|
CONNECTION_ERROR: 1
|
|
|
|
HTTP_ERROR: 2
|
|
|
|
MEDIAWIKI_ERROR: 3
|
|
|
|
VALUE_UPDATE: 4
|
2021-06-05 11:12:23 +00:00
|
|
|
|
|
|
|
queue_limit = settings.get("queue_limit", 30)
|
|
|
|
|
2022-06-22 17:17:20 +00:00
|
|
|
|
2021-06-05 11:12:23 +00:00
|
|
|
class Log:
|
2022-06-22 17:17:20 +00:00
|
|
|
"""Log class represents an event that happened to a wiki fetch. Main purpose of those logs is debug and error-tracking."""
|
2021-06-05 11:12:23 +00:00
|
|
|
def __init__(self, **kwargs):
|
2022-06-22 17:17:20 +00:00
|
|
|
self.type: LogType = kwargs["type"]
|
|
|
|
self.time: int = int(time.time())
|
|
|
|
self.title: str = kwargs["title"]
|
|
|
|
self.details: Optional[str] = kwargs.get("details", None)
|
2021-06-05 11:12:23 +00:00
|
|
|
|
|
|
|
class LimitedList(list):
|
|
|
|
def __init__(self, *args):
|
|
|
|
list.__init__(self, *args)
|
|
|
|
|
|
|
|
def append(self, obj: Log) -> None:
|
|
|
|
if len(self) > queue_limit:
|
|
|
|
self.pop()
|
|
|
|
|
|
|
|
|
|
|
|
class Statistics:
|
2022-06-22 17:17:20 +00:00
|
|
|
def __init__(self, rc_id: Optional[int], discussion_id: Optional[int]):
|
2022-07-24 20:02:25 +00:00
|
|
|
self.last_request: Optional[aiohttp.web_request.Request] = None
|
2021-06-05 11:12:23 +00:00
|
|
|
self.last_checked_rc: Optional[int] = None
|
|
|
|
self.last_action: Optional[int] = rc_id
|
|
|
|
self.last_checked_discussion: Optional[int] = None
|
|
|
|
self.last_post: Optional[int] = discussion_id
|
|
|
|
self.logs: LimitedList = LimitedList()
|
|
|
|
|
|
|
|
def update(self, *args: Log, **kwargs: dict[str, Union[float, int]]):
|
|
|
|
for key, value in kwargs:
|
|
|
|
self.__setattr__(key, value)
|
|
|
|
for log in args:
|
|
|
|
self.logs.append(log)
|