#!/usr/bin/env node /* * Run all analytics reports output JSON to disk. * * Usage: analytics * * Defaults to printing JSON to STDOUT. * * --output: Output to a directory. * --publish: Publish to an S3 bucket. * --only: only run one report. * --slim: Where supported, use totals only (omit the `data` array). * Only applies to JSON, and reports where "slim": true. * --csv: CSV instead of JSON. * --frequency: Limit to reports with this 'frequency' value. * --debug: print debug details on STDOUT */ var Analytics = require("../analytics"), config = require("../config"), fs = require("fs"), path = require('path'), async = require("async") csv = require("fast-csv"); // AWS credentials are looked for in env vars or in ~/.aws/config. // AWS bucket and path need to be set in env vars mentioned in config.js. var AWS = require("aws-sdk"); var publish = function(name, data, extension, options, callback) { if (options.debug) console.log("[" + name + "] Publishing to " + config.aws.bucket + "..."); var mime = {".json": "application/json", ".csv": "text/csv"}; new AWS.S3({params: {Bucket: config.aws.bucket}}).upload({ Key: config.aws.path + "/" + name + extension, Body: data, ContentType: mime[extension], ACL: "public-read", CacheControl: "max-age=" + (config.aws.cache || 0) }, callback); }; var run = function(options) { if (!options) options = {}; if (options.debug) options.verbose = options.debug; if (options.verbose) options.debug = options.verbose; // can be overridden to only do one report var names; if (options.only) names = [options.only]; else if (options.frequency) { names = []; var all = Object.keys(Analytics.reports); for (var i=0; i