2010-07-12 22:04:42 +08:00
|
|
|
$(document).ready(function() {
|
2010-07-13 20:03:36 +08:00
|
|
|
var url = this.location.toString();
|
|
|
|
url = url.indexOf('#') == -1 ? '#overview' : url;
|
|
|
|
apply_url(url);
|
|
|
|
timer = setInterval('update()', 5000);
|
2010-07-12 22:04:42 +08:00
|
|
|
});
|
|
|
|
|
2010-07-13 20:03:36 +08:00
|
|
|
var current_page;
|
2010-07-13 00:11:42 +08:00
|
|
|
var timer;
|
|
|
|
|
2010-07-13 20:03:36 +08:00
|
|
|
function apply_url(url) {
|
|
|
|
current_page = url.split('#', 2)[1];
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2010-07-12 22:04:42 +08:00
|
|
|
function update() {
|
2010-07-14 01:06:57 +08:00
|
|
|
with_req('/json/' + current_page, function(text) {
|
2010-07-13 00:31:16 +08:00
|
|
|
var json = JSON.parse(text);
|
2010-07-13 20:03:36 +08:00
|
|
|
var template = eval('template_' + current_page + '();');
|
|
|
|
var html = format(template, json);
|
2010-07-13 00:31:16 +08:00
|
|
|
replace_content('main', html);
|
|
|
|
update_status('ok', json['datetime']);
|
2010-07-13 20:03:36 +08:00
|
|
|
$('a').removeClass('selected').unbind().click(function() {
|
|
|
|
apply_url(this.href);
|
|
|
|
});
|
|
|
|
$('a[href="#' + current_page + '"]').addClass('selected');
|
2010-07-12 22:04:42 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2010-07-13 00:31:16 +08:00
|
|
|
function replace_content(id, html) {
|
|
|
|
$("#" + id).empty();
|
|
|
|
$(html).appendTo("#" + id);
|
|
|
|
}
|
|
|
|
|
2010-07-12 22:04:42 +08:00
|
|
|
function format(template, json) {
|
|
|
|
try {
|
2010-07-13 00:11:42 +08:00
|
|
|
var tmpl = jsontemplate.Template(template,
|
|
|
|
{more_formatters: more_formatters});
|
|
|
|
return tmpl.expand(json);
|
2010-07-12 22:04:42 +08:00
|
|
|
} catch (err) {
|
|
|
|
alert(err['name'] + ": " + err['message']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-13 00:11:42 +08:00
|
|
|
function more_formatters(name) {
|
|
|
|
if (name == 'bytes')
|
|
|
|
return format_bytes;
|
2010-07-16 23:35:58 +08:00
|
|
|
else if (name == 'boolean')
|
|
|
|
return format_boolean;
|
|
|
|
else if (name == 'exclusive')
|
|
|
|
return format_exclusive;
|
2010-07-13 00:11:42 +08:00
|
|
|
else
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function format_bytes(bytes) {
|
|
|
|
function f(n, p) {
|
|
|
|
if (n > 1024) return f(n / 1024, p + 1);
|
|
|
|
else return [n, p];
|
|
|
|
}
|
|
|
|
|
|
|
|
[num, power] = f(bytes, 0);
|
|
|
|
var powers = ['B', 'kB', 'MB', 'GB', 'TB'];
|
|
|
|
return (power == 0 ? num : num.toFixed(1)) + powers[power];
|
|
|
|
}
|
|
|
|
|
2010-07-16 23:35:58 +08:00
|
|
|
function format_boolean(b) {
|
|
|
|
return b ? "●" : "○";
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO I think this proves jsontemplate has got to go
|
|
|
|
function format_exclusive(s) {
|
|
|
|
return format_boolean(s.length > 0);
|
|
|
|
}
|
|
|
|
|
2010-07-13 00:31:16 +08:00
|
|
|
function update_status(status, datetime) {
|
|
|
|
var text;
|
|
|
|
if (status == 'ok')
|
|
|
|
text = "Last update: " + datetime;
|
|
|
|
else if (status == 'timeout')
|
|
|
|
text = "Warning: server reported busy at " + datetime;
|
|
|
|
else if (status == 'error')
|
|
|
|
text = "Error: could not connect to server at " + datetime;
|
|
|
|
|
|
|
|
var html = format('<p class="status-{status}">{text}</p>', {status: status,
|
|
|
|
text: text});
|
|
|
|
replace_content('status', html);
|
|
|
|
}
|
|
|
|
|
2010-07-12 22:04:42 +08:00
|
|
|
function with_req(path, fun) {
|
|
|
|
var json;
|
|
|
|
var req = new XMLHttpRequest();
|
|
|
|
req.open( "GET", path, true );
|
|
|
|
req.onreadystatechange = function () {
|
|
|
|
if (req.readyState == 4) {
|
|
|
|
if (req.status == 200) {
|
|
|
|
fun(req.responseText);
|
|
|
|
}
|
2010-07-13 00:31:16 +08:00
|
|
|
else if (req.status == 408) {
|
|
|
|
update_status('timeout', new Date());
|
|
|
|
}
|
2010-07-13 00:11:42 +08:00
|
|
|
else if (req.status == 0) {
|
2010-07-13 00:31:16 +08:00
|
|
|
update_status('error', new Date());
|
2010-07-13 00:11:42 +08:00
|
|
|
}
|
2010-07-12 22:04:42 +08:00
|
|
|
else {
|
|
|
|
alert("Got response code " + req.status);
|
2010-07-13 00:11:42 +08:00
|
|
|
clearInterval(timer);
|
2010-07-12 22:04:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
req.send(null);
|
|
|
|
}
|
2010-07-13 20:03:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
function debug(str) {
|
|
|
|
$('<p>' + str + '</p>').appendTo('#debug');
|
|
|
|
}
|