Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from nodejitsu/feature-meta
Add "meta" property to event data and associated Reactors
- Loading branch information
Showing
10 changed files
with
490 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* all-meta.js: Stream for filtering events with a given meta key (or set of keys). | ||
* | ||
* (C) 2012, Nodejitsu Inc. | ||
* | ||
*/ | ||
|
||
var util = require('util'), | ||
HasMeta = require('./has-meta'); | ||
|
||
// | ||
// ### function AllMeta (keys|key0, key1, ..., keyN) | ||
// #### @keys|key0..keyN {Array|arguments} Full set of keys to filter over. | ||
// Constructor function of the AllMeta stream responsible for filtering | ||
// events with any of a given key (or set of keys). | ||
// | ||
var AllMeta = module.exports = function () { | ||
HasMeta.apply(this, arguments); | ||
this.type = 'all'; | ||
}; | ||
|
||
// | ||
// Inherit from HasMeta reactor. | ||
// | ||
util.inherits(AllMeta, HasMeta); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* any-meta.js: Stream for filtering events with any of a given meta key (or set of keys). | ||
* | ||
* (C) 2012, Nodejitsu Inc. | ||
* | ||
*/ | ||
|
||
var util = require('util'), | ||
HasMeta = require('./has-meta'); | ||
|
||
// | ||
// ### function AnyMeta (keys|key0, key1, ..., keyN) | ||
// #### @keys|key0..keyN {Array|arguments} Full set of keys to filter over. | ||
// Constructor function of the AnyMeta stream responsible for filtering | ||
// events with any of a given key (or set of keys). | ||
// | ||
var AnyMeta = module.exports = function () { | ||
HasMeta.apply(this, arguments); | ||
this.type = 'any'; | ||
}; | ||
|
||
// | ||
// Inherit from HasMeta reactor. | ||
// | ||
util.inherits(AnyMeta, HasMeta); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* has-meta.js: Stream for filtering events with a given meta key (or set of keys). | ||
* | ||
* (C) 2012, Nodejitsu Inc. | ||
* | ||
*/ | ||
|
||
var util = require('util'), | ||
ReadWriteStream = require('../common/read-write-stream'); | ||
|
||
// | ||
// ### function HasMeta ([type], keys|key0, key, ..., keyN) | ||
// #### @type {any|all} Type of tag filtering to perform: any or all. | ||
// #### @keys|key0..keyN {Array|arguments} Full set of keys to filter over. | ||
// Constructor function of the HasMeta stream responsible for filtering | ||
// events with a given meta key (or set of keys). | ||
// | ||
var HasMeta = module.exports = function (type) { | ||
ReadWriteStream.call(this); | ||
|
||
var keys = Array.prototype.slice.call(arguments); | ||
|
||
if (type === 'any' || type === 'all') { | ||
this.type = type; | ||
keys.splice(0, 1); | ||
} | ||
else { | ||
this.type === 'any'; | ||
} | ||
|
||
// | ||
// Create a lookup table of any values provided. | ||
// If no value is provided set the key to `null` | ||
// since we will be checking for `undefined`. | ||
// | ||
this.lookup = keys.reduce(function (all, key) { | ||
if (Array.isArray(key)) { | ||
key.forEach(function (kkey) { | ||
all[kkey] = null; | ||
}); | ||
} | ||
else if (typeof key === 'object') { | ||
Object.keys(key).forEach(function (kkey) { | ||
all[kkey] = key[kkey]; | ||
}); | ||
} | ||
else { | ||
all[key] = null; | ||
} | ||
|
||
return all; | ||
}, {}); | ||
|
||
// | ||
// Store the list of keys for iterating over later. | ||
// | ||
this.keys = Object.keys(this.lookup); | ||
}; | ||
|
||
// | ||
// Inherit from ReadWriteStream | ||
// | ||
util.inherits(HasMeta, ReadWriteStream); | ||
|
||
// | ||
// ### function write (data) | ||
// #### @data {Object} JSON to rollup. | ||
// Only filters `data` according to `this.keys`. | ||
// | ||
HasMeta.prototype.write = function (data) { | ||
var self = this; | ||
|
||
// | ||
// If there are no tags on the data return | ||
// | ||
if (!data.meta) { | ||
return; | ||
} | ||
|
||
// | ||
// Helper function for checking a given `key`. | ||
// | ||
function hasKey(key) { | ||
return data.meta[key] !== undefined | ||
&& (self.lookup[key] === null || | ||
self.lookup[key] === data.meta[key]); | ||
} | ||
|
||
var valid = this.type === 'all' | ||
? this.keys.every(hasKey) | ||
: this.keys.some(hasKey); | ||
|
||
if (!valid) { | ||
return; | ||
} | ||
|
||
this.emit('data', data); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* meta.js: Stream for setting the value (and any meta) from a second reactor on any data received. | ||
* | ||
* (C) 2013, Nodejitsu Inc. | ||
* | ||
*/ | ||
|
||
var util = require('util'), | ||
ReadWriteStream = require('../common/read-write-stream'), | ||
Reactor = require('./reactor'); | ||
|
||
// | ||
// ### function Meta (tag, reactor) | ||
// #### @key {string} Meta key to use for the value of `reactor`. | ||
// #### @reactor {godot.reactor().type()} Reactor to be created | ||
// Constructor function for the Meta stream responsible for setting | ||
// the value (and any meta) from a second reactor on any data received. | ||
// | ||
var Meta = module.exports = function (key, reactor) { | ||
ReadWriteStream.call(this); | ||
|
||
var self = this; | ||
|
||
this.key = key; | ||
this.stream = new ReadWriteStream(); | ||
this.reactor = reactor.createStream(this.stream); | ||
this.reactor.on('data', function (data) { | ||
data.meta = data.meta || {}; | ||
data.meta[key] = data.metric; | ||
data.metric = data._metric; | ||
delete data._metric; | ||
self.emit('data', data); | ||
}); | ||
}; | ||
|
||
// | ||
// Inherit from ReadWriteStream | ||
// | ||
util.inherits(Meta, ReadWriteStream); | ||
|
||
// | ||
// ### function write (data) | ||
// Writes the `data` to the meta stream associated | ||
// with this instance and sets `_metric` so it can | ||
// be replaced later on. | ||
// | ||
Meta.prototype.write = function (data) { | ||
data._metric = data.metric; | ||
this.stream.write(data); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
[ | ||
{ | ||
"service": "charlie/app/health/ping", | ||
"metric": 1, | ||
"meta": { | ||
"a": 1, | ||
"b": 1, | ||
"c": 1 | ||
} | ||
}, | ||
{ | ||
"service": "charlie/app/health/ping", | ||
"metric": 1, | ||
"meta": { | ||
"a": 1 | ||
} | ||
}, | ||
{ | ||
"service": "charlie/app/health/ping", | ||
"metric": 1, | ||
"meta": { | ||
"a": 1, | ||
"b": 1, | ||
"c": 1 | ||
} | ||
}, | ||
{ | ||
"service": "charlie/app/health/ping", | ||
"metric": 1, | ||
"meta": { | ||
"a": 1 | ||
} | ||
}, | ||
{ | ||
"service": "charlie/app/health/ping", | ||
"metric": 1, | ||
"meta": { | ||
"a": 1, | ||
"b": 1, | ||
"c": 1 | ||
} | ||
}, | ||
{ | ||
"service": "charlie/app/health/ping", | ||
"metric": 1, | ||
"meta": { | ||
"a": 1 | ||
} | ||
}, | ||
{ | ||
"service": "charlie/app/health/ping", | ||
"metric": 1, | ||
"meta": { | ||
"a": 1, | ||
"b": 1, | ||
"c": 1 | ||
} | ||
}, | ||
{ | ||
"service": "charlie/app/health/ping", | ||
"metric": 1, | ||
"meta": { | ||
"a": 1 | ||
} | ||
}, | ||
{ | ||
"service": "charlie/app/health/ping", | ||
"metric": 1, | ||
"meta": { | ||
"a": 1, | ||
"b": 1, | ||
"c": 1 | ||
} | ||
}, | ||
{ | ||
"service": "charlie/app/health/ping", | ||
"metric": 1, | ||
"meta": { | ||
"a": 1 | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.