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

skinner

v0.4.0

Published

aggregate multi-dimensional data points

Downloads

3

Readme

skinner: aggregate multi-dimensional data points

Skinner provides functions for summarizing a bunch of multi-dimensional data points. It's best described by example. Here are some data points describing populations of US cities:

datapoints = [
    { 'fields': { 'city': 'Springfield', 'state': 'MA' }, 'value': 153000 },
    { 'fields': { 'city': 'Boston',      'state': 'MA' }, 'value': 636000 },
    { 'fields': { 'city': 'Worcestor',   'state': 'MA' }, 'value': 183000 },
    { 'fields': { 'city': 'Fresno',      'state': 'CA' }, 'value': 505000 },
    { 'fields': { 'city': 'Springfield', 'state': 'OR' }, 'value':  60000 },
    { 'fields': { 'city': 'Portland',    'state': 'OR' }, 'value': 600000 }
];

You can sum all of the values:

assert.deepEqual(skinner.aggregate(datapoints), [ 2137000 ]);

In this case, aggregate returned an array of one aggregated data point, which was just the number 2137000.

You can break out the results by 'state':

assert.deepEqual(skinner.aggregate(datapoints, [ 'state' ]),
    [ [ 'MA', 972000 ],
      [ 'CA', 505000 ],
      [ 'OR', 660000 ] ]);

In this case, aggregate summed the values and grouped the sums by state.

Obviously, you can do the same with city name (not including state), which sums results of Springfield, since that's in two states:

assert.deepEqual(skinner.aggregate(datapoints, [ 'city' ]),
    [ [ 'Springfield', 213000 ],
      [ 'Boston',      636000 ],
      [ 'Worcestor',   183000 ],
      [ 'Fresno',      505000 ],
      [ 'Portland',    600000 ] ]);

You can also break out the results by more than one column:

assert.deepEqual(skinner.aggregate(datapoints, [ 'state', 'city' ]),
    [ [ 'MA', 'Springfield', 153000 ],
      [ 'MA', 'Boston',      636000 ],
      [ 'MA', 'Worcestor',   183000 ],
      [ 'CA', 'Fresno',      505000 ],
      [ 'OR', 'Springfield', 60000 ],
      [ 'OR', 'Portland',    600000 ] ]);

The order that you specify breakdowns determines the order in which the values are output. If you do [ 'city', 'state' ], you'll get data points that look like [ 'Springfield', 'MA', 153000 ]. If you do [ 'state', 'city' ], you'll get [ 'MA', 'Springfield', 153000 ].

Bucketizing numbers

Here's a set of data points describing CPU utilization on four 2-CPU systems where the CPUs are called "cpu0" and "cpu1" on each system:

datapoints = [
    { 'fields': { 'host': 'host1', 'cpu': 'cpu0', 'util': 83 }, 'value': 1 },
    { 'fields': { 'host': 'host1', 'cpu': 'cpu1', 'util': 13 }, 'value': 1 },
    { 'fields': { 'host': 'host2', 'cpu': 'cpu0', 'util': 37 }, 'value': 1 },
    { 'fields': { 'host': 'host2', 'cpu': 'cpu1', 'util': 53 }, 'value': 1 },
    { 'fields': { 'host': 'host3', 'cpu': 'cpu0', 'util': 88 }, 'value': 1 },
    { 'fields': { 'host': 'host3', 'cpu': 'cpu1', 'util':  9 }, 'value': 1 },
    { 'fields': { 'host': 'host4', 'cpu': 'cpu0', 'util': 98 }, 'value': 1 },
    { 'fields': { 'host': 'host4', 'cpu': 'cpu1', 'util':  5 }, 'value': 1 }
];

As above, the "value" here is the count of data points having the specified fields (e.g., the count of CPUs on host "host1" with name "cpu0" and utilization "83", which is always 1 in this case).

The difference between this data set and the previous one is that "util" is an arbitrary number, and if we want to break out results by "util", we probably don't want a separate result for every possible utilization value. Instead, we bucketize the utilization values. We'll use a linear bucketizer with step "10", which means we'll group the "util" values into equal-sized buckets of 10 units each:

bucketizers = {
    'util': skinner.makeLinearBucketizer(10)
};

Now, you can summarize CPU utilization across all systems with a single histogram:

var expand = mod_skinner.ordinalToBounds.bind(null, bucketizers.util);
assert.deepEqual(
    expand(skinner.aggregate(datapoints, [ 'util' ], bucketizers)),
    [ [ [ [ 0, 9 ], 2 ],
        [ [ 10, 19 ], 1 ],
        [ [ 30, 39 ], 1 ],
        [ [ 50, 59 ], 1 ],
        [ [ 80, 89 ], 2 ],
        [ [ 90, 99 ], 1 ] ] ]);

These results tell us that we had 2 CPUs with utilization between 0 and 9 (inclusive), 2 CPUs with utilization between 80 and 89, and 1 CPU each having utilizations 10-19, 30-39, and 90-99.

If you leave out the "expand" bit, then you get bucket indexes rather than values. For a linear bucketizer of step 10, bucket index 2 covers ranges 20 to 30. This format is generally more useful when you're shipping data around or doing other aggregations or calculations with it.

You can break out these results by host (and using the un-expanded form):

assert.deepEqual(
    skinner.aggregate(datapoints, [ 'host', 'util' ], bucketizers),
    [ [ 'host1', 1, 1 ],
      [ 'host1', 8, 1 ],
      [ 'host2', 3, 1 ],
      [ 'host2', 5, 1 ],
      [ 'host3', 0, 1 ],
      [ 'host3', 8, 1 ],
      [ 'host4', 0, 1 ],
      [ 'host4', 9, 1 ] ]);

Besides the linear bucketizer, there's a log-linear bucketizer. For details on what that does, see the DTrace llquantize() function. To see how to use it, check the source for makeLogLinearBucketizer.

Streaming interface

For large numbers of data points where you don't want to keep all data points in memory at once, you can use the object-mode streaming interface. You write JSON objects to it, and it emits the final summary. You can also fetch the summary so far at any time using the result() method. For example, using the city/state datapoints above:

bucketizers = {};

stream = skinner.createAggregator({
    'bucketizers': bucketizers,
    'decomps': [ 'city' ]
});

datapoints.forEach(function (pt) { stream.write(pt); });
stream.end();

/* These two print the same thing. */
console.log(stream.result());
stream.on('data', function (result) { console.log(result); });

Notes

Error checking is not great at the moment. (Most input errors result in assertion failures.) Patches welcome.

You might also want to check out krill, which filters similar-looking data points. Between krill and skinner, you can slice and dice data points in lots of different ways.