File size: 5,075 Bytes
1f26706 |
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# Ultroid - UserBot
# Copyright (C) 2021-2025 TeamUltroid
#
# This file is a part of < https://github.com/TeamUltroid/Ultroid/ >
# PLease read the GNU Affero General Public License in
# <https://www.github.com/TeamUltroid/Ultroid/blob/main/LICENSE/>.
"""
✘ Commands Available
• `{i}usage`
Get overall usage.
• `{i}usage heroku`
Get heroku stats.
• `{i}usage db`
Get database storage usage.
"""
import math
import shutil
from random import choice
from pyUltroid.fns import some_random_headers
from . import (
HOSTED_ON,
LOGS,
Var,
async_searcher,
get_string,
humanbytes,
udB,
ultroid_cmd,
)
HEROKU_API = None
HEROKU_APP_NAME = None
if HOSTED_ON == "heroku":
heroku_api, app_name = Var.HEROKU_API, Var.HEROKU_APP_NAME
try:
if heroku_api and app_name:
import heroku3
Heroku = heroku3.from_key(heroku_api)
app = Heroku.app(app_name)
HEROKU_API = heroku_api
HEROKU_APP_NAME = app_name
except BaseException as er:
LOGS.exception(er)
@ultroid_cmd(pattern="usage")
async def usage_finder(event):
x = await event.eor(get_string("com_1"))
try:
opt = event.text.split(" ", maxsplit=1)[1]
except IndexError:
return await x.edit(simple_usage())
if opt == "db":
await x.edit(db_usage())
elif opt == "heroku":
is_hk, hk = await heroku_usage()
await x.edit(hk)
else:
await x.edit(await get_full_usage())
def simple_usage():
try:
import psutil
except ImportError:
return "Install 'psutil' to use this..."
total, used, free = shutil.disk_usage(".")
cpuUsage = psutil.cpu_percent()
memory = psutil.virtual_memory().percent
disk = psutil.disk_usage("/").percent
upload = humanbytes(psutil.net_io_counters().bytes_sent)
down = humanbytes(psutil.net_io_counters().bytes_recv)
TOTAL = humanbytes(total)
USED = humanbytes(used)
FREE = humanbytes(free)
return get_string("usage_simple").format(
TOTAL,
USED,
FREE,
upload,
down,
cpuUsage,
memory,
disk,
)
async def heroku_usage():
try:
import psutil
except ImportError:
return (
False,
"'psutil' not installed!\nPlease Install it to use this.\n`pip3 install psutil`",
)
if not (HEROKU_API and HEROKU_APP_NAME):
if HOSTED_ON == "heroku":
return False, "Please fill `HEROKU_API` and `HEROKU_APP_NAME`"
return (
False,
f"`This command is only for Heroku Users, You are using {HOSTED_ON}`",
)
user_id = Heroku.account().id
headers = {
"User-Agent": choice(some_random_headers),
"Authorization": f"Bearer {heroku_api}",
"Accept": "application/vnd.heroku+json; version=3.account-quotas",
}
her_url = f"https://api.heroku.com/accounts/{user_id}/actions/get-quota"
try:
result = await async_searcher(her_url, headers=headers, re_json=True)
except Exception as er:
return False, str(er)
quota = result["account_quota"]
quota_used = result["quota_used"]
remaining_quota = quota - quota_used
percentage = math.floor(remaining_quota / quota * 100)
minutes_remaining = remaining_quota / 60
hours = math.floor(minutes_remaining / 60)
minutes = math.floor(minutes_remaining % 60)
App = result["apps"]
try:
App[0]["quota_used"]
except IndexError:
AppQuotaUsed = 0
AppPercentage = 0
else:
AppQuotaUsed = App[0]["quota_used"] / 60
AppPercentage = math.floor(App[0]["quota_used"] * 100 / quota)
AppHours = math.floor(AppQuotaUsed / 60)
AppMinutes = math.floor(AppQuotaUsed % 60)
total, used, free = shutil.disk_usage(".")
_ = shutil.disk_usage("/")
disk = _.used / _.total * 100
cpuUsage = psutil.cpu_percent()
memory = psutil.virtual_memory().percent
upload = humanbytes(psutil.net_io_counters().bytes_sent)
down = humanbytes(psutil.net_io_counters().bytes_recv)
TOTAL = humanbytes(total)
USED = humanbytes(used)
FREE = humanbytes(free)
return True, get_string("usage").format(
Var.HEROKU_APP_NAME,
AppHours,
AppMinutes,
AppPercentage,
hours,
minutes,
percentage,
TOTAL,
USED,
FREE,
upload,
down,
cpuUsage,
memory,
disk,
)
def db_usage():
if udB.name == "Mongo":
total = 512
elif udB.name == "Redis":
total = 30
elif udB.name == "SQL":
total = 20
total = total * (2**20)
used = udB.usage
a = f"{humanbytes(used)}/{humanbytes(total)}"
b = f"{str(round((used / total) * 100, 2))}%"
return f"**{udB.name}**\n\n**Storage Used**: `{a}`\n**Usage percentage**: **{b}**"
async def get_full_usage():
is_hk, hk = await heroku_usage()
her = hk if is_hk else ""
rd = db_usage()
return her + "\n\n" + rd
|