1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-25 15:45:23 +00:00

eliminate code repetition with class structure

This commit is contained in:
catborise 2022-11-03 10:20:16 +03:00
parent a1eab70e2d
commit e2a15d926c

View file

@ -138,30 +138,9 @@ def compute_graph(request, compute_id):
:param compute_id: :param compute_id:
:return: :return:
""" """
compute = get_object_or_404(Compute, pk=compute_id) comp_mgr = ComputeManager(compute_id)
try: data = comp_mgr.compute_graph()
conn = wvmHostDetails(
compute.hostname,
compute.login,
compute.password,
compute.type,
)
current_time = timezone.now().strftime("%H:%M:%S")
cpu_usage = conn.get_cpu_usage()
mem_usage = conn.get_memory_usage()
conn.close()
except libvirtError:
cpu_usage = {"usage": 0}
mem_usage = {"usage": 0}
current_time = 0
data = json.dumps(
{
"cpudata": cpu_usage["usage"],
"memdata": mem_usage,
"timeline": current_time,
}
)
response = HttpResponse() response = HttpResponse()
response["Content-Type"] = "text/javascript" response["Content-Type"] = "text/javascript"
response.write(data) response.write(data)
@ -177,17 +156,87 @@ def get_compute_disk_buses(request, compute_id, arch, machine, disk):
:param disk: :param disk:
:return: :return:
""" """
data = dict() comp_mgr = ComputeManager(compute_id)
compute = get_object_or_404(Compute, pk=compute_id) return HttpResponse(comp_mgr.get_disk_buses(arch, machine, disk))
try:
conn = wvmConnect(
compute.hostname, def get_compute_machine_types(request, compute_id, arch):
compute.login, """
compute.password, :param request:
compute.type, :param compute_id:
:param arch:
:return:
"""
comp_mgr = ComputeManager(compute_id)
return HttpResponse(comp_mgr.get_machine_types(arch))
def get_compute_video_models(request, compute_id, arch, machine):
"""
:param request:
:param compute_id:
:param arch:
:param machine:
:return:
"""
comp_mgr = ComputeManager(compute_id)
return HttpResponse(comp_mgr.get_video_models(arch, machine))
def get_dom_capabilities(request, compute_id, arch, machine):
"""
:param request:
:param compute_id:
:param arch:
:param machine:
:return:
"""
comp_mgr = ComputeManager(compute_id)
return HttpResponse(comp_mgr.get_dom_capabilities(arch, machine))
class ComputeManager:
def __init__(self, compute_id):
self.compute = get_object_or_404(Compute, pk=compute_id)
self.conn = wvmConnect(
self.compute.hostname,
self.compute.login,
self.compute.password,
self.compute.type,
) )
disk_device_types = conn.get_disk_device_types(arch, machine) def get_video_models(self, arch, machine):
data = dict()
try:
data["videos"] = self.conn.get_video_models(arch, machine)
except libvirtError:
pass
return json.dumps(data)
def get_dom_capabilities(self, arch, machine):
data = dict()
try:
data["videos"] = self.conn.get_disk_device_types(arch, machine)
data["bus"] = self.conn.get_disk_device_types(arch, machine)
except libvirtError:
pass
return json.dumps(data)
def get_machine_types(self, arch):
data = dict()
try:
data["machines"] = self.conn.get_machine_types(arch)
except libvirtError:
pass
return json.dumps(data)
def get_disk_buses(self, arch, machine, disk):
data = dict()
try:
disk_device_types = self.conn.get_disk_device_types(arch, machine)
if disk in disk_device_types: if disk in disk_device_types:
if disk == "disk": if disk == "disk":
@ -201,76 +250,29 @@ def get_compute_disk_buses(request, compute_id, arch, machine, disk):
except libvirtError: except libvirtError:
pass pass
return HttpResponse(json.dumps(data)) return json.dumps(data)
def compute_graph(self):
def get_compute_machine_types(request, compute_id, arch):
"""
:param request:
:param compute_id:
:param arch:
:return:
"""
data = dict()
try: try:
compute = get_object_or_404(Compute, pk=compute_id) conn = wvmHostDetails(
conn = wvmConnect( self.compute.hostname,
compute.hostname, self.compute.login,
compute.login, self.compute.password,
compute.password, self.compute.type,
compute.type,
) )
data["machines"] = conn.get_machine_types(arch) current_time = timezone.now().strftime("%H:%M:%S")
cpu_usage = conn.get_cpu_usage()
mem_usage = conn.get_memory_usage()
conn.close()
except libvirtError: except libvirtError:
pass cpu_usage = {"usage": 0}
mem_usage = {"usage": 0}
current_time = 0
return HttpResponse(json.dumps(data)) return json.dumps(
{
"cpudata": cpu_usage["usage"],
def get_compute_video_models(request, compute_id, arch, machine): "memdata": mem_usage,
""" "timeline": current_time,
:param request: }
:param compute_id:
:param arch:
:param machine:
:return:
"""
data = dict()
try:
compute = get_object_or_404(Compute, pk=compute_id)
conn = wvmConnect(
compute.hostname,
compute.login,
compute.password,
compute.type,
) )
data["videos"] = conn.get_video_models(arch, machine)
except libvirtError:
pass
return HttpResponse(json.dumps(data))
def get_dom_capabilities(request, compute_id, arch, machine):
"""
:param request:
:param compute_id:
:param arch:
:param machine:
:return:
"""
data = dict()
try:
compute = get_object_or_404(Compute, pk=compute_id)
conn = wvmConnect(
compute.hostname,
compute.login,
compute.password,
compute.type,
)
data["videos"] = conn.get_disk_device_types(arch, machine)
data["bus"] = conn.get_disk_device_types(arch, machine)
except libvirtError:
pass
return HttpResponse(json.dumps(data))