Files
core/test_topic_followers_debug.py
2025-08-28 19:42:03 +03:00

118 lines
5.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
Отладочный скрипт для проверки проблемы с количеством подписчиков топиков.
Проблема: все топики возвращают followers: 1
"""
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from storage.db import local_session
from orm.topic import Topic, TopicFollower
from orm.author import Author
from sqlalchemy import text, func
def debug_topic_followers():
"""Отладка проблемы с подписчиками топиков"""
print("🔍 Отладка проблемы с подписчиками топиков")
print("=" * 50)
with local_session() as session:
# 1. Проверяем общее количество топиков
total_topics = session.query(func.count(Topic.id)).scalar()
print(f"📊 Всего топиков в БД: {total_topics}")
# 2. Проверяем общее количество записей в topic_followers
total_followers = session.query(func.count(TopicFollower.topic)).scalar()
print(f"👥 Всего записей в topic_followers: {total_followers}")
# 3. Проверяем уникальные топики с подписчиками
topics_with_followers = session.query(func.count(func.distinct(TopicFollower.topic))).scalar()
print(f"🎯 Уникальных топиков с подписчиками: {topics_with_followers}")
# 4. Проверяем уникальных подписчиков
unique_followers = session.query(func.count(func.distinct(TopicFollower.follower))).scalar()
print(f"👤 Уникальных подписчиков: {unique_followers}")
# 5. Проверяем распределение подписчиков по топикам
print("\n📈 Распределение подписчиков по топикам:")
followers_distribution = session.execute(text("""
SELECT topic, COUNT(DISTINCT follower) as followers_count
FROM topic_followers
GROUP BY topic
ORDER BY followers_count DESC
LIMIT 10
""")).fetchall()
for topic_id, count in followers_distribution:
topic_title = session.query(Topic.title).where(Topic.id == topic_id).scalar()
print(f" Топик {topic_id} ({topic_title}): {count} подписчиков")
# 6. Проверяем конкретные топики из вашего примера
print("\n🎯 Проверка конкретных топиков:")
test_topic_ids = [715, 82, 166] # ID из вашего примера
for topic_id in test_topic_ids:
topic = session.query(Topic).where(Topic.id == topic_id).first()
if topic:
followers_count = session.query(func.count(TopicFollower.follower)).where(
TopicFollower.topic == topic_id
).scalar()
print(f" Топик {topic_id} ({topic.title}): {followers_count} подписчиков")
# Показываем детали подписчиков
if followers_count > 0:
followers = session.query(TopicFollower.follower).where(
TopicFollower.topic == topic_id
).all()
follower_ids = [f[0] for f in followers]
print(f" Подписчики: {follower_ids}")
else:
print(f" ❌ Топик {topic_id} не найден")
# 7. Проверяем SQL запрос, который используется в get_topics_with_stats
print("\n🔍 Проверка SQL запроса из get_topics_with_stats:")
test_topic_ids_str = ",".join(map(str, test_topic_ids))
followers_stats_query = f"""
SELECT topic, COUNT(DISTINCT follower) as followers_count
FROM topic_followers tf
WHERE topic IN ({test_topic_ids_str})
GROUP BY topic
"""
print(f"SQL: {followers_stats_query}")
try:
result = session.execute(text(followers_stats_query)).fetchall()
print("Результат:")
for topic_id, count in result:
print(f" Топик {topic_id}: {count} подписчиков")
except Exception as e:
print(f"❌ Ошибка выполнения SQL: {e}")
# 8. Проверяем, есть ли проблемы с JOIN
print("\n🔗 Проверка JOIN с таблицей author:")
try:
join_query = f"""
SELECT tf.topic, tf.follower, a.name as author_name
FROM topic_followers tf
JOIN author a ON tf.follower = a.id
WHERE tf.topic IN ({test_topic_ids_str})
ORDER BY tf.topic, tf.follower
"""
join_result = session.execute(text(join_query)).fetchall()
print("Результат JOIN:")
for topic_id, follower_id, author_name in join_result:
print(f" Топик {topic_id} -> Подписчик {follower_id} ({author_name})")
except Exception as e:
print(f"❌ Ошибка JOIN: {e}")
if __name__ == "__main__":
debug_topic_followers()