Windshaft-cartodb/lib/cartodb/models/dataview/histogram.js

73 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-09-06 19:47:19 +08:00
const debug = require('debug')('windshaft:dataview:histogram');
const NumericHistogram = require('./histograms/numeric-histogram');
const DateHistogram = require('./histograms/date-histogram');
2017-09-06 21:52:13 +08:00
const DATE_HISTOGRAM = 'DateHistogram';
const NUMERIC_HISTOGRAM = 'NumericHistogram';
2017-09-06 21:52:13 +08:00
module.exports = class Histogram {
2017-09-08 22:09:57 +08:00
constructor (query, options, queries) {
this.query = query;
2017-09-11 19:54:46 +08:00
this.options = options || {};
this.queries = queries;
this.histogramImplementation = this._getHistogramImplementation();
}
2017-09-11 19:54:46 +08:00
_getHistogramImplementation (override) {
2017-09-08 22:09:57 +08:00
let implementation = null;
switch (this._getHistogramSubtype(override)) {
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);
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);
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)) {
return DATE_HISTOGRAM;
2017-09-06 19:43:54 +08:00
}
2017-09-11 23:17:42 +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.hasOwnProperty('aggregation') || override.hasOwnProperty('aggregation'));
2017-09-11 23:17:42 +08:00
}
getResult (psql, override, callback) {
this.histogramImplementation = this._getHistogramImplementation(override);
this.histogramImplementation.getResult(psql, override, callback);
}
// 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) {
this.histogramImplementation.sql(psql, override, callback);
}
format (result, override) {
return this.histogramImplementation.format(result, override);
}
getType () {
return this.histogramImplementation.getType();
}
toString () {
return this.histogramImplementation.toString();
}
};