/
views.py
139 lines (113 loc) · 3.87 KB
/
views.py
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
# (c) 2016 Mark Hamilton, <mark.lee.hamilton@gmail.com>
#
# This file is part of testpool
#
# Testbed is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Testbed is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Testdb. If not, see <http://www.gnu.org/licenses/>.
"""
View profile information.
"""
import json
import logging
from django.shortcuts import render_to_response
from testpooldb import models
LOGGER = logging.getLogger("django.testpool")
# pylint: disable=R0902
# pylint: disable=R0903
class ProfileStats(object):
""" Provides individual profile stats used in the profile view. """
def __init__(self, profile):
"""Contruct a profile view. """
##
# pylint: disable=C0103
# The ID is needed for the JSON view.
self.id = profile.id
#
self.connection = profile.host.connection
self.name = profile.name
self.resource_max = profile.resource_max
self.vm_ready = 0
self.vm_reserved = 0
self.vm_pending = 0
self.vm_bad = 0
for item in models.VM.objects.filter(profile=profile):
if item.status == models.VM.RESERVED:
self.vm_reserved += 1
elif item.status == models.VM.PENDING:
self.vm_pending += 1
elif item.status == models.VM.READY:
self.vm_ready += 1
elif item.status == models.VM.BAD:
self.vm_bad += 1
def profile_list(_):
""" Summarize product information. """
LOGGER.debug("profile")
profiles = models.Profile.objects.all()
profiles = [ProfileStats(item) for item in profiles]
html_data = {"profiles": profiles}
return render_to_response("profile/list.html", html_data)
def detail(_, profile):
""" Provide profile details. """
LOGGER.debug("profile/detail/%s", profile)
profile1 = models.Profile.objects.get(name=profile)
vms = [item for item in models.VM.objects.filter(profile=profile1)]
html_data = {
"vms": vms,
"profile": profile1
}
return render_to_response("profile/detail.html", html_data)
def dashboard(_):
""" Provide summary of all profiles. """
LOGGER.debug("index")
categories = ["Available", "Used"]
dataset = models.Profile.objects.all().order_by("name")
series = []
x_axis = []
max_tick = 10 # Just some value to help render the graph.
x_axis = [item.name for item in dataset]
value = {
"name": "Available",
"color": "green",
"data": [item.resource_available() for item in dataset]
}
series.append(value)
value = {
"name": "Used",
"color": "red",
"data": [item.resource_used() for item in dataset]
}
series.append(value)
if dataset:
max_tick = max([item.resource_max for item in dataset])
chart = {
"chart": {
'type': "bar",
"animation": "false",
},
"title": {'text': 'Available Resources by Profile'},
"xAxis": {
"categories": x_axis
},
"yAxis": {
"min": 0,
"max": max_tick,
"allowDecimals": "false",
"endOnTick": "true",
"title": {"text": "Profile Usage"},
},
"legend": {"reversed": "true"},
"plotOptions": {"series": {"stacking": "normal"}},
"series": series
}
dump = json.dumps(chart, indent=2)
return render_to_response('profile/index.html', {'chart': dump})