-
Notifications
You must be signed in to change notification settings - Fork 1
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 #1 from ashpool/v0.0.2-beta
V0.0.2 beta
- Loading branch information
Showing
10 changed files
with
138 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,3 +28,5 @@ node_modules | |
.idea/ | ||
.coverrun | ||
reports/ | ||
.coverdata/ | ||
debug/ |
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 |
---|---|---|
@@ -1,41 +1,34 @@ | ||
var CarbonClient = require('./carbon'); | ||
var client = require('./carbon'), | ||
metric = require('./metric'); | ||
|
||
function GraphiteClient (properties) { | ||
this._carbon = properties.carbon; | ||
} | ||
|
||
GraphiteClient.createClient = function (carbonDsn) { | ||
return new this({ | ||
carbon: new CarbonClient({dsn: carbonDsn}) | ||
carbon: new client({url: carbonDsn}) | ||
}); | ||
}; | ||
|
||
GraphiteClient.flatten = function (obj, flat, prefix) { | ||
flat = flat || {}; | ||
prefix = prefix || ''; | ||
|
||
for (var key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
var value = obj[key]; | ||
if (typeof value === 'object') { | ||
this.flatten(value, flat, prefix + key + '.'); | ||
} else { | ||
flat[prefix + key] = value; | ||
} | ||
} | ||
} | ||
|
||
return flat; | ||
}; | ||
|
||
/** | ||
* Writes metric with timestamp to Graphite | ||
* | ||
* @param metrics an object with values, e.g. {home:{indoor:{temp:21.2}}} | ||
* @param timestamp defaults to Date.now() | ||
* @returns {RSVP.Promise} a promise | ||
*/ | ||
GraphiteClient.prototype.write = function (metrics, timestamp) { | ||
timestamp = timestamp || Date.now(); | ||
timestamp = Math.floor(timestamp / 1000); | ||
return this._carbon.write(GraphiteClient.flatten(metrics), timestamp); | ||
return this._carbon.write(metric.flatten(metrics), timestamp); | ||
}; | ||
|
||
/** | ||
* Ends the connection | ||
*/ | ||
GraphiteClient.prototype.end = function () { | ||
this._carbon.end(); | ||
return this._carbon.end(); | ||
}; | ||
|
||
module.exports = GraphiteClient; |
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,18 @@ | ||
module.exports = { | ||
flatten: function (obj, flat, prefix) { | ||
flat = flat || {}; | ||
prefix = prefix || ''; | ||
|
||
for (var key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
var value = obj[key]; | ||
if (typeof value === 'object') { | ||
this.flatten(value, flat, prefix + key + '.'); | ||
} else { | ||
flat[prefix + key] = value; | ||
} | ||
} | ||
} | ||
return flat; | ||
} | ||
}; |
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,37 @@ | ||
/*jshint undef:false */ | ||
var chaiAsPromised = require('chai-as-promised'), | ||
chai = require('chai'); | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
describe('CarbonClient', function () { | ||
var Carbon = require('./../lib/carbon'), | ||
client = new Carbon({dsn: 'plaintext://127.0.0.1:2003/'}), | ||
socketMock = {}; | ||
|
||
describe('#write', function () { | ||
before(function () { | ||
client._socket = socketMock; | ||
}); | ||
|
||
it('writes flattened metrics encoded as utf-8', function (done) { | ||
var metric = {'home.indoor.temp': 21.2}, | ||
timestamp = 1427727486200; | ||
socketMock.write = function (lines, encoding, cb) { | ||
lines.should.equal('home.indoor.temp 21.2 1427727486200\n'); | ||
encoding.should.equal('utf-8'); | ||
cb(); | ||
}; | ||
client.write(metric, timestamp).should.eventually.equal('home.indoor.temp 21.2 1427727486200\n').notify(done); | ||
}); | ||
it('rejects when a socket error occur', function (done) { | ||
var metric = {'home.indoor.temp': 21.2}, | ||
timestamp = 1427727486200; | ||
socketMock.write = function (lines, encoding, cb) { | ||
cb(new Error('fail')); | ||
}; | ||
client.write(metric, timestamp).should.eventually.be.rejected.notify(done); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
require('./carbon'); | ||
require('./graphite'); | ||
require('./graphite'); | ||
require('./metric'); |
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,29 @@ | ||
/*jshint undef:false */ | ||
var chaiAsPromised = require('chai-as-promised'), | ||
chai = require('chai'), | ||
metric = require('./../lib/metric'); | ||
|
||
chai.should(); | ||
chai.use(chaiAsPromised); | ||
|
||
describe('metric', function () { | ||
describe('#flatten', function () { | ||
it('flattens objects', function () { | ||
metric.flatten({home: {indoor: {temp: 21.2}}}).should.deep.equal({'home.indoor.temp': 21.2}); | ||
}); | ||
it('flattens objects two properties', function () { | ||
metric.flatten({home: {indoor: {temp: 21.2, humidity: 94.5}}}) | ||
.should.deep.equal({'home.indoor.humidity': 94.5, 'home.indoor.temp': 21.2}); | ||
}); | ||
it('flattens objects three properties', function () { | ||
metric.flatten({foz: {baz: {foo: 42, bar: 1337, soz: 101}}}) | ||
.should.deep.equal({'foz.baz.bar': 1337, 'foz.baz.foo': 42, 'foz.baz.soz': 101}); | ||
}); | ||
it('handles empty objects', function () { | ||
metric.flatten({}).should.deep.equal({}); | ||
}); | ||
it('handles shallow objects', function () { | ||
metric.flatten({temp: 27.1}).should.deep.equal({temp: 27.1}); | ||
}); | ||
}); | ||
}); |