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

redis-metrics

v1.4.2

Published

Easy metric tracking and aggregation using Redis

Downloads

37

Readme

redis-metrics

Easy metric tracking and aggregation using Redis.

This module was originally created to provide an easy way of storing and viewing aggregated counter and trend statistics.

In a sense, the libary tries to provide sugar-coated method calls for storing and fetching Redis data to report counts and trends. The first design goal is to make counting simpler.

Read on below or read more on the documentation site.

Build Status codecov

Install

$ npm install --save redis-metrics

Use

Basic counter:

// Create an instance
var RedisMetrics = require('redis-metrics');
var metrics = new RedisMetrics();

// If you need to catch uncaught exceptions, add an error handler to the client.
metrics.client.on('error', function(err) { ... });

// Create a counter for a "pageview" event and increment it three times.
var myCounter = metrics.counter('pageview');
myCounter.incr();
myCounter.incr();
myCounter.incr();

// Fetch the count for myCounter, using a callback.
myCounter.count(function(cnt) {
  console.log(cnt); // Outputs 3 to the console.
});

// Fetch the count for myCounter, using promise.
myCounter.count().then(function(cnt) {
  console.log(cnt); // Outputs 3 to the console.
});

Time-aware counter.

// Create an instance
var RedisMetrics = require('redis-metrics');
var metrics = new RedisMetrics();

// If you need to catch uncaught exceptions, add an error handler to the client.
metrics.client.on('error', function(err) { ... });

// Use the timeGranularity option to specify how specific the counter should be
// when incrementing.
var myCounter = metrics.counter('pageview', { timeGranularity: 'hour' });

// Fetch the count for myCounter for the current year.
myCounter.count('year')
  .then(function(cnt) {
    console.log(cnt); // Outputs 3 to the console.
  });

// Fetch the count for each of the last two hours.
// We are using moment here for convenience.
var moment = require('moment');
var now = moment();
var lastHour = moment(now).subtract(1, 'hours');
myCounter.countRange('hour', lastHour, now)
  .then(function(obj) {
    // "obj" is an object with timestamps as keys and counts as values.
    // For example something like this:
    // {
    //   '2015-04-15T11:00:00+00:00': 2,
    //   '2015-04-15T12:00:00+00:00': 3
    // }
  });

// Fetch the count for each day in the last 30 days
var thirtyDaysAgo = moment(now).subtract(30, 'days');
myCounter.countRange('day', thirtyDaysAgo, now)
  .then(function(obj) {
    // "obj" contains counter information for each of the last thirty days.
    // For example something like this:
    // {
    //   '2015-03-16T00:00:00+00:00': 2,
    //   '2015-03-17T00:00:00+00:00': 3,
    //   ...
    //   '2015-04-15T00:00:00+00:00': 1
    // }
  });

// Fetch the count for the last 60 seconds...
// ... Sorry, you can't do that because the counter is only set up to track by
// the hour.

Redis Namespace

By default keys are stored in Redis as c:{name}:{period}. If you prefer to use a different Redis namespace than c, you can pass this in as an option:

var myCounter = metrics.counter('pageview', { timeGranularity: 'hour', namespace: 'stats' });`

Test

Run tests including code coverage:

$ npm test

Documentation

The internal module documentation is based on jsdoc and can be generated with:

$ npm run docs