core/resolvers/stat.py

166 lines
5.3 KiB
Python
Raw Normal View History

2024-04-08 07:38:58 +00:00
from sqlalchemy import and_, distinct, func, join, select
2024-02-22 23:49:34 +00:00
from sqlalchemy.orm import aliased
2024-02-21 17:12:47 +00:00
2024-04-08 07:38:58 +00:00
from orm.author import Author, AuthorFollower
2024-02-25 10:29:57 +00:00
from orm.reaction import Reaction, ReactionKind
2024-04-08 07:38:58 +00:00
from orm.shout import Shout, ShoutAuthor, ShoutTopic
from orm.topic import Topic, TopicFollower
2024-02-21 17:12:47 +00:00
from services.db import local_session
2024-04-09 13:43:06 +00:00
from services.cache import cache_author
2024-04-09 18:15:38 +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_topic = aliased(ShoutTopic)
2024-04-09 16:38:02 +00:00
aliased_authors = aliased(ShoutAuthor)
aliased_followers = aliased(TopicFollower)
2024-04-08 18:33:47 +00:00
aliased_shout = aliased(Shout)
2024-02-23 19:43:50 +00:00
2024-04-09 18:05:24 +00:00
# shouts
2024-04-09 16:38:02 +00:00
q = q.outerjoin(aliased_shout_topic, aliased_shout_topic.topic == Topic.id)
2024-04-09 18:05:24 +00:00
q = q.add_columns(func.count(distinct(aliased_shout_topic.shout)).label('shouts_stat'))
2024-04-09 16:38:02 +00:00
2024-04-09 18:05:24 +00:00
# authors
2024-04-09 18:08:47 +00:00
q = q.outerjoin(aliased_shout, and_(
aliased_shout.id == aliased_shout_topic.shout,
aliased_shout.published_at.is_not(None),
aliased_shout.deleted_at.is_(None)
))
2024-04-09 19:06:00 +00:00
q = q.outerjoin(aliased_authors, aliased_shout.authors.contains(aliased_authors.id))
2024-04-09 18:51:24 +00:00
q = q.add_columns(func.count(distinct(aliased_authors.author)).label('authors_stat'))
2024-04-09 16:38:02 +00:00
2024-04-09 18:05:24 +00:00
# followers
2024-04-09 16:38:02 +00:00
q = q.outerjoin(aliased_followers, aliased_followers.topic == Topic.id)
q = q.add_columns(
func.count(distinct(aliased_followers.follower)).label('followers_stat')
)
2024-04-09 18:05:24 +00:00
# comments
2024-04-09 16:38:02 +00:00
sub_comments = (
2024-03-06 10:43:30 +00:00
select(
2024-04-09 16:40:44 +00:00
Shout.id.label('shout_id'),
func.coalesce(func.count(Reaction.id)).label('comments_count')
2024-03-06 10:43:30 +00:00
)
2024-04-09 16:48:02 +00:00
.join(ShoutTopic, ShoutTopic.shout == Shout.id)
.join(Topic, ShoutTopic.topic == Topic.id)
2024-04-09 16:38:02 +00:00
.outerjoin(
2024-03-06 10:43:30 +00:00
Reaction,
and_(
2024-03-06 12:08:20 +00:00
Reaction.shout == Shout.id,
2024-03-06 10:43:30 +00:00
Reaction.kind == ReactionKind.COMMENT.value,
Reaction.deleted_at.is_(None),
),
)
.group_by(Shout.id)
.subquery()
2024-02-22 23:49:34 +00:00
)
2024-04-09 16:38:02 +00:00
q = q.outerjoin(sub_comments, aliased_shout_topic.shout == sub_comments.c.shout_id)
q = q.add_columns(func.coalesce(sub_comments.c.comments_count, 0).label('comments_stat'))
2024-03-06 10:43:30 +00:00
2024-04-09 16:38:02 +00:00
group_list = [Topic.id, sub_comments.c.comments_count]
q = q.group_by(*group_list)
2024-04-09 18:15:38 +00:00
logger.debug(q)
2024-02-22 23:49:34 +00:00
return q
2024-04-09 08:17:32 +00:00
def add_author_stat_columns(q):
2024-02-23 19:43:50 +00:00
aliased_shout_author = aliased(ShoutAuthor)
2024-03-06 09:25:55 +00:00
aliased_authors = aliased(AuthorFollower)
aliased_followers = aliased(AuthorFollower)
2024-02-25 21:06:37 +00:00
2024-03-01 06:56:36 +00:00
q = q.outerjoin(aliased_shout_author, aliased_shout_author.author == Author.id)
2024-03-06 09:25:55 +00:00
q = q.add_columns(
2024-03-28 12:56:32 +00:00
func.count(distinct(aliased_shout_author.shout)).label('shouts_stat')
2024-03-06 09:25:55 +00:00
)
2024-03-01 06:56:36 +00:00
2024-03-06 09:25:55 +00:00
q = q.outerjoin(aliased_authors, aliased_authors.follower == Author.id)
q = q.add_columns(
2024-03-28 12:56:32 +00:00
func.count(distinct(aliased_authors.author)).label('authors_stat')
2024-03-06 09:25:55 +00:00
)
2024-03-01 06:56:36 +00:00
2024-03-06 09:25:55 +00:00
q = q.outerjoin(aliased_followers, aliased_followers.author == Author.id)
q = q.add_columns(
2024-03-28 12:56:32 +00:00
func.count(distinct(aliased_followers.follower)).label('followers_stat')
2024-03-06 09:25:55 +00:00
)
2024-03-01 06:56:36 +00:00
2024-03-28 20:33:56 +00:00
# Create a subquery for comments count
sub_comments = (
2024-03-28 20:59:26 +00:00
select(
2024-04-08 06:17:05 +00:00
Author.id, func.coalesce(func.count(Reaction.id)).label('comments_count')
2024-03-28 20:59:26 +00:00
)
2024-03-28 20:33:56 +00:00
.outerjoin(
Reaction,
and_(
Reaction.created_by == Author.id,
Reaction.kind == ReactionKind.COMMENT.value,
Reaction.deleted_at.is_(None),
),
2024-03-28 20:19:07 +00:00
)
2024-03-28 20:33:56 +00:00
.group_by(Author.id)
.subquery()
)
2024-03-28 20:19:07 +00:00
2024-03-28 20:33:56 +00:00
q = q.outerjoin(sub_comments, Author.id == sub_comments.c.id)
q = q.add_columns(sub_comments.c.comments_count)
2024-04-09 16:38:02 +00:00
group_list = [Topic.id, sub_comments.c.comments_count]
2024-03-28 20:59:26 +00:00
2024-03-28 21:29:28 +00:00
q = q.group_by(*group_list)
2024-03-28 20:59:26 +00:00
2024-02-24 18:45:38 +00:00
return q
2024-02-24 16:12:35 +00:00
2024-04-09 08:17:32 +00:00
def get_with_stat(q):
2024-03-12 11:59:36 +00:00
try:
2024-03-28 12:56:32 +00:00
is_author = f'{q}'.lower().startswith('select author')
is_topic = f'{q}'.lower().startswith('select topic')
2024-03-12 11:59:36 +00:00
if is_author:
2024-04-09 08:17:32 +00:00
q = add_author_stat_columns(q)
2024-03-12 11:59:36 +00:00
elif is_topic:
q = add_topic_stat_columns(q)
records = []
with local_session() as session:
2024-03-14 07:21:04 +00:00
result = session.execute(q)
for cols in result:
2024-03-12 11:59:36 +00:00
entity = cols[0]
stat = dict()
2024-03-28 12:56:32 +00:00
stat['shouts'] = cols[1]
stat['authors'] = cols[2]
stat['followers'] = cols[3]
2024-03-12 11:59:36 +00:00
if is_author:
2024-03-28 12:56:32 +00:00
stat['comments'] = cols[4]
2024-03-12 12:05:45 +00:00
entity.stat = stat
2024-03-12 11:59:36 +00:00
records.append(entity)
except Exception as exc:
2024-03-14 07:21:04 +00:00
import traceback
traceback.print_exc()
2024-03-12 11:59:36 +00:00
raise Exception(exc)
2024-02-22 23:08:43 +00:00
return records
2024-02-21 17:12:47 +00:00
2024-02-23 18:10:11 +00:00
def author_follows_authors(author_id: int):
2024-03-28 12:56:32 +00:00
af = aliased(AuthorFollower, name='af')
2024-02-23 20:15:16 +00:00
q = (
2024-02-24 18:45:38 +00:00
select(Author)
2024-02-24 18:56:09 +00:00
.select_from(join(Author, af, Author.id == af.author))
2024-02-24 18:45:38 +00:00
.where(af.follower == author_id)
2024-02-23 20:15:16 +00:00
)
2024-02-25 08:27:08 +00:00
return get_with_stat(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-24 18:45:38 +00:00
select(Topic)
.select_from(join(Topic, TopicFollower, Topic.id == TopicFollower.topic))
.where(TopicFollower.follower == author_id)
2024-02-23 20:15:16 +00:00
)
2024-02-25 08:27:08 +00:00
return get_with_stat(q)
2024-04-09 13:43:06 +00:00
async def update_author_stat(author: Author):
2024-04-09 19:06:00 +00:00
author_with_stat = get_with_stat(select(Author).where(Author.id==author.id))
2024-04-09 13:43:06 +00:00
if isinstance(author_with_stat, Author):
author_dict = author_with_stat.dict()
await cache_author(author_dict)