This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
from sqlalchemy import func, distinct, select, join, and_
|
||||
from sqlalchemy import func, distinct, select, join
|
||||
from sqlalchemy.orm import aliased
|
||||
|
||||
from orm.reaction import Reaction, ReactionKind
|
||||
from orm.topic import TopicFollower, Topic
|
||||
from resolvers.rating import load_author_ratings
|
||||
from services.db import local_session
|
||||
from orm.author import AuthorFollower, Author, AuthorRating
|
||||
from orm.shout import ShoutTopic, ShoutAuthor, Shout
|
||||
from orm.author import AuthorFollower, Author
|
||||
from orm.shout import ShoutTopic, ShoutAuthor
|
||||
from services.logger import root_logger as logger
|
||||
|
||||
|
||||
@@ -16,11 +16,22 @@ def add_topic_stat_columns(q):
|
||||
|
||||
q = (
|
||||
q.outerjoin(aliased_shout_topic, aliased_shout_topic.topic == Topic.id)
|
||||
.add_columns(func.count(distinct(aliased_shout_topic.shout)).label("shouts_stat"))
|
||||
.outerjoin(aliased_shout_author, aliased_shout_topic.shout == aliased_shout_author.shout)
|
||||
.add_columns(func.count(distinct(aliased_shout_author.author)).label("authors_stat"))
|
||||
.add_columns(
|
||||
func.count(distinct(aliased_shout_topic.shout)).label('shouts_stat')
|
||||
)
|
||||
.outerjoin(
|
||||
aliased_shout_author,
|
||||
aliased_shout_topic.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"))
|
||||
.add_columns(
|
||||
func.count(distinct(aliased_topic_follower.follower)).label(
|
||||
'followers_stat'
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
q = q.group_by(Topic.id)
|
||||
@@ -35,11 +46,21 @@ def add_author_stat_columns(q):
|
||||
|
||||
q = (
|
||||
q.outerjoin(aliased_shout_author, aliased_shout_author.author == Author.id)
|
||||
.add_columns(func.count(distinct(aliased_shout_author.shout)).label("shouts_stat"))
|
||||
.add_columns(
|
||||
func.count(distinct(aliased_shout_author.shout)).label('shouts_stat')
|
||||
)
|
||||
.outerjoin(aliased_author_authors, aliased_author_authors.follower == Author.id)
|
||||
.add_columns(func.count(distinct(aliased_shout_author.author)).label("authors_stat"))
|
||||
.outerjoin(aliased_author_followers, aliased_author_followers.author == Author.id)
|
||||
.add_columns(func.count(distinct(aliased_author_followers.follower)).label("followers_stat"))
|
||||
.add_columns(
|
||||
func.count(distinct(aliased_shout_author.author)).label('authors_stat')
|
||||
)
|
||||
.outerjoin(
|
||||
aliased_author_followers, aliased_author_followers.author == Author.id
|
||||
)
|
||||
.add_columns(
|
||||
func.count(distinct(aliased_author_followers.follower)).label(
|
||||
'followers_stat'
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
q = q.group_by(Author.id)
|
||||
@@ -47,104 +68,6 @@ def add_author_stat_columns(q):
|
||||
return q
|
||||
|
||||
|
||||
def count_author_comments_rating(session, author_id) -> int:
|
||||
replied_alias = aliased(Reaction)
|
||||
replies_likes = (
|
||||
session.query(replied_alias)
|
||||
.join(Reaction, replied_alias.id == Reaction.reply_to)
|
||||
.where(
|
||||
and_(
|
||||
replied_alias.created_by == author_id,
|
||||
replied_alias.kind == ReactionKind.COMMENT.value,
|
||||
)
|
||||
)
|
||||
.filter(replied_alias.kind == ReactionKind.LIKE.value)
|
||||
.count()
|
||||
) or 0
|
||||
replies_dislikes = (
|
||||
session.query(replied_alias)
|
||||
.join(Reaction, replied_alias.id == Reaction.reply_to)
|
||||
.where(
|
||||
and_(
|
||||
replied_alias.created_by == author_id,
|
||||
replied_alias.kind == ReactionKind.COMMENT.value,
|
||||
)
|
||||
)
|
||||
.filter(replied_alias.kind == ReactionKind.DISLIKE.value)
|
||||
.count()
|
||||
) or 0
|
||||
|
||||
return replies_likes - replies_dislikes
|
||||
|
||||
|
||||
def count_author_shouts_rating(session, author_id) -> int:
|
||||
shouts_likes = (
|
||||
session.query(Reaction, Shout)
|
||||
.join(Shout, Shout.id == Reaction.shout)
|
||||
.filter(
|
||||
and_(
|
||||
Shout.authors.any(id=author_id),
|
||||
Reaction.kind == ReactionKind.LIKE.value,
|
||||
)
|
||||
)
|
||||
.count()
|
||||
or 0
|
||||
)
|
||||
shouts_dislikes = (
|
||||
session.query(Reaction, Shout)
|
||||
.join(Shout, Shout.id == Reaction.shout)
|
||||
.filter(
|
||||
and_(
|
||||
Shout.authors.any(id=author_id),
|
||||
Reaction.kind == ReactionKind.DISLIKE.value,
|
||||
)
|
||||
)
|
||||
.count()
|
||||
or 0
|
||||
)
|
||||
return shouts_likes - shouts_dislikes
|
||||
|
||||
|
||||
def load_author_ratings(author: Author):
|
||||
with local_session() as session:
|
||||
comments_count = (
|
||||
session.query(Reaction)
|
||||
.filter(
|
||||
and_(
|
||||
Reaction.created_by == author.id,
|
||||
Reaction.kind == ReactionKind.COMMENT.value,
|
||||
Reaction.deleted_at.is_(None),
|
||||
)
|
||||
)
|
||||
.count()
|
||||
)
|
||||
likes_count = (
|
||||
session.query(AuthorRating)
|
||||
.filter(
|
||||
and_(AuthorRating.author == author.id, AuthorRating.plus.is_(True))
|
||||
)
|
||||
.count()
|
||||
)
|
||||
dislikes_count = (
|
||||
session.query(AuthorRating)
|
||||
.filter(
|
||||
and_(
|
||||
AuthorRating.author == author.id, AuthorRating.plus.is_not(True)
|
||||
)
|
||||
)
|
||||
.count()
|
||||
)
|
||||
author.stat['rating'] = likes_count - dislikes_count
|
||||
author.stat['rating_shouts'] = count_author_shouts_rating(
|
||||
session, author.id
|
||||
)
|
||||
author.stat['rating_comments'] = count_author_comments_rating(
|
||||
session, author.id
|
||||
)
|
||||
author.stat['commented'] = comments_count
|
||||
return author
|
||||
|
||||
|
||||
def execute_with_ministat(q):
|
||||
records = []
|
||||
with local_session() as session:
|
||||
@@ -176,11 +99,11 @@ def get_topics_with_stat(q):
|
||||
|
||||
|
||||
def author_follows_authors(author_id: int):
|
||||
af = aliased(AuthorFollower, name="af")
|
||||
af = aliased(AuthorFollower, name='af')
|
||||
q = (
|
||||
select(Author).select_from(
|
||||
join(Author, af, Author.id == int(af.author))
|
||||
).where(af.follower == author_id)
|
||||
select(Author)
|
||||
.select_from(join(Author, af, Author.id == int(af.author)))
|
||||
.where(af.follower == author_id)
|
||||
)
|
||||
q = add_author_stat_columns(q)
|
||||
return execute_with_ministat(q)
|
||||
@@ -188,9 +111,9 @@ def author_follows_authors(author_id: int):
|
||||
|
||||
def author_follows_topics(author_id: int):
|
||||
q = (
|
||||
select(Topic).select_from(
|
||||
join(Topic, TopicFollower, Topic.id == TopicFollower.topic)
|
||||
).where(TopicFollower.follower == author_id)
|
||||
select(Topic)
|
||||
.select_from(join(Topic, TopicFollower, Topic.id == TopicFollower.topic))
|
||||
.where(TopicFollower.follower == author_id)
|
||||
)
|
||||
|
||||
q = add_topic_stat_columns(q)
|
||||
@@ -207,5 +130,5 @@ def query_follows(author_id: int):
|
||||
'communities': [{'id': 1, 'name': 'Дискурс', 'slug': 'discours'}],
|
||||
}
|
||||
except Exception as e:
|
||||
logger.exception(f"An error occurred while executing query_follows: {e}")
|
||||
raise Exception("An error occurred while executing query_follows") from e
|
||||
logger.exception(f'An error occurred while executing query_follows: {e}')
|
||||
raise Exception('An error occurred while executing query_follows') from e
|
||||
|
Reference in New Issue
Block a user