Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add daily and weekly averages.

  • Loading branch information...
commit 69d4c004181c58511b51292bd76c441f1d365a8e 1 parent 081c945
Paul Mucur authored
9 build.sh
... ... @@ -1,4 +1,5 @@
1   -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
2   -sed -i "$ s/.*/# `git show-ref --heads | awk '{print $1}'`/" spendthrift.manifest
3   -git add stc.js spendthrift.manifest
4   -
  1 +#!/bin/bash
  2 +SHA1=$(git show-ref --heads | awk '{print $1}')
  3 +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
  4 +sed -i "" "$ s/.*/# $SHA1/" spendthrift.manifest
  5 +git add js/stc.js spendthrift.manifest
2  st.css → css/st.css
@@ -3,7 +3,7 @@
3 3 padding: 0;
4 4 }
5 5 html {
6   - background: #ffe url(bg.png);
  6 + background: #ffe url(../images/bg.png);
7 7 color: #000;
8 8 font: large Baskerville, Georgia;
9 9 padding: 20px;
0  bg.png → images/bg.png
File renamed without changes
0  loading.png → images/loading.png
File renamed without changes
8 index.html
@@ -3,9 +3,9 @@
3 3 <head>
4 4 <meta charset="utf-8">
5 5 <title>Spendthrift</title>
6   - <link rel="stylesheet" type="text/css" href="st.css">
  6 + <link rel="stylesheet" type="text/css" href="css/st.css">
7 7 <link rel="apple-touch-icon" href="apple-touch-icon.png">
8   - <link rel="apple-touch-startup-image" href="loading.png">
  8 + <link rel="apple-touch-startup-image" href="images/loading.png">
9 9 <meta name="viewport" content="user-scalable=no, width=device-width">
10 10 <meta name="apple-mobile-web-app-capable" content="yes">
11 11 <meta name="apple-mobile-web-app-status-bar-style" content="black">
@@ -33,6 +33,8 @@
33 33 <table>
34 34 <caption>Averages</caption>
35 35 <tbody>
  36 + <tr><th>Daily</th><td>&pound;<span id="daily_average">0</span></td></tr>
  37 + <tr class="even"><th>Weekly</th><td>&pound;<span id="weekly_average">0</span></td></tr>
36 38 <tr><th>Mondays</th><td>&pound;<span id="monday">0</span></td></tr>
37 39 <tr class="even"><th>Tuesdays</th><td>&pound;<span id="tuesday">0</span></td></tr>
38 40 <tr><th>Wednesdays</th><td>&pound;<span id="wednesday">0</span></td></tr>
@@ -46,6 +48,6 @@
46 48 <caption>Breakdowns</caption>
47 49 <tbody id="breakdowns"></tbody>
48 50 </table>
49   - <script type="text/javascript" src="stc.js"></script>
  51 + <script type="text/javascript" src="js/stc.js"></script>
50 52 </body>
51 53 </html>
14 st.js → js/st.js
@@ -7,6 +7,8 @@ function errorHandler(transaction, error) {
7 7 function updateGrandTotal(transaction, results) {
8 8 var result = results.rows.item(0)
9 9 document.getElementById('total').textContent = (result['total'] || 0) / 100.0;
  10 + document.getElementById('daily_average').textContent = ((result['total'] || 0) / 100.0) / numberOfDays;
  11 + document.getElementById('weekly_average').textContent = ((result['total'] || 0) / 100.0) / numberOfWeeks;
10 12 }
11 13
12 14 function updateWeeklyTotal(transaction, results) {
@@ -26,6 +28,9 @@ function updateBreakdowns(transaction, results) {
26 28 var result = results.rows.item(i);
27 29 var newBreakdown = document.createElement('tr');
28 30 breakdowns.appendChild(newBreakdown);
  31 + if (i % 2 == 1) {
  32 + newBreakdown.className = "even";
  33 + }
29 34 newBreakdown.innerHTML = '<th>' + result['spent_at_date'] + '</th><td>&pound;' + result['total'] / 100.0 + '</td>';
30 35 }
31 36 }
@@ -59,12 +64,14 @@ function updateAverages(transaction, results) {
59 64 }
60 65 }
61 66
62   -function setNumberOfWeeks(transaction, results) {
  67 +function setNumberOfDaysAndWeeks(transaction, results) {
63 68 var oldestValues = results.rows.item(0)["oldest"].split(/\D/);
64 69 var oldest = new Date(oldestValues[0], oldestValues[1] - 1, oldestValues[2], oldestValues[3], oldestValues[4], oldestValues[5]);
65 70 var distance = new Date() - oldest;
66 71
67   - numberOfWeeks = Math.ceil(((((distance / 1000) / 60) / 60) / 24) / 7);
  72 + /* 1 day = 86,400,000 milliseconds. */
  73 + numberOfDays = Math.ceil(distance / 86400000);
  74 + numberOfWeeks = Math.ceil(numberOfDays / 7);
68 75 }
69 76
70 77 function updatePage(transaction, results) {
@@ -72,7 +79,7 @@ function updatePage(transaction, results) {
72 79 transaction.executeSql('SELECT SUM(amount) AS total FROM spends WHERE spent_at > datetime(\'now\', \'-7 days\', \'weekday 1\', \'start of day\')', [], updateWeeklyTotal, errorHandler);
73 80 transaction.executeSql('SELECT SUM(amount) AS total FROM spends WHERE spent_at > datetime(\'now\', \'start of month\', \'start of day\')', [], updateMonthlyTotal, errorHandler);
74 81 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);
75   - transaction.executeSql('SELECT MIN(spent_at) AS oldest FROM spends', [], setNumberOfWeeks, errorHandler);
  82 + transaction.executeSql('SELECT MIN(spent_at) AS oldest FROM spends', [], setNumberOfDaysAndWeeks, errorHandler);
76 83 transaction.executeSql('SELECT strftime(\'%w\', spent_at) AS weekday, SUM(amount) AS total FROM spends GROUP BY weekday', [], updateAverages, errorHandler);
77 84 }
78 85
@@ -98,6 +105,7 @@ if (!window.openDatabase) {
98 105 try {
99 106 var db = openDatabase('spendthrift', '1.0', 'Spendthrift Database', 65536);
100 107 var numberOfWeeks = 1;
  108 + var numberOfDays = 1;
101 109 } catch(e) {
102 110 alert("Error opening database " + e);
103 111 }
11 stc.js → js/stc.js
... ... @@ -1,8 +1,9 @@
1   -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}
2   -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>"}}
  1 +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}
  2 +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}
  3 +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>"}}
3 4 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=
4   -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)}
  5 +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)}
5 6 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",
6   -[],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)}
7   -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.");
  7 +[],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)}
  8 +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.");
8 9 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)});
8 spendthrift.manifest
... ... @@ -1,7 +1,7 @@
1 1 CACHE MANIFEST
2 2
3   -stc.js
4   -st.css
5   -bg.png
  3 +js/stc.js
  4 +css/st.css
  5 +images/bg.png
6 6
7   -# 6a70e14150e99d938864e3eb38dd52e69120c7b6
  7 +# 081c9457f7d1c8c36cf74aaa768f17579b94175a

0 comments on commit 69d4c00

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