summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-dockerman/luasrc/view/dockerman/container_stats.htm
blob: b5600e49797852ca96271f7280417bba7be60c97 (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
<script type="text/javascript">//<![CDATA[
	let last_bw_tx
	let last_bw_rx
	let interval = 3
	function progressbar(v, m, pc, np, f) {
		m = m || 100

		return String.format(
			'<div style="width:100%%; max-width:500px; position:relative; border:1px solid #999999">' +
			'<div style="background-color:#CCCCCC; width:%d%%; height:15px">' +
			'<div style="position:absolute; left:0; top:0; text-align:center; width:100%%; color:#000000">' +
			'<small>%s '+(f?f:'/')+' %s ' + (np ? "" : '(%d%%)') + '</small>' +
			'</div>' +
			'</div>' +
			'</div>', pc, v, m, pc, f
		);
	}

	function niceBytes(bytes, decimals) {
		if (bytes == 0) return '0 Bytes';
		var k = 1000,
			dm = decimals + 1 || 3,
			sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
			i = Math.floor(Math.log(bytes) / Math.log(k));
		return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
	}

	XHR.poll(interval, '<%=luci.dispatcher.build_url("admin/docker/container_stats")%>/<%=self.container_id%>', { status: 1 },
		function (x, info) {
			var e;

			if (e = document.getElementById('cbi-table-cpu-value'))
				e.innerHTML = progressbar(
					(info.cpu_percent), 100, (info.cpu_percent ? info.cpu_percent : 0));
			if (e = document.getElementById('cbi-table-memory-value'))
				e.innerHTML = progressbar(
					niceBytes(info.memory.mem_useage),
					niceBytes(info.memory.mem_limit),
					((100 / (info.memory.mem_limit ? info.memory.mem_limit : 100)) * (info.memory.mem_useage ? info.memory.mem_useage : 0))
				);

			for (var eth in info.bw_rxtx) {
				if (!document.getElementById("cbi-table-speed_" + eth + "-value")) {
					let tab = document.getElementById("cbi-table-cpu").parentNode
					let div = document.getElementById('cbi-table-cpu').cloneNode(true);
					div.id = "cbi-table-speed_" + eth;
					div.children[0].innerHTML = "<%:Upload/Download%>: " + eth
					div.children[1].id = "cbi-table-speed_" + eth + "-value"
					tab.appendChild(div)
				}
				if (!document.getElementById("cbi-table-network_" + eth + "-value")) {
					let tab = document.getElementById("cbi-table-cpu").parentNode
					let div = document.getElementById('cbi-table-cpu').cloneNode(true);
					div.id = "cbi-table-network_" + eth;
					div.children[0].innerHTML = "<%:TX/RX%>: " + eth
					div.children[1].id = "cbi-table-network_" + eth + "-value"
					tab.appendChild(div)
				}
				e = document.getElementById("cbi-table-network_" + eth + "-value")
				e.innerHTML = progressbar(
					'↑'+niceBytes(info.bw_rxtx[eth].bw_tx),
					'↓'+niceBytes(info.bw_rxtx[eth].bw_rx),
					null,
					true, " "
				);
				e = document.getElementById("cbi-table-speed_" + eth + "-value")
				if (! last_bw_tx) last_bw_tx = info.bw_rxtx[eth].bw_tx
				if (! last_bw_rx) last_bw_rx = info.bw_rxtx[eth].bw_rx
				e.innerHTML = progressbar(
					'↑'+niceBytes((info.bw_rxtx[eth].bw_tx - last_bw_tx)/interval)+'/s',
					'↓'+niceBytes((info.bw_rxtx[eth].bw_rx - last_bw_rx)/interval)+'/s',
					null,
					true, " "
				);
				last_bw_tx = info.bw_rxtx[eth].bw_tx
				last_bw_rx = info.bw_rxtx[eth].bw_rx
			}

		});
//]]></script>