mirror of
https://github.com/retspen/webvirtcloud
synced 2024-10-31 19:44:16 +00:00
eliminate code repetition with class structure
This commit is contained in:
parent
a1eab70e2d
commit
e2a15d926c
1 changed files with 92 additions and 90 deletions
|
@ -138,30 +138,9 @@ def compute_graph(request, compute_id):
|
|||
:param compute_id:
|
||||
:return:
|
||||
"""
|
||||
compute = get_object_or_404(Compute, pk=compute_id)
|
||||
try:
|
||||
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
|
||||
comp_mgr = ComputeManager(compute_id)
|
||||
data = comp_mgr.compute_graph()
|
||||
|
||||
data = json.dumps(
|
||||
{
|
||||
"cpudata": cpu_usage["usage"],
|
||||
"memdata": mem_usage,
|
||||
"timeline": current_time,
|
||||
}
|
||||
)
|
||||
response = HttpResponse()
|
||||
response["Content-Type"] = "text/javascript"
|
||||
response.write(data)
|
||||
|
@ -177,17 +156,87 @@ def get_compute_disk_buses(request, compute_id, arch, machine, disk):
|
|||
:param disk:
|
||||
:return:
|
||||
"""
|
||||
data = dict()
|
||||
compute = get_object_or_404(Compute, pk=compute_id)
|
||||
try:
|
||||
conn = wvmConnect(
|
||||
compute.hostname,
|
||||
compute.login,
|
||||
compute.password,
|
||||
compute.type,
|
||||
comp_mgr = ComputeManager(compute_id)
|
||||
return HttpResponse(comp_mgr.get_disk_buses(arch, machine, disk))
|
||||
|
||||
|
||||
def get_compute_machine_types(request, compute_id, arch):
|
||||
"""
|
||||
:param request:
|
||||
: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 == "disk":
|
||||
|
@ -201,76 +250,29 @@ def get_compute_disk_buses(request, compute_id, arch, machine, disk):
|
|||
except libvirtError:
|
||||
pass
|
||||
|
||||
return HttpResponse(json.dumps(data))
|
||||
return json.dumps(data)
|
||||
|
||||
|
||||
def get_compute_machine_types(request, compute_id, arch):
|
||||
"""
|
||||
:param request:
|
||||
:param compute_id:
|
||||
:param arch:
|
||||
:return:
|
||||
"""
|
||||
data = dict()
|
||||
def compute_graph(self):
|
||||
try:
|
||||
compute = get_object_or_404(Compute, pk=compute_id)
|
||||
conn = wvmConnect(
|
||||
compute.hostname,
|
||||
compute.login,
|
||||
compute.password,
|
||||
compute.type,
|
||||
conn = wvmHostDetails(
|
||||
self.compute.hostname,
|
||||
self.compute.login,
|
||||
self.compute.password,
|
||||
self.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:
|
||||
pass
|
||||
cpu_usage = {"usage": 0}
|
||||
mem_usage = {"usage": 0}
|
||||
current_time = 0
|
||||
|
||||
return HttpResponse(json.dumps(data))
|
||||
|
||||
|
||||
def get_compute_video_models(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,
|
||||
return json.dumps(
|
||||
{
|
||||
"cpudata": cpu_usage["usage"],
|
||||
"memdata": mem_usage,
|
||||
"timeline": current_time,
|
||||
}
|
||||
)
|
||||
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))
|
||||
|
|
Loading…
Reference in a new issue