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