

var pull = require('pull-stream')
var tape = require('tape')

var timeview = require('../')

tape('count all seconds, minutes, hours, days', function (t) {

var agg = {}
var expected = {
  '0': 1000000,
  '1': 999960,
  '2': 16620,
  '3': 267,
  '4': 1
}

  pull(
    pull.count(1000000),
    pull.map(function (i) {
      return {ts: i*1000, value: i%2}
    }),
    pull.through(timeview(timeview.count, function (data, level, id) {
      agg[level] = (agg[level] || 0) + data.value
    })),
    pull.drain(null, function () {

      console.log(agg)
      t.deepEqual(agg, expected)
      t.end()
    })
  )

})

@@ -61,24 +61,32 @@
 ')%0A%0Avar 
-timeview
+TimeBucketReduce
  = requi
@@ -100,54 +100,100 @@
 ')%0A%0A
-tape('count all seconds, minutes, hours, days'
+function create () %7B%0A%0A%0A  return agg%0A%7D%0A%0Afunction test (name, n, step, expected) %7B%0A  tape(name
 , fu
@@ -206,16 +206,20 @@
  (t) %7B%0A%0A
+    
 var agg 
@@ -227,102 +227,169 @@
  %7B%7D%0A
-var expected = %7B%0A  '0': 1000000,%0A  '1': 999960,%0A  '2': 16620,%0A  '3': 267,%0A  '4': 1%0A%7D%0A%0A
+%0A    var TBR = TimeBucketReduce(%7B%0A      output: function (value, start, part) %7B%0A        agg%5Bpart%5D = (agg%5Bpart%5D %7C%7C 0) + 1%0A      %7D,%0A      min: 1%0A    %7D)%0A%0A  
   pull(%0A
     
@@ -384,16 +384,18 @@
   pull(%0A
+  
     pull
@@ -405,18 +405,14 @@
 unt(
-1000000
+n
 ),%0A
+  
     
@@ -441,16 +441,18 @@
 %7B%0A      
+  
 return %7B
@@ -461,12 +461,12 @@
 : i*
-1000
+step
 , va
@@ -475,16 +475,18 @@
 e: i%252%7D%0A
+  
     %7D),%0A
@@ -485,24 +485,26 @@
     %7D),%0A    
+  
 pull.through
@@ -508,238 +508,930 @@
 ugh(
-timeview(timeview.count, function (data, level, id
+TBR),%0A      pull.drain(null, function (
 ) %7B%0A
+%0A
       
-agg%5Blevel%5D = (agg%5Blevel%5D %7C%7C 0) + data.value%0A    %7D)),%0A    pull.drain(null, function () %7B%0A%0A      console.log(agg)%0A      t.deepEqual(agg, expected)%0A      t.end()%0A    %7D)%0A  )%0A
+  console.log(agg)%0A        t.deepEqual(agg, expected)%0A        t.end()%0A      %7D)%0A    )%0A    %0A  %7D)%0A%7D%0A%0Atest('count to 90 seconds', 90, 1000, %7B%0A  Seconds: 90,%0A  Minutes: 1%0A%7D)%0A%0Atest('count to 119 seconds', 119, 1000, %7B%0A  Seconds: 119,%0A  Minutes: 1%0A%7D)%0A%0Atest('count to 120 seconds', 120, 1000, %7B%0A  Seconds: 120,%0A  Minutes: 2%0A%7D)%0A%0Atest('count to 121 seconds', 121, 1000, %7B%0A  Seconds: 121,%0A  Minutes: 2%0A%7D)%0A%0Atest('count seconds to 1 hourseconds', 3600, 1000, %7B%0A  Seconds: 3600,%0A  Minutes: 60,%0A  Hours: 1%0A%7D)%0A%0Atest('count seconds to 1 day', 24*3600, 1000, %7B%0A  Seconds: 24*3600,%0A  Minutes: 24*60,%0A  Hours: 24,%0A  Date: 1%0A%7D)%0A%0A//times above days are not all the same length, so testing is hard.%0A//added +1 to get it to work...%0Atest('count minutes to one year', 365*24*60 + 1, 60000, %7B%0A  Seconds: 365*24*60 + 1,%0A  Minutes: 365*24*60,%0A  Hours: 365*24,%0A  Date: 365,%0A  Month: 12,%0A  FullYear: 1
 %0A%7D)%0A
+%0A%0A

test with different time bucket sizes