updated get_ram_and_disk method and removed fstring

This commit is contained in:
Shailaja Kumari 2024-06-27 12:14:37 +05:30
parent e42f910a50
commit e545bcefa4
Signed by: shailaja
GPG Key ID: 2B9455CAFBC4D75A

View File

@ -96,12 +96,12 @@ class ServerData:
with open('/proc/meminfo', 'r') as f: with open('/proc/meminfo', 'r') as f:
meminfo = f.read() meminfo = f.read()
ram = int([x for x in meminfo.split('\n') if 'MemTotal' in x][0].split()[1]) // 1024 ram = int([x for x in meminfo.split('\n') if 'MemTotal' in x][0].split()[1]) // 1024
# Disk space information
# Disk space information
disk = 0 disk = 0
for device in os.listdir('/sys/block'): for device in os.listdir('/sys/block'):
size_path = f'/sys/block/{device}/size' device_path = '/sys/block/{}/device'.format(device)
if os.path.exists(size_path) and os.access(size_path, os.R_OK): size_path = '/sys/block/{}/size'.format(device)
if os.path.islink(device_path):
try: try:
with open(size_path, 'r') as f: with open(size_path, 'r') as f:
size = int(f.read().strip()) size = int(f.read().strip())
@ -110,7 +110,7 @@ class ServerData:
pass # Skip the device if any exception occurs pass # Skip the device if any exception occurs
disk = disk * 512 // (1024**3) # convert to GB disk = disk * 512 // (1024**3) # convert to GB
logging.info(f"RAM: {ram}MB, Disk: {disk}GB") logging.info("RAM: {}MB, Disk: {}GB".format(ram, disk))
return ram, disk return ram, disk
def get_cpu_count(self): def get_cpu_count(self):
@ -124,12 +124,12 @@ class ServerData:
with open('/proc/cpuinfo', 'r') as f: with open('/proc/cpuinfo', 'r') as f:
cpuinfo = f.read() cpuinfo = f.read()
cpu_count = cpuinfo.count('processor') cpu_count = cpuinfo.count('processor')
logging.info(f"CPU Count: {cpu_count}") logging.info("CPU Count: {}".format(cpu_count))
return cpu_count return cpu_count
def get_bandwidth(self): def get_bandwidth(self):
bandwidth = 2000 bandwidth = 2000
logging.info(f"Bandwidth: {bandwidth}") logging.info("Bandwidth: {}".format(bandwidth))
return bandwidth return bandwidth
def get_public_ip(self): def get_public_ip(self):
@ -137,12 +137,12 @@ class ServerData:
response = urllib.request.urlopen('https://api.ipify.org') response = urllib.request.urlopen('https://api.ipify.org')
return response.read().decode() return response.read().decode()
except Exception as e: except Exception as e:
logging.error(f"Failed to get public IP: {e}") logging.error("Failed to get public IP: {}".format(e))
return '127.0.0.1' return '127.0.0.1'
def get_os(self): def get_os(self):
os_id = 27 os_id = 27
logging.info(f"OS ID: {os_id}") logging.info("OS ID: {}".format(os_id))
return os_id return os_id
def create_post_data(self): def create_post_data(self):
@ -235,7 +235,7 @@ class ServerManager:
else: else:
return response.read().decode() return response.read().decode()
except urllib.error.HTTPError as e: except urllib.error.HTTPError as e:
logging.error(f"Request failed with {e}") logging.error("Request failed with {}".format(e))
raise raise
def get_existing_servers(self): def get_existing_servers(self):
@ -249,7 +249,7 @@ class ServerManager:
return self.send_request('POST', '/api/notes', post_data) return self.send_request('POST', '/api/notes', post_data)
def update_note(self, post_data, service_id): def update_note(self, post_data, service_id):
logging.info(f"Updating note with id {service_id}...") logging.info("Updating note with id {}...".format(service_id))
return self.send_request('PUT', '/api/notes/' + str(service_id), post_data) return self.send_request('PUT', '/api/notes/' + str(service_id), post_data)
def create_server(self, post_data): def create_server(self, post_data):
@ -260,7 +260,7 @@ class ServerManager:
# remove following keys from post_data # remove following keys from post_data
for key in NON_UPDATABLE_KEYS: for key in NON_UPDATABLE_KEYS:
post_data.pop(key, None) post_data.pop(key, None)
logging.info(f"Updating server with id {server_id}...") logging.info("Updating server with id {}...".format(server_id))
return self.send_request('PUT', '/api/servers/' + str(server_id), post_data) return self.send_request('PUT', '/api/servers/' + str(server_id), post_data)
def existing_server_id(self, post_data): def existing_server_id(self, post_data):