2017-09-06 19:47:19 +08:00
|
|
|
const debug = require('debug')('windshaft:dataview:histogram');
|
2017-09-11 17:30:09 +08:00
|
|
|
const NumericHistogram = require('./histograms/numeric-histogram');
|
|
|
|
const DateHistogram = require('./histograms/date-histogram');
|
2017-09-06 21:52:13 +08:00
|
|
|
|
2017-09-08 18:48:08 +08:00
|
|
|
const DATE_HISTOGRAM = 'DateHistogram';
|
|
|
|
const NUMERIC_HISTOGRAM = 'NumericHistogram';
|
2017-09-06 21:52:13 +08:00
|
|
|
|
2017-09-08 18:48:08 +08:00
|
|
|
module.exports = class Histogram {
|
2017-09-08 22:09:57 +08:00
|
|
|
constructor (query, options, queries) {
|
2017-09-08 21:55:23 +08:00
|
|
|
this.query = query;
|
2017-09-11 19:54:46 +08:00
|
|
|
this.options = options || {};
|
2017-09-08 21:55:23 +08:00
|
|
|
this.queries = queries;
|
|
|
|
|
2017-09-11 21:34:42 +08:00
|
|
|
this.histogramImplementation = this._getHistogramImplementation();
|
2017-09-08 21:55:23 +08:00
|
|
|
}
|
|
|
|
|
2017-09-11 19:54:46 +08:00
|
|
|
_getHistogramImplementation (override) {
|
2017-09-08 22:09:57 +08:00
|
|
|
let implementation = null;
|
|
|
|
|
2017-09-08 21:55:23 +08:00
|
|
|
switch (this._getHistogramSubtype(override)) {
|
2017-09-08 18:48:08 +08:00
|
|
|
case DATE_HISTOGRAM:
|
2017-09-08 22:09:57 +08:00
|
|
|
debug('Delegating to DateHistogram with options: %j and overriding: %j', this.options, override);
|
|
|
|
implementation = new DateHistogram(this.query, this.options, this.queries);
|
2017-09-08 18:48:08 +08:00
|
|
|
break;
|
|
|
|
case NUMERIC_HISTOGRAM:
|
2017-09-08 22:09:57 +08:00
|
|
|
debug('Delegating to NumericHistogram with options: %j and overriding: %j', this.options, override);
|
|
|
|
implementation = new NumericHistogram(this.query, this.options, this.queries);
|
2017-09-08 18:48:08 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error('Unsupported Histogram type');
|
2017-09-06 19:43:54 +08:00
|
|
|
}
|
2017-09-08 22:09:57 +08:00
|
|
|
|
|
|
|
return implementation;
|
2017-09-11 19:54:46 +08:00
|
|
|
}
|
|
|
|
|
2017-09-11 23:17:42 +08:00
|
|
|
_getHistogramSubtype (override) {
|
|
|
|
if(this._isDateHistogram(override)) {
|
2017-09-08 21:55:23 +08:00
|
|
|
return DATE_HISTOGRAM;
|
2017-09-06 19:43:54 +08:00
|
|
|
}
|
2017-09-11 23:17:42 +08:00
|
|
|
|
2017-09-08 21:55:23 +08:00
|
|
|
return NUMERIC_HISTOGRAM;
|
2017-09-06 19:43:54 +08:00
|
|
|
}
|
2017-09-11 19:54:46 +08:00
|
|
|
|
2017-09-11 23:17:42 +08:00
|
|
|
_isDateHistogram (override = {}) {
|
|
|
|
return (this.options.aggregation !== undefined || override.aggregation !== undefined);
|
|
|
|
}
|
|
|
|
|
2017-09-08 18:48:08 +08:00
|
|
|
getResult (psql, override, callback) {
|
2017-09-11 21:34:42 +08:00
|
|
|
this.histogramImplementation = this._getHistogramImplementation(override);
|
|
|
|
this.histogramImplementation.getResult(psql, override, callback);
|
2017-09-06 21:49:05 +08:00
|
|
|
}
|
2017-09-08 21:53:00 +08:00
|
|
|
|
|
|
|
// In order to keep previous behaviour with overviews,
|
|
|
|
// we have to expose the following methods to bypass
|
|
|
|
// the concrete overview implementation
|
|
|
|
|
|
|
|
sql (psql, override, callback) {
|
2017-09-11 21:34:42 +08:00
|
|
|
this.histogramImplementation.sql(psql, override, callback);
|
2017-09-08 21:53:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
format (result, override) {
|
2017-09-11 21:34:42 +08:00
|
|
|
return this.histogramImplementation.format(result, override);
|
2017-09-08 21:53:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getType () {
|
2017-09-11 21:34:42 +08:00
|
|
|
return this.histogramImplementation.getType();
|
2017-09-08 21:53:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
toString () {
|
2017-09-11 21:34:42 +08:00
|
|
|
return this.histogramImplementation.toString();
|
2017-09-08 21:53:00 +08:00
|
|
|
}
|
2017-09-06 22:14:29 +08:00
|
|
|
};
|