Skip to content
Browse files

Added CSV listener to server

  • Loading branch information...
1 parent f39500c commit add4a45dfe41912289705f183040c67b393e1e41 @thinkjson thinkjson committed Oct 19, 2011
Showing with 50 additions and 3 deletions.
  1. +47 −0 listeners/csv.js
  2. +2 −2 package.json
  3. +1 −1 server.js
View
47 listeners/csv.js
@@ -0,0 +1,47 @@
+var fs = require('fs');
+exports = module.exports = function(config) {
+ /**
+ * Rotate the log
+ */
+ this.rotateLog = function() {
+ var timestamp = Math.floor((new Date()).getTime() / 1000);
+ self.oldLogfile = this.logfile;
+ self.oldLog = self.log;
+ self.logfile = self.config.buffer + self.config.input + timestamp + ".csv.part";
+ self.log = fs.createWriteStream(self.logfile);
+
+ // Push log file to archive
+ if (self.oldLog) {
+ self.oldLog.on('close', function() {
+ fs.rename(self.oldLogfile, self.config.archive + self.config.input + timestamp + ".csv", function(err) {
+ if (! err) fs.unlink(self.oldLogfile);
+ });
+ });
+
+ self.oldLog.end();
+ }
+ };
+
+ if (! config.fields) throw { message: "You must specify the fields to extract from the incoming data" };
+ var self = this;
+ self.config = config;
+ self.rotateLog();
+ this.interval = config.interval || 3600000;
+ setInterval(function() {
+ self.rotateLog();
+ }, self.interval);
+
+ /**
+ * Post a message to the telemetry server
+ * Required method
+ */
+ this.post = function(data) {
+ // FIXME - pull selected fields out of data and write as CSV row
+ var row = [];
+ for (field in self.config.fields) {
+ row.push(data[self.config.fields[field]]);
+ }
+ var raw_data = '"' + row.join('","') + '"\n';
+ self.log.write(raw_data, "utf8");
+ };
+};
View
4 package.json
@@ -1,12 +1,12 @@
{
"name": "telemetry",
"description": "a simple telemetry server written in Node.js",
- "version": "0.0.3",
+ "version": "0.0.4",
"author": "Mark Cahill <mark+npm@thinkjson.com>",
"keywords": [ "Loggly", "log", "telemetry", "firebug", "firephp" ],
"repository": {
"type": "git",
- "url": "https://thinkjson@github.com/thinkjson/node-telemetry.git"
+ "url": "https://thinkjson@github.com/lifechurch/node-telemetry.git"
},
"dependencies": {
"express": ">= 2.2.2",
View
2 server.js
@@ -13,7 +13,7 @@ for (var i = 0; i < listener_files.length; i++) {
// Load configuration and inputs
var config_loc = process.env.TELEMETRY_CONFIG || __dirname + "/config.yaml";
-var config = yaml.eval(fs.readFileSync(config_loc, 'utf8')); //FIXME - this is just for testing. the real file should be in home dir
+var config = yaml.eval(fs.readFileSync(config_loc, 'utf8'));
global.telemetry = new (require(__dirname + '/telemetry.js'))(config);
// Create the telemetry server and assign inputs

0 comments on commit add4a45

Please sign in to comment.
Something went wrong with that request. Please try again.