From 36930d666dc4613107165c8871cade3a8f25fbc7 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sat, 8 Jun 2013 22:26:03 -0700 Subject: [PATCH] rename some commands & update readme --- Readme.md | 74 ++++++++++++++++++++++++++++++++++------------------ bin/kwikemon | 4 +-- kwikemon.js | 26 +++++++++--------- server.js | 4 +-- test.js | 22 ++++++++-------- 5 files changed, 76 insertions(+), 54 deletions(-) diff --git a/Readme.md b/Readme.md index 849d626..2209893 100644 --- a/Readme.md +++ b/Readme.md @@ -11,28 +11,27 @@ npm install -g kwikemon ## Usage $ kwikemond & - $ curl -s localhost/nginx_status | grep Active | kwikemon nginx-connections + $ curl -s localhost/nginx_status | grep Active | kwikemon write nginx-connections $ curl localhost:1111/nignx-connections Active connections: 316 - $ kwikemon foo bar + $ kwikemon set foo bar $ curl localhost:1111/ foo: bar nginx-connections: Active connections: 316 Here's how it works: -- call `kwikemon thing status` to set the text for the monitor named "thing" +- call `kwikemon set thing status` to set the text for the monitor named "thing" - fire up the server, `kwikemond`, that serves up these monitors in a big list or individually Alternatively: -- continuously pipe data to `kwikemon ` on stdin +- continuously pipe data to `kwikemon write ` on stdin - every time a full line of text is received on stdin it becomes the new status for -To see everything `kwikemon` can do run it without arguments. +To see everything `kwikemon` can do run `kwikemon help`. - # or with -h or --help - $ kwikemon + $ kwikemon help This is very much a work in progress. @@ -44,12 +43,12 @@ You can use kwikemon as a library. var kwikemon = require('kiwkemon'); kwikemon.set('foo', 'bar', function(err) { - kwikemon.fetch('foo', function(err, text) { + kwikemon.get('foo', function(err, text) { console.log('foo = ' + text); }); }); - kwikemon.fetchAll(function(err, monitors) { + kwikemon.getAll(function(err, monitors) { Object.keys(monitors).forEach(function (name) { console.log(name + ' = ' + monitors[name]); }); @@ -70,28 +69,14 @@ A monitor named `nginx` stores its data in the hash `kwikemon:monitor:nginx`. Ha are: - text + - expire - created - modified - updates The list of all monitors is a set stored at `kwikemon:monitors`. -#### List - -This is when you should clean out expired entries. - - names = redis.smembers("kwikemon:monitors") - monitors = {} - for name in names: - if redis.exists("kwikemon:monitor:$name"): - monitors[name] = redis.hget("kwikemon:monitor:$name", "text") - return monitors - -#### Read - - redis.hgetall("kwikemon:monitor:nginx") - -#### Update +#### Set exists = redis.exists("kwikemon:monitor:nginx") if exists: @@ -111,11 +96,48 @@ This is when you should clean out expired entries. # optional redis.expire("kwikemon:monitor:nginx", ) -#### Delete +#### Get + + redis.hgetall("kwikemon:monitor:nginx") + +#### Remove redis.del("kwikemon:monitor:nginx") redis.srem("kwikemon:monitors", "nginx") +#### Sweep + +Clean out expired monitors. Call this before anything that relies on counting or iterating through all monitors. + + for name in redis.smembers("kwikemon:monitors"): + if not redis.exists("kwikemon:monitor:$name"): + remove(name) + +#### Count + +Sweep before running a count. + + sweep() + redis.scard("kwikemon:monitors") + +#### List names + +Sweep before listing. + + sweep() + redis.smembers("kwikemon:monitors") + +#### Get all + +Sweep before geting all. + + sweep() + monitors = {} + for name in list(): + if redis.exists("kwikemon:monitor:$name"): + monitors[name] = get(name) + return monitors + ## License diff --git a/bin/kwikemon b/bin/kwikemon index 56b5b6b..78e9f75 100755 --- a/bin/kwikemon +++ b/bin/kwikemon @@ -64,7 +64,7 @@ function expire(name, ttl) { function get(name) { if (name) { - kwikemon.fetch(name, function(err, mon) { + kwikemon.get(name, function(err, mon) { if (mon) { console.log(mon.text); process.exit(0); @@ -82,7 +82,7 @@ function get(name) { } function list() { - kwikemon.fetchAll(function(err, monitors) { + kwikemon.getAll(function(err, monitors) { Object.keys(monitors).forEach(function(name) { console.log(name + ':', monitors[name].text); }); diff --git a/kwikemon.js b/kwikemon.js index 924de29..9e772e7 100644 --- a/kwikemon.js +++ b/kwikemon.js @@ -8,11 +8,11 @@ module.exports = { // read , exists: callbackOptional(exists) -, fetch: callbackOptional(fetch) +, get: callbackOptional(get) , ttl: callbackOptional(ttl) -, list: list -, fetchAll: fetchAll , count: count +, list: list +, getAll: getAll // remove , remove: callbackOptional(remove) @@ -56,15 +56,15 @@ function redis(newRedis) { // that accepts the callback is returned, with the // rest of the arguments fixed (like bind). // -// function fetch(id, cb) { db.fetch(id, cb); } -// fetch = callbackOptional(fetch); +// function get(id, cb) { db.get(id, cb); } +// get = callbackOptional(get); // // function print(err, x) { if (err) throw err; console.log(x); } // -// fetch(1, print); +// get(1, print); // -// var fetch1 = fetch(1); -// fetch1(print); +// var get1 = get(1); +// get1(print); function callbackOptional(fn, ctx) { return function() { var args = Array.prototype.slice.call(arguments); @@ -135,7 +135,7 @@ function writer(name) { return le; } -function fetch(name, cb) { +function get(name, cb) { redis().hgetall(k(name), cb); } @@ -202,20 +202,20 @@ function list(cb) { }); } -function fetchAll(cb) { +function getAll(cb) { var monitors = {}; list(function(err, names) { if (err) return cb(err); - var fetchers = names.sort().map(function(name) { + var geters = names.sort().map(function(name) { return function(done) { - fetch(name, function(err, text) { + get(name, function(err, text) { if (err) return done(err); monitors[name] = text; done(); }); }; }); - async.parallel(fetchers, function(err, _) { + async.parallel(geters, function(err, _) { if (err) return cb(err); cb(null, monitors) }); diff --git a/server.js b/server.js index 23ce653..1c41287 100644 --- a/server.js +++ b/server.js @@ -40,7 +40,7 @@ function handleRequest(req, res) { name = name.replace(RegExp('\.' + type + '$'), ''); } if (name) { - kwikemon.fetch(name, function(err, text) { + kwikemon.get(name, function(err, text) { if (err) { res.end('error: ' + (err.message || 'unknown')); return; @@ -50,7 +50,7 @@ function handleRequest(req, res) { } // all else { - kwikemon.fetchAll(function(err, monitors) { + kwikemon.getAll(function(err, monitors) { if (err) { res.end('error: ' + (err.message || 'unknown')); return; diff --git a/test.js b/test.js index c768289..55042a0 100755 --- a/test.js +++ b/test.js @@ -19,7 +19,7 @@ describe("kwikemon", function() { describe("#set", function() { it("should set text", function(done) { kwikemon.set('foo', 'bar', function(err) { - kwikemon.fetch('foo', function(err, mon) { + kwikemon.get('foo', function(err, mon) { assert(mon.text == 'bar'); done(); }); @@ -28,7 +28,7 @@ describe("kwikemon", function() { it("should overwrite text", function(done) { kwikemon.set('foo', 'baz', function(err) { - kwikemon.fetch('foo', function(err, mon) { + kwikemon.get('foo', function(err, mon) { assert(mon.text == 'baz'); done(); }); @@ -92,12 +92,12 @@ describe("kwikemon", function() { }); }); - describe("#fetch", function() { - it("should fetch the last text monitored", function(done) { + describe("#get", function() { + it("should get the last text monitored", function(done) { async.series([ kwikemon.set('foo', 'bar') , kwikemon.set('foo', 'marcellus') - , kwikemon.fetch('foo') + , kwikemon.get('foo') ], function(err, results) { var mon = results[2]; @@ -107,8 +107,8 @@ describe("kwikemon", function() { ); }); - it("should fetch null for non-existent monitors", function(done) { - kwikemon.fetch('non-existent', function(err, mon) { + it("should get null for non-existent monitors", function(done) { + kwikemon.get('non-existent', function(err, mon) { assert(mon == null); done(); }); @@ -116,7 +116,7 @@ describe("kwikemon", function() { }); describe("#ttl", function() { - it("should fetch the last TTL set", function(done) { + it("should get the last TTL set", function(done) { kwikemon.set('foo', 'bar', { ttl: 300 }, function(err) { kwikemon.ttl('foo', function(err, ttl) { assert(ttl <= 300); @@ -144,13 +144,13 @@ describe("kwikemon", function() { }); }); - describe("#fetchAll", function() { - it("should fetch all monitors", function(done) { + describe("#getAll", function() { + it("should get all monitors", function(done) { async.series([ kwikemon.set('a', '1') , kwikemon.set('b', '2') , kwikemon.set('c', '3') - , kwikemon.fetchAll + , kwikemon.getAll ], function(err, results) { var monitors = results.pop()