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

bitkompagniet-statistics-aggregator

v0.1.2

Published

A group-reduce function interface for statistical presentation

Downloads

5

Readme

Build Status

A slim wrapper of logic and sanity around a standard group-reduce operation that provides an interface specific to statistical data presentation.

It revolves around the concept of sets, dimensions and metrics, which are common to data presentation API's such as Google Adx.

In most cases, you don't need this package. Instead, you should use the relevant database aggregate functions (SQL GROUP BY / SUM or mongodb's .mapReduce(), for example).

Install

npm install --save bitkompagniet-statistics-aggregator

Basic use

It contains just a single function:

var dataset = [
	{ website: 'some.com', date: '2016-06-06', revenue: 56.5 },
	{ website: 'some.com', date: '2016-06-07', revenue: 80 },
	{ website: 'other.com', date: '2016-06-05', revenue: 20.5 },
	{ website: 'other.com', date: '2016-06-05', revenue: 10 },
	{ website: 'other.com', date: '2016-06-06', revenue: 100 },
];

var aggregator = require('bitkompagniet-statistics-aggregator');

var perWebsite = aggregator(dataset, ['website'], { revenue: 'sum' });
// [ 
//   { website: 'some.com', revenue: 136.6 }, 
//   { website: 'other.com', revenue: 130.5 } 
// ];

var perDate = aggregator(dataset, ['date'], { revenue: 'sum' });
// [ 
//   { date: '2016-06-06', revenue: 156.5 }, 
//   { date: '2016-06-05', revenue: 30.5 }, 
//   { date: '2016-06-07', revenue: 80 } 
// ];

API

aggregator(dataset, dimensions, metrics)

dataset array

[ { website: 'some.com', revenue: 80 }, { website: 'other.com', revenue: 70 } ]

Dataset is assumed to be an array of (relatively) uniform plain objects. Some of the keys in these objects are data we want to query, filter, sort and group by. We call these the dimensional fields. Others are those we want to reduce or map. We call these the metrics.

dimensions array

[ 'website', 'date' ]

The dataset is first grouped by a combination of all dimensions. The dimensions are given as an array of strings, each corresponding to an object key. All reductions are performed in these groups, so the resulting set will be of the same length as the number of combinations in the dataset.

metrics object

{ revenue: 'sum', visitors: (total, value) => total + value }

An object where keys represent dataset keys, and values are strings denoting the standard function for aggregation, or, alternatively, a custom function. The only built-in functions at this time is:

  • sum
  • average (untested)

If supplied as a custom function, it has the following signature:

function(total, value, index, item) { ... }
  • total: the accumulated value carried through the reduction.
  • value: the current value.
  • index: the index of the current value.
  • item: the current, entire dataset item.

Returns array

The result will be an array of the aggregation results, similar to the dataset, but will only contain the keys given in dimensions and metrics, respectively. Any extra keys will be removed in the reduction.