Add Stats button in Status

Co-authored-by: Juan <jusidama18@users.noreply.github.com>
Co-authored-by: BruteWoorse <brutewooorse@users.noreply.github.com>
Signed-off-by: breakdowns <hafitz666@outlook.co.id>
This commit is contained in:
Hafitz Setya 2021-07-13 11:08:52 +07:00 committed by GitHub
parent 8cb3dec990
commit 65f1c05465
Signed by untrusted user: GitHub
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,10 +1,11 @@
from telegram import InlineKeyboardMarkup from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import CallbackContext, CallbackQueryHandler
from telegram.message import Message from telegram.message import Message
from telegram.update import Update from telegram.update import Update
import psutil, shutil import psutil, shutil
import time import time
from bot import AUTO_DELETE_MESSAGE_DURATION, LOGGER, bot, \ from bot import AUTO_DELETE_MESSAGE_DURATION, LOGGER, bot, \
status_reply_dict, status_reply_dict_lock, download_dict, download_dict_lock, botStartTime status_reply_dict, status_reply_dict_lock, download_dict, download_dict_lock, botStartTime, dispatcher
from bot.helper.ext_utils.bot_utils import get_readable_message, get_readable_file_size, get_readable_time, MirrorStatus from bot.helper.ext_utils.bot_utils import get_readable_message, get_readable_file_size, get_readable_time, MirrorStatus
from telegram.error import TimedOut, BadRequest from telegram.error import TimedOut, BadRequest
@ -67,13 +68,7 @@ def delete_all_messages():
def update_all_messages(): def update_all_messages():
total, used, free = shutil.disk_usage('.')
free = get_readable_file_size(free)
currentTime = get_readable_time(time.time() - botStartTime)
msg = get_readable_message() msg = get_readable_message()
msg += f"<b>CPU:</b> {psutil.cpu_percent()}%" \
f" <b>RAM:</b> {psutil.virtual_memory().percent}%" \
f" <b>DISK:</b> {psutil.disk_usage('/').percent}%"
with download_dict_lock: with download_dict_lock:
dlspeed_bytes = 0 dlspeed_bytes = 0
uldl_bytes = 0 uldl_bytes = 0
@ -91,27 +86,22 @@ def update_all_messages():
uldl_bytes += float(speedy.split('M')[0]) * 1048576 uldl_bytes += float(speedy.split('M')[0]) * 1048576
dlspeed = get_readable_file_size(dlspeed_bytes) dlspeed = get_readable_file_size(dlspeed_bytes)
ulspeed = get_readable_file_size(uldl_bytes) ulspeed = get_readable_file_size(uldl_bytes)
msg += f"\n<b>FREE:</b> {free} | <b>UPTIME:</b> {currentTime}\n<b>DL:</b> {dlspeed}ps 🔻 | <b>UL:</b> {ulspeed}ps 🔺\n" msg += f"<b>DL:</b> {dlspeed}ps 🔻 | <b>UL:</b> {ulspeed}ps 🔺"
with status_reply_dict_lock: with status_reply_dict_lock:
for chat_id in list(status_reply_dict.keys()): for chat_id in list(status_reply_dict.keys()):
if status_reply_dict[chat_id] and msg != status_reply_dict[chat_id].text: if status_reply_dict[chat_id] and msg != status_reply_dict[chat_id].text:
if len(msg) == 0: if len(msg) == 0:
msg = "Starting DL" msg = "Starting DL"
try: try:
editMessage(msg, status_reply_dict[chat_id]) keyboard = [[InlineKeyboardButton("♻️ Stats", callback_data="stats_")]]
editMessage(msg, status_reply_dict[chat_id], reply_markup=InlineKeyboardMarkup(keyboard))
except Exception as e: except Exception as e:
LOGGER.error(str(e)) LOGGER.error(str(e))
status_reply_dict[chat_id].text = msg status_reply_dict[chat_id].text = msg
def sendStatusMessage(msg, bot): def sendStatusMessage(msg, bot):
total, used, free = shutil.disk_usage('.')
free = get_readable_file_size(free)
currentTime = get_readable_time(time.time() - botStartTime)
progress = get_readable_message() progress = get_readable_message()
progress += f"<b>CPU:</b> {psutil.cpu_percent()}%" \
f" <b>RAM:</b> {psutil.virtual_memory().percent}%" \
f" <b>DISK:</b> {psutil.disk_usage('/').percent}%"
with download_dict_lock: with download_dict_lock:
dlspeed_bytes = 0 dlspeed_bytes = 0
uldl_bytes = 0 uldl_bytes = 0
@ -129,7 +119,7 @@ def sendStatusMessage(msg, bot):
uldl_bytes += float(speedy.split('M')[0]) * 1048576 uldl_bytes += float(speedy.split('M')[0]) * 1048576
dlspeed = get_readable_file_size(dlspeed_bytes) dlspeed = get_readable_file_size(dlspeed_bytes)
ulspeed = get_readable_file_size(uldl_bytes) ulspeed = get_readable_file_size(uldl_bytes)
progress += f"\n<b>FREE:</b> {free} | <b>UPTIME:</b> {currentTime}\n<b>DL:</b> {dlspeed}ps 🔻 | <b>UL:</b> {ulspeed}ps 🔺\n" progress += f"<b>DL:</b> {dlspeed}ps 🔻 | <b>UL:</b> {ulspeed}ps 🔺"
with status_reply_dict_lock: with status_reply_dict_lock:
if msg.message.chat.id in list(status_reply_dict.keys()): if msg.message.chat.id in list(status_reply_dict.keys()):
try: try:
@ -144,3 +134,27 @@ def sendStatusMessage(msg, bot):
progress = "Starting DL" progress = "Starting DL"
message = sendMessage(progress, bot, msg) message = sendMessage(progress, bot, msg)
status_reply_dict[msg.message.chat.id] = message status_reply_dict[msg.message.chat.id] = message
def pop_up_stats(update, context):
query = update.callback_query
stats = bot_sys_stats()
query.answer(text=stats, show_alert=True)
def bot_sys_stats():
currentTime = get_readable_time(time.time() - botStartTime)
cpu = psutil.cpu_percent()
mem = psutil.virtual_memory().percent
disk = psutil.disk_usage("/").percent
total, used, free = shutil.disk_usage('.')
free = get_readable_file_size(free)
stats = f"""
Bot Uptime: {currentTime}
Free Disk: {free}
CPU: {cpu}%
RAM: {mem}%
DISK: {disk}%
"""
return stats
dispatcher.add_handler(CallbackQueryHandler(pop_up_stats, pattern="stats_"))