2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2017-09-11 19:53:05 +08:00
|
|
|
const BaseDataview = require('../base');
|
|
|
|
|
|
|
|
const TYPE = 'histogram';
|
|
|
|
|
|
|
|
module.exports = class BaseHistogram extends BaseDataview {
|
|
|
|
constructor (query, options, queries) {
|
|
|
|
super();
|
|
|
|
|
|
|
|
if (typeof options.column !== 'string') {
|
|
|
|
throw new Error('Histogram expects `column` in widget options');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.query = query;
|
|
|
|
this.queries = queries;
|
|
|
|
this.column = options.column;
|
|
|
|
this.bins = options.bins;
|
|
|
|
|
|
|
|
this._columnType = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
sql (psql, override, callback) {
|
|
|
|
if (!callback) {
|
|
|
|
callback = override;
|
|
|
|
override = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._columnType === null) {
|
|
|
|
this.getColumnType(psql, this.column, this.queries.no_filters, (err, type) => {
|
|
|
|
// assume numeric, will fail later
|
|
|
|
this._columnType = 'numeric';
|
|
|
|
if (!err && !!type) {
|
|
|
|
this._columnType = Object.keys(type).find(function (key) {
|
|
|
|
return type[key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.sql(psql, override, callback);
|
|
|
|
}, true); // use read-only transaction
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._buildQuery(psql, override, callback);
|
|
|
|
}
|
|
|
|
|
2017-09-11 23:19:02 +08:00
|
|
|
format (result, override) {
|
|
|
|
const histogram = this._getSummary(result, override);
|
|
|
|
histogram.bins = this._getBuckets(result);
|
|
|
|
return histogram;
|
|
|
|
}
|
|
|
|
|
2017-09-11 19:53:05 +08:00
|
|
|
getType () {
|
|
|
|
return TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
toString () {
|
|
|
|
return JSON.stringify({
|
|
|
|
_type: TYPE,
|
|
|
|
_column: this.column,
|
|
|
|
_query: this.query
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-09-12 16:14:55 +08:00
|
|
|
_hasOverridenRange (override) {
|
|
|
|
return override && override.hasOwnProperty('start') && override.hasOwnProperty('end');
|
|
|
|
}
|
|
|
|
|
2017-09-12 01:10:06 +08:00
|
|
|
_getBinStart (override = {}) {
|
2017-09-12 16:14:55 +08:00
|
|
|
if (this._hasOverridenRange(override)) {
|
2017-09-11 23:19:02 +08:00
|
|
|
return Math.min(override.start, override.end);
|
|
|
|
}
|
2017-09-12 16:14:55 +08:00
|
|
|
|
2017-09-11 23:19:02 +08:00
|
|
|
return override.start || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getBinEnd (override = {}) {
|
2017-09-12 16:14:55 +08:00
|
|
|
if (this._hasOverridenRange(override)) {
|
2017-09-11 19:53:05 +08:00
|
|
|
return Math.max(override.start, override.end);
|
|
|
|
}
|
2017-09-12 16:14:55 +08:00
|
|
|
|
2017-09-11 19:53:05 +08:00
|
|
|
return override.end || 0;
|
|
|
|
}
|
|
|
|
|
2017-09-11 23:19:02 +08:00
|
|
|
_getBinsCount (override = {}) {
|
2017-09-11 19:53:05 +08:00
|
|
|
return override.bins || 0;
|
|
|
|
}
|
2017-09-12 16:16:16 +08:00
|
|
|
};
|