This commit is contained in:
parent
8f532b0023
commit
b8f08c3411
|
@ -1,4 +1,4 @@
|
||||||
from sqlalchemy import and_
|
from sqlalchemy import and_, func, case, true, select, distinct
|
||||||
from sqlalchemy.orm import aliased
|
from sqlalchemy.orm import aliased
|
||||||
|
|
||||||
from orm.author import AuthorRating, Author
|
from orm.author import AuthorRating, Author
|
||||||
|
@ -131,3 +131,50 @@ def load_author_ratings(session, author: Author):
|
||||||
author.stat['rating_comments'] = count_author_comments_rating(session, author.id)
|
author.stat['rating_comments'] = count_author_comments_rating(session, author.id)
|
||||||
author.stat['commented'] = comments_count
|
author.stat['commented'] = comments_count
|
||||||
return author
|
return author
|
||||||
|
|
||||||
|
|
||||||
|
def add_rating_columns(q, group_list):
|
||||||
|
# old karma
|
||||||
|
q = q.outerjoin(AuthorRating, AuthorRating.author == Author.id)
|
||||||
|
q = q.add_columns(
|
||||||
|
func.sum(case((AuthorRating.plus == true(), 1), else_=0)).label('likes_count'),
|
||||||
|
func.sum(case((AuthorRating.plus != true(), 1), else_=0)).label('dislikes_count'),
|
||||||
|
)
|
||||||
|
|
||||||
|
# by shouts
|
||||||
|
subq = select(Reaction).where(
|
||||||
|
and_(
|
||||||
|
Reaction.shout == Shout.id,
|
||||||
|
Shout.authors.any(id=Author.id),
|
||||||
|
Reaction.reply_to.is_(None),
|
||||||
|
Reaction.deleted_at.is_(None)
|
||||||
|
)
|
||||||
|
).subquery()
|
||||||
|
|
||||||
|
q = q.outerjoin(subq, subq.c.shout == Shout.id)
|
||||||
|
q = q.add_columns(
|
||||||
|
func.count(distinct(case((subq.c.kind == ReactionKind.LIKE.value, 1)))).label('shouts_likes'),
|
||||||
|
func.count(distinct(case((subq.c.kind == ReactionKind.DISLIKE.value, 1)))).label('shouts_dislikes'),
|
||||||
|
)
|
||||||
|
group_list.extend([subq.c.shouts_likes, subq.c.shouts_dislikes])
|
||||||
|
|
||||||
|
# by comments
|
||||||
|
replied_comment = aliased(Reaction)
|
||||||
|
subq = select(Reaction).where(
|
||||||
|
and_(
|
||||||
|
replied_comment.kind == ReactionKind.COMMENT.value,
|
||||||
|
replied_comment.created_by == Author.id,
|
||||||
|
Reaction.reply_to == replied_comment.id,
|
||||||
|
Reaction.kind.in_([ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]),
|
||||||
|
Reaction.deleted_at.is_(None)
|
||||||
|
)
|
||||||
|
).subquery()
|
||||||
|
|
||||||
|
q = q.outerjoin(subq, subq.c.reply_to == replied_comment.id)
|
||||||
|
q = q.add_columns(
|
||||||
|
func.count(distinct(case((subq.c.kind == ReactionKind.LIKE.value, 1)))).label('comments_likes'),
|
||||||
|
func.count(distinct(case((subq.c.kind == ReactionKind.DISLIKE.value, 1)))).label('comments_dislikes'),
|
||||||
|
)
|
||||||
|
group_list.extend([subq.c.comments_likes, subq.c.comments_dislikes])
|
||||||
|
|
||||||
|
return q, group_list
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from sqlalchemy import func, distinct, select, join, and_, case, true
|
from sqlalchemy import func, distinct, select, join, and_
|
||||||
from sqlalchemy.orm import aliased
|
from sqlalchemy.orm import aliased
|
||||||
|
|
||||||
from orm.reaction import Reaction, ReactionKind
|
from orm.reaction import Reaction, ReactionKind
|
||||||
|
from resolvers.rating import add_rating_columns
|
||||||
from orm.topic import TopicFollower, Topic
|
from orm.topic import TopicFollower, Topic
|
||||||
from services.db import local_session
|
from services.db import local_session
|
||||||
from orm.author import AuthorFollower, Author, AuthorRating
|
from orm.author import AuthorFollower, Author
|
||||||
from orm.shout import ShoutTopic, ShoutAuthor, Shout
|
from orm.shout import ShoutTopic, ShoutAuthor, Shout
|
||||||
from services.logger import root_logger as logger
|
from services.logger import root_logger as logger
|
||||||
from services.rediscache import redis
|
from services.rediscache import redis
|
||||||
|
@ -103,33 +104,12 @@ def add_author_stat_columns(q, with_rating=False):
|
||||||
|
|
||||||
q = q.outerjoin(sub_comments, Author.id == sub_comments.c.id)
|
q = q.outerjoin(sub_comments, Author.id == sub_comments.c.id)
|
||||||
q = q.add_columns(sub_comments.c.comments_count)
|
q = q.add_columns(sub_comments.c.comments_count)
|
||||||
|
group_list = [Author.id, sub_comments.c.comments_count]
|
||||||
q = q.outerjoin(AuthorRating, AuthorRating.author == Author.id)
|
|
||||||
q = q.add_columns(
|
|
||||||
func.sum(case((AuthorRating.plus == true(), 1), else_=0)).label('likes_count'),
|
|
||||||
func.sum(case((AuthorRating.plus != true(), 1), else_=0)).label('dislikes_count'),
|
|
||||||
)
|
|
||||||
|
|
||||||
if with_rating:
|
if with_rating:
|
||||||
subq = select(Reaction).where(
|
q, group_list = add_rating_columns(q, group_list)
|
||||||
and_(
|
|
||||||
Reaction.shout == Shout.id,
|
|
||||||
Shout.authors.any(id=Author.id),
|
|
||||||
Reaction.reply_to.is_(None),
|
|
||||||
Reaction.deleted_at.is_(None)
|
|
||||||
)
|
|
||||||
).subquery()
|
|
||||||
|
|
||||||
q = q.outerjoin(subq, subq.c.shout == Shout.id)
|
q = q.group_by(*group_list)
|
||||||
q = q.add_columns(
|
|
||||||
func.count(case((subq.c.kind == ReactionKind.LIKE.value, subq.c.id), else_=None)).label('shouts_likes'),
|
|
||||||
func.count(case((subq.c.kind == ReactionKind.DISLIKE.value, subq.c.id), else_=None)).label('shouts_dislikes'),
|
|
||||||
)
|
|
||||||
|
|
||||||
q = q.group_by(
|
|
||||||
Author.id,
|
|
||||||
sub_comments.c.comments_count
|
|
||||||
)
|
|
||||||
|
|
||||||
return q
|
return q
|
||||||
|
|
||||||
|
@ -154,10 +134,10 @@ def get_with_stat(q, with_rating=False):
|
||||||
if is_author:
|
if is_author:
|
||||||
stat['comments'] = cols[4]
|
stat['comments'] = cols[4]
|
||||||
if with_rating:
|
if with_rating:
|
||||||
# logger.debug('author, shouts, authors, followers, comments, author_likes, author_dislikes, shouts_likes, shouts_dislikes, comment_likes, comments_dislikes')
|
|
||||||
logger.debug(cols)
|
logger.debug(cols)
|
||||||
stat['rating'] = cols[6] - cols[7]
|
stat['rating'] = cols[6] - cols[7]
|
||||||
# stat['rating_shouts'] = cols[8] - cols[9]
|
stat['rating_shouts'] = cols[8] - cols[9]
|
||||||
|
stat['rating_comments'] = cols[10] - cols[11]
|
||||||
entity.stat = stat
|
entity.stat = stat
|
||||||
records.append(entity)
|
records.append(entity)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user