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

bucket-maker

v0.1.3

Published

bucket-maker makes bucket of sliced timline for each span you requested.

Downloads

2

Readme

bucket-maker

To reduce I/O cost, bucket-maker makes bucket of sliced timline for each unit you requested.
Name of the bucket is defined by its day and slot.

  • day is a number of days from unix epoch
  • slot is a number of unit from 00:00
  • unit is defined by you

This is calculated like as follows:

unit = 5000;                                                // msec
days = Date.now() / (60*60*24*1000);                        // 17764.309366608795
day  = Math.floor(days);                                    // 17764
slot = Math.ceil( (days - day) * (60*60*24*1000) / unit );  // 5346

Demo

$ git clone https://github.com/RayKitajima/Bucket-Maker.git
$ cd Bucket-Maker

$ node demo/timestamp.js

demo/timestamp.js write out timestamp every sec into the bucket.
The bucket is made for every five sec.
So the bucket contains five entries in it.

At a grance

// get 5sec(msec) bucket maker
let bucket = require('bucket-maker').createBucket({
	unit: 5000
});

// put timestamp every sec
setInterval(function(){
	bucket.put(Date.now());
},1000);

// will write out
$ ls 
17764.4761.bucket  17764.4762.bucket

// will contain
$ cat 17764.4762.bucket
1534833405881
1534833406883
1534833407888
1534833408893
1534833409897

Options

Internally bucket is an array. And it will periodically dump and write them by dumper and writer.

| name | what is | |:----------|:--------------------------------------------------------------------| | prefix | prefix for output bucket name | | suffix | suffix for output bucket name. default <suffix> is '.bucket' | | unit | bucket unit as msec | | spool | where to write out bucket file | | dumper | how to dump elements in the bucket. by default assume them as stirng and join() them | | writer | how to write out dumped object. by default write out to file named as <prefix><day>.<slot><suffix>. | | start | whether to start writing at created | | cron_time | frequency of checking bucket | | log | how to system log |

Practice

const pid  = process.pid;
const host = os.hostname();
const prefix = pid + '.' + host + '.';

let bucket = require('bucket-maker').createBucket({
	prefix : prefix,
	suffix : '.bucket',
	unit   : 5000,
	spool  : '/path/to/buckets',
	dumper : function(items){ return items.map( x => JSON.stringify(x) ).join('\n') },
});

setInterval(function(){
	bucket.put(os.loadavg());
},1000);

Reporting load avarage every sec. And make 5sec bucket.

$ ls 
4825.localhost.17764.4761.bucket  4825.localhost.17764.4762.bucket

More reducing I/O cost with redis

const client = require('redis').createClient();

const writer = function(maker){
	let names = Object.keys(maker.Buckets);
	let now = Date.now();
	for( let i=0; i<names.length; i++ ){
		if( maker.Buckets[names[i]].cutoff > now ){ continue; }   // skip: still putting into the bucket
		let dump = maker.dumper(maker.Buckets[names[i]].buffer);
		let key  = name[i];                                       // wait:17764.4762.bucket
		client.set(key,dump,function(err,res){
			if( err ){
				maker.log(err);
			}else{
				delete maker.Buckets[names[i]];
			}
		});
	}
};

let bucket = require('bucket-maker').createBucket({
	prefix : 'wait:',
	unit   : 5000,
	dumper : function(items){ return items.map( x => JSON.stringify(x) ).join('\n') },
	writer : writer,
});

setInterval(function(){
	bucket.put(os.loadavg());
},1000);

This writes out loadavg report into redis.

const client = require('redis').createClient();

const drain = function(){
	client.keys('wait:*',function(err,keys){
		for( let i=0; i<keys.length; i++ ){
			client.get(keys[i],function(err,content){
				let bucket_name = keys[i].split(':')[1]; // 17764.4762.bucket
				fs.writeFileSync(bucket_name,content);
				client.del(keys[i]);
			});
		}
	});
};

setInterval(drain,5000);

This drains bucket to the file system.

If you would like to implement your own writer, you have to know about internal code, and should write several idiom to define what to write out.

License

MIT