Took out getting last records for hooks to common part

This commit is contained in:
norohind 2022-04-19 15:16:40 +03:00
parent ac7d25297f
commit 96ef3d4644
Signed by: norohind
GPG Key ID: 01C3BECC26FB59E1
4 changed files with 37 additions and 44 deletions

12
Hook.py
View File

@ -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

View File

@ -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()

View File

@ -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]

View File

@ -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())
hs.add_on_insert_hook(UpdateSpecialSquad())