Allow override start and end params

This commit is contained in:
Daniel García Aubert 2017-06-02 12:15:43 +02:00
parent 1d66e49910
commit 9c34428984
2 changed files with 68 additions and 7 deletions

View File

@ -8,7 +8,7 @@ dot.templateSettings.strip = false;
var columnTypeQueryTpl = dot.template(
'SELECT pg_typeof({{=it.column}})::oid FROM ({{=it.query}}) _cdb_histogram_column_type limit 1'
);
// var columnCastTpl = dot.template("date_part('epoch', {{=it.column}})");
var columnCastTpl = dot.template("date_part('epoch', {{=it.column}})");
var BIN_MIN_NUMBER = 6;
var BIN_MAX_NUMBER = 48;
@ -110,6 +110,28 @@ var dateBasicsQueryTpl = dot.template([
')'
].join(' \n'));
var overrideBasicsQueryTpl = dot.template([
'basics AS (',
' SELECT',
' max({{=it._end}}) AS max_val, min({{=it._start}}) AS min_val,',
' avg({{=it._column}}) AS avg_val, count(1) AS total_rows',
' FROM ({{=it._query}}) _cdb_basics',
')'
].join('\n'));
var dateOverrideBasicsQueryTpl = dot.template([
'basics AS (',
' SELECT',
' max({{=it._end}}) AS max_val,',
' min({{=it._start}}) AS min_val,',
' avg(date_part(\'epoch\', {{=it._column}})) AS avg_val,',
' min(date_trunc(\'{{=it._aggregation}}\', TO_TIMESTAMP({{=it._start}})::timestamp)) AS start_date,',
' max(TO_TIMESTAMP({{=it._end}})::timestamp) AS end_date,',
' count(1) AS total_rows',
' FROM ({{=it._query}}) _cdb_basics',
')'
].join(' \n'));
var dateBinsQueryTpl = dot.template([
'bins AS (',
' SELECT',
@ -181,6 +203,7 @@ var DATE_OIDS = {
};
Histogram.prototype.sql = function(psql, override, callback) {
// jshint maxcomplexity: 7
if (!callback) {
callback = override;
override = {};
@ -209,10 +232,13 @@ Histogram.prototype.sql = function(psql, override, callback) {
return null;
}
if (this._columnType === 'date') {
if (this._columnType === 'date' && this.aggregation !== undefined) {
return this._buildDateHistogramQuery(override, callback);
}
if (this._columnType === 'date') {
_column = columnCastTpl({ column: this.column});
}
var _query = this.query;
var basicsQuery, binsQuery;
@ -284,11 +310,23 @@ Histogram.prototype._buildDateHistogramQuery = function (override, callback) {
var _query = this.query;
var _aggregation = override && override.aggregation ? override.aggregation : this.aggregation;
var dateBasicsQuery = dateBasicsQueryTpl({
_query: _query,
_column: _column,
_aggregation: _aggregation
});
var dateBasicsQuery;
if (override && _.has(override, 'start') && _.has(override, 'end')) {
dateBasicsQuery = dateOverrideBasicsQueryTpl({
_query: _query,
_column: _column,
_aggregation: _aggregation,
_start: getBinStart(override),
_end: getBinEnd(override)
});
} else {
dateBasicsQuery = dateBasicsQueryTpl({
_query: _query,
_column: _column,
_aggregation: _aggregation
});
}
var dateBinsQuery = [
dateBinsQueryTpl({

View File

@ -156,6 +156,7 @@ describe('histogram-dataview for date column type', function() {
assert.ok(!err, err);
assert.equal(dataview.type, 'histogram');
assert.ok(dataview.bin_width > 0, 'Unexpected bin width: ' + dataview.bin_width);
assert.equal(dataview.bins.length, 15);
dataview.bins.forEach(function(bin) {
assert.ok(bin.min <= bin.max, 'bin min < bin max: ' + JSON.stringify(bin));
});
@ -174,6 +175,7 @@ describe('histogram-dataview for date column type', function() {
assert.ok(!err, err);
assert.equal(dataview.type, 'histogram');
assert.ok(dataview.bin_width > 0, 'Unexpected bin width: ' + dataview.bin_width);
assert.equal(dataview.bins.length, 61);
dataview.bins.forEach(function(bin) {
assert.ok(bin.min <= bin.max, 'bin min < bin max: ' + JSON.stringify(bin));
});
@ -181,4 +183,25 @@ describe('histogram-dataview for date column type', function() {
done();
});
});
it('should override start and end', function (done) {
var params = {
start: 1180659600, // 2007-06-01 00:00:00
end: 1193792400 // 2007-10-31 01:00:00
};
this.testClient = new TestClient(mapConfig, 1234);
this.testClient.getDataview('date_histogram', params, function(err, dataview) {
assert.ok(!err, err);
assert.equal(dataview.type, 'histogram');
assert.ok(dataview.bin_width > 0, 'Unexpected bin width: ' + dataview.bin_width);
assert.equal(dataview.bins.length, 5);
dataview.bins.forEach(function(bin) {
assert.ok(bin.min <= bin.max, 'bin min < bin max: ' + JSON.stringify(bin));
});
done();
});
});
});