queries-refactoring-2
All checks were successful
Deploy on push / deploy (push) Successful in 26s

This commit is contained in:
Untone 2024-05-18 14:15:05 +03:00
parent 7d97f40826
commit fd7bd385fc
7 changed files with 46 additions and 34 deletions

View File

@ -30,7 +30,9 @@ async def update_author(_, info, profile):
Author.update(author, profile)
session.add(author)
session.commit()
[author] = get_with_stat(select(Author).where(Author.user == user_id))
author_query = select(Author).where(Author.user == user_id)
[author] = get_with_stat(author_query)
if author:
await cache_author(author.dict())
return {"error": None, "author": author}
except Exception as exc:
@ -330,7 +332,7 @@ async def get_author_followers(_, _info, slug: str):
return followers
author_follower_alias = aliased(AuthorFollower, name="af")
q = select(Author).join(
followers_query = select(Author).join(
author_follower_alias,
and_(
author_follower_alias.author == author_id,
@ -338,7 +340,7 @@ async def get_author_followers(_, _info, slug: str):
Author.id != author_id, # exclude the author from the followers
),
)
followers = get_with_stat(q)
followers = get_with_stat(followers_query)
if isinstance(followers, list):
followers_ids = [r.id for r in followers]
for follower in followers:

View File

@ -22,8 +22,8 @@ from services.search import search_service
async def cache_by_id(entity, entity_id: int):
q = select(entity).filter(entity.id == entity_id)
[x] = get_with_stat(q)
caching_query = select(entity).filter(entity.id == entity_id)
[x] = get_with_stat(caching_query)
if not x:
return

View File

@ -23,8 +23,8 @@ from services.schema import mutation, query
async def cache_by_slug(what: str, slug: str):
is_author = what == "AUTHOR"
alias = Author if is_author else Topic
q = select(alias).filter(alias.slug == slug)
[x] = get_with_stat(q)
caching_query = select(alias).filter(alias.slug == slug)
[x] = get_with_stat(caching_query)
if not x:
return
@ -56,7 +56,8 @@ async def follow(_, info, what, slug):
follower_id = int(follower_id)
error = author_follow(follower_id, slug)
if not error:
[author] = get_with_stat(select(Author).filter(Author.slug == slug))
author_query = select(Author).filter(Author.slug == slug)
[author] = get_with_stat(author_query)
if author:
author_dict = author.dict()
author_id = int(author_dict.get("id", 0))
@ -109,7 +110,8 @@ async def unfollow(_, info, what, slug):
# NOTE: after triggers should update cached stats
if not error:
logger.info(f"@{follower_dict.get('slug')} unfollowed @{slug}")
[author] = get_with_stat(select(Author).filter(Author.slug == slug))
author_query = select(Author).filter(Author.slug == slug)
[author] = get_with_stat(author_query)
if author:
author_dict = author.dict()
author_id = author.id
@ -288,13 +290,13 @@ def author_unfollow(follower_id, slug):
@query.field("get_topic_followers")
async def get_topic_followers(_, _info, slug: str, topic_id: int) -> List[Author]:
q = select(Author)
q = (
q.join(TopicFollower, TopicFollower.follower == Author.id)
topic_followers_query = select(Author)
topic_followers_query = (
topic_followers_query.join(TopicFollower, TopicFollower.follower == Author.id)
.join(Topic, Topic.id == TopicFollower.topic)
.filter(or_(Topic.slug == slug, Topic.id == topic_id))
)
return get_with_stat(q)
return get_with_stat(topic_followers_query)
@query.field("get_shout_followers")

View File

@ -205,6 +205,7 @@ def get_with_stat(q):
except Exception as exc:
import traceback
logger.debug(q)
traceback.print_exc()
logger.error(exc, exc_info=True)
return records
@ -212,26 +213,27 @@ def get_with_stat(q):
def author_follows_authors(author_id: int):
af = aliased(AuthorFollower, name="af")
q = (
author_follows_authors_query = (
select(Author)
.select_from(join(Author, af, Author.id == af.author))
.where(af.follower == author_id)
)
return get_with_stat(q)
return get_with_stat(author_follows_authors_query)
def author_follows_topics(author_id: int):
q = (
author_follows_topics_query = (
select(Topic)
.select_from(join(Topic, TopicFollower, Topic.id == TopicFollower.topic))
.where(TopicFollower.follower == author_id)
)
return get_with_stat(q)
return get_with_stat(author_follows_topics_query)
async def update_author_stat(author_id: int):
author_query = select(Author).where(Author.id == author_id)
try:
[author_with_stat] = get_with_stat(select(Author).where(Author.id == author_id))
[author_with_stat] = get_with_stat(author_query)
if isinstance(author_with_stat, Author):
author_dict = author_with_stat.dict()
await cache_author(author_dict)

View File

@ -16,7 +16,8 @@ def get_topics_all(_, _info):
@cache_region.cache_on_arguments(cache_key)
def _get_topics_all():
return get_with_stat(select(Topic))
topics_query = select(Topic)
return get_with_stat(topics_query)
return _get_topics_all()
@ -27,29 +28,35 @@ def get_topics_by_community(_, _info, community_id: int):
@cache_region.cache_on_arguments(cache_key)
def _get_topics_by_community():
q = select(Topic).where(Topic.community == community_id)
return get_with_stat(q)
topics_by_community_query = select(Topic).where(Topic.community == community_id)
return get_with_stat(topics_by_community_query)
return _get_topics_by_community()
@query.field("get_topics_by_author")
async def get_topics_by_author(_, _info, author_id=0, slug="", user=""):
q = select(Topic)
topics_by_author_query = select(Topic)
if author_id:
q = q.join(Author).where(Author.id == author_id)
topics_by_author_query = topics_by_author_query.join(Author).where(
Author.id == author_id
)
elif slug:
q = q.join(Author).where(Author.slug == slug)
topics_by_author_query = topics_by_author_query.join(Author).where(
Author.slug == slug
)
elif user:
q = q.join(Author).where(Author.user == user)
topics_by_author_query = topics_by_author_query.join(Author).where(
Author.user == user
)
return get_with_stat(q)
return get_with_stat(topics_by_author_query)
@query.field("get_topic")
def get_topic(_, _info, slug: str):
q = select(Topic).filter(Topic.slug == slug)
result = get_with_stat(q)
topic_query = select(Topic).filter(Topic.slug == slug)
result = get_with_stat(topic_query)
for topic in result:
return topic

View File

@ -107,8 +107,8 @@ def after_reaction_update(mapper, connection, reaction: Reaction):
def after_author_update(_mapper, _connection, author: Author):
logger.info("after author update")
q = select(Author).where(Author.id == author.id)
result = get_with_stat(q)
author_query = select(Author).where(Author.id == author.id)
result = get_with_stat(author_query)
if result:
[author_with_stat] = result
if author_with_stat:

View File

@ -60,9 +60,8 @@ class WebhookEndpoint(HTTPEndpoint):
author = Author(user=user_id, slug=slug, name=name, pic=pic)
session.add(author)
session.commit()
[author_with_stat] = get_with_stat(
select(Author).filter(Author.id == author.id)
)
author_query = select(Author).filter(Author.user == user_id)
[author_with_stat] = get_with_stat(author_query)
if author_with_stat:
await cache_author(author_with_stat)