Created
February 19, 2014 18:44
-
-
Save bdotdub/9098678 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var $apps = $(".app"); | |
var costTable = function(csv, year, month, appName, $app, type) { | |
console.log(csv, appName, $app, type); | |
var $dynos = $app.find("." + type + " tbody tr"); | |
for (var j = 0; j < $dynos.length; j++) { | |
var $dyno = $($dynos.get(j)); | |
var $fields = $dyno.find("td"); | |
if ($fields.length != 4) { | |
continue; | |
} | |
var name = $($fields[0]).text().replace(/[\n,]/g, "").replace(/^ +/, "").replace(/ +$/, ""); | |
var hours = $($fields[1]).text().replace(/[\n,]/g, "").replace(/^ +/, "").replace(/ +$/, ""); | |
var rate = $($fields[2]).text().replace(/[\n,]/g, "").replace(/^ +/, "").replace(/ +$/, ""); | |
var cost = $($fields[3]).text().replace(/[\n,]/g, "").replace(/^ +/, "").replace(/ +$/, ""); | |
csv += [ year, month, appName, type, name, hours, rate, cost ].join(); | |
csv += "\n"; | |
} | |
return csv; | |
} | |
var csv = "data:text/csv;charset=utf-8," | |
csv += "year,month,app,type,name,hours,rate,cost\n"; | |
var components = document.location.href.split("/"); | |
var year = components[components.length - 2]; | |
var month = components[components.length - 1]; | |
for (var i = 0; i < $apps.length; i++) { | |
var $app = $($apps.get(i)); | |
var title = $app.find(".app-title .title").text(); | |
csv = costTable(csv, year, month, title, $app, "dynos"); | |
csv = costTable(csv, year, month, title, $app, "addons"); | |
console.log(csv) | |
} | |
var encodedUri = encodeURI(csv); | |
var link = document.createElement("a"); | |
link.setAttribute("href", encodedUri); | |
link.setAttribute("download", "" + year + "0" + month + "invoice.csv"); | |
link.click(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment