Abstract model for support both DBMS

This commit is contained in:
norohind committed 2021-11-28 03:18:41 +03:00
1 parent bfd2f785a9
commit 1a692df820
4 files changed
+77 -32

No files matched your search

+11 -4
View File
@@ -1,4 +1,7 @@
import model.postgres_model
from model.postgres_model import PostgresModel
from model.sqlite_model import Sqlite3Model
from model.abstract_model import AbstractModel
import utils
from EDMCLogging import get_main_logger
import os
@@ -7,17 +10,21 @@ logger = get_main_logger()
env_choose = os.getenv('DB_NAME')
model: AbstractModel
if env_choose == 'postgres':
logger.info('Using postgres DB')
from .import postgres_model as model
model = PostgresModel()
elif env_choose == 'sqlite':
logger.info('Using sqlite DB')
from .import sqlite_model as model
model = Sqlite3Model()
else:
logger.info('Using sqlite DB')
from . import sqlite_model as model
model = Sqlite3Model()
model.open_model()
model.get_diff_action_id = utils.measure(model.get_diff_action_id)
model.get_activity_changes = utils.measure(model.get_activity_changes)
+25
View File
@@ -0,0 +1,25 @@
import abc
class AbstractModel(abc.ABC):
@abc.abstractmethod
def open_model(self) -> None:
raise NotImplemented
@abc.abstractmethod
def close_model(self) -> None:
raise NotImplemented
@abc.abstractmethod
def get_activity_changes(self, platform: str, leaderboard_type: str, limit: int, low_timestamp, high_timestamp)\
-> list:
raise NotImplemented
@abc.abstractmethod
def insert_leaderboard_db(self, leaderboard_list: dict) -> None:
raise NotImplemented
@abc.abstractmethod
def get_diff_action_id(self, action_id: int) -> list:
raise NotImplemented
+20 -15
View File
@@ -1,6 +1,5 @@
import json
import os
import sqlite3
import typing
import psycopg2.extensions
@@ -8,13 +7,19 @@ import psycopg2.extras
from .sqlite_cache import cache
from . import postgres_sql_requests
from .abstract_model import AbstractModel
import utils
from EDMCLogging import get_main_logger
logger = get_main_logger()
logger.propagate = False
db: psycopg2.extensions.connection = psycopg2.connect(
class PostgresModel(AbstractModel):
db: psycopg2.extensions.connection
def open_model(self):
self.db: psycopg2.extensions.connection = psycopg2.connect(
user=os.environ['DB_USERNAME'], # user2
password=os.environ['DB_PASSWORD'], # 1
host=os.environ['DB_HOSTNAME'], # 192.168.1.68
@@ -22,12 +27,15 @@ db: psycopg2.extensions.connection = psycopg2.connect(
database=os.environ['DB_DATABASE'], # test0
cursor_factory=psycopg2.extras.DictCursor)
with db:
with db.cursor() as cursor:
with self.db:
with self.db.cursor() as cursor:
cursor.execute(postgres_sql_requests.schema_create) # schema creation
def close_model(self):
self.db.close()
def get_activity_changes(platform: str, leaderboard_type: str, limit: int, low_timestamp, high_timestamp) -> list:
def get_activity_changes(self, platform: str, leaderboard_type: str, limit: int, low_timestamp, high_timestamp)\
-> list:
cache_key: str = f'{platform}_{leaderboard_type}_{limit}_{low_timestamp}_{high_timestamp}'
cached_result: typing.Union[str, None] = cache.get(cache_key)
@@ -37,7 +45,7 @@ def get_activity_changes(platform: str, leaderboard_type: str, limit: int, low_t
logger.debug(f'Not cached result for {cache_key}')
with db.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cursor:
with self.db.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cursor:
cursor.execute(postgres_sql_requests.select_activity_pretty_names, {
'LB_type': utils.LeaderboardTypes(leaderboard_type.lower()).value,
'platform': utils.Platform(platform.upper()).value,
@@ -53,8 +61,7 @@ def get_activity_changes(platform: str, leaderboard_type: str, limit: int, low_t
return result
def insert_leaderboard_db(leaderboard_list: dict) -> None:
def insert_leaderboard_db(self, leaderboard_list: dict) -> None:
"""
Takes leaderboard as list, it platform, type, db connection and insert leaderboard to DB
@@ -68,7 +75,7 @@ def insert_leaderboard_db(leaderboard_list: dict) -> None:
action_id: int # not last, current that we will use
with db.cursor() as cursor:
with self.db.cursor() as cursor:
cursor.execute(postgres_sql_requests.select_last_action_id)
action_id_fetch_one: typing.Union[None, dict[str, int]] = cursor.fetchone()
@@ -83,17 +90,15 @@ def insert_leaderboard_db(leaderboard_list: dict) -> None:
for squad in leaderboard:
squad.update({'action_id': action_id, 'LB_type': LB_type, 'platform': platform})
with db:
with db.cursor() as cursor:
with self.db:
with self.db.cursor() as cursor:
cursor.executemany(
postgres_sql_requests.insert_leader_board,
leaderboard)
cache.delete_all() # drop cache
@utils.measure
def get_diff_action_id(action_id: int) -> list:
def get_diff_action_id(self, action_id: int) -> list:
"""
Takes action_id and returns which squadrons has been changed in leaderboard as in action_id and
experience they got in compassion to action_id - 1 for the same leaderboard and platform
@@ -110,7 +115,7 @@ def get_diff_action_id(action_id: int) -> list:
logger.debug(f'Not cached result for {cache_key}')
with db.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cursor:
with self.db.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cursor:
cursor.execute(postgres_sql_requests.select_diff_by_action_id, {'action_id': action_id})
result: list = cursor.fetchall()
+21 -13
View File
@@ -3,6 +3,7 @@ import typing
import json
from . import sqlite_sql_requests
from .abstract_model import AbstractModel
from .sqlite_cache import cache
import utils
from EDMCLogging import get_main_logger
@@ -10,15 +11,23 @@ from EDMCLogging import get_main_logger
logger = get_main_logger()
logger.propagate = False
db: sqlite3.Connection = sqlite3.connect('squads_stat.sqlite3', check_same_thread=False)
db.executescript(sqlite_sql_requests.schema_create) # schema creation
class Sqlite3Model(AbstractModel):
db: sqlite3.Connection
def open_model(self):
self.db = sqlite3.connect('squads_stat.sqlite3', check_same_thread=False)
self.db.executescript(sqlite_sql_requests.schema_create) # schema creation
# thx https://stackoverflow.com/a/48789604
db.row_factory = lambda c, r: dict(zip([col[0] for col in c.description], r))
self.db.row_factory = lambda c, r: dict(zip([col[0] for col in c.description], r))
def close_model(self):
self.db.close()
def get_activity_changes(platform: str, leaderboard_type: str, limit: int, low_timestamp, high_timestamp) -> list:
def get_activity_changes(self, platform: str, leaderboard_type: str, limit: int, low_timestamp, high_timestamp)\
-> list:
cache_key: str = f'{platform}_{leaderboard_type}_{limit}_{low_timestamp}_{high_timestamp}'
cached_result: typing.Union[str, None] = cache.get(cache_key)
@@ -28,7 +37,7 @@ def get_activity_changes(platform: str, leaderboard_type: str, limit: int, low_t
logger.debug(f'Not cached result for {cache_key}')
sql_req: sqlite3.Cursor = db.execute(sqlite_sql_requests.select_activity_pretty_names, {
sql_req: sqlite3.Cursor = self.db.execute(sqlite_sql_requests.select_activity_pretty_names, {
'LB_type': utils.LeaderboardTypes(leaderboard_type.lower()).value,
'platform': utils.Platform(platform.upper()).value,
'limit': limit,
@@ -43,8 +52,7 @@ def get_activity_changes(platform: str, leaderboard_type: str, limit: int, low_t
return result
def insert_leaderboard_db(leaderboard_list: dict) -> None:
def insert_leaderboard_db(self, leaderboard_list: dict) -> None:
"""
Takes leaderboard as list, it platform, type, db connection and insert leaderboard to DB
:param leaderboard_list: list from request_leaderboard
@@ -57,7 +65,7 @@ def insert_leaderboard_db(leaderboard_list: dict) -> None:
action_id: int # not last, current that we will use
sql_req_action_id: sqlite3.Cursor = db.execute(sqlite_sql_requests.select_last_action_id)
sql_req_action_id: sqlite3.Cursor = self.db.execute(sqlite_sql_requests.select_last_action_id)
action_id_fetch_one: typing.Union[None, dict[str, int]] = sql_req_action_id.fetchone()
if action_id_fetch_one is None:
# i.e. first launch
@@ -70,15 +78,14 @@ def insert_leaderboard_db(leaderboard_list: dict) -> None:
for squad in leaderboard:
squad.update({'action_id': action_id, 'LB_type': LB_type, 'platform': platform})
with db:
db.executemany(
with self.db:
self.db.executemany(
sqlite_sql_requests.insert_leader_board,
leaderboard)
cache.delete_all() # drop cache
def get_diff_action_id(action_id: int) -> list:
def get_diff_action_id(self, action_id: int) -> list:
"""
Takes action_id and returns which squadrons has been changed in leaderboard as in action_id and
experience they got in compassion to action_id - 1 for the same leaderboard and platform
@@ -93,7 +100,8 @@ def get_diff_action_id(action_id: int) -> list:
return json.loads(cached_result)
logger.debug(f'Not cached result for {cache_key}')
sql_req: sqlite3.Cursor = db.execute(sqlite_sql_requests.select_diff_by_action_id, {'action_id': action_id})
sql_req: sqlite3.Cursor = self.db.execute(sqlite_sql_requests.select_diff_by_action_id,
{'action_id': action_id})
result: list = sql_req.fetchall()
if not cache.disabled: