author-stats-upgrade
Some checks failed
Deploy on push / deploy (push) Failing after 3m47s

This commit is contained in:
2025-08-31 22:12:18 +03:00
parent 4660f9b000
commit 832f6529e7
2 changed files with 52 additions and 52 deletions

View File

@@ -42,7 +42,7 @@ class AuthorsBy(TypedDict, total=False):
🔢 Базовые метрики: "shouts" (публикации), "followers" (подписчики) 🔢 Базовые метрики: "shouts" (публикации), "followers" (подписчики)
🏷️ Контент: "topics" (темы), "comments" (комментарии) 🏷️ Контент: "topics" (темы), "comments" (комментарии)
👥 Социальные: "coauthors" (соавторы), "replies_count" (ответы на контент) 👥 Социальные: "coauthors" (соавторы), "replies_count" (ответы на контент)
⭐ Рейтинг: "rating" (общий), "rating_shouts" (публикации), "rating_comments" (комментарии) ⭐ Рейтинг: "rating_shouts" (публикации), "rating_comments" (комментарии)
👁️ Вовлечённость: "viewed_shouts" (просмотры) 👁️ Вовлечённость: "viewed_shouts" (просмотры)
📝 Алфавит: "name" (по имени) 📝 Алфавит: "name" (по имени)
@@ -112,7 +112,7 @@ async def get_authors_with_stats(
- topics: Уникальные темы участия - topics: Уникальные темы участия
- coauthors: Количество соавторов - coauthors: Количество соавторов
- followers: Подписчики - followers: Подписчики
- rating: Общий рейтинг (rating_shouts + rating_comments) - authors: Количество авторов, на которых подписан
- rating_shouts: Рейтинг публикаций (реакции) - rating_shouts: Рейтинг публикаций (реакции)
- rating_comments: Рейтинг комментариев (реакции) - rating_comments: Рейтинг комментариев (реакции)
- comments: Созданные комментарии - comments: Созданные комментарии
@@ -161,7 +161,6 @@ async def get_authors_with_stats(
if order_value in [ if order_value in [
"shouts", "shouts",
"followers", "followers",
"rating",
"comments", "comments",
"topics", "topics",
"coauthors", "coauthors",
@@ -476,46 +475,16 @@ async def get_authors_with_stats(
comments_stats = {row[0]: row[1] for row in session.execute(text(comments_stats_query), params)} comments_stats = {row[0]: row[1] for row in session.execute(text(comments_stats_query), params)}
logger.debug(f"Comments stats retrieved: {comments_stats}") logger.debug(f"Comments stats retrieved: {comments_stats}")
# 💬 Статистика по вызванным комментариям (ответы на комментарии + комментарии на посты) # 👥 Статистика по количеству уникальных авторов, на которых подписан данный автор
logger.debug("Executing replies_count statistics query") logger.debug("Executing authors statistics query")
authors_stats_query = f"""
# Ответы на комментарии автора SELECT follower, COUNT(DISTINCT following) as authors_count
replies_to_comments_query = f""" FROM author_follower
SELECT r1.created_by as author_id, COUNT(DISTINCT r2.id) as replies_count WHERE follower IN ({placeholders})
FROM reaction r1 GROUP BY follower
JOIN reaction r2 ON r1.id = r2.reply_to AND r2.deleted_at IS NULL
WHERE r1.created_by IN ({placeholders}) AND r1.deleted_at IS NULL
AND r1.kind IN ('COMMENT', 'QUOTE')
AND r2.kind IN ('COMMENT', 'QUOTE')
GROUP BY r1.created_by
""" """
replies_to_comments_stats = { authors_stats = {row[0]: row[1] for row in session.execute(text(authors_stats_query), params)}
row[0]: row[1] for row in session.execute(text(replies_to_comments_query), params) logger.debug(f"Authors stats retrieved: {authors_stats}")
}
logger.debug(f"Replies to comments stats retrieved: {replies_to_comments_stats}")
# Комментарии на посты автора
comments_on_posts_query = f"""
SELECT sa.author as author_id, COUNT(DISTINCT r.id) as replies_count
FROM shout_author sa
JOIN shout s ON sa.shout = s.id AND s.deleted_at IS NULL AND s.published_at IS NOT NULL
JOIN reaction r ON s.id = r.shout AND r.deleted_at IS NULL
WHERE sa.author IN ({placeholders})
AND r.kind IN ('COMMENT', 'QUOTE')
GROUP BY sa.author
"""
comments_on_posts_stats = {
row[0]: row[1] for row in session.execute(text(comments_on_posts_query), params)
}
logger.debug(f"Comments on posts stats retrieved: {comments_on_posts_stats}")
# Объединяем статистику
replies_count_stats = {}
for author_id in author_ids:
replies_to_comments = replies_to_comments_stats.get(author_id, 0)
comments_on_posts = comments_on_posts_stats.get(author_id, 0)
replies_count_stats[author_id] = replies_to_comments + comments_on_posts
logger.debug(f"Combined replies count stats: {replies_count_stats}")
# ⭐ Статистика по рейтингу публикаций (сумма реакций на публикации автора) # ⭐ Статистика по рейтингу публикаций (сумма реакций на публикации автора)
logger.debug("Executing rating_shouts statistics query") logger.debug("Executing rating_shouts statistics query")
@@ -557,14 +526,46 @@ async def get_authors_with_stats(
} }
logger.debug(f"Rating comments stats retrieved: {rating_comments_stats}") logger.debug(f"Rating comments stats retrieved: {rating_comments_stats}")
# 📈 Общий рейтинг (сумма рейтингов публикаций и комментариев) # 💬 Статистика по вызванным комментариям (ответы на комментарии + комментарии на посты)
logger.debug("Calculating overall rating") logger.debug("Executing replies_count statistics query")
overall_rating_stats = {}
# Ответы на комментарии автора
replies_to_comments_query = f"""
SELECT r1.created_by as author_id, COUNT(DISTINCT r2.id) as replies_count
FROM reaction r1
JOIN reaction r2 ON r1.id = r2.reply_to AND r2.deleted_at IS NULL
WHERE r1.created_by IN ({placeholders}) AND r1.deleted_at IS NULL
AND r1.kind IN ('COMMENT', 'QUOTE')
AND r2.kind IN ('COMMENT', 'QUOTE')
GROUP BY r1.created_by
"""
replies_to_comments_stats = {
row[0]: row[1] for row in session.execute(text(replies_to_comments_query), params)
}
logger.debug(f"Replies to comments stats retrieved: {replies_to_comments_stats}")
# Комментарии на посты автора
comments_on_posts_query = f"""
SELECT sa.author as author_id, COUNT(DISTINCT r.id) as replies_count
FROM shout_author sa
JOIN shout s ON sa.shout = s.id AND s.deleted_at IS NULL AND s.published_at IS NOT NULL
JOIN reaction r ON s.id = r.shout AND r.deleted_at IS NULL
WHERE sa.author IN ({placeholders})
AND r.kind IN ('COMMENT', 'QUOTE')
GROUP BY sa.author
"""
comments_on_posts_stats = {
row[0]: row[1] for row in session.execute(text(comments_on_posts_query), params)
}
logger.debug(f"Comments on posts stats retrieved: {comments_on_posts_stats}")
# Объединяем статистику
replies_count_stats = {}
for author_id in author_ids: for author_id in author_ids:
shouts_rating = rating_shouts_stats.get(author_id, 0) replies_to_comments = replies_to_comments_stats.get(author_id, 0)
comments_rating = rating_comments_stats.get(author_id, 0) comments_on_posts = comments_on_posts_stats.get(author_id, 0)
overall_rating_stats[author_id] = shouts_rating + comments_rating replies_count_stats[author_id] = replies_to_comments + comments_on_posts
logger.debug(f"Overall rating stats calculated: {overall_rating_stats}") logger.debug(f"Combined replies count stats: {replies_count_stats}")
# 👁️ Статистика по просмотрам публикаций (используем ViewedStorage для получения агрегированных данных) # 👁️ Статистика по просмотрам публикаций (используем ViewedStorage для получения агрегированных данных)
logger.debug("Calculating viewed_shouts statistics from ViewedStorage") logger.debug("Calculating viewed_shouts statistics from ViewedStorage")
@@ -601,7 +602,7 @@ async def get_authors_with_stats(
"topics": topics_stats.get(author.id, 0), "topics": topics_stats.get(author.id, 0),
"coauthors": coauthors_stats.get(author.id, 0), "coauthors": coauthors_stats.get(author.id, 0),
"followers": followers_stats.get(author.id, 0), "followers": followers_stats.get(author.id, 0),
"rating": overall_rating_stats.get(author.id, 0), "authors": authors_stats.get(author.id, 0),
"rating_shouts": rating_shouts_stats.get(author.id, 0), "rating_shouts": rating_shouts_stats.get(author.id, 0),
"rating_comments": rating_comments_stats.get(author.id, 0), "rating_comments": rating_comments_stats.get(author.id, 0),
"comments": comments_stats.get(author.id, 0), "comments": comments_stats.get(author.id, 0),

View File

@@ -9,8 +9,7 @@ type AuthorStat {
coauthors: Int # Количество уникальных соавторов coauthors: Int # Количество уникальных соавторов
followers: Int # Количество подписчиков followers: Int # Количество подписчиков
# Рейтинговая система # Рейтинговая система # Общий рейтинг (rating_shouts + rating_comments)
rating: Int # Общий рейтинг (rating_shouts + rating_comments)
rating_shouts: Int # Рейтинг публикаций (сумма реакций LIKE/AGREE/ACCEPT/PROOF/CREDIT минус DISLIKE/DISAGREE/REJECT/DISPROOF) rating_shouts: Int # Рейтинг публикаций (сумма реакций LIKE/AGREE/ACCEPT/PROOF/CREDIT минус DISLIKE/DISAGREE/REJECT/DISPROOF)
rating_comments: Int # Рейтинг комментариев (реакции на комментарии автора) rating_comments: Int # Рейтинг комментариев (реакции на комментарии автора)