stat-fix-5
This commit is contained in:
parent
2e3e79f51e
commit
ebe034a527
|
@ -18,7 +18,6 @@ from resolvers.reaction import reacted_shouts_updates as followed_reactions
|
|||
|
||||
|
||||
def add_author_stat_columns(q):
|
||||
|
||||
shout_author_aliased = aliased(ShoutAuthor)
|
||||
q = q.outerjoin(shout_author_aliased).add_columns(
|
||||
func.count(distinct(shout_author_aliased.shout)).label("shouts_stat")
|
||||
|
@ -36,22 +35,17 @@ def add_author_stat_columns(q):
|
|||
|
||||
rating_aliased = aliased(Reaction)
|
||||
# q = q.add_columns(literal(0).label("rating_stat"))
|
||||
q = (
|
||||
q.outerjoin(rating_aliased, rating_aliased.shout == shout_author_aliased.shout)
|
||||
.add_columns(
|
||||
func.coalesce(func.sum(case(
|
||||
(and_(
|
||||
rating_aliased.kind == ReactionKind.LIKE.value,
|
||||
rating_aliased.reply_to.is_(None)
|
||||
), 1),
|
||||
(and_(
|
||||
rating_aliased.kind == ReactionKind.DISLIKE.value,
|
||||
rating_aliased.reply_to.is_(None)
|
||||
), -1),
|
||||
else_=0
|
||||
)), 0)
|
||||
.label("rating_stat")
|
||||
)
|
||||
q = q.outerjoin(rating_aliased, rating_aliased.shout == shout_author_aliased.shout).add_columns(
|
||||
func.coalesce(
|
||||
func.sum(
|
||||
case(
|
||||
(and_(rating_aliased.kind == ReactionKind.LIKE.value, rating_aliased.reply_to.is_(None)), 1),
|
||||
(and_(rating_aliased.kind == ReactionKind.DISLIKE.value, rating_aliased.reply_to.is_(None)), -1),
|
||||
else_=0,
|
||||
)
|
||||
),
|
||||
0,
|
||||
).label("rating_stat")
|
||||
)
|
||||
|
||||
q = q.add_columns(literal(0).label("commented_stat"))
|
||||
|
|
|
@ -15,12 +15,9 @@ from orm.author import Author
|
|||
def add_reaction_stat_columns(q):
|
||||
aliased_reaction = aliased(Reaction)
|
||||
|
||||
q = (
|
||||
q.outerjoin(aliased_reaction, Reaction.id == aliased_reaction.reply_to).add_columns(
|
||||
func.sum(aliased_reaction.id)
|
||||
.label("reacted_stat"),
|
||||
func.sum(case((aliased_reaction.kind == ReactionKind.COMMENT.value, 1), else_=0))
|
||||
.label("commented_stat"),
|
||||
q = q.outerjoin(aliased_reaction, Reaction.id == aliased_reaction.reply_to).add_columns(
|
||||
func.sum(aliased_reaction.id).label("reacted_stat"),
|
||||
func.sum(case((aliased_reaction.kind == ReactionKind.COMMENT.value, 1), else_=0)).label("commented_stat"),
|
||||
func.sum(
|
||||
case(
|
||||
(aliased_reaction.kind == ReactionKind.AGREE.value, 1),
|
||||
|
@ -33,9 +30,8 @@ def add_reaction_stat_columns(q):
|
|||
(aliased_reaction.kind == ReactionKind.DISLIKE.value, -1),
|
||||
else_=0,
|
||||
)
|
||||
)
|
||||
.label("rating_stat"),
|
||||
))
|
||||
).label("rating_stat"),
|
||||
)
|
||||
|
||||
return q, aliased_reaction
|
||||
|
||||
|
@ -187,7 +183,9 @@ async def create_reaction(_, info, reaction):
|
|||
return {"error": "You can't vote twice"}
|
||||
|
||||
opposite_reaction_kind = (
|
||||
ReactionKind.DISLIKE.value if reaction["kind"] == ReactionKind.LIKE.value else ReactionKind.LIKE.value
|
||||
ReactionKind.DISLIKE.value
|
||||
if reaction["kind"] == ReactionKind.LIKE.value
|
||||
else ReactionKind.LIKE.value
|
||||
)
|
||||
opposite_reaction = (
|
||||
session.query(Reaction)
|
||||
|
@ -405,11 +403,7 @@ async def load_reactions_by(_, info, by, limit=50, offset=0):
|
|||
] in result_rows:
|
||||
reaction.created_by = author
|
||||
reaction.shout = shout
|
||||
reaction.stat = {
|
||||
"rating": rating_stat,
|
||||
"commented": commented_stat,
|
||||
"reacted": reacted_stat
|
||||
}
|
||||
reaction.stat = {"rating": rating_stat, "commented": commented_stat, "reacted": reacted_stat}
|
||||
reactions.append(reaction)
|
||||
|
||||
# sort if by stat is present
|
||||
|
@ -429,10 +423,7 @@ def reacted_shouts_updates(follower_id: int, limit=50, offset=0) -> List[Shout]:
|
|||
.join(Reaction)
|
||||
.filter(Reaction.created_by == follower_id)
|
||||
.filter(Reaction.created_at > author.last_seen)
|
||||
.options(
|
||||
joinedload(Reaction.created_by),
|
||||
joinedload(Reaction.shout)
|
||||
)
|
||||
.options(joinedload(Reaction.created_by), joinedload(Reaction.shout))
|
||||
.limit(limit)
|
||||
.offset(offset)
|
||||
.all()
|
||||
|
|
|
@ -17,12 +17,7 @@ def add_stat_columns(q):
|
|||
aliased_reaction = aliased(Reaction)
|
||||
q = q.outerjoin(aliased_reaction).add_columns(
|
||||
func.sum(aliased_reaction.id).label("reacted_stat"),
|
||||
func.sum(
|
||||
case(
|
||||
(aliased_reaction.kind == ReactionKind.COMMENT.value, 1),
|
||||
else_=0
|
||||
)
|
||||
).label("commented_stat"),
|
||||
func.sum(case((aliased_reaction.kind == ReactionKind.COMMENT.value, 1), else_=0)).label("commented_stat"),
|
||||
func.sum(
|
||||
case(
|
||||
(aliased_reaction.kind == ReactionKind.AGREE.value, 1),
|
||||
|
@ -47,7 +42,6 @@ def add_stat_columns(q):
|
|||
return q
|
||||
|
||||
|
||||
|
||||
def apply_filters(q, filters, author_id=None):
|
||||
# LoadShoutsFilters handling
|
||||
if filters.get("reacted") and author_id:
|
||||
|
@ -175,12 +169,7 @@ async def load_shouts_by(_, info, options):
|
|||
|
||||
shouts = []
|
||||
with local_session() as session:
|
||||
for [
|
||||
shout,
|
||||
reacted_stat,
|
||||
commented_stat,
|
||||
rating_stat,
|
||||
] in session.execute(q).unique():
|
||||
for [shout, reacted_stat, commented_stat, rating_stat, _last_comment] in session.execute(q).unique():
|
||||
shout.stat = {
|
||||
"viewed": ViewedStorage.get_shout(shout.slug),
|
||||
"reacted": reacted_stat,
|
||||
|
|
|
@ -24,10 +24,8 @@ def add_topic_stat_columns(q):
|
|||
q = (
|
||||
q.outerjoin(ShoutTopic, Topic.id == ShoutTopic.topic)
|
||||
.add_columns(func.count(distinct(ShoutTopic.shout)).label("shouts_stat"))
|
||||
|
||||
.outerjoin(aliased_shout_author, ShoutTopic.shout == aliased_shout_author.shout)
|
||||
.add_columns(func.count(distinct(aliased_shout_author.author)).label("authors_stat"))
|
||||
|
||||
.outerjoin(aliased_topic_follower)
|
||||
.add_columns(func.count(distinct(aliased_topic_follower.follower)).label("followers_stat"))
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue
Block a user