core/services/db.py

93 lines
2.6 KiB
Python
Raw Normal View History

2023-12-24 17:46:50 +00:00
import math
2023-12-24 14:25:57 +00:00
import time
import logging
2023-11-22 16:38:39 +00:00
# from contextlib import contextmanager
from typing import Any, Callable, Dict, TypeVar
# from psycopg2.errors import UniqueViolation
2023-12-24 14:25:57 +00:00
from sqlalchemy import Column, Integer, create_engine, event
from sqlalchemy.ext.declarative import declarative_base
2023-11-22 16:38:39 +00:00
from sqlalchemy.orm import Session
from sqlalchemy.sql.schema import Table
2023-12-24 14:25:57 +00:00
from sqlalchemy.engine import Engine
2023-11-22 16:38:39 +00:00
from settings import DB_URL
2023-11-03 10:10:22 +00:00
2023-12-24 14:25:57 +00:00
logging.basicConfig()
logger = logging.getLogger("\t [sqlalchemy.profiler]\t")
logger.setLevel(logging.DEBUG)
@event.listens_for(Engine, "before_cursor_execute")
def before_cursor_execute(conn, cursor, statement, parameters, context, executemany):
conn.info.setdefault("query_start_time", []).append(time.time())
2023-12-24 22:06:27 +00:00
# logger.debug(f" {statement}")
2023-12-24 14:25:57 +00:00
@event.listens_for(Engine, "after_cursor_execute")
def after_cursor_execute(conn, cursor, statement, parameters, context, executemany):
total = time.time() - conn.info["query_start_time"].pop(-1)
2023-12-25 07:48:50 +00:00
total = math.floor(total * 10000) / 10000
2023-12-24 22:42:39 +00:00
if total > 35:
2023-12-25 07:48:50 +00:00
print(f"\n{statement}\n----------------- Finished in {total} s ")
2023-12-24 14:25:57 +00:00
2023-11-03 10:10:22 +00:00
engine = create_engine(DB_URL, echo=False, pool_size=10, max_overflow=20)
T = TypeVar("T")
REGISTRY: Dict[str, type] = {}
2022-09-03 10:50:14 +00:00
2023-11-22 16:38:39 +00:00
# @contextmanager
def local_session(src=""):
return Session(bind=engine, expire_on_commit=False)
# try:
# yield session
# session.commit()
# except Exception as e:
# if not (src == "create_shout" and isinstance(e, UniqueViolation)):
# import traceback
# session.rollback()
# print(f"[services.db] {src}: {e}")
# traceback.print_exc()
# raise Exception("[services.db] exception")
# finally:
# session.close()
2021-08-05 16:49:08 +00:00
class Base(declarative_base()):
2022-09-03 10:50:14 +00:00
__table__: Table
__tablename__: str
__new__: Callable
__init__: Callable
2023-01-31 07:36:54 +00:00
__allow_unmapped__ = True
2023-01-31 07:44:06 +00:00
__abstract__ = True
2022-09-03 10:50:14 +00:00
__table_args__ = {"extend_existing": True}
2023-01-31 07:44:06 +00:00
id = Column(Integer, primary_key=True)
2022-09-03 10:50:14 +00:00
def __init_subclass__(cls, **kwargs):
REGISTRY[cls.__name__] = cls
def dict(self) -> Dict[str, Any]:
column_names = self.__table__.columns.keys()
2023-11-22 16:38:39 +00:00
if "_sa_instance_state" in column_names:
column_names.remove("_sa_instance_state")
2023-11-03 10:10:22 +00:00
try:
return {c: getattr(self, c) for c in column_names}
except Exception as e:
print(f"[services.db] Error dict: {e}")
return {}
2023-11-22 16:38:39 +00:00
def update(self, values: Dict[str, Any]) -> None:
for key, value in values.items():
if hasattr(self, key):
setattr(self, key, value)