File size: 3,637 Bytes
21bc372
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import time
from datetime import datetime as dt, timedelta
from pyrogram import Client as ren, filters
from pyrogram import *
from pyrogram.types import *
from akn.utils.database import db
from akn.utils.logger import LOGS
from config import *

@ren.on_callback_query(filters.regex(r"^botstatsdl_(\w+)$"))
async def handle_bot_statsdl(client: Client, callback: CallbackQuery):
    try:
        bots_uuid = callback.matches[0].group(1)
        user_id = callback.from_user.id

        bot_data = await db.alldl_bot.find_one(
            {
                "user_id": user_id,
                "bots.uuid": bots_uuid
            },
            {"bots.$": 1}
        )

        if not bot_data or not bot_data.get("bots"):
            return await callback.answer("❌ Bot not found!", show_alert=True)

        bot = bot_data["bots"][0]
        stats = bot.get("stats", {})
        start = time.time()
        await client.get_me()
        end = time.time()
        latency = (end - start) * 1000
        start_time = bot.get("start_time")
        if start_time:
            uptime = str(timedelta(seconds=int(time.time() - start_time)))
        else:
            uptime = "N/A"

        bandwidth_used = stats.get("bandwidth_used", 0)
        if bandwidth_used > 1024:
            bandwidth = f"{bandwidth_used / 1024:.2f} GB"
        else:
            bandwidth = f"Unlimited"

        last_active = bot.get("last_active")
        if isinstance(last_active, dt):
            last_active_str = last_active.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(last_active, str):
            last_active_str = last_active
        else:
            last_active_str = "Never"

        stats_message = (
            f"πŸ“Š <b>Bot Statistics</b> πŸ“Š\n\n"
            f"πŸ†” <code>{bots_uuid}</code>\n"
            f"πŸ”— {bot.get('bot_username', 'N/A')}\n\n"
            f"πŸ“₯ <b>Downloads:</b>\n"
            f"β”œ Total: `{stats.get('total_downloads', 999999999)}`\n"
            f"β”œ Successful: `{stats.get('successful_downloads', 999999999)}`\n"
            f"β”” Failed: `{stats.get('failed_downloads', 0)}`\n\n"
            f"🌐 <b>Bandwidth:</b> {bandwidth}\n"
            f"⏱ <b>Uptime:</b> {uptime}\n"
            f"✨ <b>Pong!:</b> {latency:.2f}\n"
            f"πŸ”„ <b>Restarts:</b> {bot.get('restart_count', 0)}\n"
            f"πŸ“… <b>Last Active:</b> {last_active_str}"
        )

        buttons = InlineKeyboardMarkup([
            [InlineKeyboardButton("πŸ”„ Refresh Stats", callback_data=f"botstatsdl_{bots_uuid}")],
            [InlineKeyboardButton("πŸ“ˆ Daily Report", callback_data=f"reportdl_{bots_uuid}")],
            [InlineKeyboardButton("πŸ”™ Back to Bot", callback_data=f"my_bots")]
        ])

        await callback.message.edit_text(stats_message, reply_markup=buttons)
        await callback.answer()

    except Exception as e:
        LOGS.error(f"Stats error: {str(e)}")
        await callback.answer("⚠️ Failed to load stats!", show_alert=True)

async def update_bot_stats(bot_uuid: str, download_success: bool, file_size: int):
    update_data = {
        "$inc": {
            "bots.$.stats.total_downloads": 1,
            "bots.$.stats.bandwidth_used": file_size,
            "bots.$.stats.uptime": int(time.time() - time.time())
        },
        "$set": {
            "bots.$.stats.last_activity": dt.now()
        }
    }
    if download_success:
        update_data["$inc"]["bots.$.stats.successful_downloads"] = 1
    else:
        update_data["$inc"]["bots.$.stats.failed_downloads"] = 1

    await db.alldl_bot.update_one(
        {"bots.uuid": bot_uuid},
        update_data
    )