Socket
Book a DemoInstallSign in
Socket

bitkompagniet-statistics-aggregator

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bitkompagniet-statistics-aggregator

A group-reduce function interface for statistical presentation

0.1.2
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

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.

FAQs

Package last updated on 07 Jul 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.