Untitled
unknown
plain_text
a year ago
2.9 kB
11
Indexable
import asyncio
from datetime import datetime, timedelta
from database.api import FindReqDB, InstanceAggregate
from database.core import (
get_conn,
get_collection,
)
from logs_settings.scheduler_tasks_logger import SchedulerTasksLogger
logger = SchedulerTasksLogger()
async def get_branches_with_single_payments(conn):
"""Получение филиалов с одним ненулевым платежом."""
bot_clients_collection = get_collection(conn)
branches_cursor = await FindReqDB(
collection=bot_clients_collection,
match={
"accounts.process": True,
"accounts.payments": {"$size": 1},
"accounts.payments.amount": {"$gt": 0}
}
).find("accounts.chat_api.instanceId", "accounts.CRM_data.branch", full_obj=False)
print("get_branches_with_single_payments: Выполнен запрос к базе данных")
branches = await branches_cursor.to_list(length=None)
print(f"get_branches_with_single_payments: Получено {len(branches)} филиалов")
return branches
async def check_instance_disconnection():
"""Проверка отключений инстансов за последнюю неделю."""
print("check_instance_disconnection: Начало выполнения")
one_week_ago = datetime.now() - timedelta(weeks=1)
conn = get_conn()
branches = await get_branches_with_single_payments(conn)
print(f"check_instance_disconnection: Получено {len(branches)} филиалов для проверки")
instance_ids = []
branch_map = {}
for branch in branches:
for account in branch["accounts"]:
instance_id = account["chat_api"]["instanceId"]
branch_id = account["CRM_data"]["branch"]
instance_ids.append(instance_id)
branch_map[instance_id] = branch_id
print(f"Собрано {len(instance_ids)} инстансов")
results = await InstanceAggregate(conn).get_instance_disconnection_data(one_week_ago=one_week_ago,
instance_ids=instance_ids)
print(f"Получено {len(results)} результатов отключений")
disconnected_instances = [
{
"instance_id": result["_id"],
"branch_id": branch_map[result["_id"]],
"disconnect_count": result["disconnect_count"]
}
for result in results
]
for instance in disconnected_instances:
print(f"send_notification_to_amo {instance['instance_id']} {instance['branch_id']}")
print(f"{len(disconnected_instances)} инстансов с отключениями более 5 раз")
return disconnected_instances
if __name__ == '__main__':
disconnected_instances = asyncio.run(check_instance_disconnection())
Editor is loading...
Leave a Comment