Revision 4e03ba30 snf-astakos-app/astakos/im/static/im/js/usage.js
b/snf-astakos-app/astakos/im/static/im/js/usage.js | ||
---|---|---|
39 | 39 |
return sign + (j ? intPart.substr(0, j) + thousandsSep : '') + intPart.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + thousandsSep) + (decimals ? decPoint + Math.abs(number - intPart).toFixed(decimals).slice(2) : ''); |
40 | 40 |
}; |
41 | 41 |
|
42 |
function UsageClient(settings) { |
|
42 |
|
|
43 |
DO_LOG = false |
|
44 |
|
|
45 |
LOG = DO_LOG ? _.bind(console.log, console) : function() {}; |
|
46 |
WARN = DO_LOG ? _.bind(console.warn, console) : function() {}; |
|
47 |
|
|
48 |
var default_usage_cls_map = { |
|
49 |
0: 'green', |
|
50 |
33: 'yellow', |
|
51 |
66: 'red' |
|
52 |
} |
|
53 |
|
|
54 |
function UsageView(settings) { |
|
43 | 55 |
this.settings = settings; |
44 | 56 |
this.url = this.settings.url; |
45 | 57 |
this.container = $(this.settings.container); |
58 |
this.meta = this.settings.meta; |
|
59 |
this.groups = this.settings.groups; |
|
60 |
this.el = {}; |
|
61 |
this.usage_cls_map = this.settings.usage_cls_map || default_usage_cls_map; |
|
62 |
this.initialize(); |
|
46 | 63 |
} |
47 | 64 |
|
48 |
UsageClient.prototype.load = function() { |
|
49 |
var self = this; |
|
50 |
$.ajax(this.url, { |
|
51 |
'success': function(data) { |
|
52 |
self.update(data); |
|
53 |
} |
|
54 |
}) |
|
55 |
} |
|
56 | 65 |
|
57 |
function setText(el, valueFrom, valueTo, direction, modifier) { |
|
58 |
//valueTo = parseInt(valueTo); |
|
59 |
//text = valueFrom; |
|
66 |
_.extend(UsageView.prototype, { |
|
67 |
tpls: { |
|
68 |
'main': '<div class="stats clearfix"><ul></ul></div>', |
|
69 |
'quotas': "#quotaTpl" |
|
70 |
}, |
|
60 | 71 |
|
61 |
//if (valueFrom >= valueTo) { |
|
62 |
//valueFrom = valueTo; |
|
63 |
//} |
|
64 |
|
|
65 |
var text = valueTo; |
|
66 |
if (modifier) { |
|
67 |
text = modifier(text); |
|
68 |
} |
|
69 |
el.html(text); |
|
72 |
initialize: function() { |
|
73 |
LOG("Initializing UsageView", this.settings); |
|
74 |
this.initResources(); |
|
70 | 75 |
|
71 |
//if (valueTo > valueFrom) { |
|
72 |
//window.setTimeout(function() { |
|
73 |
//setText(el, parseInt(valueFrom) + step, parseInt(valueTo)); |
|
74 |
//}, 10) |
|
75 |
//} |
|
76 |
} |
|
76 |
// initial usage set ???? |
|
77 |
this.quotas = {}; |
|
78 |
if (this.settings.quotas && _.keys(this.settings.quotas).length > 0) { |
|
79 |
this.setQuotas(this.settings.quotas); |
|
80 |
} |
|
81 |
this.initLayout(); |
|
82 |
this.updateQuotas(); |
|
83 |
}, |
|
84 |
|
|
85 |
$: function(selector) { |
|
86 |
return this.container; |
|
87 |
}, |
|
88 |
|
|
89 |
render: function(tpl, params) { |
|
90 |
LOG("Rendering", tpl, params); |
|
91 |
var tpl = this.tpls[tpl]; |
|
92 |
if (/^[#\.]/.exec(tpl)) { |
|
93 |
tpl = $(tpl).html(); |
|
94 |
} |
|
95 |
var rendered = Mustache.render(tpl, params); |
|
96 |
return $(rendered); |
|
97 |
}, |
|
98 |
|
|
99 |
initLayout: function() { |
|
100 |
LOG("Initializing layout"); |
|
101 |
this.el.main = this.render('main'); |
|
102 |
this.container.append(this.el.main); |
|
103 |
var ul = this.container.find("ul"); |
|
104 |
this.el.list = this.render('quotas', { |
|
105 |
'resources':this.resources_ordered |
|
106 |
}); |
|
107 |
ul.append(this.el.list); |
|
108 |
}, |
|
109 |
|
|
110 |
initResources: function() { |
|
111 |
var ordered = this.meta.resources_order; |
|
112 |
var resources = {}; |
|
113 |
var resources_ordered = []; |
|
114 |
|
|
115 |
_.each(this.meta.resources, function(group, index) { |
|
116 |
_.each(group[1], function(resource, rindex) { |
|
117 |
var resource_index = ordered.length; |
|
118 |
if (!_.contains(ordered, resource.name)) { |
|
119 |
ordered.push(resource.name); |
|
120 |
} else { |
|
121 |
resource_index = ordered.indexOf(resource.name); |
|
122 |
} |
|
123 |
resource.index = resource_index; |
|
124 |
resource.resource_name = resource.name.split(".")[1]; |
|
125 |
resources[resource.name] = resource; |
|
126 |
}) |
|
127 |
}); |
|
128 |
|
|
129 |
resources_ordered = _.map(ordered, function(rk) { return resources[rk] }); |
|
130 |
this.resources = resources; |
|
131 |
this.resources_ordered = resources_ordered; |
|
132 |
|
|
133 |
LOG("Resources initialized", this.resources_ordered, this.resources); |
|
134 |
}, |
|
135 |
|
|
136 |
updateLayout: function() { |
|
137 |
LOG("Updating layout", this.quotas); |
|
138 |
var self = this; |
|
139 |
_.each(this.quotas, function(value, key) { |
|
140 |
var usage = self.getUsage(key); |
|
141 |
var el = self.$().find("li[data-resource='"+key+"']"); |
|
142 |
self.updateResourceElement(el, usage); |
|
143 |
}) |
|
144 |
}, |
|
145 |
|
|
146 |
updateResourceElement: function(el, usage) { |
|
147 |
el.find(".currValue").text(usage.curr); |
|
148 |
el.find(".maxValue").text(usage.max); |
|
149 |
el.find(".bar span").css({width:usage.perc+"%"}); |
|
150 |
el.find(".bar .value").text(usage.perc+"%"); |
|
151 |
var left = usage.label_left == 'auto' ? |
|
152 |
usage.label_left : usage.label_left + "%"; |
|
153 |
el.find(".bar .value").css({left:left}); |
|
154 |
el.find(".bar .value").css({color:usage.label_color}); |
|
155 |
el.removeClass("green yellow red"); |
|
156 |
el.addClass(usage.cls); |
|
157 |
}, |
|
158 |
|
|
159 |
getUsage: function(resource_name) { |
|
160 |
var resource = this.quotas[resource_name]; |
|
161 |
var resource_meta = this.resources[resource_name]; |
|
162 |
var value, limit, percentage; |
|
163 |
|
|
164 |
limit = resource.limit; |
|
165 |
value = resource.limit - resource.available; |
|
166 |
if (value < 0 ) { value = 0 } |
|
167 |
|
|
168 |
percentage = (value/limit) * 100; |
|
169 |
if (value == 0) { percentage = 0 } |
|
170 |
if (resource.available <= 0) { |
|
171 |
percentage = 100; |
|
172 |
} |
|
77 | 173 |
|
78 |
UsageClient.prototype.updateEntry = function(key, data) { |
|
174 |
if (resource_meta.unit == 'bytes') { |
|
175 |
value = humanize.filesize(value); |
|
176 |
limit = humanize.filesize(limit); |
|
177 |
} |
|
79 | 178 |
|
80 |
var entry = $('li[data-resourcekey=\''+key+'\']'); |
|
81 |
var currentEl = entry.find("span.currValue"); |
|
82 |
var maxEl = entry.find("span.maxValue"); |
|
83 |
var ratioEl = entry.find("div.bar"); |
|
84 |
var barEl = entry.find("div.bar span"); |
|
85 |
var percentageEl = ratioEl.find("em"); |
|
86 |
var units = entry.data("units"); |
|
87 |
var infoEl = entry.find(".info"); |
|
179 |
var cls = 'green'; |
|
180 |
_.each(this.usage_cls_map, function(ucls, u){ |
|
181 |
if (percentage >= u) { |
|
182 |
cls = ucls |
|
183 |
} |
|
184 |
}) |
|
88 | 185 |
|
89 |
var current = data.currValue; |
|
90 |
var max = data.maxValue; |
|
186 |
var label_left = percentage >= 30 ? percentage - 17 : 'auto'; |
|
187 |
var label_col = label_left == 'auto' ? 'inherit' : '#fff'; |
|
188 |
percentage = humanize.numberFormat(percentage, 0); |
|
189 |
qdata = {'curr': value, 'max': limit, 'perc': percentage, 'cls': cls, |
|
190 |
'label_left': label_left, 'label_color': label_col} |
|
191 |
_.extend(qdata, resource); |
|
192 |
console.log(resource_name, value, resource); |
|
193 |
return qdata |
|
194 |
}, |
|
195 |
|
|
196 |
setQuotas: function(data) { |
|
197 |
LOG("Set quotas", data); |
|
198 |
var self = this; |
|
199 |
this.quotas = data; |
|
200 |
_.each(this.quotas, function(v, k) { |
|
201 |
var r = self.resources[k]; |
|
202 |
var usage = self.getUsage(k); |
|
203 |
r.usage = usage; |
|
204 |
self.resources[k].usage = usage; |
|
205 |
self.resources_ordered[r.index].usage = usage; |
|
206 |
}); |
|
207 |
}, |
|
208 |
|
|
209 |
_ajaxOptions: function() { |
|
210 |
var token = $.cookie(this.settings.cookie_name).split("|")[1]; |
|
211 |
return { |
|
212 |
'url': this.url, |
|
213 |
'headers': { |
|
214 |
'X-Auth-Token': token |
|
215 |
}, |
|
216 |
} |
|
217 |
}, |
|
91 | 218 |
|
92 |
modifier = function(v) { return v; } |
|
93 |
if (units == 'bytes') { |
|
94 |
modifier = humanize.filesize; |
|
219 |
updateQuotas: function() { |
|
220 |
LOG("Updating quotas"); |
|
221 |
var self = this; |
|
222 |
this.getQuotas(function(data){ |
|
223 |
self.setQuotas(data.system); |
|
224 |
self.updateLayout(); |
|
225 |
}) |
|
226 |
}, |
|
227 |
|
|
228 |
getQuotas: function(callback) { |
|
229 |
var options = this._ajaxOptions(); |
|
230 |
options.success = callback; |
|
231 |
LOG("Calling quotas API", options); |
|
232 |
$.ajax(options); |
|
95 | 233 |
} |
96 |
|
|
97 |
setText(maxEl, infoEl.data('maxvalue'), max, infoEl.data('maxvalue') > max, |
|
98 |
modifier); |
|
99 |
setText(currentEl, infoEl.data('currvalue'), current, |
|
100 |
infoEl.data('currvalue') > current, modifier); |
|
101 | 234 |
|
102 |
var percentage = humanize.numberFormat(data.ratio, 1); |
|
103 |
setText(percentageEl, percentageEl.data('value'), |
|
104 |
percentage, percentageEl.data('value') > percentage, |
|
105 |
function(v) { return v + '% '}); |
|
106 |
|
|
107 |
var width = data.ratio; |
|
108 |
if (width > 100) { width = 100; } |
|
109 |
if (width < 0) { width = 0; } |
|
110 |
|
|
111 |
width = humanize.numberFormat(width, 1); |
|
112 |
barEl.css({'width': width + '%'}); |
|
113 |
width = width*342/100; |
|
114 |
|
|
115 |
if (percentage > 22) { |
|
116 |
percentageEl.addClass("hovered"); |
|
117 |
var left = width-percentageEl.width() -10; |
|
118 |
percentageEl.css({'left': left+"px", 'color':'#fff'}); |
|
119 |
} else { |
|
120 |
percentageEl.removeClass("hovered"); |
|
121 |
percentageEl.css({'left': width+"px", 'color':'#222'}); |
|
122 |
} |
|
123 |
percentageEl.data('value', percentage); |
|
124 |
|
|
125 |
entry.removeClass("red green yellow"); |
|
126 |
entry.addClass(data.load_class); |
|
127 |
|
|
128 |
entry.find(".info").data("currvalue", data.currValue); |
|
129 |
entry.find(".info").data("maxvalue", data.maxValue); |
|
130 |
} |
|
131 |
|
|
132 |
UsageClient.prototype.update = function(data) { |
|
133 |
var usage = {}, self = this; |
|
134 |
_.each(data, function(e) { usage[e.name] = e}); |
|
135 |
|
|
136 |
_.each(usage, function(data, key) { |
|
137 |
self.updateEntry(key, data); |
|
138 |
}); |
|
139 |
} |
|
235 |
}); |
|
140 | 236 |
|
141 |
window.UsageClient = UsageClient;
|
|
237 |
window.UsageView = UsageView;
|
|
142 | 238 |
})(); |
Also available in: Unified diff