-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
221 lines (182 loc) · 7.53 KB
/
helpers.py
File metadata and controls
221 lines (182 loc) · 7.53 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import pandas as pd
import os
import json
import re
# import numpy as np
class DataHost:
@staticmethod
def get_vcpus_used(host):
allocation_file = open('data/allocation.txt', 'r')
vcpus_used = 0
for line in allocation_file:
parts = line.strip().split()
if len(parts) >= 6 and parts[1] == host:
vcpus_used = int(parts[5])
break
allocation_file.close()
return vcpus_used
@staticmethod
def get_vcpus_ratio(host):
vcpus_ratio = 0.0
ratio_file = open('data/ratio.txt', 'r')
for line in ratio_file:
parts = line.strip().split(', ')
if len(parts) == 3 and parts[0] == host:
vcpus_ratio = float(parts[1])
ratio_file.close()
return vcpus_ratio
class Formatter:
@staticmethod
def format_ram(ram):
if ram >= 1024:
return str(ram // 1024) + 'G'
else:
return str(ram) + 'M'
@staticmethod
def format_public(is_public):
return 'Yes' if is_public else 'No'
@staticmethod
def format_swap(swap):
if swap >= 1024:
swap = int(swap)
return str(swap // 1024) + 'G'
elif swap < 1024:
swap = int(swap)
return str(swap) + 'M'
else:
return '-'
@staticmethod
def format_properties(properties):
return '-' if pd.isna(properties) else properties
def calculate_total_capacity_allocation_ratio(ratio, formatted_data):
total_capacity = 0
total_capacity_memory = 0
# Loop melalui data yang telah diformat sebelumnya
for item in formatted_data:
vcpus_ratio = item['vCPUs Ratio']
compute_name = item['Compute Name']
# Cek apakah host/compute memiliki rasio yang sesuai
if vcpus_ratio == ratio:
vcpus_capacity = item['VCPUs']['Capacity']
total_capacity += vcpus_capacity
memory_capacity = item['Memory']['Capacity']
total_capacity_memory += memory_capacity
return total_capacity, total_capacity_memory
def calculate_total_usage_allocation_ratio(ratio, formatted_data):
total_usage = 0
total_usage_memory = 0
# Loop melalui data yang telah diformat sebelumnya
for item in formatted_data:
vcpus_ratio = item['vCPUs Ratio']
compute_name = item['Compute Name']
# Cek apakah host/compute memiliki rasio yang sesuai
if vcpus_ratio == ratio:
vcpus_usage = item['VCPUs']['Used']
total_usage += vcpus_usage
memory_usage = item['Memory']['Used']
total_usage_memory += memory_usage
return total_usage, total_usage_memory
def calculate_total_available_allocation_ratio(ratio, formatted_data):
total_capacity, total_capacity_memory = calculate_total_capacity_allocation_ratio(ratio, formatted_data)
total_usage, total_usage_memory = calculate_total_usage_allocation_ratio(ratio, formatted_data)
total_available = total_capacity - total_usage
total_available_memory = total_capacity_memory - total_usage_memory
return total_available, total_available_memory
# Fungsi untuk membaca data dari reserved.json
def read_reserved_data():
reserved_data_file = 'data/reserved.json'
reserved_data = {}
if os.path.exists(reserved_data_file):
with open(reserved_data_file, 'r') as f:
try:
reserved_data = json.load(f)
except json.decoder.JSONDecodeError:
# Tangani jika file JSON kosong atau tidak valid
reserved_data = {}
return reserved_data
# Fungsi untuk menghitung total reserved untuk suatu rasio alokasi
def calculate_total_reserved_allocation_ratio(ratio, formatted_data, reserved_data):
total_reserved = 0
total_reserved_memory = 0
for item in formatted_data:
vcpus_ratio = item['vCPUs Ratio']
compute_name = item['Compute Name']
if vcpus_ratio == ratio:
# Cek apakah host/compute memiliki rasio yang sesuai
if compute_name in reserved_data:
reserved_item = reserved_data[compute_name]
if reserved_item['CPU']:
total_reserved += int(reserved_item['CPU'])
if reserved_item['RAM']:
total_reserved_memory += int(reserved_item['RAM']) * 1024 # Konversi dari GB ke MB
return total_reserved, total_reserved_memory
def calculate_total_maintenance_allocation_ratio(ratio, formatted_data, reserved_data):
total_maintenance = 0
total_maintenance_memory = 0
for item in formatted_data:
vcpus_ratio = item['vCPUs Ratio']
compute_name = item['Compute Name']
if vcpus_ratio == ratio:
# Cek apakah host/compute memiliki rasio yang sesuai
if compute_name in reserved_data:
reserved_item = reserved_data[compute_name]
kebutuhan = reserved_item.get('Kebutuhan', '')
# Periksa apakah ada kebutuhan "Backup for maintenance" (case insensitive)
if "backup for maintenance" in kebutuhan.lower():
vcpus_capacity = item['VCPUs']['Capacity']
vcpus_used = item['VCPUs']['Used']
available_vcpus = vcpus_capacity - vcpus_used
total_maintenance += available_vcpus
memory_capacity = item['Memory']['Capacity']
memory_used = item['Memory']['Used']
available_memory = memory_capacity - memory_used
total_maintenance_memory += available_memory
return total_maintenance, total_maintenance_memory
def extract_cephdf_data():
# Initialize variables to store the extracted values
total = avail = raw_used = raw_used_percentage = "N/A"
# Open the file and read it line by line
with open("data/cephdf.txt", "r") as file:
lines = file.readlines()
# Initialize a section flag
in_raw_storage_section = False
for line in lines:
# Check if the line starts with "RAW STORAGE" to identify the section
if line.startswith("--- RAW STORAGE ---"):
in_raw_storage_section = True
continue
elif line.startswith("--- POOLS ---"):
in_raw_storage_section = False
continue
# Process lines in the "RAW STORAGE" section
if in_raw_storage_section:
parts = re.split(r'\s{2,}', line.strip())
if len(parts) == 6 and parts[0] == "TOTAL":
total = parts[1]
avail = parts[2]
raw_used = parts[3]
raw_used_percentage = parts[5]
# print(total, avail, raw_used, raw_used_percentage)
# Return the extracted values as a dictionary
return {
"Total": total,
"Avail": avail,
"Raw Used": raw_used,
"%Raw Used": raw_used_percentage
}
def update_or_add_reserved_data(data, compute_name, cpu, ram, kebutuhan):
if compute_name not in data:
data[compute_name] = {}
if cpu:
data[compute_name]['CPU'] = cpu
if ram:
data[compute_name]['RAM'] = ram
if kebutuhan:
data[compute_name]['Kebutuhan'] = kebutuhan
def get_sorted_computes():
with open('data/ratio.txt', 'r') as file:
lines = file.readlines()
compute_names = [line.split(',')[0].strip() for line in lines]
sorted_computes = sorted(compute_names)
# print(sorted_computes)
return sorted_computes