Files
monitoring-pf/diagnose.py
2026-06-04 14:55:41 +03:00

90 lines
4.2 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.
r"""Одноразовая диагностика: проверяет всю цепочку мониторинга.
Запуск: & "<...>\.venv\Scripts\python.exe" diagnose.py
Только чтение БД + проверка токена TG + живой re-fetch. Сообщения НЕ шлёт."""
from __future__ import annotations
import sys
import httpx
from app.config import settings
from app.db import SessionLocal
from app.models import CompetitorListing, Employee, PriceHistory, Project
from app.services.monitor import check_project
def line(c: str = "-") -> None:
print(c * 60)
def main() -> None:
db = SessionLocal()
try:
# --- 1. Содержимое БД ---
line("=")
print("1) ДАННЫЕ В БАЗЕ")
line("=")
projects = db.query(Project).all()
if not projects:
print(" ! В базе нет ни одного проекта.")
return
for p in projects:
owner = p.owner
chat = owner.tg_chat_id if owner else None
chat_str = chat if chat else "НЕ ЗАДАН (уведомления не дойдут!)"
print(f"\n Проект #{p.id}: {p.title}")
print(f" тип: {p.deal_type.value} | наша цена: {p.our_price}")
print(f" владелец: {owner.name if owner else ''} | tg_chat_id: {chat_str}")
print(f" последняя проверка: {p.last_checked_at or 'ещё не было'}")
listings = p.listings
print(f" отслеживаемых объявлений конкурентов: {len(listings)}")
for l in listings:
hist = db.query(PriceHistory).filter(PriceHistory.listing_id == l.id).count()
price = f"{l.current_price:,.0f} {l.currency}".replace(",", " ") if l.current_price else "БЕЗ ЦЕНЫ"
print(f" - [{l.source.value}] {l.status.value} | {price} | история: {hist} зап.")
print(f" {(l.title or 'без названия')[:70]}")
print(f" брокер: {l.agency_name or ''} | {l.url}")
# --- 2. Telegram токен ---
line("=")
print("2) TELEGRAM")
line("=")
if not settings.tg_bot_token:
print(" ! TG_BOT_TOKEN не задан в .env")
else:
try:
r = httpx.get(
f"https://api.telegram.org/bot{settings.tg_bot_token}/getMe",
timeout=15.0,
)
if r.status_code == 200 and r.json().get("ok"):
bot = r.json()["result"]
print(f" OK токен валиден: @{bot.get('username')} ({bot.get('first_name')})")
else:
print(f" ! getMe вернул {r.status_code}: {r.text}")
except Exception as e:
print(f" ! Ошибка связи с Telegram: {e}")
print(f" ADMIN_CHAT_ID: {settings.admin_chat_id or 'не задан'}")
# --- 3. Живой re-fetch (то, что делает сканер раз в 4 ч) ---
line("=")
print("3) ЖИВАЯ ПЕРЕПРОВЕРКА (re-fetch всех объявлений)")
line("=")
for p in projects:
print(f"\n Проект #{p.id}: {p.title} — проверяю {len(p.listings)} объявл...")
changes = check_project(db, p)
if changes:
print(f" обнаружено изменений: {len(changes)} (в реальном прогоне ушли бы в TG):")
for c in changes:
print(" " + c.replace("\n", " | ").replace("<b>", "").replace("</b>", ""))
else:
print(" изменений нет — все объявления успешно перезагружены, цены прежние.")
line("=")
print("Готово. Если у объявлений есть цены и статус active, а токен валиден — цепочка рабочая.")
finally:
db.close()
if __name__ == "__main__":
sys.exit(main())