Files
hotels/monitor_db_recovery.py
Фёдор 0cf3297290 Проект аудита отелей: основные скрипты и документация
- Краулеры: smart_crawler.py, regional_crawler.py
- Аудит: audit_orel_to_excel.py, audit_chukotka_to_excel.py
- РКН проверка: check_rkn_registry.py, recheck_unclear_rkn.py
- Отчёты: create_orel_horizontal_report.py
- Обработка: process_all_hotels_embeddings.py
- Документация: README.md, DB_SCHEMA_REFERENCE.md
2025-10-16 10:52:09 +03:00

91 lines
2.9 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
"""
Мониторинг восстановления базы данных
"""
from urllib.parse import unquote
import psycopg2
import time
from datetime import datetime
DB_CONFIG = {
'host': "147.45.189.234",
'database': "default_db",
'user': "gen_user",
'password': unquote("2~~9_%5EkVsU%3F2%5CS"),
'connect_timeout': 5
}
def check_db():
"""Проверить состояние БД"""
try:
conn = psycopg2.connect(**DB_CONFIG)
cur = conn.cursor()
# Проверим базовые таблицы
cur.execute('SELECT COUNT(*) FROM hotel_main')
hotels = cur.fetchone()[0]
cur.execute('SELECT COUNT(*) FROM hotel_main WHERE region_name = %s', ('Чукотский автономный округ',))
chukotka = cur.fetchone()[0]
cur.execute('''
SELECT
COUNT(DISTINCT h.id) as total,
COUNT(DISTINCT p.hotel_id) as processed
FROM hotel_main h
LEFT JOIN hotel_website_processed p ON p.hotel_id = h.id
WHERE h.region_name = 'г. Санкт-Петербург'
AND h.website_address IS NOT NULL
AND h.website_address != ''
AND h.website_address != 'Нет'
''')
spb_total, spb_processed = cur.fetchone()
cur.close()
conn.close()
return {
'status': 'OK',
'hotels_total': hotels,
'chukotka_hotels': chukotka,
'spb_total': spb_total,
'spb_processed': spb_processed,
'spb_percent': spb_processed/spb_total*100 if spb_total > 0 else 0
}
except Exception as e:
return {'status': 'ERROR', 'error': str(e)}
def main():
"""Мониторинг восстановления"""
print("🔍 МОНИТОРИНГ ВОССТАНОВЛЕНИЯ БД")
print("=" * 50)
start_time = datetime.now()
while True:
current_time = datetime.now()
elapsed = (current_time - start_time).total_seconds()
result = check_db()
if result['status'] == 'OK':
print(f"🎉 БД ВОССТАНОВЛЕНА! Время восстановления: {elapsed/60:.1f} мин")
print(f"📊 Статистика:")
print(f" Всего отелей: {result['hotels_total']}")
print(f" Чукотка: {result['chukotka_hotels']} отелей")
print(f" СПб: {result['spb_processed']}/{result['spb_total']} ({result['spb_percent']:.1f}%)")
break
else:
print(f"[{current_time.strftime('%H:%M:%S')}] ❌ БД недоступна: {result['error']}")
time.sleep(30) # Проверяем каждые 30 секунд
if __name__ == "__main__":
main()