Team Ai
Apppublic

azils3/bash

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
usage.py189 linesDownload Raw Back to plugins
1# Ultroid - UserBot2# Copyright (C) 2021-2025 TeamUltroid3#4# This file is a part of < https://github.com/TeamUltroid/Ultroid/ >5# PLease read the GNU Affero General Public License in6# <https://www.github.com/TeamUltroid/Ultroid/blob/main/LICENSE/>.7"""8✘ Commands Available9 10• `{i}usage`11    Get overall usage.12 13• `{i}usage heroku`14   Get heroku stats.15 16• `{i}usage db`17   Get database storage usage.18"""19 20import math21import shutil22from random import choice23 24from pyUltroid.fns import some_random_headers25 26from . import (27    HOSTED_ON,28    LOGS,29    Var,30    async_searcher,31    get_string,32    humanbytes,33    udB,34    ultroid_cmd,35)36 37HEROKU_API = None38HEROKU_APP_NAME = None39 40if HOSTED_ON == "heroku":41    heroku_api, app_name = Var.HEROKU_API, Var.HEROKU_APP_NAME42    try:43        if heroku_api and app_name:44            import heroku345 46            Heroku = heroku3.from_key(heroku_api)47            app = Heroku.app(app_name)48            HEROKU_API = heroku_api49            HEROKU_APP_NAME = app_name50    except BaseException as er:51        LOGS.exception(er)52 53 54@ultroid_cmd(pattern="usage")55async def usage_finder(event):56    x = await event.eor(get_string("com_1"))57    try:58        opt = event.text.split(" ", maxsplit=1)[1]59    except IndexError:60        return await x.edit(simple_usage())61 62    if opt == "db":63        await x.edit(db_usage())64    elif opt == "heroku":65        is_hk, hk = await heroku_usage()66        await x.edit(hk)67    else:68        await x.edit(await get_full_usage())69 70 71def simple_usage():72    try:73        import psutil74    except ImportError:75        return "Install 'psutil' to use this..."76    total, used, free = shutil.disk_usage(".")77    cpuUsage = psutil.cpu_percent()78    memory = psutil.virtual_memory().percent79    disk = psutil.disk_usage("/").percent80    upload = humanbytes(psutil.net_io_counters().bytes_sent)81    down = humanbytes(psutil.net_io_counters().bytes_recv)82    TOTAL = humanbytes(total)83    USED = humanbytes(used)84    FREE = humanbytes(free)85    return get_string("usage_simple").format(86        TOTAL,87        USED,88        FREE,89        upload,90        down,91        cpuUsage,92        memory,93        disk,94    )95 96 97async def heroku_usage():98    try:99        import psutil100    except ImportError:101        return (102            False,103            "'psutil' not installed!\nPlease Install it to use this.\n`pip3 install psutil`",104        )105    if not (HEROKU_API and HEROKU_APP_NAME):106        if HOSTED_ON == "heroku":107            return False, "Please fill `HEROKU_API` and `HEROKU_APP_NAME`"108        return (109            False,110            f"`This command is only for Heroku Users, You are using {HOSTED_ON}`",111        )112    user_id = Heroku.account().id113    headers = {114        "User-Agent": choice(some_random_headers),115        "Authorization": f"Bearer {heroku_api}",116        "Accept": "application/vnd.heroku+json; version=3.account-quotas",117    }118    her_url = f"https://api.heroku.com/accounts/{user_id}/actions/get-quota"119    try:120        result = await async_searcher(her_url, headers=headers, re_json=True)121    except Exception as er:122        return False, str(er)123    quota = result["account_quota"]124    quota_used = result["quota_used"]125    remaining_quota = quota - quota_used126    percentage = math.floor(remaining_quota / quota * 100)127    minutes_remaining = remaining_quota / 60128    hours = math.floor(minutes_remaining / 60)129    minutes = math.floor(minutes_remaining % 60)130    App = result["apps"]131    try:132        App[0]["quota_used"]133    except IndexError:134        AppQuotaUsed = 0135        AppPercentage = 0136    else:137        AppQuotaUsed = App[0]["quota_used"] / 60138        AppPercentage = math.floor(App[0]["quota_used"] * 100 / quota)139    AppHours = math.floor(AppQuotaUsed / 60)140    AppMinutes = math.floor(AppQuotaUsed % 60)141    total, used, free = shutil.disk_usage(".")142    _ = shutil.disk_usage("/")143    disk = _.used / _.total * 100144    cpuUsage = psutil.cpu_percent()145    memory = psutil.virtual_memory().percent146    upload = humanbytes(psutil.net_io_counters().bytes_sent)147    down = humanbytes(psutil.net_io_counters().bytes_recv)148    TOTAL = humanbytes(total)149    USED = humanbytes(used)150    FREE = humanbytes(free)151    return True, get_string("usage").format(152        Var.HEROKU_APP_NAME,153        AppHours,154        AppMinutes,155        AppPercentage,156        hours,157        minutes,158        percentage,159        TOTAL,160        USED,161        FREE,162        upload,163        down,164        cpuUsage,165        memory,166        disk,167    )168 169 170def db_usage():171    if udB.name == "Mongo":172        total = 512173    elif udB.name == "Redis":174        total = 30175    elif udB.name == "SQL":176        total = 20177    total = total * (2**20)178    used = udB.usage179    a = f"{humanbytes(used)}/{humanbytes(total)}"180    b = f"{str(round((used / total) * 100, 2))}%"181    return f"**{udB.name}**\n\n**Storage Used**: `{a}`\n**Usage percentage**: **{b}**"182 183 184async def get_full_usage():185    is_hk, hk = await heroku_usage()186    her = hk if is_hk else ""187    rd = db_usage()188    return her + "\n\n" + rd189