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

node_ranaly

v0.1.1

Published

Ranaly client library

Downloads

6

Readme

Ranaly - a node.js ranaly client

Build Status

Ranaly可以非常简单地统计项目中的各种数据,本项目是ranaly的node.js客户端。想要了解更多关于ranaly的介绍请访问ranaly项目主页

安装

npm install ranaly

使用方法

首先加载ranaly库:

var ranaly = require('ranaly');

而后创建ranaly连接:

var client = ranaly.createClient(port, host, keyPrefix);

其中posthost是Redis数据库的端口号和主机地址,keyPrefix用来指定ranaly向Redis加入的键的前缀以防止命名冲突,默认值是RANALY:

如果程序中已经使用node_redis库建立了到Redis的连接,也可以将该实例传入createClient函数:

var redis = require('redis').createClient();
var client = ranaly.createClient(redis, keyPrefix);

Ranaly支持3种数据类型,分别是Amount、Realtime和DataList。

Amount

创建一个Amount实例:

var users = new client.Amount('Users');

incr

incr方法用来增加实例的值,如每当有新用户注册时可以通过如下方法增加用户数量:

users.incr(function (err, total) {
	console.log('用户总数为:' + total + '个');
});

incr函数的定义是:

incr([increment, [when, [callback]])

其中increment指增加的数量,默认为1。when指增长发生的时间,Date类型,默认为new Date(),即当前时间。callback的第二个参数返回增长后的总数。

get

get方法用来获取实例在若干个时间的数量,如:

users.get(['20130218', '20130219'], function (err, result) {
	console.log(result);
});

第一个参数是时间的数组,时间的表示方法为YYYYMMDDYYYYMMDDHH。如想获取今天和当前小时的注册用户数量:

var now = moment(); // 需要使用moment库
users.get([now.format('YYYYMMDD'), now.format('YYYYMMDDHH')], function (err, results) {
	console.log('今天新注册的用户数量:' + results[0]);
	console.log('当前小时新注册的用户数量:' + results[1]);
});

sum

sum方法用来获取实例在若干个时间内总共的数量,使用方法和get一样,不再赘述。特例是当第一个参数为空时,sum会返回该Amount实例的总数。如:

users.sum([], function (err, result) {
	console.log('用户总数为:' + total + '个');
});

Realtime

创建一个Realtime实例:

var memory = new client.Realtime('Memory');

incr

incr方法用来递增实例的值,如增加当前内存占用的空间:

memory.incr(1, function (err, result) {
	console.log('当前内存占用为:' + result);
});

其中第一个参数表示增加的数量,如果省略则默认为1。

set

set方法用来设置实例的值,如:

memory.set(20);

get

get方法用来获得实例的值,如:

memory.get(function (err, result) {
	console.log('当前内存占用为:' + result);
});

实时通知

当修改了某个Realtime实例的值后,ranaly会使用Redis的PUBLISH命令派发通知,channel可以通过实例的channel属性获得,如:

var sub = redis.createClient();
sub.subscribe(memory.channel);
sub.on('message', function (channel, message) {
	if (channel === memory.channel) {
		console.log('当前内存占用为:' + message);
	}
});

DataList

创建一个DataList实例:

var userAvatars = new client.DataList('Avatars');

push

push方法用来向实例加入一个元素,可以是字符串、数组、数组或对象类型,如:

userAvatars.push({
	url: 'http://demo.com/avatar.png',
	userID: 17
}, 50, function (err, length) {
});

其中第二个参数表示保留的记录数量,默认为100。

len

len方法用来获得实例的大小,如:

userAvatars.len(function (err, length) {
});

range

range方法用来获得队列中的某个片段,第一个参数表示起始元素索引,第二个元素表示末尾元素索引。索引从0开始,支持负索引,即-1表示队列中最后一个元素。如:

userAvatars.range(0, -1, function (err, avatars) {
	avatars.forEach(function (avatar) {
		console.log(avatar.url);
	});
});