This repository has been archived by the owner on Dec 20, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 147254f
Showing
8 changed files
with
232 additions
and
0 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
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,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) |
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,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'); | ||
}); | ||
} | ||
} | ||
|
||
|
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,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; |
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,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; |
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,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"] | ||
} |
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,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)" } | ||
] | ||
} | ||
] | ||
} |
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,11 @@ | ||
<h1>{{= name }}</h1> | ||
<table> | ||
<tbody> | ||
{{ metrics.forEach(function(metric) { }} | ||
<tr> | ||
<td>{{= metric.name }}</td> | ||
<td>{{= metric.count }}</td> | ||
</tr> | ||
{{ }) }} | ||
</tbody> | ||
</table> |