<script type="text/javascript">
	const units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
	function niceBytes(x) {
		let l = 0, n = parseInt(x, 10) || 0;
		while (n >= 1024 && ++l) {
			n = n / 1024;
		}
		return (n.toFixed(n < 10 && l > 0 ? 1 : 0) + ' ' + units[l]);
	}

	fnWindowLoad = function () {
		XHR.get('<%=luci.dispatcher.build_url("admin/docker/get_system_df")%>/', null, (x, info)=>{
			if(!info || !info.Containers || !info.Containers.forEach) return
			info.Containers.forEach(item=>{
				const size_c =	document.getElementsByClassName("container_size_" + item.Id)
				size_c[0].title = "RW Size: " + niceBytes(item.SizeRw) + " / RootFS Size(Include Image): " + niceBytes(item.SizeRootFs)
				size_c[0].innerText = "Size: " + niceBytes(item.SizeRw) + "/" + niceBytes(item.SizeRootFs)
			})
		})
		let lines = document.querySelectorAll('[id^=cbi-containers-]')
		let last_bw_tx = {}
		let last_bw_rx = {}
		let interval = 30
		let containers = []
		lines.forEach((item) => {
			let containerId = item.id.match(/cbi-containers-.+_id_(.*)/)
			if (!containerId) { return }
			containerId = containerId[1]
			if (item.getElementsByClassName("container_not_running").length > 0) { return }
			XHR.poll(interval, '<%=luci.dispatcher.build_url("admin/docker/container_stats")%>/' + containerId, null, (x, info) => {
				// handle stats info
				if (!info) { return }
				item.childNodes.forEach((cell) => {
					if (cell && cell.attributes) {
						if (cell.getAttribute("data-name") == "_status" || cell.childNodes[1] && cell.childNodes[1].id.match(/_status/)) {
							let runningStats = cell.getElementsByClassName("container_cpu_status")
							runningStats[0].innerText = "CPU: " + info.cpu_percent + "%"
							runningStats = cell.getElementsByClassName("container_mem_status")
							runningStats[0].innerText = "MEM: " + niceBytes(info.memory.mem_useage)
							runningStats = cell.getElementsByClassName("container_network_status")
							for (var eth in info.bw_rxtx) {
								if (last_bw_tx[containerId] != undefined && last_bw_rx[containerId] != undefined) {
									runningStats[0].innerText = '￪' + niceBytes((info.bw_rxtx[eth].bw_tx - last_bw_tx[containerId]) / interval) + '/s ￬' + niceBytes((info.bw_rxtx[eth].bw_rx - last_bw_rx[containerId]) / interval) + '/s'
								}
								last_bw_rx[containerId] = info.bw_rxtx[eth].bw_rx
								last_bw_tx[containerId] = info.bw_rxtx[eth].bw_tx
							}
						}
					}
				})
			})
			// containers.push(containerId)
		})
		// XHR.post('<%=luci.dispatcher.build_url("admin/docker/containers_stats")%>', {
		// 	containers: JSON.stringify(containers)
		// }, (x, info) => {
		// 	lines.forEach((item) => {
		// 		if (!info) { return }

		// 		let containerId = item.id.match(/cbi-containers-.+_id_(.*)/)
		// 		if (!containerId) { return }
		// 		containerId = containerId[1]
		// 		if (!info[containerId]) { return }
		// 		infoC = info[containerId]
		// 		if (item.getElementsByClassName("container_not_running").length > 0) { return }
		// 		item.childNodes.forEach((cell) => {
		// 			if (cell && cell.attributes) {
		// 				if (cell.getAttribute("data-name") == "_status" || cell.childNodes[1] && cell.childNodes[1].id.match(/_status/)) {
		// 					let runningStats = cell.getElementsByClassName("container_cpu_status")
		// 					runningStats[0].innerText = "CPU: " + infoC.cpu_percent + "%"
		// 					runningStats = cell.getElementsByClassName("container_mem_status")
		// 					runningStats[0].innerText = "MEM: " + niceBytes(infoC.memory.mem_useage)
		// 					runningStats = cell.getElementsByClassName("container_network_status")
		// 					for (var eth in infoC.bw_rxtx) {
		// 						if (last_bw_tx[containerId] != undefined && last_bw_rx[containerId] != undefined) {
		// 							runningStats[0].innerText = '￪' + niceBytes((infoC.bw_rxtx[eth].bw_tx - last_bw_tx[containerId]) / interval) + '/s ￬' + niceBytes((infoC.bw_rxtx[eth].bw_rx - last_bw_rx[containerId]) / interval) + '/s'
		// 						}
		// 						last_bw_rx[containerId] = infoC.bw_rxtx[eth].bw_rx
		// 						last_bw_tx[containerId] = infoC.bw_rxtx[eth].bw_tx
		// 					}
		// 				}
		// 			}
		// 		})
		// 	})
		// })


		XHR.run()
		XHR.halt()
	}
</script>