Avoid nested ternaries for date histograms

This commit is contained in:
Daniel García Aubert 2017-09-06 19:15:39 +02:00
parent b63a67a5b8
commit 63a7ee08d0

View File

@ -413,9 +413,19 @@ module.exports = class Histogram extends BaseDataview {
return override && override.hasOwnProperty('bins');
}
buildDateHistogramQueryTpl (ctx) {
return `
WITH
${ctx._override && ctx._override.hasOwnProperty('start') && ctx._override.hasOwnProperty('end') ?
dateOverrideBasicsQueryTpl(ctx) :
dateBasicsQueryTpl(ctx)},
${dateBinsQueryTpl(ctx)},
${nullsQueryTpl(ctx)}
${dateHistogramQueryTpl(ctx)}
`;
}
_buildDateHistogramQuery (psql, override, callback) {
var _column = this.column;
var _query = this.query;
var _aggregation = override && override.aggregation ? override.aggregation : this.aggregation;
var _offset = override && Number.isFinite(override.offset) ? override.offset : this.offset;
@ -438,54 +448,16 @@ module.exports = class Histogram extends BaseDataview {
return null;
}
var dateBasicsQuery;
if (override && override.hasOwnProperty('start') && override.hasOwnProperty('end')) {
dateBasicsQuery = dateOverrideBasicsQueryTpl({
_query: _query,
_column: _column,
_aggregation: _aggregation,
_start: this.getBinStart(override),
_end: this.getBinEnd(override),
_offset: this.parseOffset(_offset, _aggregation)
});
} else {
dateBasicsQuery = dateBasicsQueryTpl({
_query: _query,
_column: _column,
_aggregation: _aggregation,
_offset: this.parseOffset(_offset, _aggregation)
});
}
var dateBinsQuery = [
dateBinsQueryTpl({
_aggregation: _aggregation
})
].join(',\n');
var nullsQuery = nullsQueryTpl({
_query: _query,
_column: _column
});
var dateHistogramQuery = dateHistogramQueryTpl({
_query: _query,
_column: _column,
const histogramSql = this.buildDateHistogramQueryTpl({
_override: override,
_query: this.query,
_column: this.column,
_aggregation: _aggregation,
_start: this.getBinStart(override),
_end: this.getBinEnd(override),
_offset: this.parseOffset(_offset, _aggregation)
});
var histogramSql = [
"WITH",
[
dateBasicsQuery,
dateBinsQuery,
nullsQuery
].join(',\n'),
dateHistogramQuery
].join('\n');
debug(histogramSql);
return callback(null, histogramSql);