Skip to content

Instantly share code, notes, and snippets.

@harageth
Last active May 16, 2016 20:46
Show Gist options
  • Save harageth/1483c628e74db9a83996490b6f120f3d to your computer and use it in GitHub Desktop.
Save harageth/1483c628e74db9a83996490b6f120f3d to your computer and use it in GitHub Desktop.
tbody td { white-space: nowrap; overflow: hidden; text-overflow: ellipsis;}
span.extra { font-size: 70%; opacity: 0.5; }
tr.small { color: #555; }
tr.medium { color: #550; }
tr.large { color: #a50;}
tr.huge { color: #f00; font-weight: bold;}
var tag = document.createElement("script");
tag.setAttribute("src", "https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.0/Chart.min.js");
document.head.appendChild(tag);
agent.container.appendChild(domBuilder([
["h1", "Disk Analyser"],
["table",
["thead",
["tr",
["th", "Size"],
["th", "Largest Files"]]],
["tbody$tbody"],
["thead",
["tr",
["th", "Total Size"],
["th", "Largest Paths"]]],
["tbody$tbody2"]],
], agent));
var onError = console.error.bind(console);
var root = "/";
agent.largefiles(root, 20, 1048576, onError, onUpdate)(onDone);
function onDone(err, list) {
if (err) { throw err; }
onUpdate(list);
agent.container.appendChild(domBuilder(["p", "Done!"]));
}
function colors(size) {
if (size < 10*1024*1024) return "small";
if (size < 200*1024*1024) return "medium";
if (size < 4*1024*1024*1024) return "large";
return "huge";
}
function humanize(size) {
return ["span", {title:size},
size < 1024 ? size + "b" :
size < 1024*1024 ? (Math.floor(size / 1024 * 10)/10) + "Kb" :
size < 1025*1024*1024 ? (Math.floor(size / 1024 /1024 * 10)/10) + "Mb" :
(Math.floor(size / 1024 /1024 / 1024 * 10)/10) + "Gb"];
}
function highlight(path) {
var matches = path.match(/^(.*\/)([^\/]+)$/);
return matches ? ["span", {title:path},
["span.extra", matches[1]],
["span.main", matches[2]],
] : path;
}
function onUpdate(list) {
agent.tbody.textContent = "";
agent.tbody.appendChild(domBuilder(list.map(function (entry) {
return ["tr", {"class":colors(entry[1])},
["td", humanize(entry[1])],
["td", highlight(entry[0])]];
})));
}
agent.diskusage(root, 4, 16777216, onEntry, onError)(onFinished);
function sorter(l, r) {
return l[1] < r[1] ? 1 : l[1] > r[1] ? -1 : 0;
}
// Should eventually be able to compress multiple files at once
var compressFileFunction = function compressFile(fileName) {
agent.exec("tar", {"args":{"-cvfz", fileName+".tar.gz", fileName}})
}
var deleteFileFunction = function deleteFile(path) {
agent.rm(path, false)
}
function makeRecommendation(name, size) {
// if name ends in .log recommend compression
// not sure which instances we would recommend deletion
// for now I could just pic a random number of set that function so that I can test things.
if (Math.floor(Math.random())) {
return compressFileFunction
}
return deleteFileFunction
}
var entries = [];
function onEntry(path, size) {
entries.push([path,size]);
entries.sort(sorter);
agent.tbody2.textContent = "";
agent.tbody2.appendChild(domBuilder(entries.slice(0,20).map(function (entry) {
return ["tr", {"class":colors(entry[1])},
["td", humanize(entry[1])],
["td", highlight(entry[0])]];
})));
}
function onFinished(err, exists) {
if (err) { throw err; }
}
local agent = {}
agent.largefiles = largefiles
agent.diskusage = diskusage
agent.exec = exec
agent.rm = rm
return agent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment