name=fi_oulu_DiskUsage
title=Disk usage overview
description=An overview of disk usage on the server, including mounted network drives
admin_only=yes
version=1
height=1
width=1
url=/api/diskusage
---- Below this line is deemed to be HTML content if url above is empty, Javascript otherwise. If Javascript, you MUST define an object BRICKNAME_functions containing at least a function onSuccess(transport,box){}, transport contains a responseJSON object, box is the homepage brick itself.
document.fi_oulu_DiskUsage_functions={
	onSuccess:function(transport,box){
		var alertThreshold=85;
		var warnThreshold=95;
		var statusColors={
			normal: "#0c0",
			alert:  "#f60",
			warn:   "#c00",
		};
		var disks=transport.responseJSON.rows;
		box.down(".boxbody").innerHTML="";
		disks.each(function(d){
			var alertState="normal";
			if((1*d.percent)>=alertThreshold){ alertState='alert'; }
			if((1*d.percent)>=warnThreshold){ alertState='warn'; }
			var diskDiv=document.createElement("div");
			var capacityBar=document.createElement("div");
			diskDiv.appendChild(capacityBar);
			var fullBar=document.createElement("div");
			capacityBar.appendChild(fullBar);
			var percentFull=document.createElement("span");
			percentFull.innerHTML=d.percent+"%";
			diskDiv.appendChild(percentFull);
			var label=document.createElement("span");
			var parts=d.mountpoint.split("/");
			var lastPart=parts[parts.length-1];
			if(""==lastPart){ lastPart=parts[parts.length-2]; }
			//label.innerHTML=d.mountpoint;
			label.innerHTML=lastPart;
			diskDiv.title="Size "+d.size+", "+d.used+" used, "+d.free+" free ("+d.filesystem+")";
			diskDiv.appendChild(label);
			box.down(".boxbody").appendChild(diskDiv);
			$(diskDiv).setStyle({
				padding:"0.25em 0", 
				lineHeight:"1.5em",
				borderBottom:"1px solid #999",
			});
			$(capacityBar).setStyle({
				width:"40%",
				display:"inline-block",
				margin:"0em 1em 0 0", 
				backgroundColor:"#ccc", 
				borderRadius:"3px", 
				boxShadow:"2px 2px 2px #666 inset", 
				fontSize:"80%", 
				height:"1em", 
				overflow:"hidden", 
				position:"relative", 
				top:0,
			});
			$(fullBar).setStyle({
				position:"absolute", 
				top:0, 
				left:0, 
				bottom:0, 
				width:d.percent+"%", 
				backgroundColor:statusColors[alertState], 
				boxShadow:"1px 1px 1px #333 inset"
			});
			$(percentFull).setStyle({
				display:"inline-block",
				width:"4em",
				fontWeight:"bold",
				color:statusColors[alertState],
			});
		});
	},
	onFailure:function(transport,box){
		if(transport.responseJSON && transport.responseJSON.error){
			box.down(".boxbody").innerHTML=transport.responseJSON.error;
		} else {
			box.down(".boxbody").innerHTML="Could not get disk usage information.";
		}
	}
}
