histogram calculation

This commit is contained in:
javi 2015-11-06 19:47:37 +01:00
parent 4ad0dba547
commit ff4809b08c

View File

@ -490,6 +490,47 @@ var Profiler = require('../profiler');
_fetchKeySpan: function() {
this._setReady(true);
},
getHistogram: function(varName, callback) {
//{"bin":0,"start":1,"end":15.5,"freq":17,"min":1,"max":17},
var sql = [
'with width as (',
'select min({column}) as min,',
'max({column}) as max,',
'20 as buckets',
'from {table}',
'),',
'_bw as ( select (max - min)/buckets as bw from width ),',
'histogram as (',
'select width_bucket({column}, min, max, buckets) as bucket,',
'numrange(min({column})::numeric, max({column})::numeric, \'[]\') as range,',
'count(*) as freq',
'from {table}, width ',
//'where trip_time_in_secs between min and max',
'group by bucket',
'order by bucket',
')',
'select bucket*bw as start, (bucket+1)*bw as end, bucket as bin, lower(range) as min, upper(range) as max, freq from histogram, _bw;'
]
var query = format(sql.join('\n'), this.options, {
column: this.options.column,
table: this.options.table,
filters: this._generateFiltersSQL()
});
var self = this;
this.sql(query, function (data) {
if (data) {
var rows = JSON.parse(data.responseText).rows;
callback(null, rows);
} else {
callback(null);
}
});
}
};