load-random-top-fix
This commit is contained in:
parent
e0395b0ab6
commit
43f0c517b3
|
@ -105,8 +105,8 @@ async def get_my_followed(_, info):
|
||||||
communities = []
|
communities = []
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
author = session.query(Author).filter(Author.user == user_id).first()
|
author = session.query(Author).filter(Author.user == user_id).first()
|
||||||
if author:
|
if isinstance(author, Author):
|
||||||
author_id = int(author.id)
|
author_id = author.id
|
||||||
|
|
||||||
authors_query = (
|
authors_query = (
|
||||||
session.query(Author)
|
session.query(Author)
|
||||||
|
|
|
@ -2,7 +2,7 @@ import time
|
||||||
from typing import List
|
from typing import List
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from sqlalchemy import and_, asc, case, desc, func, select, text
|
from sqlalchemy import and_, asc, case, desc, func, select, text, or_
|
||||||
from sqlalchemy.orm import aliased, joinedload
|
from sqlalchemy.orm import aliased, joinedload
|
||||||
|
|
||||||
from orm.author import Author
|
from orm.author import Author
|
||||||
|
@ -18,8 +18,7 @@ logging.basicConfig()
|
||||||
logger = logging.getLogger("\t[resolvers.reaction]\t")
|
logger = logging.getLogger("\t[resolvers.reaction]\t")
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
def add_stat_columns(q):
|
def add_stat_columns(q, aliased_reaction):
|
||||||
aliased_reaction = aliased(Reaction)
|
|
||||||
|
|
||||||
q = q.outerjoin(aliased_reaction).add_columns(
|
q = q.outerjoin(aliased_reaction).add_columns(
|
||||||
func.sum(case((aliased_reaction.kind == ReactionKind.COMMENT.value, 1), else_=0)).label("comments_stat"),
|
func.sum(case((aliased_reaction.kind == ReactionKind.COMMENT.value, 1), else_=0)).label("comments_stat"),
|
||||||
|
@ -28,7 +27,7 @@ def add_stat_columns(q):
|
||||||
func.max(case((aliased_reaction.kind != ReactionKind.COMMENT.value, None),else_=aliased_reaction.created_at)).label("last_comment"),
|
func.max(case((aliased_reaction.kind != ReactionKind.COMMENT.value, None),else_=aliased_reaction.created_at)).label("last_comment"),
|
||||||
)
|
)
|
||||||
|
|
||||||
return q, aliased_reaction
|
return q
|
||||||
|
|
||||||
|
|
||||||
def reactions_follow(author_id, shout_id, auto=False):
|
def reactions_follow(author_id, shout_id, auto=False):
|
||||||
|
@ -270,7 +269,8 @@ async def update_reaction(_, info, rid, reaction):
|
||||||
user_id = info.context["user_id"]
|
user_id = info.context["user_id"]
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
q = select(Reaction).filter(Reaction.id == rid)
|
q = select(Reaction).filter(Reaction.id == rid)
|
||||||
q, aliased_reaction = add_stat_columns(q)
|
aliased_reaction = aliased(Reaction)
|
||||||
|
q = add_stat_columns(q, aliased_reaction)
|
||||||
q = q.group_by(Reaction.id)
|
q = q.group_by(Reaction.id)
|
||||||
|
|
||||||
[r, commented_stat, likes_stat, dislikes_stat, _l] = session.execute(q).unique().one()
|
[r, commented_stat, likes_stat, dislikes_stat, _l] = session.execute(q).unique().one()
|
||||||
|
@ -385,7 +385,8 @@ async def load_reactions_by(_, info, by, limit=50, offset=0):
|
||||||
)
|
)
|
||||||
|
|
||||||
# calculate counters
|
# calculate counters
|
||||||
q, aliased_reaction = add_stat_columns(q)
|
aliased_reaction = aliased(Reaction)
|
||||||
|
q = add_stat_columns(q, aliased_reaction)
|
||||||
|
|
||||||
# filter
|
# filter
|
||||||
q = apply_reaction_filters(by, q)
|
q = apply_reaction_filters(by, q)
|
||||||
|
@ -424,23 +425,41 @@ async def load_reactions_by(_, info, by, limit=50, offset=0):
|
||||||
return reactions
|
return reactions
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def reacted_shouts_updates(follower_id: int, limit=50, offset=0) -> List[Shout]:
|
def reacted_shouts_updates(follower_id: int, limit=50, offset=0) -> List[Shout]:
|
||||||
shouts: List[Shout] = []
|
shouts: List[Shout] = []
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
author = session.query(Author).where(Author.id == follower_id).first()
|
author = session.query(Author).filter(Author.id == follower_id).first()
|
||||||
if author:
|
if author:
|
||||||
shouts = (
|
# Shouts where follower is the author
|
||||||
|
shouts_author, aliased_reaction = add_stat_columns(
|
||||||
session.query(Shout)
|
session.query(Shout)
|
||||||
.join(Reaction, Reaction.shout == Shout.id)
|
.outerjoin(Reaction, and_(Reaction.shout_id == Shout.id, Reaction.created_by == follower_id))
|
||||||
.options(joinedload(Reaction.created_by))
|
.outerjoin(Author, Shout.authors.any(id=follower_id))
|
||||||
|
.options(joinedload(Shout.reactions), joinedload(Shout.authors)),
|
||||||
|
aliased(Reaction)
|
||||||
|
).filter(Author.id == follower_id).group_by(Shout.id).all()
|
||||||
|
|
||||||
|
# Shouts where follower has reactions
|
||||||
|
shouts_reactions = (
|
||||||
|
session.query(Shout)
|
||||||
|
.join(Reaction, Reaction.shout_id == Shout.id)
|
||||||
|
.options(joinedload(Shout.reactions), joinedload(Shout.authors))
|
||||||
.filter(Reaction.created_by == follower_id)
|
.filter(Reaction.created_by == follower_id)
|
||||||
.filter(Reaction.created_at > author.last_seen)
|
.group_by(Shout.id)
|
||||||
.limit(limit)
|
|
||||||
.offset(offset)
|
|
||||||
.all()
|
.all()
|
||||||
)
|
)
|
||||||
return shouts
|
|
||||||
|
|
||||||
|
# Combine shouts from both queries
|
||||||
|
shouts = list(set(shouts_author + shouts_reactions))
|
||||||
|
|
||||||
|
# Sort shouts by the `last_comment` field
|
||||||
|
shouts.sort(key=lambda shout: shout.last_comment, reverse=True)
|
||||||
|
|
||||||
|
# Apply limit and offset
|
||||||
|
shouts = shouts[offset: offset + limit]
|
||||||
|
|
||||||
|
return shouts
|
||||||
|
|
||||||
@query.field("load_shouts_followed")
|
@query.field("load_shouts_followed")
|
||||||
@login_required
|
@login_required
|
||||||
|
|
|
@ -47,8 +47,8 @@ async def get_shout(_, _info, slug=None, shout_id=None):
|
||||||
joinedload(Shout.authors),
|
joinedload(Shout.authors),
|
||||||
joinedload(Shout.topics),
|
joinedload(Shout.topics),
|
||||||
)
|
)
|
||||||
|
aliased_reaction = aliased(Reaction)
|
||||||
q, _ar = add_stat_columns(q)
|
q = add_stat_columns(q, aliased_reaction)
|
||||||
|
|
||||||
if slug is not None:
|
if slug is not None:
|
||||||
q = q.filter(Shout.slug == slug)
|
q = q.filter(Shout.slug == slug)
|
||||||
|
@ -122,7 +122,8 @@ async def load_shouts_by(_, _info, options):
|
||||||
)
|
)
|
||||||
|
|
||||||
# stats
|
# stats
|
||||||
q, _ar = add_stat_columns(q)
|
aliased_reaction = aliased(Reaction)
|
||||||
|
q = add_stat_columns(q, aliased_reaction)
|
||||||
|
|
||||||
# filters
|
# filters
|
||||||
q = apply_filters(q, options.get("filters", {}))
|
q = apply_filters(q, options.get("filters", {}))
|
||||||
|
@ -209,6 +210,7 @@ async def load_shouts_drafts(_, info):
|
||||||
async def load_shouts_feed(_, info, options):
|
async def load_shouts_feed(_, info, options):
|
||||||
user_id = info.context["user_id"]
|
user_id = info.context["user_id"]
|
||||||
|
|
||||||
|
shouts = []
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
reader = session.query(Author).filter(Author.user == user_id).first()
|
reader = session.query(Author).filter(Author.user == user_id).first()
|
||||||
if reader:
|
if reader:
|
||||||
|
@ -231,7 +233,8 @@ async def load_shouts_feed(_, info, options):
|
||||||
.where(and_(Shout.published_at.is_not(None), Shout.deleted_at.is_(None), Shout.id.in_(subquery)))
|
.where(and_(Shout.published_at.is_not(None), Shout.deleted_at.is_(None), Shout.id.in_(subquery)))
|
||||||
)
|
)
|
||||||
|
|
||||||
q, _ar = add_stat_columns(q)
|
aliased_reaction = aliased(Reaction)
|
||||||
|
q = add_stat_columns(q, aliased_reaction)
|
||||||
q = apply_filters(q, options.get("filters", {}), reader.id)
|
q = apply_filters(q, options.get("filters", {}), reader.id)
|
||||||
|
|
||||||
order_by = options.get("order_by", Shout.published_at)
|
order_by = options.get("order_by", Shout.published_at)
|
||||||
|
@ -244,7 +247,6 @@ async def load_shouts_feed(_, info, options):
|
||||||
|
|
||||||
# print(q.compile(compile_kwargs={"literal_binds": True}))
|
# print(q.compile(compile_kwargs={"literal_binds": True}))
|
||||||
|
|
||||||
shouts = []
|
|
||||||
for [shout, reacted_stat, commented_stat, _last_comment] in session.execute(q).unique():
|
for [shout, reacted_stat, commented_stat, _last_comment] in session.execute(q).unique():
|
||||||
main_topic = (
|
main_topic = (
|
||||||
session.query(Topic.slug)
|
session.query(Topic.slug)
|
||||||
|
@ -331,7 +333,8 @@ async def load_shouts_unrated(_, info, limit: int = 50, offset: int = 0):
|
||||||
# 3 or fewer votes is 0, 1, 2 or 3 votes (null, reaction id1, reaction id2, reaction id3)
|
# 3 or fewer votes is 0, 1, 2 or 3 votes (null, reaction id1, reaction id2, reaction id3)
|
||||||
q = q.having(func.count(distinct(Reaction.id)) <= 4)
|
q = q.having(func.count(distinct(Reaction.id)) <= 4)
|
||||||
|
|
||||||
q, _ar = add_stat_columns(q)
|
aliased_reaction = aliased(Reaction)
|
||||||
|
q = add_stat_columns(q, aliased_reaction)
|
||||||
|
|
||||||
q = q.group_by(Shout.id).order_by(func.random()).limit(limit).offset(offset)
|
q = q.group_by(Shout.id).order_by(func.random()).limit(limit).offset(offset)
|
||||||
user_id = info.context.get("user_id")
|
user_id = info.context.get("user_id")
|
||||||
|
@ -382,7 +385,13 @@ async def load_shouts_random_top(_, _info, options):
|
||||||
subquery = select(Shout.id).outerjoin(aliased_reaction).where(Shout.deleted_at.is_(None))
|
subquery = select(Shout.id).outerjoin(aliased_reaction).where(Shout.deleted_at.is_(None))
|
||||||
|
|
||||||
subquery = apply_filters(subquery, options.get("filters", {}))
|
subquery = apply_filters(subquery, options.get("filters", {}))
|
||||||
subquery = subquery.group_by(Shout.id).order_by(desc(get_rating_func(aliased_reaction)))
|
subquery = subquery.group_by(Shout.id).order_by(desc(func.sum(
|
||||||
|
case(
|
||||||
|
(aliased_reaction.kind == str(ReactionKind.LIKE.value), 1),
|
||||||
|
(aliased_reaction.kind == str(ReactionKind.DISLIKE.value), -1),
|
||||||
|
else_=0,
|
||||||
|
)
|
||||||
|
)))
|
||||||
|
|
||||||
random_limit = options.get("random_limit")
|
random_limit = options.get("random_limit")
|
||||||
if random_limit:
|
if random_limit:
|
||||||
|
@ -397,16 +406,18 @@ async def load_shouts_random_top(_, _info, options):
|
||||||
.where(Shout.id.in_(subquery))
|
.where(Shout.id.in_(subquery))
|
||||||
)
|
)
|
||||||
|
|
||||||
q, _ar = add_stat_columns(q)
|
aliased_reaction = aliased(Reaction)
|
||||||
|
q = add_stat_columns(q, aliased_reaction)
|
||||||
|
|
||||||
limit = options.get("limit", 10)
|
# Calculate the difference between likes_stat and dislikes_stat and use it for ordering
|
||||||
q = q.group_by(Shout.id).order_by(func.random()).limit(limit)
|
q = q.group_by(Shout.id).order_by(desc(func.sum(aliased_reaction.likes_stat - aliased_reaction.dislikes_stat))).limit(limit)
|
||||||
|
|
||||||
# print(q.compile(compile_kwargs={"literal_binds": True}))
|
# print(q.compile(compile_kwargs={"literal_binds": True}))
|
||||||
|
|
||||||
return get_shouts_from_query(q)
|
return get_shouts_from_query(q)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@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):
|
||||||
topic = get_random_topic()
|
topic = get_random_topic()
|
||||||
|
@ -420,7 +431,8 @@ async def load_shouts_random_topic(_, info, limit: int = 10):
|
||||||
.filter(and_(Shout.deleted_at.is_(None), Shout.visibility == "public", Shout.topics.any(slug=topic.slug)))
|
.filter(and_(Shout.deleted_at.is_(None), Shout.visibility == "public", Shout.topics.any(slug=topic.slug)))
|
||||||
)
|
)
|
||||||
|
|
||||||
q, _ar = add_stat_columns(q)
|
aliased_reaction = aliased(Reaction)
|
||||||
|
q = add_stat_columns(q, aliased_reaction)
|
||||||
|
|
||||||
q = q.group_by(Shout.id).order_by(desc(Shout.created_at)).limit(limit)
|
q = q.group_by(Shout.id).order_by(desc(Shout.created_at)).limit(limit)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user