topic.stat.authors-fix
All checks were successful
Deploy on push / deploy (push) Successful in 1m20s
All checks were successful
Deploy on push / deploy (push) Successful in 1m20s
This commit is contained in:
parent
abbc074474
commit
7d50638b3a
2
cache/cache.py
vendored
2
cache/cache.py
vendored
|
@ -545,7 +545,7 @@ async def get_cached_data(key: str) -> Optional[Any]:
|
||||||
try:
|
try:
|
||||||
cached_data = await redis.execute("GET", key)
|
cached_data = await redis.execute("GET", key)
|
||||||
if cached_data:
|
if cached_data:
|
||||||
logger.debug(f"Данные получены из кеша по ключу {key}")
|
logger.debug(f"Данные получены из кеша по ключу {key}: {len(cached_data)} записей")
|
||||||
return orjson.loads(cached_data)
|
return orjson.loads(cached_data)
|
||||||
return None
|
return None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
|
@ -6,7 +6,7 @@ from cache.cache import (
|
||||||
get_cached_topic_authors,
|
get_cached_topic_authors,
|
||||||
get_cached_topic_by_slug,
|
get_cached_topic_by_slug,
|
||||||
get_cached_topic_followers,
|
get_cached_topic_followers,
|
||||||
invalidate_cache_by_prefix,
|
invalidate_cache_by_prefix
|
||||||
)
|
)
|
||||||
from orm.author import Author
|
from orm.author import Author
|
||||||
from orm.topic import Topic
|
from orm.topic import Topic
|
||||||
|
@ -127,6 +127,17 @@ async def get_topics_with_stats(limit=100, offset=0, community_id=None, by=None)
|
||||||
"""
|
"""
|
||||||
followers_stats = {row[0]: row[1] for row in session.execute(text(followers_stats_query))}
|
followers_stats = {row[0]: row[1] for row in session.execute(text(followers_stats_query))}
|
||||||
|
|
||||||
|
# Запрос на получение статистики авторов для выбранных тем
|
||||||
|
authors_stats_query = f"""
|
||||||
|
SELECT st.topic, COUNT(DISTINCT sa.author) as authors_count
|
||||||
|
FROM shout_topic st
|
||||||
|
JOIN shout s ON st.shout = s.id AND s.deleted_at IS NULL AND s.published_at IS NOT NULL
|
||||||
|
JOIN shout_author sa ON sa.shout = s.id
|
||||||
|
WHERE st.topic IN ({",".join(map(str, topic_ids))})
|
||||||
|
GROUP BY st.topic
|
||||||
|
"""
|
||||||
|
authors_stats = {row[0]: row[1] for row in session.execute(text(authors_stats_query))}
|
||||||
|
|
||||||
# Формируем результат с добавлением статистики
|
# Формируем результат с добавлением статистики
|
||||||
result = []
|
result = []
|
||||||
for topic in topics:
|
for topic in topics:
|
||||||
|
@ -134,6 +145,7 @@ async def get_topics_with_stats(limit=100, offset=0, community_id=None, by=None)
|
||||||
topic_dict["stat"] = {
|
topic_dict["stat"] = {
|
||||||
"shouts": shouts_stats.get(topic.id, 0),
|
"shouts": shouts_stats.get(topic.id, 0),
|
||||||
"followers": followers_stats.get(topic.id, 0),
|
"followers": followers_stats.get(topic.id, 0),
|
||||||
|
"authors": authors_stats.get(topic.id, 0)
|
||||||
}
|
}
|
||||||
result.append(topic_dict)
|
result.append(topic_dict)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user