diff --git a/resolvers/author.py b/resolvers/author.py index b5c5df86..5999c3df 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -42,7 +42,7 @@ class AuthorsBy(TypedDict, total=False): πŸ”’ Π‘Π°Π·ΠΎΠ²Ρ‹Π΅ ΠΌΠ΅Ρ‚Ρ€ΠΈΠΊΠΈ: "shouts" (ΠΏΡƒΠ±Π»ΠΈΠΊΠ°Ρ†ΠΈΠΈ), "followers" (подписчики) 🏷️ ΠšΠΎΠ½Ρ‚Π΅Π½Ρ‚: "topics" (Ρ‚Π΅ΠΌΡ‹), "comments" (ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ) πŸ‘₯ Π‘ΠΎΡ†ΠΈΠ°Π»ΡŒΠ½Ρ‹Π΅: "coauthors" (соавторы), "replies_count" (ΠΎΡ‚Π²Π΅Ρ‚Ρ‹ Π½Π° ΠΊΠΎΠ½Ρ‚Π΅Π½Ρ‚) - ⭐ Π Π΅ΠΉΡ‚ΠΈΠ½Π³: "rating" (ΠΎΠ±Ρ‰ΠΈΠΉ), "rating_shouts" (ΠΏΡƒΠ±Π»ΠΈΠΊΠ°Ρ†ΠΈΠΈ), "rating_comments" (ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ) + ⭐ Π Π΅ΠΉΡ‚ΠΈΠ½Π³: "rating_shouts" (ΠΏΡƒΠ±Π»ΠΈΠΊΠ°Ρ†ΠΈΠΈ), "rating_comments" (ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ) πŸ‘οΈ Π’ΠΎΠ²Π»Π΅Ρ‡Ρ‘Π½Π½ΠΎΡΡ‚ΡŒ: "viewed_shouts" (просмотры) πŸ“ Алфавит: "name" (ΠΏΠΎ ΠΈΠΌΠ΅Π½ΠΈ) @@ -112,7 +112,7 @@ async def get_authors_with_stats( - topics: Π£Π½ΠΈΠΊΠ°Π»ΡŒΠ½Ρ‹Π΅ Ρ‚Π΅ΠΌΡ‹ участия - coauthors: ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ соавторов - followers: ΠŸΠΎΠ΄ΠΏΠΈΡΡ‡ΠΈΠΊΠΈ - - rating: ΠžΠ±Ρ‰ΠΈΠΉ Ρ€Π΅ΠΉΡ‚ΠΈΠ½Π³ (rating_shouts + rating_comments) + - authors: ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ Π°Π²Ρ‚ΠΎΡ€ΠΎΠ², Π½Π° ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… подписан - rating_shouts: Π Π΅ΠΉΡ‚ΠΈΠ½Π³ ΠΏΡƒΠ±Π»ΠΈΠΊΠ°Ρ†ΠΈΠΉ (Ρ€Π΅Π°ΠΊΡ†ΠΈΠΈ) - rating_comments: Π Π΅ΠΉΡ‚ΠΈΠ½Π³ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠ΅Π² (Ρ€Π΅Π°ΠΊΡ†ΠΈΠΈ) - comments: Π‘ΠΎΠ·Π΄Π°Π½Π½Ρ‹Π΅ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ @@ -161,7 +161,6 @@ async def get_authors_with_stats( if order_value in [ "shouts", "followers", - "rating", "comments", "topics", "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)} logger.debug(f"Comments stats retrieved: {comments_stats}") - # πŸ’¬ Бтатистика ΠΏΠΎ Π²Ρ‹Π·Π²Π°Π½Π½Ρ‹ΠΌ коммСнтариям (ΠΎΡ‚Π²Π΅Ρ‚Ρ‹ Π½Π° ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ + ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ Π½Π° посты) - logger.debug("Executing replies_count statistics query") - - # ΠžΡ‚Π²Π΅Ρ‚Ρ‹ Π½Π° ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ Π°Π²Ρ‚ΠΎΡ€Π° - 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 + # πŸ‘₯ Бтатистика ΠΏΠΎ количСству ΡƒΠ½ΠΈΠΊΠ°Π»ΡŒΠ½Ρ‹Ρ… Π°Π²Ρ‚ΠΎΡ€ΠΎΠ², Π½Π° ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… подписан Π΄Π°Π½Π½Ρ‹ΠΉ Π°Π²Ρ‚ΠΎΡ€ + logger.debug("Executing authors statistics query") + authors_stats_query = f""" + SELECT follower, COUNT(DISTINCT following) as authors_count + FROM author_follower + WHERE follower IN ({placeholders}) + GROUP BY follower """ - 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: - 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}") + authors_stats = {row[0]: row[1] for row in session.execute(text(authors_stats_query), params)} + logger.debug(f"Authors stats retrieved: {authors_stats}") # ⭐ Бтатистика ΠΏΠΎ Ρ€Π΅ΠΉΡ‚ΠΈΠ½Π³Ρƒ ΠΏΡƒΠ±Π»ΠΈΠΊΠ°Ρ†ΠΈΠΉ (сумма Ρ€Π΅Π°ΠΊΡ†ΠΈΠΉ Π½Π° ΠΏΡƒΠ±Π»ΠΈΠΊΠ°Ρ†ΠΈΠΈ Π°Π²Ρ‚ΠΎΡ€Π°) 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("Calculating overall rating") - overall_rating_stats = {} + # πŸ’¬ Бтатистика ΠΏΠΎ Π²Ρ‹Π·Π²Π°Π½Π½Ρ‹ΠΌ коммСнтариям (ΠΎΡ‚Π²Π΅Ρ‚Ρ‹ Π½Π° ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ + ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ Π½Π° посты) + logger.debug("Executing replies_count statistics query") + + # ΠžΡ‚Π²Π΅Ρ‚Ρ‹ Π½Π° ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ Π°Π²Ρ‚ΠΎΡ€Π° + 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: - shouts_rating = rating_shouts_stats.get(author_id, 0) - comments_rating = rating_comments_stats.get(author_id, 0) - overall_rating_stats[author_id] = shouts_rating + comments_rating - logger.debug(f"Overall rating stats calculated: {overall_rating_stats}") + 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}") # πŸ‘οΈ Бтатистика ΠΏΠΎ просмотрам ΠΏΡƒΠ±Π»ΠΈΠΊΠ°Ρ†ΠΈΠΉ (ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌ 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), "coauthors": coauthors_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_comments": rating_comments_stats.get(author.id, 0), "comments": comments_stats.get(author.id, 0), diff --git a/schema/type.graphql b/schema/type.graphql index e3fbda72..49e4fb0d 100644 --- a/schema/type.graphql +++ b/schema/type.graphql @@ -9,8 +9,7 @@ type AuthorStat { coauthors: Int # ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ ΡƒΠ½ΠΈΠΊΠ°Π»ΡŒΠ½Ρ‹Ρ… соавторов followers: Int # ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ подписчиков - # РСйтинговая систСма - rating: Int # ΠžΠ±Ρ‰ΠΈΠΉ Ρ€Π΅ΠΉΡ‚ΠΈΠ½Π³ (rating_shouts + rating_comments) + # РСйтинговая систСма # ΠžΠ±Ρ‰ΠΈΠΉ Ρ€Π΅ΠΉΡ‚ΠΈΠ½Π³ (rating_shouts + rating_comments) rating_shouts: Int # Π Π΅ΠΉΡ‚ΠΈΠ½Π³ ΠΏΡƒΠ±Π»ΠΈΠΊΠ°Ρ†ΠΈΠΉ (сумма Ρ€Π΅Π°ΠΊΡ†ΠΈΠΉ LIKE/AGREE/ACCEPT/PROOF/CREDIT минус DISLIKE/DISAGREE/REJECT/DISPROOF) rating_comments: Int # Π Π΅ΠΉΡ‚ΠΈΠ½Π³ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠ΅Π² (Ρ€Π΅Π°ΠΊΡ†ΠΈΠΈ Π½Π° ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ Π°Π²Ρ‚ΠΎΡ€Π°)