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

temporality-counter

v0.0.1

Published

Temporality Counter

Downloads

5

Readme

Temporality Counter

Temporality Counter is a simple NodeJS library that efficiently increments counters to a database based on a given temporality.

The library can then query the database and return a total count for a given time period and temporality, for example: * How many users have registered to our website? * How many web hits did my website receive during the last hour? * How many API calls were made since midnight? * How many login attempts did a user try in the last 30 minutes? * How many web purchases have been made in the past 30 days? How many so far this month?

Supported temporalities are:

  • ALL_TIME - Keeps a ever running counter.
  • DAILY - One counter for every day, each starting at midnight.
  • HOURLY - One counter for every hour, each starting at the beginning of the hour.
  • EVERY_MINUTE - One counter for every minute, each starting at the beginning of the minute.

Choose one or more temporalities that fits your requirements. The more granular the temporality (e.g. EVERY_MINUTE) the more rows will be kept in the database, and therefore potentially more expensive query times. If you wanted to know a count for the last 60 minutes then you should choose EVERY_MINUTE, for the last 24 hours then HOURLY, for the last 30 days then DAILY, and for all time then ALL_TIME.

Installation

The following will install https://www.npmjs.org/package/temporality-counter and add it as a dependency to your project:

npm install temporality-counter -S

Install Postgres

OSX:

brew install postgresql
postgres -D /usr/local/var/postgres`

For other platforms see https://wiki.postgresql.org/wiki/Detailed_installation_guides.

Create counter database and table

psql -d postgres -c 'create database counter'
git clone [email protected]:chico/temporality-counter.git
psql -d counter -a -f ./temporality-counter/sql/postgres/counter.sql
psql -d counter -c 'select * from counter'

Database support

Postgres is currently supported, more databases coming soon.

Please submit a new issue to fast track a database that you would like supported.

Usage

Increment counter

var counter = require('temporality-counter');
var counterId = 'mysite-hit', counterType = 'web';

counter.incrementAndGet(counterId, counterType, counter.temporality.EVERY_MINUTE, function(err, count) {
	console.log(count);
});

Get count

var counter = require('temporality-counter');
var counterId = 'mysite-hit', counterType = 'web';

var lastHour = new Date();
lastHour.setHours(start.getHours()-1);

counter.count(counterId, lastHour, counter.temporality.EVERY_MINUTE, function(err, count) {
	console.log(count);
});

counter.countByIdAndType(counterId, counterType, lastHour, counter.temporality.EVERY_MINUTE, function(err, count) {
	console.log(count);
});

Promises

If you prefer to use promises then please use bluebird.

npm install bluebird -S
var Promise = require('bluebird');
var counter = require('temporality-counter');
Promise.promisifyAll(counter);

var counterId = 'mysite-hit', counterType = 'web';

counter.incrementAndGetAsync(counterId, counterType, counter.temporality.EVERY_MINUTE).then(function(count) {
	console.log(count);
});

var lastHour = new Date();
lastHour.setHours(start.getHours()-1);

counter.countAsync(counterId, lastHour, counter.temporality.EVERY_MINUTE).then(function(count) {
	console.log(count);
});

counter.countByIdAndTypeAsync(counterId, counterType, lastHour, counter.temporality.EVERY_MINUTE).then(function(count) {
	console.log(count);
});

Testing

npm install -g mocha
npm test