Support quarter aggregation in histograms over date columns

This commit is contained in:
Daniel García Aubert 2017-06-05 16:04:42 +02:00
parent d01787842f
commit 95a6ad3b86
3 changed files with 47 additions and 1 deletions

View File

@ -20,7 +20,7 @@ function DataviewBackend(analysisBackend) {
this.analysisBackend = analysisBackend;
}
var DATE_AGGREGATIONS = ['minute', 'hour', 'day', 'week', 'month', 'year'];
var DATE_AGGREGATIONS = ['minute', 'hour', 'day', 'week', 'month', 'quarter', 'year'];
module.exports = DataviewBackend;

View File

@ -136,7 +136,11 @@ var dateBinsQueryTpl = dot.template([
' FROM (',
' SELECT',
' ARRAY(',
' {{?it._aggregation==="quarter"}}',
' SELECT GENERATE_SERIES(start_date, end_date, \'3 month\'::interval)',
' {{??}}',
' SELECT GENERATE_SERIES(start_date, end_date, \'1 {{=it._aggregation}}\'::interval)',
' {{?}}',
' ) AS bins_array',
' FROM basics',
' ) _cdb_bins_array',

View File

@ -307,4 +307,46 @@ describe('histogram-dataview for date column type', function() {
done();
});
});
it('should aggregate histogram using "quarter" aggregation', function (done) {
var TIMEZONE_UTC_IN_SECONDS = 0 * 3600; // UTC
var TIMEZONE_UTC_IN_MINUTES = 0 * 60; // UTC
var params = {
timezone: TIMEZONE_UTC_IN_SECONDS,
aggregation: 'quarter'
};
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, 6);
var initialTimestamp = '2007-01-01T00:00:00Z'; // UTC midnight
var binsStartInMilliseconds = dataview.bins_start * 1000;
var binsStartFormatted = moment.utc(binsStartInMilliseconds)
.utcOffset(TIMEZONE_UTC_IN_MINUTES)
.format();
assert.equal(binsStartFormatted, initialTimestamp);
dataview.bins.forEach(function(bin, index) {
var binTimestampExpected = moment.utc(initialTimestamp)
.utcOffset(TIMEZONE_UTC_IN_MINUTES)
.add(index * 3, 'month')
.format();
var binsTimestampInMilliseconds = bin.timestamp * 1000;
var binTimestampFormatted = moment.utc(binsTimestampInMilliseconds)
.utcOffset(TIMEZONE_UTC_IN_MINUTES)
.format();
assert.equal(binTimestampFormatted, binTimestampExpected);
assert.ok(bin.timestamp <= bin.min, 'bin timestamp < bin min: ' + JSON.stringify(bin));
assert.ok(bin.min <= bin.max, 'bin min < bin max: ' + JSON.stringify(bin));
});
done();
});
});
});