Skip to content
This repository has been archived by the owner on Dec 20, 2017. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jgallen23 committed May 24, 2012
0 parents commit 147254f
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#Cube Reports

This is a tool to help you send daily reports from your [cube](http://square.github.com/cube/) data.

##Installation

npm install cube-reports

##Usage

Edit the sample-config.json to put in the metrics that you care most about and then run:

cube-reports config.json

##Email settings

You need to provide your own smtp information to send emails. You can see what the config object looks like here:

[https://github.com/andris9/Nodemailer#setting-up-smtp](https://github.com/andris9/Nodemailer#setting-up-smtp)
41 changes: 41 additions & 0 deletions bin/cube-reports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@



var argv = require('optimist')
.usage('Send daily email reports\n$0 <config.json>')
.demand(1)
.options('p', {
alias: 'print',
describe: 'Print report to stdout'
})
.boolean('p')
.argv;


if (argv._.length == 1) {
var file = argv._[0];
var Reports = require('../lib/reports');

var reports = new Reports(file);

if (argv.print) {
reports.fetch(function(reports) {
reports.forEach(function(report) {
console.log(report.name);
console.log('================');
report.metrics.forEach(function(metric) {
console.log('%s: %s', metric.name, metric.count);
});
console.log('');
});

});

} else {
reports.send(function() {
console.log('Email Sent');
});
}
}


57 changes: 57 additions & 0 deletions lib/report.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var request = require('request');
var resistance = require('resistance');
var strf = require('strf');

var Report = function(host, obj) {
this.host = host;
this.name = obj.name;
this.metrics = obj.metrics;
this.to = obj.to;
};

Report.prototype.fetch = function(cb) {
var self = this;

var now = new Date();
var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
var tomorrow = new Date(today.getTime() + 1000*60*60*24);

var start = today.toISOString();
var stop = tomorrow.toISOString();

var queue = resistance.queue(function(metric, done) {
var url = strf('http://{host}/1.0/metric?expression={expression}&start={start}&stop={stop}&step={step}', {
host: self.host,
expression: metric.expression,
start: start,
stop: stop,
step: 3600000
});
request({
url: url,
json: true
}, function(err, res, body) {
var count = 0;
body.forEach(function(item) {
count += item.value;
});
metric.count = count;
metric.start = start;
metric.stop = stop;
metric.date = today.getTime();
done(metric);
});

});

queue.push(this.metrics);
queue.run(function(results) {
cb(results);
});
};

Report.prototype.toString = function() {
return this.data.name;
};

module.exports = Report;
61 changes: 61 additions & 0 deletions lib/reports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var Report = require('./report');
var resistance = require('resistance');
var nodemailer = require('nodemailer');
var fs = require('fs');
var ejs = require('ejs');
ejs.open = '{{';
ejs.close = '}}';

var Reports = function(config) {
if (typeof config === 'string') {
config = JSON.parse(fs.readFileSync(config, 'utf8'));
}

this.config = config;
this.items = config.reports.map(function(obj) {
return new Report(config.cubeHost, obj);
});
};

Reports.prototype.fetch = function(cb) {

var queue = resistance.queue(function(report, done) {
report.fetch(function(data) {
done(report);
});
});

queue.push(this.items);

queue.run(function(results) {
if (cb) {
cb(results);
}
});
};

Reports.prototype.send = function(cb) {
var self = this;
var template = fs.readFileSync(__dirname + '/../views/report.html', 'utf8');

var transport = nodemailer.createTransport("SMTP", this.config.email);

var queue = resistance.queue(function(report, done) {
var html = ejs.render(template, report);
var mailObj = {
from: self.config.email.from,
to: (typeof report.to === 'string') ? report.to : report.to.join(','),
subject: report.name + ' for ' + new Date().toDateString(),
html: html
};
transport.sendMail(mailObj, done);
});

this.fetch(function(reports) {
queue.push(reports);
queue.run(cb);
});

};

module.exports = Reports;
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "cube-reports",
"author": "Greg Allen <@jgaui> (http://jga.me)",
"description": "an email reporting tool for cube",
"homepage": "https://github.com/jgallen23/cube-reports",
"version": "0.0.1",
"repository": {
"type": "git",
"url": "https://github.com/jgallen23/cube-reports.git"
},
"dependencies": {
"ejs": "*",
"nodemailer": "*",
"optimist": "*",
"request": "*",
"resistance": "*",
"strf": "*"
},
"devDependencies": {
},
"engines": {
"node": "*"
},
"bin": { "cube-reports": "./bin/cube-reports.js" },
"keywords": ["cube", "email", "report"]
}
16 changes: 16 additions & 0 deletions sample-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"cubeHost": "127.0.0.1:1081",
"email": {

},
"reports": [
{
"name": "test",
"to": "junk@jga.me",
"metrics": [
{ "name": "Requests", "expression": "sum(request)" },
{ "name": "Logins", "expression": "sum(login)" }
]
}
]
}
11 changes: 11 additions & 0 deletions views/report.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1>{{= name }}</h1>
<table>
<tbody>
{{ metrics.forEach(function(metric) { }}
<tr>
<td>{{= metric.name }}</td>
<td>{{= metric.count }}</td>
</tr>
{{ }) }}
</tbody>
</table>

0 comments on commit 147254f

Please sign in to comment.