2010-07-12 22:04:42 +08:00
|
|
|
$(document).ready(function() {
|
2010-08-23 23:32:17 +08:00
|
|
|
app.run();
|
2010-07-13 20:03:36 +08:00
|
|
|
var url = this.location.toString();
|
2010-08-23 23:32:17 +08:00
|
|
|
if (url.indexOf('#') == -1) {
|
|
|
|
this.location = url + '#/';
|
|
|
|
}
|
2010-07-13 20:03:36 +08:00
|
|
|
timer = setInterval('update()', 5000);
|
2010-07-12 22:04:42 +08:00
|
|
|
});
|
|
|
|
|
2010-08-23 23:32:17 +08:00
|
|
|
var app = $.sammy(dispatcher);
|
|
|
|
function dispatcher() {
|
|
|
|
var sammy = this;
|
|
|
|
function path(p, r, t) {
|
|
|
|
sammy.get(p, function() {
|
|
|
|
render(r, t, p);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
path('#/', ['/overview'], 'overview');
|
|
|
|
path('#/connections', ['/connection/'], 'connections');
|
|
|
|
path('#/queues', ['/queue/'], 'queues');
|
|
|
|
path('#/channels',
|
|
|
|
['/stats/channel_queue_stats/?group_by=channel',
|
|
|
|
'/stats/channel_exchange_stats/?group_by=channel'], 'channels');
|
|
|
|
this.get('#/connections/:id', function() {
|
|
|
|
render(['/connection/' + this.params['id']], 'connection',
|
|
|
|
'#/connection');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var current_template;
|
|
|
|
var current_reqs;
|
|
|
|
var current_highlight;
|
2010-07-13 00:11:42 +08:00
|
|
|
var timer;
|
|
|
|
|
2010-08-23 23:32:17 +08:00
|
|
|
function render(reqs, template, highlight) {
|
|
|
|
current_template = template;
|
|
|
|
current_reqs = reqs;
|
|
|
|
current_highlight = highlight;
|
2010-07-13 20:03:36 +08:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2010-07-12 22:04:42 +08:00
|
|
|
function update() {
|
2010-08-23 23:32:17 +08:00
|
|
|
with_reqs(current_reqs, [], function(jsons) {
|
|
|
|
var json = merge(jsons, current_template);
|
|
|
|
var html = format(current_template, json);
|
2010-07-13 00:31:16 +08:00
|
|
|
replace_content('main', html);
|
|
|
|
update_status('ok', json['datetime']);
|
2010-08-23 23:32:17 +08:00
|
|
|
$('a').removeClass('selected');
|
|
|
|
$('a[href="' + current_highlight + '"]').addClass('selected');
|
2010-08-07 00:52:26 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function with_reqs(reqs, acc, fun) {
|
|
|
|
if (reqs.length > 0) {
|
|
|
|
// alert(reqs[0]);
|
|
|
|
with_req('/json' + reqs[0], function(text) {
|
|
|
|
acc.push(jQuery.parseJSON(text));
|
2010-08-23 23:32:17 +08:00
|
|
|
with_reqs(reqs.slice(1), acc, fun);
|
2010-08-07 00:52:26 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fun(acc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function merge(jsons, template) {
|
|
|
|
if (template == 'channels') {
|
|
|
|
var stats0 = jsons[0].stats;
|
|
|
|
var stats1 = jsons[1].stats;
|
|
|
|
for (var i = 0; i < stats0.length; i++) {
|
|
|
|
for (var j = 0; j < stats1.length; j++) {
|
|
|
|
if (stats0[i].channel == stats1[j].channel) {
|
|
|
|
for (var key in stats1[j].msg_stats) {
|
|
|
|
stats0[i].msg_stats[key] = stats1[j].msg_stats[key];
|
|
|
|
}
|
|
|
|
stats1[j].used = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (var j = 0; j < stats1.length; j++) {
|
|
|
|
if (stats1[j].used == undefined) {
|
|
|
|
stats0.push(stats1[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return jsons[0];
|
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-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-08-23 23:32:17 +08:00
|
|
|
debug(err['name'] + ": " + err['message']);
|
2010-07-12 22:04:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2010-08-23 23:32:17 +08:00
|
|
|
debug("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');
|
|
|
|
}
|