# counter-by-time-bucket

> Counter By Time Bucket ==========

Latest version **1.0.1** (published 2016-12-20) · 0 weekly downloads

## Install

```sh
npm install counter-by-time-bucket
pnpm add counter-by-time-bucket
yarn add counter-by-time-bucket
bun add counter-by-time-bucket
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2016-12-20 |
| First published | 2014-10-15 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | henry42 |

## Links

- npm: https://www.npmjs.com/package/counter-by-time-bucket
- Repository: https://github.com/henry42/counter-by-time-bucket
- Homepage: https://github.com/henry42/counter-by-time-bucket#readme
- Issues: https://github.com/henry42/counter-by-time-bucket/issues
- npm.io page: https://npm.io/package/counter-by-time-bucket

## Recent versions

- 1.0.1 (latest) — 2016-12-20
- 1.0.0 — 2014-10-15

## README

Counter By Time Bucket
==========

A simple counter base time, is used to get counts in recent time period.

##Install:

    npm install counter-by-time-bucket

##Usage:
Example:

    var TimeBucket = require('counter-by-time-bucket');

    var timeBucket = new TimeBucket(3000,1000);


    //add a to 4 in 100ms
    setTimeout(function(){
       timeBucket.put('a',2);
       timeBucket.put('a',2);
    },100);

    //add a to 6 in 1100ms
    setTimeout(function(){
       timeBucket.put('a',2);
    },1100);

    //add a to 7 in 2100ms
    setTimeout(function(){
       timeBucket.put('a',1);
    },2100);

    /*
    +-----+   +-----+   +-----+ 
    |     |   |     |   |     | 
    |  4  |   |  2  |   |  1  | 
    |     |   |     |   |     | 
    +-----+   +-----+   +-----+ 
    0       1s        2s      3s
    */

    //get last 3s a's count after 3100ms
    setTimeout(function(){
       console.info(timeBucket.get('a',3000),'should be 7');
    },3100);


    //get last 3s a's count after 5100ms
    setTimeout(function(){
       console.info(timeBucket.get('a',3000),'should be 1');
    },5100);

###new TimeBucket( totalSize , bucketSize )

returns `timeBucket` object, you must assign totalSize and bucketSize (millisecond).

###timeBucket.put( key , value )

add `value` right now, `value` must be a number.

###timeBucket.get( key , span )

returns the `key` count in last `span` millisecond. It will throw error if `span` is larger than totalSize.

---
_Source: https://npm.io/package/counter-by-time-bucket · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
