zazu-app-table
Version:
A simple table app for zazu dashboard engine.
163 lines (131 loc) • 4.5 kB
JavaScript
module.exports = TableApp;
var q = require('q'),
debug = require('debug')('zazu:app:table'),
util = require('util'),
csvParse = require('csv-parse');
function TableApp(config, router) {
if (!config.limit) {
config.limit = 10;
}
this.config = config;
this.dataCache = null;
this.request = require('request').defaults({
auth: config.authUser && config.authPass ? {
user: config.authUser,
pass: config.authPass
} : null,
headers: {
'Accept': 'text/' + config.fileFormat
}
});
router.get('/config', function (req, res) {
return res.json({
reloadInterval: config.reloadInterval,
firstColumnContainHeader: !!config.firstColumnContainHeader,
showTitleBar: !!config.showTitleBar
});
});
var func;
switch (config.fileFormat) {
case 'csv':
func = this.getCsv;
break;
case 'json':
func = this.getJson;
break;
default:
throw new Error(util.format('Invalid fileFormat "%s"', config.fileFormat));
}
router.get('/data', func.bind(this));
this.interval = setInterval(this.reloadData.bind(this), (config.reloadInterval || 600) * 1000);
}
TableApp.prototype.reloadData = function () {
return q.Promise(function (resolve, reject) {
this.request.get(this.config.fileUrl, function (error, response, body) {
if (error) {
return reject(error);
}
this.dataCache = body;
resolve(body);
}.bind(this));
}.bind(this));
};
TableApp.prototype.getHeader = function (data) {
var defaultHeader = this.config.firstLineContainHeader ? data.shift() : [],
defaultCustomHeader = this.config.customHeaders || [],
columns = data[data.length - 1].length,
customHeader = new Array(columns),
i=0;
if(this.config.firstLineContainHeader) {
for (i = 0; i < customHeader.length; i++) {
if (defaultCustomHeader[i] && defaultCustomHeader[i].title !== '') {
customHeader[i] = defaultCustomHeader[i].title;
} else if (defaultHeader[i]) {
customHeader[i] = defaultHeader[i];
} else {
customHeader[i] = '';
}
}
debug('table header: ', customHeader);
return customHeader;
} else if (defaultCustomHeader.length > 0) {
for (i = 0; i < customHeader.length; i++) {
if (!!defaultCustomHeader[i] && defaultCustomHeader[i].title !== '') {
customHeader[i] = defaultCustomHeader[i].title;
} else {
customHeader[i] = '';
}
}
debug('table header: ', customHeader);
return customHeader;
}
return defaultHeader;
};
TableApp.prototype.getData = function () {
return q.Promise(function (resolve, reject) {
if (this.dataCache) {
resolve(this.dataCache);
} else {
this.reloadData().then(resolve, reject);
}
}.bind(this));
};
TableApp.prototype.getCsv = function (req, res) {
this.getData().then(function (data) {
debug('Got data: ', data);
csvParse(data, {delimiter: this.config.delimiter, comment: '#'}, function (err, data) {
if (err) {
res.status(500).send(err);
}
if (!data) {
return res.status(404).send('no data returned');
}
debug('Parsed data: ', data);
res.json({
header: this.getHeader(data),
rows: data.slice(0, this.config.limit)
});
}.bind(this));
}.bind(this), function (error) {
res.status(500).send(error.toString());
}.bind(this));
};
TableApp.prototype.getJson = function (req, res) {
this.getData().then(function (data) {
debug('Got data: ', data);
try {
var parsed = JSON.parse(data);
} catch (e) {
res.status(500).send(e.toString());
}
if (!parsed) {
return res.status(404).send('no data returned');
}
res.json({
header: this.getHeader(parsed),
rows: parsed.slice(0, this.config.limit)
});
}.bind(this), function (error) {
res.status(500).send(error.toString());
}.bind(this));
};