Skip to content

Instantly share code, notes, and snippets.

@mwielondek
Last active March 26, 2017 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwielondek/488b0aa1194d239892fdfa807b7e74b4 to your computer and use it in GitHub Desktop.
Save mwielondek/488b0aa1194d239892fdfa807b7e74b4 to your computer and use it in GitHub Desktop.
Javascript for Automation (JXA) task to compute what percentage of completed tasks fall into which Area in Cultured Code's Things
// how many days to go back
var dayDiff = 21;
var d = new Date();
d.setDate(d.getDate() - dayDiff);
var things = Application('Things');
var logbook = things.lists[6].toDos;
var counter = {};
for (var i = 0; i < logbook.length; i++) {
var todo = logbook[i];
if (todo.completionDate() < d) break;
var area = "Undef. Area";
if (todo.area()) {
area = todo.area().name();
} else if (todo.project() && todo.project().area()) {
area = todo.project().area().name();
}
counter[area] = (counter[area] || 0) + 1;
}
// Sort in order of completed todos
var keys = Object.keys(counter).sort(function(a,b) {
return counter[b] - counter[a];
});
// Get total completed todos
var total = Object.keys(counter).reduce(function(a,b) {
return a + counter[b];
}, 0);
// Get longest Area name (for padding)
var maxAreaNameLen = Object.keys(counter).reduce(function(a,b) {
return Math.max(a,b.length);
}, 0);
console.log('Breakdown by area:');
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var procentage = Number(counter[key]/total*100).toFixed(0) + "%";
console.log(pad(maxAreaNameLen, key, " "), pad(14, counter[key] + ' tasks =', " "), procentage);
}
console.log('\n - Total number completed tasks in the last', dayDiff, 'days:', total);
function pad(width, string, padding) {
return (width <= string.length) ? string : pad(width, padding + string, padding)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment