Skip to content
Browse files

Add daily and weekly averages.

  • Loading branch information...
1 parent 081c945 commit 69d4c004181c58511b51292bd76c441f1d365a8e @mudge committed Mar 4, 2010
Showing with 32 additions and 20 deletions.
  1. +5 −4 build.sh
  2. +1 −1 { → css}/st.css
  3. 0 { → images}/bg.png
  4. 0 { → images}/loading.png
  5. +5 −3 index.html
  6. +11 −3 { → js}/st.js
  7. +6 −5 { → js}/stc.js
  8. +4 −4 spendthrift.manifest
View
9 build.sh
@@ -1,4 +1,5 @@
-curl -s --data-urlencode js_code@st.js --data output_info=compiled_code --data compilation_level=SIMPLE_OPTIMIZATIONS -o stc.js http://closure-compiler.appspot.com/compile
-sed -i "$ s/.*/# `git show-ref --heads | awk '{print $1}'`/" spendthrift.manifest
-git add stc.js spendthrift.manifest
-
+#!/bin/bash
+SHA1=$(git show-ref --heads | awk '{print $1}')
+curl -s --data-urlencode js_code@js/st.js --data output_info=compiled_code --data compilation_level=SIMPLE_OPTIMIZATIONS -o js/stc.js http://closure-compiler.appspot.com/compile
+sed -i "" "$ s/.*/# $SHA1/" spendthrift.manifest
+git add js/stc.js spendthrift.manifest
View
2 st.css → css/st.css
@@ -3,7 +3,7 @@
padding: 0;
}
html {
- background: #ffe url(bg.png);
+ background: #ffe url(../images/bg.png);
color: #000;
font: large Baskerville, Georgia;
padding: 20px;
View
0 bg.png → images/bg.png
File renamed without changes
View
0 loading.png → images/loading.png
File renamed without changes
View
8 index.html
@@ -3,9 +3,9 @@
<head>
<meta charset="utf-8">
<title>Spendthrift</title>
- <link rel="stylesheet" type="text/css" href="st.css">
+ <link rel="stylesheet" type="text/css" href="css/st.css">
<link rel="apple-touch-icon" href="apple-touch-icon.png">
- <link rel="apple-touch-startup-image" href="loading.png">
+ <link rel="apple-touch-startup-image" href="images/loading.png">
<meta name="viewport" content="user-scalable=no, width=device-width">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
@@ -33,6 +33,8 @@
<table>
<caption>Averages</caption>
<tbody>
+ <tr><th>Daily</th><td>&pound;<span id="daily_average">0</span></td></tr>
+ <tr class="even"><th>Weekly</th><td>&pound;<span id="weekly_average">0</span></td></tr>
<tr><th>Mondays</th><td>&pound;<span id="monday">0</span></td></tr>
<tr class="even"><th>Tuesdays</th><td>&pound;<span id="tuesday">0</span></td></tr>
<tr><th>Wednesdays</th><td>&pound;<span id="wednesday">0</span></td></tr>
@@ -46,6 +48,6 @@
<caption>Breakdowns</caption>
<tbody id="breakdowns"></tbody>
</table>
- <script type="text/javascript" src="stc.js"></script>
+ <script type="text/javascript" src="js/stc.js"></script>
</body>
</html>
View
14 st.js → js/st.js
@@ -7,6 +7,8 @@ function errorHandler(transaction, error) {
function updateGrandTotal(transaction, results) {
var result = results.rows.item(0)
document.getElementById('total').textContent = (result['total'] || 0) / 100.0;
+ document.getElementById('daily_average').textContent = ((result['total'] || 0) / 100.0) / numberOfDays;
+ document.getElementById('weekly_average').textContent = ((result['total'] || 0) / 100.0) / numberOfWeeks;
}
function updateWeeklyTotal(transaction, results) {
@@ -26,6 +28,9 @@ function updateBreakdowns(transaction, results) {
var result = results.rows.item(i);
var newBreakdown = document.createElement('tr');
breakdowns.appendChild(newBreakdown);
+ if (i % 2 == 1) {
+ newBreakdown.className = "even";
+ }
newBreakdown.innerHTML = '<th>' + result['spent_at_date'] + '</th><td>&pound;' + result['total'] / 100.0 + '</td>';
}
}
@@ -59,20 +64,22 @@ function updateAverages(transaction, results) {
}
}
-function setNumberOfWeeks(transaction, results) {
+function setNumberOfDaysAndWeeks(transaction, results) {
var oldestValues = results.rows.item(0)["oldest"].split(/\D/);
var oldest = new Date(oldestValues[0], oldestValues[1] - 1, oldestValues[2], oldestValues[3], oldestValues[4], oldestValues[5]);
var distance = new Date() - oldest;
- numberOfWeeks = Math.ceil(((((distance / 1000) / 60) / 60) / 24) / 7);
+ /* 1 day = 86,400,000 milliseconds. */
+ numberOfDays = Math.ceil(distance / 86400000);
+ numberOfWeeks = Math.ceil(numberOfDays / 7);
}
function updatePage(transaction, results) {
transaction.executeSql('SELECT SUM(amount) AS total FROM spends', [], updateGrandTotal, errorHandler);
transaction.executeSql('SELECT SUM(amount) AS total FROM spends WHERE spent_at > datetime(\'now\', \'-7 days\', \'weekday 1\', \'start of day\')', [], updateWeeklyTotal, errorHandler);
transaction.executeSql('SELECT SUM(amount) AS total FROM spends WHERE spent_at > datetime(\'now\', \'start of month\', \'start of day\')', [], updateMonthlyTotal, errorHandler);
transaction.executeSql('SELECT spent_at, date(spent_at) AS spent_at_date, SUM(amount) AS total FROM spends GROUP BY spent_at_date', [], updateBreakdowns, errorHandler);
- transaction.executeSql('SELECT MIN(spent_at) AS oldest FROM spends', [], setNumberOfWeeks, errorHandler);
+ transaction.executeSql('SELECT MIN(spent_at) AS oldest FROM spends', [], setNumberOfDaysAndWeeks, errorHandler);
transaction.executeSql('SELECT strftime(\'%w\', spent_at) AS weekday, SUM(amount) AS total FROM spends GROUP BY weekday', [], updateAverages, errorHandler);
}
@@ -98,6 +105,7 @@ if (!window.openDatabase) {
try {
var db = openDatabase('spendthrift', '1.0', 'Spendthrift Database', 65536);
var numberOfWeeks = 1;
+ var numberOfDays = 1;
} catch(e) {
alert("Error opening database " + e);
}
View
11 stc.js → js/stc.js
@@ -1,8 +1,9 @@
-function nullDataHandler(){}function errorHandler(a,b){alert("There was an error (code "+b.code+"): "+b.message);return false}function updateGrandTotal(a,b){a=b.rows.item(0);document.getElementById("total").textContent=(a.total||0)/100}function updateWeeklyTotal(a,b){a=b.rows.item(0);document.getElementById("weekly_total").textContent=(a.total||0)/100}function updateMonthlyTotal(a,b){a=b.rows.item(0);document.getElementById("monthly_total").textContent=(a.total||0)/100}
-function updateBreakdowns(a,b){a=document.getElementById("breakdowns");a.innerHTML="";for(var c=0;c<b.rows.length;c++){var d=b.rows.item(c),f=document.createElement("tr");a.appendChild(f);f.innerHTML="<th>"+d.spent_at_date+"</th><td>&pound;"+d.total/100+"</td>"}}
+function nullDataHandler(){}function errorHandler(a,b){alert("There was an error (code "+b.code+"): "+b.message);return false}function updateGrandTotal(a,b){a=b.rows.item(0);document.getElementById("total").textContent=(a.total||0)/100;document.getElementById("daily_average").textContent=(a.total||0)/100/numberOfDays;document.getElementById("weekly_average").textContent=(a.total||0)/100/numberOfWeeks}
+function updateWeeklyTotal(a,b){a=b.rows.item(0);document.getElementById("weekly_total").textContent=(a.total||0)/100}function updateMonthlyTotal(a,b){a=b.rows.item(0);document.getElementById("monthly_total").textContent=(a.total||0)/100}
+function updateBreakdowns(a,b){a=document.getElementById("breakdowns");a.innerHTML="";for(var c=0;c<b.rows.length;c++){var d=b.rows.item(c),f=document.createElement("tr");a.appendChild(f);if(c%2==1)f.className="even";f.innerHTML="<th>"+d.spent_at_date+"</th><td>&pound;"+d.total/100+"</td>"}}
function updateAverages(a,b){for(a=0;a<b.rows.length;a++){var c=b.rows.item(a);switch(c.weekday){case "0":document.getElementById("sunday").textContent=c.total/100/numberOfWeeks;break;case "1":document.getElementById("monday").textContent=c.total/100/numberOfWeeks;break;case "2":document.getElementById("tuesday").textContent=c.total/100/numberOfWeeks;break;case "3":document.getElementById("wednesday").textContent=c.total/100/numberOfWeeks;break;case "4":document.getElementById("thursday").textContent=
-c.total/100/numberOfWeeks;break;case "5":document.getElementById("friday").textContent=c.total/100/numberOfWeeks;break;case "6":document.getElementById("saturday").textContent=c.total/100/numberOfWeeks;break}}}function setNumberOfWeeks(a,b){a=b.rows.item(0).oldest.split(/\D/);a=new Date(a[0],a[1]-1,a[2],a[3],a[4],a[5]);numberOfWeeks=Math.ceil((new Date-a)/1E3/60/60/24/7)}
+c.total/100/numberOfWeeks;break;case "5":document.getElementById("friday").textContent=c.total/100/numberOfWeeks;break;case "6":document.getElementById("saturday").textContent=c.total/100/numberOfWeeks;break}}}function setNumberOfDaysAndWeeks(a,b){a=b.rows.item(0).oldest.split(/\D/);a=new Date(a[0],a[1]-1,a[2],a[3],a[4],a[5]);numberOfDays=Math.ceil((new Date-a)/864E5);numberOfWeeks=Math.ceil(numberOfDays/7)}
function updatePage(a){a.executeSql("SELECT SUM(amount) AS total FROM spends",[],updateGrandTotal,errorHandler);a.executeSql("SELECT SUM(amount) AS total FROM spends WHERE spent_at > datetime('now', '-7 days', 'weekday 1', 'start of day')",[],updateWeeklyTotal,errorHandler);a.executeSql("SELECT SUM(amount) AS total FROM spends WHERE spent_at > datetime('now', 'start of month', 'start of day')",[],updateMonthlyTotal,errorHandler);a.executeSql("SELECT spent_at, date(spent_at) AS spent_at_date, SUM(amount) AS total FROM spends GROUP BY spent_at_date",
-[],updateBreakdowns,errorHandler);a.executeSql("SELECT MIN(spent_at) AS oldest FROM spends",[],setNumberOfWeeks,errorHandler);a.executeSql("SELECT strftime('%w', spent_at) AS weekday, SUM(amount) AS total FROM spends GROUP BY weekday",[],updateAverages,errorHandler)}
-function addSpend(){var a=document.getElementById("description"),b=a.value,c=document.getElementById("amount"),d=c.value;if(parseFloat(d)>0){var f=d*100;db.transaction(function(g){g.executeSql("INSERT INTO spends (amount, description) VALUES (?, ?)",[f,b],updatePage,errorHandler)})}c.value="";a.value="";return false}if(window.openDatabase)try{var db=openDatabase("spendthrift","1.0","Spendthrift Database",65536),numberOfWeeks=1}catch(e){alert("Error opening database "+e)}else alert("Spendthrift is not supported by your current browser.");
+[],updateBreakdowns,errorHandler);a.executeSql("SELECT MIN(spent_at) AS oldest FROM spends",[],setNumberOfDaysAndWeeks,errorHandler);a.executeSql("SELECT strftime('%w', spent_at) AS weekday, SUM(amount) AS total FROM spends GROUP BY weekday",[],updateAverages,errorHandler)}
+function addSpend(){var a=document.getElementById("description"),b=a.value,c=document.getElementById("amount"),d=c.value;if(parseFloat(d)>0){var f=d*100;db.transaction(function(g){g.executeSql("INSERT INTO spends (amount, description) VALUES (?, ?)",[f,b],updatePage,errorHandler)})}c.value="";a.value="";return false}if(window.openDatabase)try{var db=openDatabase("spendthrift","1.0","Spendthrift Database",65536),numberOfWeeks=1,numberOfDays=1}catch(e){alert("Error opening database "+e)}else alert("Spendthrift is not supported by your current browser.");
db.transaction(function(a){a.executeSql("CREATE TABLE IF NOT EXISTS spends (id INTEGER PRIMARY KEY, amount INTEGER NOT NULL, description NVARCHAR, spent_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);",[],nullDataHandler,errorHandler)});db.transaction(function(a){updatePage(a)});
View
8 spendthrift.manifest
@@ -1,7 +1,7 @@
CACHE MANIFEST
-stc.js
-st.css
-bg.png
+js/stc.js
+css/st.css
+images/bg.png
-# 6a70e14150e99d938864e3eb38dd52e69120c7b6
+# 081c9457f7d1c8c36cf74aaa768f17579b94175a

0 comments on commit 69d4c00

Please sign in to comment.
Something went wrong with that request. Please try again.