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-28 01:04:48 +08:00
|
|
|
var [reqs, template] = dispatch(current_page);
|
|
|
|
|
|
|
|
with_req('/json' + reqs[0], function(text) {
|
|
|
|
var json = jQuery.parseJSON(text);
|
2010-07-24 00:24:01 +08:00
|
|
|
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-28 01:04:48 +08:00
|
|
|
function dispatch(page) {
|
|
|
|
if (page == 'consuming-channels') {
|
|
|
|
return [['/stats/channel_queue_stats/?group_by=channel'],
|
|
|
|
'consuming-channels'];
|
|
|
|
}
|
2010-08-06 20:59:03 +08:00
|
|
|
else if (page == 'connection') {
|
2010-08-06 00:01:01 +08:00
|
|
|
return [['/connection/'], 'connections'];
|
|
|
|
}
|
2010-08-06 20:59:03 +08:00
|
|
|
else if (page == 'queue') {
|
|
|
|
return [['/queue/'], 'queues'];
|
|
|
|
}
|
2010-07-28 01:04:48 +08:00
|
|
|
else {
|
|
|
|
return [['/' + page], current_page.split('/', 1)];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-17 00:38:06 +08:00
|
|
|
var tmpl = new EJS({url: '/js/tmpl/' + template + '.ejs'});
|
|
|
|
return tmpl.render(json);
|
2010-07-12 22:04:42 +08:00
|
|
|
} catch (err) {
|
2010-07-17 00:38:06 +08:00
|
|
|
clearInterval(timer);
|
2010-07-12 22:04:42 +08:00
|
|
|
alert(err['name'] + ": " + err['message']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2010-07-17 00:38:06 +08:00
|
|
|
var html = format('status', {status: status, text: text});
|
2010-07-13 00:31:16 +08:00
|
|
|
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-24 00:24:01 +08:00
|
|
|
else if (req.status == 404) {
|
|
|
|
var html = format('404', {});
|
|
|
|
replace_content('main', html);
|
|
|
|
}
|
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');
|
|
|
|
}
|