Passed
Push — master ( c6e319...53c329 )
by Anas
09:14
created

modules.system.system()   B

Complexity

Conditions 3

Size

Total Lines 52
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 39
nop 2
dl 0
loc 52
rs 8.9439
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
from modules.logging import logging_decorator
4
from telegram.ext import CommandHandler
5
from telegram import ChatAction
6
from datetime import datetime
7
from uptime import uptime
8
import platform
9
import cpuinfo
10
import psutil
11
12
13
def module_init(gd):
14
    commands = gd.config["commands"]
15
    for command in commands:
16
        gd.dp.add_handler(CommandHandler(command, system))
17
18
19
def get_size(bytes, suffix="B"):
20
    factor = 1024
21
    for unit in ["", "K", "M", "G", "T", "P"]:
22
        if bytes < factor:
23
            return f"{bytes:.2f}{unit}{suffix}"
24
        bytes /= factor
25
26
27
def seconds_to_str(seconds):
28
    days, remainder = divmod(seconds, 86400)
29
    hours, remainder = divmod(remainder, 3600)
30
    minutes, seconds = divmod(remainder, 60)
31
    return "{} day(s), {:02} hours, {:02} minutes, {:02} seconds".format(days, hours, minutes, seconds)
32
33
34
@logging_decorator("system")
35
def system(bot, update):
36
    update.message.chat.send_action(ChatAction.TYPING)
37
38
    # System information
39
    uname = platform.uname()
40
    a = "="*10, " 💻 System Info ", "="*9
41
    sys_header = "".join(a)
42
    sys_info = "{}\n\nSystem: {}\nNode Name: {}\nVersion: {}\n".format(sys_header, uname.system, uname.node, uname.version)
43
44
    # Uptime
45
    b = "="*14, " Uptime ", "="*13
46
    upt_header = "".join(b)
47
    f = int(uptime())
48
    pcuptime = seconds_to_str(f)
49
    up = "{}\n\nUptime: {}\n".format(upt_header, pcuptime)
50
51
    # CPU information
52
    c = "="*15, " CPU ", "="*15
53
    cpu_header = "".join(c)
54
    cpumodel = cpuinfo.get_cpu_info()['brand']
55
    cpufreq = psutil.cpu_freq()
56
    cpu_info = "{}\n\n{}\nPhysical cores: {}\nTotal cores: {}\nMax Frequency: {:.2f} Mhz\nCurrent Frequency: {:.2f} Mhz\nCPU Usage: {}%\n".format(
57
                cpu_header, cpumodel, psutil.cpu_count(logical=False), psutil.cpu_count(logical=True), cpufreq.max, cpufreq.current, psutil.cpu_percent(percpu=False, interval=1))
58
59
    # RAM Information
60
    d = "="*15, " RAM ", "="*15
61
    ram_header = "".join(d)
62
    svmem = psutil.virtual_memory()
63
64
    ram_info = "{}\n\nTotal: {}\nAvailable: {} ({:.2f}%)\nUsed: {} ({:.2f}%)\n".format(
65
                ram_header, get_size(svmem.total), get_size(svmem.available), 100-svmem.percent, get_size(svmem.used), svmem.percent)
66
67
    # HDD Information
68
    e = "="*15, " HDD ", "="*15, "\n"
69
    hdd_header = "".join(e)
70
    hdd_info = hdd_header
71
    partitions = psutil.disk_partitions()
72
    for partition in partitions:
73
        try:
74
            partition_usage = psutil.disk_usage(partition.mountpoint)
75
        except PermissionError:
76
            continue
77
        disk_info = "\nDevice: {}\nFile system: {}\nTotal Size: {}\nUsed: {} ({:.2f}%)\nFree: {} ({:.2f}%)\n".format(
78
                    partition.device, partition.fstype, get_size(partition_usage.total), 
79
                    get_size(partition_usage.used), partition_usage.percent, get_size(partition_usage.free), 
80
                    100-partition_usage.percent)
81
        hdd_info = "{}{}".format(hdd_info, disk_info)
82
83
    # Combine everything
84
    server_status = "```\n{}\n{}\n{}\n{}\n{}\n```".format(sys_info, up, cpu_info, ram_info, hdd_info)
85
    update.message.reply_text(server_status, parse_mode="Markdown")
86