From 96ef3d4644fa8e9c8b142611ae3c96bc135e5941 Mon Sep 17 00:00:00 2001 From: norohind <60548839+norohind@users.noreply.github.com> Date: Tue, 19 Apr 2022 15:16:40 +0300 Subject: [PATCH] Took out getting last records for hooks to common part --- Hook.py | 12 ++++++++++-- HookSystem.py | 20 ++++++++++++++++---- hooks/RuSquads.py | 20 +++----------------- hooks/SpecialSquads.py | 29 ++++++++--------------------- 4 files changed, 37 insertions(+), 44 deletions(-) diff --git a/Hook.py b/Hook.py index 9a2e609..c86a1f3 100644 --- a/Hook.py +++ b/Hook.py @@ -9,7 +9,15 @@ class Hook(ABC): # See at Hook class as to observer in observer pattern """ @abstractmethod - def update(self, operation_id: int) -> None: + def update(self, operation_id: int, latest: list[dict]) -> None: + """ + + :param operation_id: operation id + :param latest: latest information about squad in operation_id, in case of delete, it 1 last record, + in case of update it 2 records for update and 1 record for discovery + :return: + """ + raise NotImplemented @staticmethod @@ -23,7 +31,7 @@ class Hook(ABC): # See at Hook class as to observer in observer pattern :return: """ - db = sqlite3.connect(f'file:{os.environ["DB_PATH"]}?mode=ro', check_same_thread=False, uri=True) + db = sqlite3.connect(f'file:{os.environ["DB_PATH"]}?mode=ro&nolock=1', check_same_thread=False, uri=True) db.row_factory = lambda c, r: dict(zip([col[0] for col in c.description], r)) return db diff --git a/HookSystem.py b/HookSystem.py index b990469..c96418c 100644 --- a/HookSystem.py +++ b/HookSystem.py @@ -3,6 +3,8 @@ import functools import threading from Hook import Hook import importlib.machinery +import HookUtils +import copy def check_int(func: callable) -> callable: @@ -46,17 +48,27 @@ class HookSystem: @check_int def notify_inserted(self, operation_id: int) -> None: - self._notify(operation_id, self.hooks_inserted) + last_records = Hook.get_db().execute( + HookUtils.SQL_REQUESTS.GET_HISTORICAL_INFO, + {'limit': 2, 'operation_id': operation_id} + ).fetchall() + + self._notify(operation_id, self.hooks_inserted, copy.deepcopy(last_records)) @check_int def notify_deleted(self, operation_id: int) -> None: - self._notify(operation_id, self.hooks_deleted) + last_records = Hook.get_db().execute( + HookUtils.SQL_REQUESTS.GET_HISTORICAL_INFO, + {'limit': 1, 'operation_id': operation_id} + ).fetchall() + + self._notify(operation_id, self.hooks_deleted, copy.deepcopy(last_records)) @staticmethod - def _notify(operation_id, hooks: list[Hook]) -> None: + def _notify(operation_id, hooks: list[Hook], latest: list[dict]) -> None: for hook in hooks: threading.Thread( name=f'hook-{hook.__class__.__name__}-{operation_id}', target=hook.update, - args=(operation_id,) + args=(operation_id, latest) ).start() diff --git a/hooks/RuSquads.py b/hooks/RuSquads.py index 2185099..d028f43 100644 --- a/hooks/RuSquads.py +++ b/hooks/RuSquads.py @@ -10,14 +10,8 @@ class DeleteRuSquad(Hook): Send alert to discord if was removed russian squad """ - def update(self, operation_id: int) -> None: - last_record: dict = self.get_db().execute( - HookUtils.SQL_REQUESTS.GET_HISTORICAL_INFO, - { - 'limit': 1, - 'operation_id': operation_id - } - ).fetchone() + def update(self, operation_id: int, last_records) -> None: + last_record: dict = last_records[0] if last_record is not None: # i.e. we have a record in db for this squad if 32 in json.loads(last_record['user_tags']): # 32 - russian tag @@ -31,15 +25,7 @@ class UpdateRuSquad(Hook): Send alert to discord if something important was changed for ru squad """ - def update(self, operation_id: int) -> None: - last_records = self.get_db().execute( - HookUtils.SQL_REQUESTS.GET_HISTORICAL_INFO, - { - 'limit': 2, - 'operation_id': operation_id - } - ).fetchall() - + def update(self, operation_id: int, last_records: list[dict]) -> None: if len(last_records) == 1: # Squad just discovered record = last_records[0] diff --git a/hooks/SpecialSquads.py b/hooks/SpecialSquads.py index aeed5c3..3d63ee8 100644 --- a/hooks/SpecialSquads.py +++ b/hooks/SpecialSquads.py @@ -6,12 +6,13 @@ import HookUtils import sqlite3 from loguru import logger -special_squads_db = sqlite3.connect('file:SPECIAL_SQUADRONS.sqlite?mode=ro', uri=True, check_same_thread=False) +special_squads_db = sqlite3.connect('file:SPECIAL_SQUADRONS.sqlite?mode=ro&nolock=1', uri=True, check_same_thread=False) schema = "create table if not exists special_squadrons (id integer primary key, name text);" def is_special_squadron(squad_id: int) -> bool: - if bool(special_squads_db.execute('select count(*) from special_squadrons where id = ?;', (squad_id,)).fetchone()[0]): + if bool(special_squads_db.execute('select count(*) from special_squadrons where id = ?;', (squad_id,)).fetchone()[ + 0]): logger.info(f'Special squadron: {squad_id}') return True @@ -20,14 +21,8 @@ def is_special_squadron(squad_id: int) -> bool: class DeleteSpecialSquad(Hook): - def update(self, operation_id: int) -> None: - last_record: dict = self.get_db().execute( - HookUtils.SQL_REQUESTS.GET_HISTORICAL_INFO, - { - 'limit': 1, - 'operation_id': operation_id - } - ).fetchone() + def update(self, operation_id: int, last_records) -> None: + last_record: dict = last_records[0] if last_record is None: return @@ -36,20 +31,12 @@ class DeleteSpecialSquad(Hook): return message = f'Deleted SPECIAL squad `{last_record["name"]}` [last_record["tag"]]\nplatform: {last_record["platform"]}, members: {last_record["member_count"]}, ' \ - f'created: {last_record["created"]}, owner: `{last_record["owner_name"]}`' + f'created: {last_record["created"]}, owner: `{last_record["owner_name"]}`' HookUtils.notify_discord(message) class UpdateSpecialSquad(Hook): - def update(self, operation_id: int) -> None: - last_records: list[dict] = self.get_db().execute( - HookUtils.SQL_REQUESTS.GET_HISTORICAL_INFO, - { - 'limit': 2, - 'operation_id': operation_id - } - ).fetchall() - + def update(self, operation_id: int, last_records: list[dict]) -> None: if not is_special_squadron(last_records[0]['squad_id']): return @@ -90,4 +77,4 @@ def setup(hs: HookSystem): db.close() hs.add_on_delete_hook(DeleteSpecialSquad()) - hs.add_on_insert_hook(UpdateSpecialSquad()) \ No newline at end of file + hs.add_on_insert_hook(UpdateSpecialSquad())