revert-2-queries-less-price
All checks were successful
Deploy on push / deploy (push) Successful in 25s
All checks were successful
Deploy on push / deploy (push) Successful in 25s
This commit is contained in:
parent
b99ed1a7d1
commit
31320c9972
|
@ -8,7 +8,7 @@ from orm.reaction import Reaction, ReactionKind
|
||||||
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
from orm.shout import Shout, ShoutAuthor, ShoutTopic
|
||||||
from orm.topic import Topic, TopicFollower
|
from orm.topic import Topic, TopicFollower
|
||||||
from resolvers.reaction import add_reaction_stat_columns
|
from resolvers.reaction import add_reaction_stat_columns
|
||||||
from resolvers.topic import random_topic_query
|
from resolvers.topic import get_topics_random
|
||||||
from services.auth import login_required
|
from services.auth import login_required
|
||||||
from services.db import local_session
|
from services.db import local_session
|
||||||
from services.schema import query
|
from services.schema import query
|
||||||
|
@ -461,32 +461,36 @@ async def load_shouts_random_top(_, _info, options):
|
||||||
|
|
||||||
@query.field('load_shouts_random_topic')
|
@query.field('load_shouts_random_topic')
|
||||||
async def load_shouts_random_topic(_, info, limit: int = 10):
|
async def load_shouts_random_topic(_, info, limit: int = 10):
|
||||||
random_topic_subquery = random_topic_query(1).subquery()
|
[topic] = get_topics_random(None, None, 1)
|
||||||
|
if topic:
|
||||||
|
shouts = fetch_shouts_by_topic(topic, limit)
|
||||||
|
if shouts:
|
||||||
|
return {'topic': topic, 'shouts': shouts}
|
||||||
|
return {
|
||||||
|
'error': 'failed to get random topic after few retries',
|
||||||
|
'shouts': [],
|
||||||
|
'topic': {},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def fetch_shouts_by_topic(topic, limit):
|
||||||
q = (
|
q = (
|
||||||
select(Topic, Shout)
|
select(Shout)
|
||||||
.select_from(Topic)
|
.options(joinedload(Shout.authors), joinedload(Shout.topics))
|
||||||
.join(Shout, Shout.topics.any(Topic.id == random_topic_subquery.c.id))
|
|
||||||
.options(joinedload(Shout.authors))
|
|
||||||
.filter(
|
.filter(
|
||||||
and_(
|
and_(
|
||||||
Shout.deleted_at.is_(None),
|
Shout.deleted_at.is_(None),
|
||||||
Shout.featured_at.is_not(None),
|
Shout.featured_at.is_not(None),
|
||||||
Shout.topics.any(slug=Topic.slug),
|
Shout.topics.any(slug=topic.slug),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
aliased_reaction = aliased(Reaction)
|
aliased_reaction = aliased(Reaction)
|
||||||
q = add_reaction_stat_columns(q, aliased_reaction)
|
q = add_reaction_stat_columns(q, aliased_reaction)
|
||||||
q = q.group_by(Shout.id, Topic.slug).order_by(desc(Shout.created_at)).limit(limit)
|
|
||||||
result = local_session().execute(q)
|
|
||||||
|
|
||||||
if result:
|
q = q.group_by(Shout.id).order_by(desc(Shout.created_at)).limit(limit)
|
||||||
topic, shouts = result[0]
|
|
||||||
return {'topic': topic, 'shouts': shouts}
|
|
||||||
|
|
||||||
return {
|
shouts = get_shouts_from_query(q)
|
||||||
'error': 'failed to get random topic after few retries',
|
|
||||||
'shouts': [],
|
return shouts
|
||||||
'topic': None
|
|
||||||
}
|
|
||||||
|
|
|
@ -122,7 +122,11 @@ def topic_unfollow(follower_id, slug):
|
||||||
|
|
||||||
@query.field('get_topics_random')
|
@query.field('get_topics_random')
|
||||||
def get_topics_random(_, _info, amount=12):
|
def get_topics_random(_, _info, amount=12):
|
||||||
q = random_topic_query(amount)
|
q = select(Topic)
|
||||||
|
q = q.join(ShoutTopic)
|
||||||
|
q = q.group_by(Topic.id)
|
||||||
|
q = q.having(func.count(distinct(ShoutTopic.shout)) > 2)
|
||||||
|
q = q.order_by(func.random()).limit(amount)
|
||||||
|
|
||||||
topics = []
|
topics = []
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
|
@ -130,12 +134,3 @@ def get_topics_random(_, _info, amount=12):
|
||||||
topics.append(topic)
|
topics.append(topic)
|
||||||
|
|
||||||
return topics
|
return topics
|
||||||
|
|
||||||
|
|
||||||
def random_topic_query(amount: int):
|
|
||||||
q = select(Topic)
|
|
||||||
q = q.join(ShoutTopic)
|
|
||||||
q = q.group_by(Topic.id)
|
|
||||||
q = q.having(func.count(distinct(ShoutTopic.shout)) > 2)
|
|
||||||
q = q.order_by(func.random()).limit(amount)
|
|
||||||
return q
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user