npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

timeseries-bins

v3.0.2

Published

Bin points into hours, days, weeks, quarters or years for timeseries plots

Downloads

60

Readme

Timestamp Bins

This little library attempts to emulate how Grafana works with a series of raw data points when you ask it to bin by an interval (hour, day, week, month, quarter, year) over a period of time, aggregating with a function like sum or ave, and filling empty bins with a value or the previous bin.

Input Data

The input data is expected to look something like this:

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

Usage

var timeseries = require( 'timeseries-bins' );
var options = {
  data: data_points,
  timestampField: "timestamp",
  fcn: "sum",
  interval: "hour",
  start: 1378511041582,
  end: 1378511841582,
  tz: "America/Los_Angeles",
  fill: 0,
};
timeseries( options, function( err, bins ) {
  if ( err ) exit( err );
  console.table( bins );
});

Options

"interval" can be any interval that day.js understands (day, week, month, quarter, year). It can also be something like "15min" or "30sec" and if so, will behave like InfluxDB (round down to the nearest period specified and create buckets every period specified). "fcn" can be one of "sum", "mean", "min", "max", "count". "fill" can be either a number, or the string "previous", "nans", "zeros".

If interval is not specified or is null, then you will get a rollup ... am array with a single point which performed the function over all of the raw data points.

Your data points can contain values that are strings. These are passed though unmodified. The bin will contain the string value that was in the last point dropped into that bin.

The "fcn" can be an object instead of a string. If your data points have multiple values, and you want to aggregate different values with different functions, you can:

  fcn: {
    speed: "mean",
    fuel: "sum",
    odometer: "max"
  }

You can also add an option "fields" option, an array of field names you want returned in the final binned result.

Promises

If the callback argument is not supplied, the function will return a promise.