summaryrefslogtreecommitdiffhomepage
path: root/html/stats.js
blob: 89c674ab32e0f53acbbb58990bf1ada101e66fd8 (plain)
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
// started on main page load
function init() {
	init_stats();
}

function set_status(message)
{
 if (message == "") {
	document.getElementById("status").textContent = message;
	document.getElementById("status").style.display = 'inline';
 } else {
	document.getElementById("status").textContent = "";
	document.getElementById("status").style.display = 'none';
 }
}

var websocket;

//
// Callbacks for websocket data of different types
//

function on_stats(numberofdocuments, numberofconnections, dbsizegross, dbsizenet)
{
	document.getElementById("numberofdocuments").textContent = numberofdocuments;
	document.getElementById("numberofconnections").textContent = numberofconnections;
	document.getElementById("dbsizegross").textContent = dbsizegross;
	document.getElementById("dbsizenet").textContent = dbsizenet;
}

function on_version(version)
{
	document.getElementById("version").textContent = version;
}

function on_message(e) {
	var parser = new DOMParser();
	var xmlDocument = parser.parseFromString(e.data, "text/xml");
	
	var type = xmlDocument.getElementsByTagName("type")[0].textContent;

	if (type == "stats") {
		on_stats(xmlDocument.getElementsByTagName("numberofdocuments")[0].textContent,
                         xmlDocument.getElementsByTagName("numberofconnections")[0].textContent,
                         xmlDocument.getElementsByTagName("dbsizegross")[0].textContent,
                         xmlDocument.getElementsByTagName("dbsizenet")[0].textContent);
	} else if (type == "version") {
		on_version(xmlDocument.getElementsByTagName("version")[0].textContent);
	} else if (type == "error") {
		alert(xmlDocument.getElementsByTagName("message")[0].textContent);
	} else {
		alert("Unhandled message type: " + e.data + "|" + type);
	}
}

function connect_websocket() {
	document.getElementById("reconnect").style.display = 'none';
        set_status("Connecting...");
	var newlocation = location.origin + location.pathname;
	newlocation = newlocation.replace(/^http/, 'ws');
	newlocation = newlocation.replace(/stats.html$/, '');
	if (newlocation.slice(-1) != "/")
		newlocation += "/";
	newlocation += "websocket";

	websocket = new WebSocket(newlocation);

	websocket.onmessage = function(e) { on_message(e); };

	websocket.onopen = function(e) {
		websocket.send("<request><command>getversion</command></request>");
		websocket.send("<request><command>getstats</command></request>");
                set_status(""); // ok
	};
	
	websocket.onclose = function(e) {
		alert("Server connection closed.");
		document.getElementById("reconnect").style.display = 'inline';
	};

	websocket.onerror = function(e) {
		alert("Error: Server connection closed.");
		document.getElementById("reconnect").style.display = 'inline';
	};
}

// button in html
function on_reconnect_click() {
	connect_websocket();
}

function init_stats() {
        set_status("Loading...");
        connect_websocket();
}