blob: 15bb923593c65a0cee27a2ad1de633cb15d75c93 [file] [log] [blame]
Alex Fandriantodd974bb2014-09-05 16:18:06 -07001var histogram = require('bars');
Alex Fandriantodd974bb2014-09-05 16:18:06 -07002
3module.exports = {
Alex Fandrianto9de4ebb2014-09-19 16:46:17 -07004 'shouldFormat': shouldFormat,
5 'format': format
Alex Fandriantodd974bb2014-09-05 16:18:06 -07006};
7
8/*
Alex Fandrianto9de4ebb2014-09-19 16:46:17 -07009 * Format if the appropriate histogram fields are present.
Alex Fandriantodd974bb2014-09-05 16:18:06 -070010 * TODO(alexfandrianto): Negotiate a better way of identifying histogram data.
11 */
Alex Fandrianto9de4ebb2014-09-19 16:46:17 -070012function shouldFormat(input) {
Alex Fandriantodd974bb2014-09-05 16:18:06 -070013 return input.count !== undefined && input.sum !== undefined &&
14 input.buckets !== undefined;
15}
16
17/*
Alex Fandrianto9de4ebb2014-09-19 16:46:17 -070018 * The histogram is formatted with bars (a fork of ascii-histogram).
19 * TODO(alexfandrianto): Consider using a prettier formatting package.
Alex Fandriantodd974bb2014-09-05 16:18:06 -070020 */
Alex Fandrianto9de4ebb2014-09-19 16:46:17 -070021function format(input) {
Alex Fandriantodd974bb2014-09-05 16:18:06 -070022 var histData = {};
23 input.buckets.forEach(function(obj) {
24 histData[obj.lowBound] = obj.count;
25 });
Alex Fandrianto3844bec2014-09-30 11:29:24 -070026 return histogram(histData, { bar: '*', width: 20 });
Alex Fandriantodd974bb2014-09-05 16:18:06 -070027}