Git Product home page Git Product logo

timestream-aggregates's Introduction

timestream-aggregates

NPM

Aggregation functions for objectMode streams. Contains a set of stream Transforms that accept objectMode streams with a sequenceKey and aggregate all other values of each record into chunks at regular intervals.

This is most useful for timeseries data as the chunked aggregation function is designed to slice data by time.

The interval slicing function is floordate.

var spigot = require("stream-spigot")
var agg = require("timestream-aggregates")
var concat = require("concat-stream")

function series() {
  return spigot({objectMode: true}, [
    {time: 1378511041582, speed: 1, odometer: 0,   fuel: 100},
    {time: 1378511141582, speed: 4, odometer: 11,  fuel: 98},
    {time: 1378511241582, speed: 3, odometer: 22,  fuel: 97},
    {time: 1378511341582, speed: 25, odometer: 99,  fuel: 76},
    {time: 1378511441582, speed: 50, odometer: 155, fuel: 70},
    {time: 1378511541582, speed: 50, odometer: 241, fuel: 62},
    {time: 1378511641582, speed: 122, odometer: 755, fuel: 18},
    {time: 1378511741582, speed: 31, odometer: 780, fuel: 15},
    {time: 1378511841582, speed: 0, odometer: 780, fuel: 15},
  ])
}

series()
  .pipe(agg.sum("time"))
  .pipe(concat(console.log))

/*
[ { time: 0, speed: 286, odometer: 2843, fuel: 551 } ]
*/

series()
  .pipe(agg.sum("time", "hour"))
  .pipe(concat(console.log))

/*
[ { time: 1378508400000, speed: 1, odometer: 0, fuel: 100 },
  { time: 1378512000000, speed: 82, odometer: 287, fuel: 341 },
  { time: 1378515600000, speed: 203, odometer: 1776, fuel: 95 },
  { time: 1378519200000, speed: 0, odometer: 780, fuel: 15 } ]
*/

series()
  .pipe(agg.mean("time", "hour"))
  .pipe(concat(console.log))

/*
[ { time: 1378508400000, speed: 1, odometer: 0, fuel: 100 },
  { time: 1378512000000, speed: 20.5, odometer: 71.75, fuel: 85.25 },
  { time: 1378515600000, speed: 67.66666666666667, odometer: 592, fuel: 31.666666666666668 },
  { time: 1378519200000, speed: 0, odometer: 780, fuel: 15 } ]
*/

API

All aggregates accept an interval slice that it will partition the streams into. This can either be a raw number, or any of the intervals accepted by floordate:

  • s, sec, secs, second, seconds
  • m, min, mins, minute, minutes
  • h, hr, hrs, hour, hours
  • d, day, days
  • w, wk, wks, week, weeks
  • M, mon, mons, month, months
  • q, qtr, qtrs, quarter, quarters
  • y, yr, yrs, year, years

If no interval is specified, the operation is applied over every record resulting in a single record.

sum(seqKey [,interval])

Sums all numeric values during each interval by key. Uses the stats-lite library.

mean(seqKey [,interval])

Averages (mean) all numeric values during each interval by key. Uses the stats-lite library.

mode(seqKey [,interval])

Averages (mode) all numeric values during each interval by key. Uses the stats-lite library.

median(seqKey [,interval])

Averages (median) all numeric values during each interval by key. Uses the stats-lite library.

variance(seqKey [,interval])

Calculates the variance of all numeric values during each interval by key. Uses the stats-lite library.

stdev(seqKey [,interval])

Calculates the standard deviation of all numeric values during each interval by key. Uses the stats-lite library.

percentile(seqKey [,interval], percent)

Calculates the specified percentile of all numeric values during each interval by key. Uses the stats-lite library.

min(seqKey [,interval])

Returns records where each key is the minimum value (Math.min) in each interval by key.

max(seqKey [,interval])

Returns records where each key is the maximum value (Math.max) in each interval by key.

count(seqKey [,interval])

Returns records where each key is the number of values in each interval by key.

first(seqKey [,interval])

Returns records where each key is the first (chronologically) value in each interval by key.

last(seqKey [,interval])

Returns records where each key is the last (chronologically) value in each interval by key.

sample(seqKey [,interval])

Returns records where each key a random member of the records in each interval by key.

LICENSE

MIT

timestream-aggregates's People

Contributors

brycebaril avatar

Stargazers

Vladislav Lewin avatar Ashemah Harrison avatar Dmitry Chusovitin avatar  avatar timelyportfolio avatar ivan l. avatar Athan avatar Alexandre Strzelewicz avatar Andrejs Jermakovics avatar

Watchers

 avatar timelyportfolio avatar James Cloos avatar

Forkers

dulichan

timestream-aggregates's Issues

Tests failing on node >= v0.11

Hello,

we've some got problems under node v0.11.14.

Cannot read property 'v' of undefined

[root@localhost timestream-aggregates]# npm test

> [email protected] test /tmp/timestream-aggregates
> node test/

TAP version 13
# init
ok 1 sum is a function
ok 2 mean is a function
ok 3 median is a function
ok 4 mode is a function
ok 5 variance is a function
ok 6 stdev is a function
ok 7 percentile is a function
ok 8 min is a function
ok 9 max is a function
ok 10 first is a function
ok 11 last is a function
ok 12 count is a function
ok 13 sample is a function
# sum entire series
ok 14 Got expected results
# sum entire series (with timestamp)
not ok 15 Got expected results
  ---
    operator: deepEqual
    expected: [{"_t":1378450800000,"gen":55}]
    actual:   [{"_t":1378425600000,"gen":55}]
    at: check (/tmp/timestream-aggregates/test/index.js:46:7)
  ...
 # sum multi column
 /tmp/timestream-aggregates/node_modules/tape/index.js:65
     throw err
             ^
TypeError: Cannot read property 'v' of undefined
    at Transform.slot [as _transform] (/tmp/timestream-aggregates/factory.js:42:35)
    at Transform._read (/tmp/timestream-aggregates/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:184:10)
    at Transform._write (/tmp/timestream-aggregates/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:172:12)
    at doWrite (/tmp/timestream-aggregates/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:237:10)
    at writeOrBuffer (/tmp/timestream-aggregates/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:227:5)
    at Transform.Writable.write (/tmp/timestream-aggregates/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:194:11)
    at Spigot.ondata (_stream_readable.js:539:20)
    at Spigot.emit (events.js:107:17)
    at readableAddChunk (_stream_readable.js:162:16)
    at Spigot.Readable.push (_stream_readable.js:125:10)
npm ERR! Test failed.  See above for more details.

Test "sum entire series (with timestamp)" failing on node v0.10.35

[root@localhost timestream-aggregates]# npm test

> [email protected] test /tmp/node_modules/timestream-aggregates
> node test/

TAP version 13
# init
ok 1 sum is a function
ok 2 mean is a function
ok 3 median is a function
ok 4 mode is a function
ok 5 variance is a function
ok 6 stdev is a function
ok 7 percentile is a function
ok 8 min is a function
ok 9 max is a function
ok 10 first is a function
ok 11 last is a function
ok 12 count is a function
ok 13 sample is a function
# sum entire series
ok 14 Got expected results
# sum entire series (with timestamp)
not ok 15 Got expected results
  ---
    operator: deepEqual
    expected: [{"_t":1378450800000,"gen":55}]
    actual:   [{"_t":1378425600000,"gen":55}]
    at: check (/tmp/node_modules/timestream-aggregates/test/index.js:46:7)
  ...
# sum multi column
ok 16 Got expected results
# partial sum
ok 17 Got expected results
# mean entire series
ok 18 Got expected results
# partial mean
ok 19 Got expected results
# mode entire series
ok 20 Got expected results
# partial mode
ok 21 Got expected results
# median entire series
ok 22 Got expected results
# partial median
ok 23 Got expected results
# variance entire series
ok 24 Got expected results
# partial variance
ok 25 Got expected results
# stdev entire series
ok 26 Got expected results
# partial stdev
ok 27 Got expected results
# percentile entire series
ok 28 Got expected results
# partial percentile
ok 29 Got expected results
# min entire series
ok 30 Got expected results
# partial min
ok 31 Got expected results
# max entire series
ok 32 Got expected results
# partial max
ok 33 Got expected results
# count entire series
ok 34 Got expected results
# partial count
ok 35 Got expected results
# first entire series
ok 36 Got expected results
# partial first
ok 37 Got expected results
# last entire series
ok 38 Got expected results
# partial last
ok 39 Got expected results
# sample entire series
ok 40 Has correct timestamp
ok 41 Random value is in correct range
# partial sample
ok 42 Has correct timestamp
ok 43 Random value is in correct range
ok 44 Has correct timestamp
ok 45 Random value is in correct range
ok 46 Has correct timestamp
ok 47 Random value is in correct range

1..47
# tests 47
# pass  46
# fail  1

npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.