Browse Source

Add support for sporadically repeating transactions

master
Chris Smith 13 years ago
parent
commit
777f927d3b
2 changed files with 22 additions and 10 deletions
  1. 21
    9
      analyser.js
  2. 1
    1
      index.html

+ 21
- 9
analyser.js View File

@@ -353,8 +353,10 @@ function calculateRepeatTransactions(data) {
353 353
  $('#repeats tr.data').remove();
354 354
  var table = $('#repeats table');
355 355
 
356
- var descs = {};
356
+ // This assumes data is sorted by date
357
+ var timeSpan = Date.parseYMD(data[data.length - 1].Date.date) - Date.parseYMD(data[0].Date.date);
357 358
 
359
+ var descs = {};
358 360
  $.each(data, function() {
359 361
   if (!descs[this.Description]) { descs[this.Description] = []; }
360 362
   descs[this.Description].push(this);
@@ -382,22 +384,32 @@ function calculateRepeatTransactions(data) {
382 384
   // I may have just made this metric up. Sue me.
383 385
   var stability = average.deviation / average.mean;
384 386
   var periodInDays = average.mean / (1000 * 60 * 60 * 24);
387
+  var stretch = average.mean * differences.length / timeSpan;
388
+
389
+  if (stretch > 0.5) {
390
+   // Happens across a decent proportion of our timespan
391
+   var monthValue, periodText, classes;
385 392
 
386
-  if (stability < 0.5) {
387
-   // Seems quite reliable...
388
-   if ((periodInDays >= 5 && periodInDays <= 9) || (periodInDays >= 27 && periodInDays <= 32)) {
389
-    // Roughly weekly or monthly
390
-    var monthValue = (periodInDays <= 9 ? 4 : 1) * averageAmount.mean;
393
+   if (stability < 0.5 && ((periodInDays >= 5 && periodInDays <= 9) || (periodInDays >= 27 && periodInDays <= 32))) {
394
+    // Stable and roughly weekly or monthly
395
+    monthValue = (periodInDays <= 9 ? 4 : 1) * averageAmount.mean;
396
+    periodText = periodInDays <= 9 ? 'Weekly' : 'Monthly';
397
+    classes = 'data';
398
+   } else {
399
+    // Somewhat sporadic
400
+    monthValue = averageAmount.mean * 30.4 / periodInDays;
401
+    periodText = 'Sporadic (~' + periodInDays.toFixed(1) + ' days)';
402
+    classes = 'data sporadic';
403
+   }
391 404
 
392
-    var tr = $('<tr class="data"/>').appendTo(table);
405
+    var tr = $('<tr/>').addClass(classes).appendTo(table);
393 406
     $('<td/>').text(desc).appendTo(tr);
394 407
     $('<td/>').text(this[0].Category ? this[0].Category : 'Unsorted').appendTo(tr);
395
-    $('<td/>').text(periodInDays <= 9 ? 'Weekly' : 'Monthly').appendTo(tr);
408
+    $('<td/>').text(periodText).appendTo(tr);
396 409
     $('<td class="amount"/>').text(averageAmount.mean.toCurrency()).appendTo(tr);
397 410
     $('<td class="amount"/>').text(monthValue.toCurrency()).appendTo(tr);
398 411
 
399 412
     monthTotal += monthValue;
400
-   }
401 413
   }
402 414
  });
403 415
 

+ 1
- 1
index.html View File

@@ -19,7 +19,7 @@
19 19
    #header { background: url('res/gradient.png') repeat-x; height: 100px; margin: 0 0 10px 0; overflow: hidden; z-index: 0; }
20 20
    #header h1 { color: white; margin: 0; font-size: 60px; position: absolute; top: 42px; left: 20px; z-index: 2; }
21 21
    #headershadow { font-size: 60px; position: absolute; top: 42px; left: 25px; font-weight: bold; z-index: 1; }
22
-   h2 { margin-top: 10; }
22
+   h2 { margin-top: 10px; }
23 23
    table { border-collapse: collapse; }
24 24
    tr.alt td { background-color: #ddd; }
25 25
    td { padding: 3px; }

Loading…
Cancel
Save