This commit is contained in:
parent
c7fe7f458c
commit
314c54969b
|
@ -45,13 +45,14 @@ async def get_author(_, _info, slug='', author_id=None):
|
||||||
if slug:
|
if slug:
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
aliased_author = aliased(Author)
|
aliased_author = aliased(Author)
|
||||||
q = select(aliased_author).filter(Author.slug == slug)
|
q = select(aliased_author).filter(aliased_author.slug == slug)
|
||||||
[author] = session.execute(q)
|
[author] = session.execute(q)
|
||||||
author_id = aliased_author.id
|
author_id = aliased_author.id
|
||||||
if author_id:
|
if author_id:
|
||||||
cache = await redis.execute('GET', f'id:{author_id}:author')
|
cache = await redis.execute('GET', f'id:{author_id}:author')
|
||||||
if not cache:
|
if not cache:
|
||||||
q = select(Author).where(Author.id == author_id)
|
aliased_author = aliased(Author)
|
||||||
|
q = select(aliased_author).where(aliased_author.id == author_id)
|
||||||
[author] = get_with_stat(q)
|
[author] = get_with_stat(q)
|
||||||
if author:
|
if author:
|
||||||
await update_author_cache(author)
|
await update_author_cache(author)
|
||||||
|
@ -219,6 +220,6 @@ def get_author_followers(_, _info, slug: str):
|
||||||
|
|
||||||
|
|
||||||
@query.field('search_authors')
|
@query.field('search_authors')
|
||||||
def search_authors(_, info, text: str):
|
def search_authors(_, info, t: str):
|
||||||
q = search(select(Author), text)
|
q = search(select(Author), t)
|
||||||
return get_with_stat(q)
|
return get_with_stat(q)
|
||||||
|
|
|
@ -50,7 +50,7 @@ def add_author_stat_columns(q):
|
||||||
)
|
)
|
||||||
.outerjoin(aliased_author_authors, aliased_author_authors.follower == Author.id)
|
.outerjoin(aliased_author_authors, aliased_author_authors.follower == Author.id)
|
||||||
.add_columns(
|
.add_columns(
|
||||||
func.count(distinct(aliased_shout_author.author)).label('authors_stat')
|
func.count(distinct(aliased_author_authors.author)).label('authors_stat')
|
||||||
)
|
)
|
||||||
.outerjoin(
|
.outerjoin(
|
||||||
aliased_author_followers, aliased_author_followers.author == Author.id
|
aliased_author_followers, aliased_author_followers.author == Author.id
|
||||||
|
|
|
@ -14,23 +14,6 @@ from settings import DB_URL
|
||||||
import warnings
|
import warnings
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
|
||||||
# Функция для вывода полного трейсбека при предупреждениях
|
|
||||||
def warning_with_traceback(message, category, filename, lineno, line=None):
|
|
||||||
tb = traceback.format_stack()
|
|
||||||
tb_str = ''.join(tb)
|
|
||||||
return f'{message} ({filename}, {lineno}): {category.__name__}\n{tb_str}'
|
|
||||||
|
|
||||||
|
|
||||||
# Установка функции вывода трейсбека для предупреждений SQLAlchemy
|
|
||||||
warnings.formatwarning = warning_with_traceback
|
|
||||||
warnings.simplefilter('always', exc.SAWarning)
|
|
||||||
|
|
||||||
|
|
||||||
# Установка функции вывода трейсбека для предупреждений SQLAlchemy
|
|
||||||
warnings.showwarning = warning_with_traceback
|
|
||||||
warnings.simplefilter('always', exc.SAWarning)
|
|
||||||
|
|
||||||
# Подключение к базе данных SQLAlchemy
|
# Подключение к базе данных SQLAlchemy
|
||||||
engine = create_engine(DB_URL, echo=False, pool_size=10, max_overflow=20)
|
engine = create_engine(DB_URL, echo=False, pool_size=10, max_overflow=20)
|
||||||
inspector = inspect(engine)
|
inspector = inspect(engine)
|
||||||
|
@ -39,22 +22,6 @@ T = TypeVar('T')
|
||||||
REGISTRY: Dict[str, type] = {}
|
REGISTRY: Dict[str, type] = {}
|
||||||
|
|
||||||
|
|
||||||
# Перехватчики для журнала запросов SQLAlchemy
|
|
||||||
@event.listens_for(Engine, 'before_cursor_execute')
|
|
||||||
def before_cursor_execute(conn, cursor, statement, parameters, context, executemany):
|
|
||||||
conn.query_start_time = time.time()
|
|
||||||
|
|
||||||
|
|
||||||
# noinspection PyUnusedLocal
|
|
||||||
@event.listens_for(Engine, 'after_cursor_execute')
|
|
||||||
def after_cursor_execute(conn, cursor, statement, parameters, context, executemany):
|
|
||||||
if hasattr(conn, '_query_start_time'):
|
|
||||||
elapsed = time.time() - conn.query_start_time
|
|
||||||
conn.query_start_time = None
|
|
||||||
query = f'{statement}'.replace('\n', ' ')
|
|
||||||
logger.debug(f"\n{query}\n{'*' * math.floor(elapsed)} {elapsed:.3f} s\n")
|
|
||||||
|
|
||||||
|
|
||||||
# noinspection PyUnusedLocal
|
# noinspection PyUnusedLocal
|
||||||
def local_session(src=''):
|
def local_session(src=''):
|
||||||
return Session(bind=engine, expire_on_commit=False)
|
return Session(bind=engine, expire_on_commit=False)
|
||||||
|
@ -92,3 +59,31 @@ class Base(declarative_base()):
|
||||||
|
|
||||||
make_searchable(Base.metadata)
|
make_searchable(Base.metadata)
|
||||||
Base.metadata.create_all(engine)
|
Base.metadata.create_all(engine)
|
||||||
|
|
||||||
|
|
||||||
|
# Функция для вывода полного трейсбека при предупреждениях
|
||||||
|
def warning_with_traceback(message: Warning | str, category, filename: str, lineno: int, file=None, line=None):
|
||||||
|
tb = traceback.format_stack()
|
||||||
|
tb_str = ''.join(tb)
|
||||||
|
return f'{message} ({filename}, {lineno}): {category.__name__}\n{tb_str}'
|
||||||
|
|
||||||
|
|
||||||
|
# Установка функции вывода трейсбека для предупреждений SQLAlchemy
|
||||||
|
warnings.showwarning = warning_with_traceback
|
||||||
|
warnings.simplefilter('always', exc.SAWarning)
|
||||||
|
|
||||||
|
|
||||||
|
# Перехватчики для журнала запросов SQLAlchemy
|
||||||
|
@event.listens_for(Engine, 'before_cursor_execute')
|
||||||
|
def before_cursor_execute(conn, cursor, statement, parameters, context, executemany):
|
||||||
|
conn.query_start_time = time.time()
|
||||||
|
|
||||||
|
|
||||||
|
# noinspection PyUnusedLocal
|
||||||
|
@event.listens_for(Engine, 'after_cursor_execute')
|
||||||
|
def after_cursor_execute(conn, cursor, statement, parameters, context, executemany):
|
||||||
|
if hasattr(conn, '_query_start_time'):
|
||||||
|
elapsed = time.time() - conn.query_start_time
|
||||||
|
conn.query_start_time = None
|
||||||
|
query = f'{statement}'.replace('\n', ' ')
|
||||||
|
logger.debug(f"\n{query}\n{'*' * math.floor(elapsed)} {elapsed:.3f} s\n")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user