2024-02-24 16:12:35 +00:00
|
|
|
from sqlalchemy import func, distinct, select, join, and_
|
2024-02-22 23:49:34 +00:00
|
|
|
from sqlalchemy.orm import aliased
|
2024-02-21 17:12:47 +00:00
|
|
|
|
2024-02-24 16:12:35 +00:00
|
|
|
from orm.reaction import Reaction, ReactionKind
|
2024-02-22 23:49:34 +00:00
|
|
|
from orm.topic import TopicFollower, Topic
|
2024-02-21 17:12:47 +00:00
|
|
|
from services.db import local_session
|
2024-02-24 16:12:35 +00:00
|
|
|
from orm.author import AuthorFollower, Author, AuthorRating
|
2024-02-24 16:23:53 +00:00
|
|
|
from orm.shout import ShoutTopic, ShoutAuthor, Shout
|
2024-02-23 18:10:11 +00:00
|
|
|
from services.logger import root_logger as logger
|
2024-02-21 17:12:47 +00:00
|
|
|
|
|
|
|
|
2024-02-22 23:49:34 +00:00
|
|
|
def add_topic_stat_columns(q):
|
2024-02-23 19:43:50 +00:00
|
|
|
aliased_shout_author = aliased(ShoutAuthor)
|
|
|
|
aliased_topic_follower = aliased(TopicFollower)
|
|
|
|
aliased_shout_topic = aliased(ShoutTopic)
|
|
|
|
|
2024-02-22 23:49:34 +00:00
|
|
|
q = (
|
2024-02-23 20:15:16 +00:00
|
|
|
q.outerjoin(aliased_shout_topic, aliased_shout_topic.topic == Topic.id)
|
2024-02-23 19:43:50 +00:00
|
|
|
.add_columns(func.count(distinct(aliased_shout_topic.shout)).label("shouts_stat"))
|
|
|
|
.outerjoin(aliased_shout_author, aliased_shout_topic.shout == aliased_shout_author.shout)
|
2024-02-23 20:15:16 +00:00
|
|
|
.add_columns(func.count(distinct(aliased_shout_author.author)).label("authors_stat"))
|
2024-02-23 19:43:50 +00:00
|
|
|
.outerjoin(aliased_topic_follower)
|
|
|
|
.add_columns(func.count(distinct(aliased_topic_follower.follower)).label("followers_stat"))
|
2024-02-22 23:49:34 +00:00
|
|
|
)
|
|
|
|
|
2024-02-23 19:43:50 +00:00
|
|
|
q = q.group_by(Topic.id)
|
2024-02-22 23:49:34 +00:00
|
|
|
|
|
|
|
return q
|
|
|
|
|
|
|
|
|
|
|
|
def add_author_stat_columns(q):
|
2024-02-23 19:43:50 +00:00
|
|
|
aliased_shout_author = aliased(ShoutAuthor)
|
|
|
|
aliased_author_authors = aliased(AuthorFollower)
|
|
|
|
aliased_author_followers = aliased(AuthorFollower)
|
|
|
|
|
2024-02-22 23:49:34 +00:00
|
|
|
q = (
|
2024-02-23 19:43:50 +00:00
|
|
|
q.outerjoin(aliased_shout_author, aliased_shout_author.author == Author.id)
|
|
|
|
.add_columns(func.count(distinct(aliased_shout_author.shout)).label("shouts_stat"))
|
|
|
|
.outerjoin(aliased_author_authors, aliased_author_authors.follower == Author.id)
|
2024-02-23 20:26:12 +00:00
|
|
|
.add_columns(func.count(distinct(aliased_shout_author.author)).label("authors_stat"))
|
2024-02-23 19:43:50 +00:00
|
|
|
.outerjoin(aliased_author_followers, aliased_author_followers.author == Author.id)
|
|
|
|
.add_columns(func.count(distinct(aliased_author_followers.follower)).label("followers_stat"))
|
2024-02-21 17:12:47 +00:00
|
|
|
)
|
2024-02-22 23:49:34 +00:00
|
|
|
|
2024-02-23 19:43:50 +00:00
|
|
|
q = q.group_by(Author.id)
|
2024-02-22 23:49:34 +00:00
|
|
|
|
2024-02-21 17:12:47 +00:00
|
|
|
return q
|
|
|
|
|
|
|
|
|
2024-02-24 16:23:53 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2024-02-24 16:12:35 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2024-02-23 01:08:29 +00:00
|
|
|
def execute_with_ministat(q):
|
2024-02-22 23:08:43 +00:00
|
|
|
records = []
|
2024-02-21 17:12:47 +00:00
|
|
|
with local_session() as session:
|
2024-02-23 11:40:38 +00:00
|
|
|
for [entity, shouts_stat, authors_stat, followers_stat] in session.execute(q):
|
|
|
|
entity.stat = {
|
2024-02-21 17:12:47 +00:00
|
|
|
'shouts': shouts_stat,
|
2024-02-22 23:08:43 +00:00
|
|
|
'authors': authors_stat,
|
2024-02-23 16:35:40 +00:00
|
|
|
'followers': followers_stat,
|
2024-02-21 17:12:47 +00:00
|
|
|
}
|
2024-02-23 11:40:38 +00:00
|
|
|
records.append(entity)
|
2024-02-21 17:12:47 +00:00
|
|
|
|
2024-02-22 23:08:43 +00:00
|
|
|
return records
|
2024-02-21 17:12:47 +00:00
|
|
|
|
|
|
|
|
2024-02-24 16:12:35 +00:00
|
|
|
def get_authors_with_stat(q, ratings=False):
|
2024-02-22 23:49:34 +00:00
|
|
|
q = add_author_stat_columns(q)
|
2024-02-24 16:12:35 +00:00
|
|
|
authors = execute_with_ministat(q)
|
|
|
|
if ratings:
|
|
|
|
authors_with_ratings = []
|
|
|
|
for author in authors:
|
|
|
|
authors_with_ratings.append(load_author_ratings(author))
|
|
|
|
return authors_with_ratings
|
|
|
|
return authors
|
2024-02-22 23:49:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_topics_with_stat(q):
|
|
|
|
q = add_topic_stat_columns(q)
|
2024-02-23 01:08:29 +00:00
|
|
|
return execute_with_ministat(q)
|
2024-02-23 16:35:40 +00:00
|
|
|
|
|
|
|
|
2024-02-23 18:10:11 +00:00
|
|
|
def author_follows_authors(author_id: int):
|
2024-02-23 20:42:49 +00:00
|
|
|
af = aliased(AuthorFollower, name="af")
|
2024-02-23 20:15:16 +00:00
|
|
|
q = (
|
2024-02-23 20:42:49 +00:00
|
|
|
select(Author).select_from(
|
2024-02-24 10:22:35 +00:00
|
|
|
join(Author, af, Author.id == int(af.author))
|
2024-02-23 20:42:49 +00:00
|
|
|
).where(af.follower == author_id)
|
2024-02-23 20:15:16 +00:00
|
|
|
)
|
|
|
|
q = add_author_stat_columns(q)
|
|
|
|
return execute_with_ministat(q)
|
2024-02-23 18:10:11 +00:00
|
|
|
|
2024-02-23 19:14:08 +00:00
|
|
|
|
2024-02-23 18:10:11 +00:00
|
|
|
def author_follows_topics(author_id: int):
|
2024-02-23 20:15:16 +00:00
|
|
|
q = (
|
2024-02-23 20:42:49 +00:00
|
|
|
select(Topic).select_from(
|
2024-02-24 10:22:35 +00:00
|
|
|
join(Topic, TopicFollower, Topic.id == int(TopicFollower.topic))
|
2024-02-23 20:42:49 +00:00
|
|
|
).where(TopicFollower.follower == author_id)
|
2024-02-23 20:15:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
q = add_topic_stat_columns(q)
|
|
|
|
return execute_with_ministat(q)
|
2024-02-23 18:10:11 +00:00
|
|
|
|
2024-02-23 16:35:40 +00:00
|
|
|
|
2024-02-23 18:10:11 +00:00
|
|
|
def query_follows(author_id: int):
|
|
|
|
try:
|
|
|
|
topics = author_follows_topics(author_id)
|
|
|
|
authors = author_follows_authors(author_id)
|
|
|
|
return {
|
|
|
|
'topics': topics,
|
|
|
|
'authors': authors,
|
|
|
|
'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
|