core/resolvers/stat.py

238 lines
7.6 KiB
Python
Raw Normal View History

2024-03-12 12:26:36 +00:00
import json
2024-03-28 20:39:12 +00:00
from sqlalchemy import func, distinct, select, join, and_, case, true
2024-02-22 23:49:34 +00:00
from sqlalchemy.orm import aliased
2024-02-21 17:12:47 +00:00
2024-02-25 10:29:57 +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-25 10:29:57 +00:00
from orm.author import AuthorFollower, Author, AuthorRating
from orm.shout import ShoutTopic, ShoutAuthor, Shout
2024-03-12 11:59:36 +00:00
from services.logger import root_logger as logger
2024-03-12 12:26:36 +00:00
from services.rediscache import redis
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-24 18:45:38 +00:00
.add_columns(
2024-03-28 12:56:32 +00:00
func.count(distinct(aliased_shout_topic.shout)).label('shouts_stat')
2024-02-24 18:45:38 +00:00
)
.outerjoin(
aliased_shout_author,
aliased_shout_topic.shout == aliased_shout_author.shout,
)
.add_columns(
2024-03-28 12:56:32 +00:00
func.count(distinct(aliased_shout_author.author)).label('authors_stat')
2024-02-24 18:45:38 +00:00
)
2024-02-23 19:43:50 +00:00
.outerjoin(aliased_topic_follower)
2024-02-24 18:45:38 +00:00
.add_columns(
func.count(distinct(aliased_topic_follower.follower)).label(
2024-03-28 12:56:32 +00:00
'followers_stat'
2024-02-24 18:45:38 +00:00
)
)
2024-03-06 10:43:30 +00:00
)
# Create a subquery for comments count
2024-03-11 12:13:46 +00:00
_sub_comments = (
2024-03-06 10:43:30 +00:00
select(
2024-03-28 12:56:32 +00:00
Shout.id, func.coalesce(func.count(Reaction.id), 0).label('comments_count')
2024-03-06 10:43:30 +00:00
)
.join(
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-03-11 12:13:46 +00:00
# q = q.outerjoin(sub_comments, aliased_shout_topic.shout == sub_comments.c.id)
# q = q.add_columns(
# func.coalesce(func.sum(sub_comments.c.comments_count), 0).label('comments_stat')
# )
2024-03-06 10:43:30 +00:00
2024-03-06 12:17:46 +00:00
q = q.group_by(Topic.id)
2024-02-22 23:49:34 +00:00
return q
2024-03-28 19:26:46 +00:00
def add_author_stat_columns(q, with_rating=False):
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
select_list = [
Author.id,
func.coalesce(func.count(Reaction.id)).label('comments_count'),
]
2024-03-28 20:19:07 +00:00
2024-03-28 20:33:56 +00:00
sub_comments = (
select(*select_list)
.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-03-28 19:41:48 +00:00
2024-03-28 19:26:46 +00:00
if with_rating:
2024-03-28 20:19:07 +00:00
# Create a subquery for ratings counters
select_list = [
2024-03-28 20:26:45 +00:00
Author.id,
2024-03-28 19:26:46 +00:00
func.sum(case((AuthorRating.plus == true(), 1), else_=0)).label('likes_count'),
func.sum(case((AuthorRating.plus != true(), 1), else_=0)).label('dislikes_count'),
2024-03-28 19:51:09 +00:00
func.sum(case((and_(Reaction.kind == ReactionKind.LIKE.value, Shout.authors.any(id=Author.id)), 1), else_=0)).label('shouts_likes'),
func.sum(case((and_(Reaction.kind == ReactionKind.DISLIKE.value, Shout.authors.any(id=Author.id)), 1), else_=0)).label('shouts_dislikes'),
2024-03-28 20:19:07 +00:00
]
sub_rating = (
select(*select_list)
.outerjoin(
Reaction,
and_(
Reaction.created_by == Author.id,
2024-03-28 20:39:12 +00:00
Reaction.reply_to.is_(None),
2024-03-28 20:19:07 +00:00
Reaction.deleted_at.is_(None),
),
)
.group_by(Author.id)
.subquery()
2024-02-24 16:23:53 +00:00
)
2024-03-28 20:19:07 +00:00
q = q.outerjoin(sub_rating, Author.id == sub_rating.c.id)
2024-03-28 19:26:46 +00:00
q = q.add_columns(
2024-03-28 20:19:07 +00:00
sub_rating.c.likes_count,
sub_rating.c.dislikes_count,
sub_rating.c.shouts_likes,
sub_rating.c.shouts_dislikes,
2024-03-28 19:26:46 +00:00
)
2024-03-28 19:29:51 +00:00
q = q.group_by(
Author.id,
2024-03-28 19:51:09 +00:00
sub_comments.c.comments_count,
2024-03-28 20:19:07 +00:00
sub_rating.c.likes_count,
sub_rating.c.dislikes_count,
sub_rating.c.shouts_likes,
sub_rating.c.shouts_dislikes,
2024-03-28 19:29:51 +00:00
)
2024-03-28 20:33:56 +00:00
else:
q = q.group_by(
Author.id,
sub_comments.c.comments_count
)
2024-03-01 06:59:19 +00:00
2024-02-24 18:45:38 +00:00
return q
2024-02-24 16:12:35 +00:00
2024-03-28 17:45:03 +00:00
def get_with_stat(q, with_rating=False):
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-03-28 19:26:46 +00:00
q = add_author_stat_columns(q, with_rating)
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-28 17:45:03 +00:00
if with_rating:
2024-03-28 20:19:07 +00:00
logger.debug('author, shouts, authors, followers, comments, rating, rating_shouts')
2024-03-28 19:10:01 +00:00
logger.debug(cols)
2024-03-28 20:19:07 +00:00
stat['rating'] = cols[6] - cols[6]
2024-03-28 19:31:33 +00:00
stat['rating_shouts'] = cols[7] - cols[8]
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-03-12 12:26:36 +00:00
async def get_authors_with_stat_cached(q):
2024-03-28 16:14:39 +00:00
# logger.debug(q)
2024-03-12 12:26:36 +00:00
try:
records = []
with local_session() as session:
2024-03-12 13:23:01 +00:00
for [x] in session.execute(q):
2024-03-28 16:45:21 +00:00
stat_str = await redis.execute('GET', f'author:{x.id}')
2024-03-28 16:14:39 +00:00
x.stat = json.loads(stat_str).get('stat') if isinstance(stat_str, str) else {}
2024-03-12 12:50:57 +00:00
records.append(x)
except Exception as exc:
raise Exception(exc)
return records
async def get_topics_with_stat_cached(q):
try:
records = []
2024-03-12 13:21:28 +00:00
current = None
2024-03-12 12:50:57 +00:00
with local_session() as session:
2024-03-12 13:23:01 +00:00
for [x] in session.execute(q):
2024-03-12 13:21:28 +00:00
current = x
2024-03-12 12:50:57 +00:00
stat_str = await redis.execute('GET', f'topic:{x.id}')
2024-03-12 12:26:36 +00:00
if isinstance(stat_str, str):
x.stat = json.loads(stat_str).get('stat')
records.append(x)
except Exception as exc:
2024-03-12 13:21:28 +00:00
logger.error(current)
2024-03-12 12:26:36 +00:00
raise Exception(exc)
return records
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)