From 813f2daa3a29eb7637fab2e2bb4e8a4bfd974e6f Mon Sep 17 00:00:00 2001 From: Mathieu Lecarme Date: Sat, 30 Jun 2012 00:34:35 +0200 Subject: [PATCH 1/2] Build and test from npm. --- package.json | 8 ++++ rrd.js | 109 ------------------------------------------- rrdRecord.js | 11 ----- test/rrd-test.coffee | 6 +-- 4 files changed, 11 insertions(+), 123 deletions(-) delete mode 100644 rrd.js delete mode 100644 rrdRecord.js diff --git a/package.json b/package.json index 0260cce..3c20eb8 100644 --- a/package.json +++ b/package.json @@ -10,4 +10,12 @@ "type" : "git" , "url" : "https://github.com/plainlystated/coffeescript-rrd" } + , "devDepencencies" :{ + "coffee-script": ">=1.3.3", + "vows": ">=0.6.3" + } + , "scripts": { + "test": "LANG=EN_us ./node_modules/.bin/vows test/rrd-test.coffee" + , "install": "./node_modules/.bin/coffee -c -b *.coffee" + } } diff --git a/rrd.js b/rrd.js deleted file mode 100644 index 1e04027..0000000 --- a/rrd.js +++ /dev/null @@ -1,109 +0,0 @@ -(function() { - var RRD, RRDRecord, exec, fs, spawn, sys; - sys = require('sys'); - exec = require('child_process').exec; - spawn = require('child_process').spawn; - fs = require('fs'); - RRDRecord = require('./rrdRecord').RRDRecord; - RRD = (function() { - var _rrdTime; - function RRD(filename) { - this.filename = filename; - } - RRD.prototype.create = function(rrdArgs, options, cb) { - var cmdArgs, err, proc, start, _ref; - start = (_ref = options.start) != null ? _ref : new Date; - cmdArgs = ["create", this.filename, "--start", _rrdTime(start), "--step", 300].concat(rrdArgs); - console.log(" - rrdtool " + (cmdArgs.join(" "))); - proc = spawn("rrdtool", cmdArgs); - err = ""; - proc.stderr.on('data', function(data) { - return err += data; - }); - return proc.on('exit', function(code) { - if (code === 0) { - return cb(void 0, 'ok'); - } else { - return cb(err, void 0); - } - }); - }; - RRD.prototype.destroy = function(cb) { - return fs.unlink(this.filename, cb); - }; - RRD.prototype.dump = function(cb) { - return this.rrdSpawn("dump", [], cb); - }; - RRD.prototype.rrdExec = function(command, cmd_args, cb) { - var cmd; - cmd = "rrdtool " + command + " " + this.filename + " " + cmd_args; - console.log(cmd); - return exec(cmd, { - maxBuffer: 500 * 1024 - }, cb); - }; - RRD.prototype.rrdSpawn = function(command, args, cb) { - var err, out, proc; - proc = spawn("rrdtool", [command, this.filename].concat(args)); - err = ""; - out = ""; - proc.stderr.on('data', function(data) { - return err += data; - }); - proc.stdout.on('data', function(data) { - return out += data; - }); - return proc.on('exit', function(code) { - if (code === 0) { - return cb(null, out); - } else { - return cb(err, null); - } - }); - }; - RRD.prototype.update = function(time, values, cb) { - return this.rrdSpawn("update", ["" + (_rrdTime(time)) + ":" + (values.join(':'))], cb); - }; - RRD.prototype.fetch = function(start, end, cb) { - return this.rrdExec("fetch", "AVERAGE --start " + start + " --end " + end, function(err, data) { - var fieldNames, fields, i, line, lines, record, records; - if (err) { - cb(err.message); - return; - } - lines = data.split("\n"); - fieldNames = lines.shift().replace(new RegExp("^ +"), "").split(new RegExp(" +")); - lines.shift(); - records = (function() { - var _i, _len, _ref, _results; - _results = []; - for (_i = 0, _len = lines.length; _i < _len; _i++) { - line = lines[_i]; - if (line === "") { - continue; - } - if (line.match(" nan ")) { - continue; - } - fields = line.split(new RegExp("[: ]+")); - record = new RRDRecord(fields.shift(), fieldNames); - for (i = 0, _ref = fields.length - 1; 0 <= _ref ? i <= _ref : i >= _ref; 0 <= _ref ? i++ : i--) { - record[fieldNames[i]] = fields[i]; - } - _results.push(record); - } - return _results; - })(); - return cb(void 0, records); - }); - }; - _rrdTime = function(date) { - return Math.round(date.valueOf() / 1000); - }; - return RRD; - })(); - RRD.restore = function(filenameXML, filenameRRD, cb) { - return exec("rrdtool restore " + filenameXML + " " + filenameRRD, cb); - }; - exports.RRD = RRD; -}).call(this); diff --git a/rrdRecord.js b/rrdRecord.js deleted file mode 100644 index 9af18a5..0000000 --- a/rrdRecord.js +++ /dev/null @@ -1,11 +0,0 @@ -(function() { - var RRDRecord; - RRDRecord = (function() { - function RRDRecord(timestamp, fieldNames) { - this.timestamp = timestamp; - this.fieldNames = fieldNames; - } - return RRDRecord; - })(); - exports.RRDRecord = RRDRecord; -}).call(this); diff --git a/test/rrd-test.coffee b/test/rrd-test.coffee index 00a4b33..2956102 100644 --- a/test/rrd-test.coffee +++ b/test/rrd-test.coffee @@ -58,7 +58,7 @@ vows.describe('RRD').addBatch( assert.equal(err, "ERROR: opening 'invalid.rrd': No such file or directory\n") 'a valid RRD': - topic: new RRD('valid.rrd') + topic: new RRD('test/valid.rrd') 'when fetching': topic: (rrd) -> @@ -102,7 +102,7 @@ vows.describe('RRD').addBatch( 'when restoring': 'a valid file': topic: () -> - RRD.restore('valid.xml', "tmp-restore.rrd", @callback) + RRD.restore('test/valid.xml', "tmp-restore.rrd", @callback) return 'returns no error': (err) -> @@ -110,7 +110,7 @@ vows.describe('RRD').addBatch( 'an invalid file': topic: () -> - RRD.restore('invalid.xml', "tmp-restore.rrd", @callback) + RRD.restore('test/invalid.xml', "tmp-restore.rrd", @callback) return # The underlying process never returns an error here.. need to figure out a better way to handle this From ff36ce448809c40954ab78e1cbef6cf3164cca49 Mon Sep 17 00:00:00 2001 From: Mathieu Lecarme Date: Sat, 30 Jun 2012 00:35:24 +0200 Subject: [PATCH 2/2] Not used. --- rrd.coffee | 1 - 1 file changed, 1 deletion(-) diff --git a/rrd.coffee b/rrd.coffee index 3847eb3..d6460aa 100644 --- a/rrd.coffee +++ b/rrd.coffee @@ -1,4 +1,3 @@ -sys = require('sys') exec = require('child_process').exec spawn = require('child_process').spawn fs = require('fs')