Alex Fandrianto | dd974bb | 2014-09-05 16:18:06 -0700 | [diff] [blame] | 1 | var histogram = require('bars'); |
Alex Fandrianto | dd974bb | 2014-09-05 16:18:06 -0700 | [diff] [blame] | 2 | |
| 3 | module.exports = { |
Alex Fandrianto | 9de4ebb | 2014-09-19 16:46:17 -0700 | [diff] [blame] | 4 | 'shouldFormat': shouldFormat, |
| 5 | 'format': format |
Alex Fandrianto | dd974bb | 2014-09-05 16:18:06 -0700 | [diff] [blame] | 6 | }; |
| 7 | |
| 8 | /* |
Alex Fandrianto | 9de4ebb | 2014-09-19 16:46:17 -0700 | [diff] [blame] | 9 | * Format if the appropriate histogram fields are present. |
Alex Fandrianto | dd974bb | 2014-09-05 16:18:06 -0700 | [diff] [blame] | 10 | * TODO(alexfandrianto): Negotiate a better way of identifying histogram data. |
| 11 | */ |
Alex Fandrianto | 9de4ebb | 2014-09-19 16:46:17 -0700 | [diff] [blame] | 12 | function shouldFormat(input) { |
Alex Fandrianto | dd974bb | 2014-09-05 16:18:06 -0700 | [diff] [blame] | 13 | return input.count !== undefined && input.sum !== undefined && |
| 14 | input.buckets !== undefined; |
| 15 | } |
| 16 | |
| 17 | /* |
Alex Fandrianto | 9de4ebb | 2014-09-19 16:46:17 -0700 | [diff] [blame] | 18 | * The histogram is formatted with bars (a fork of ascii-histogram). |
| 19 | * TODO(alexfandrianto): Consider using a prettier formatting package. |
Alex Fandrianto | dd974bb | 2014-09-05 16:18:06 -0700 | [diff] [blame] | 20 | */ |
Alex Fandrianto | 9de4ebb | 2014-09-19 16:46:17 -0700 | [diff] [blame] | 21 | function format(input) { |
Alex Fandrianto | dd974bb | 2014-09-05 16:18:06 -0700 | [diff] [blame] | 22 | var histData = {}; |
| 23 | input.buckets.forEach(function(obj) { |
| 24 | histData[obj.lowBound] = obj.count; |
| 25 | }); |
Alex Fandrianto | 3844bec | 2014-09-30 11:29:24 -0700 | [diff] [blame] | 26 | return histogram(histData, { bar: '*', width: 20 }); |
Alex Fandrianto | dd974bb | 2014-09-05 16:18:06 -0700 | [diff] [blame] | 27 | } |