Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaled date format for date histogram aggregation #12384

Merged
merged 8 commits into from
Jun 20, 2017
4 changes: 2 additions & 2 deletions src/ui/public/agg_table/__tests__/_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe('AggTable Directive', function () {
it('as min', function () {
totalsRowTest('min', [
'',
'September 28th 2014, 00:00:00.000',
'2014-09-28',
'9,283',
'September 28th 2014, 00:00:00.000',
'1',
Expand All @@ -174,7 +174,7 @@ describe('AggTable Directive', function () {
it('as max', function () {
totalsRowTest('max', [
'',
'October 3rd 2014, 00:00:00.000',
'2014-10-03',
'220,943',
'October 3rd 2014, 00:00:00.000',
'239',
Expand Down
3 changes: 3 additions & 0 deletions src/ui/public/agg_types/buckets/date_histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export function AggTypesBucketsDateHistogramProvider(timefilter, config, Private
}
};
},
getFormat: function (agg) {
return agg.buckets.getScaledDateFormatter();
},
params: [
{
name: 'field',
Expand Down
10 changes: 10 additions & 0 deletions src/ui/public/time_buckets/time_buckets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import moment from 'moment';
import { parseInterval } from 'ui/utils/parse_interval';
import { TimeBucketsCalcAutoIntervalProvider } from 'ui/time_buckets/calc_auto_interval';
import { TimeBucketsCalcEsIntervalProvider } from 'ui/time_buckets/calc_es_interval';
import { RegistryFieldFormatsProvider } from 'ui/registry/field_formats';

export function TimeBucketsProvider(Private, timefilter, config) {
const calcAuto = Private(TimeBucketsCalcAutoIntervalProvider);
const calcEsInterval = Private(TimeBucketsCalcEsIntervalProvider);
const fieldFormats = Private(RegistryFieldFormatsProvider);
const getConfig = (...args) => config.get(...args);

function isValidMoment(m) {
return m && ('isValid' in m) && m.isValid();
Expand Down Expand Up @@ -274,6 +277,13 @@ export function TimeBucketsProvider(Private, timefilter, config) {
return config.get('dateFormat');
};

TimeBuckets.prototype.getScaledDateFormatter = function () {
const DateFieldFormat = fieldFormats.getType('date');
return new DateFieldFormat({
pattern: this.getScaledDateFormat()
}, getConfig);
};


TimeBuckets.__cached__ = function (self) {
let cache = {};
Expand Down
76 changes: 27 additions & 49 deletions src/ui/public/vis/__tests__/_agg_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,21 +414,9 @@ describe('AggConfig', function () {
});
});

describe('#fieldFormatter', function () {
it('returns the fields format unless the agg type has a custom getFormat handler', function () {
let vis = new Vis(indexPattern, {
type: 'histogram',
aggs: [
{
type: 'date_histogram',
schema: 'segment',
params: { field: '@timestamp' }
}
]
});
expect(vis.aggs[0].fieldFormatter()).to.be(vis.aggs[0].getField().format.getConverterFor());

vis = new Vis(indexPattern, {
describe('#fieldFormatter - custom getFormat handler', function () {
it('returns formatter from getFormat handler', function () {
const vis = new Vis(indexPattern, {
type: 'metric',
aggs: [
{
Expand All @@ -440,53 +428,43 @@ describe('AggConfig', function () {
});
expect(vis.aggs[0].fieldFormatter()).to.be(fieldFormat.getDefaultInstance('number').getConverterFor());
});
});

it('returns the string format if the field does not have a format', function () {
const vis = new Vis(indexPattern, {
type: 'histogram',
aggs: [
{
type: 'date_histogram',
schema: 'segment',
params: { field: '@timestamp' }
}
]
});
describe('#fieldFormatter - no custom getFormat handler', function () {

const visStateAggWithoutCustomGetFormat = {
type: 'table',
aggs: [
{
type: 'terms',
schema: 'bucket',
params: { field: 'bytes' }
}
]
};
let vis;

beforeEach(function () {
vis = new Vis(indexPattern, visStateAggWithoutCustomGetFormat);
});

it('returns the field\'s formatter', function () {
expect(vis.aggs[0].fieldFormatter()).to.be(vis.aggs[0].getField().format.getConverterFor());
});

it('returns the string format if the field does not have a format', function () {
const agg = vis.aggs[0];
agg.params.field = { type: 'date', format: null };
agg.params.field = { type: 'number', format: null };
expect(agg.fieldFormatter()).to.be(fieldFormat.getDefaultInstance('string').getConverterFor());
});

it('returns the string format if their is no field', function () {
const vis = new Vis(indexPattern, {
type: 'histogram',
aggs: [
{
type: 'date_histogram',
schema: 'segment',
params: { field: '@timestamp' }
}
]
});

const agg = vis.aggs[0];
delete agg.params.field;
expect(agg.fieldFormatter()).to.be(fieldFormat.getDefaultInstance('string').getConverterFor());
});

it('returns the html converter if "html" is passed in', function () {
const vis = new Vis(indexPattern, {
type: 'histogram',
aggs: [
{
type: 'avg',
schema: 'metric',
params: { field: 'bytes' }
}
]
});

const field = indexPattern.fields.byName.bytes;
expect(vis.aggs[0].fieldFormatter('html')).to.be(field.format.getConverterFor('html'));
});
Expand Down
48 changes: 24 additions & 24 deletions test/functional/apps/visualize/_area_chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,30 +141,30 @@ export default function ({ getService, getPageObjects }) {
});

it('should show correct data', function () {
const expectedTableData = [ 'September 20th 2015, 00:00:00.000', '37',
'September 20th 2015, 03:00:00.000', '202',
'September 20th 2015, 06:00:00.000', '740',
'September 20th 2015, 09:00:00.000', '1,437',
'September 20th 2015, 12:00:00.000', '1,371',
'September 20th 2015, 15:00:00.000', '751',
'September 20th 2015, 18:00:00.000', '188',
'September 20th 2015, 21:00:00.000', '31',
'September 21st 2015, 00:00:00.000', '42',
'September 21st 2015, 03:00:00.000', '202',
'September 21st 2015, 06:00:00.000', '683',
'September 21st 2015, 09:00:00.000', '1,361',
'September 21st 2015, 12:00:00.000', '1,415',
'September 21st 2015, 15:00:00.000', '707',
'September 21st 2015, 18:00:00.000', '177',
'September 21st 2015, 21:00:00.000', '27',
'September 22nd 2015, 00:00:00.000', '32',
'September 22nd 2015, 03:00:00.000', '175',
'September 22nd 2015, 06:00:00.000', '707',
'September 22nd 2015, 09:00:00.000', '1,408',
'September 22nd 2015, 12:00:00.000', '1,355',
'September 22nd 2015, 15:00:00.000', '726',
'September 22nd 2015, 18:00:00.000', '201',
'September 22nd 2015, 21:00:00.000', '29'
const expectedTableData = [ '2015-09-20 00:00', '37',
'2015-09-20 03:00', '202',
'2015-09-20 06:00', '740',
'2015-09-20 09:00', '1,437',
'2015-09-20 12:00', '1,371',
'2015-09-20 15:00', '751',
'2015-09-20 18:00', '188',
'2015-09-20 21:00', '31',
'2015-09-21 00:00', '42',
'2015-09-21 03:00', '202',
'2015-09-21 06:00', '683',
'2015-09-21 09:00', '1,361',
'2015-09-21 12:00', '1,415',
'2015-09-21 15:00', '707',
'2015-09-21 18:00', '177',
'2015-09-21 21:00', '27',
'2015-09-22 00:00', '32',
'2015-09-22 03:00', '175',
'2015-09-22 06:00', '707',
'2015-09-22 09:00', '1,408',
'2015-09-22 12:00', '1,355',
'2015-09-22 15:00', '726',
'2015-09-22 18:00', '201',
'2015-09-22 21:00', '29'
];

return PageObjects.visualize.collapseChart()
Expand Down
20 changes: 10 additions & 10 deletions test/functional/apps/visualize/_heatmap_chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,16 @@ export default function ({ getService, getPageObjects }) {

it('should show correct data', function () {
// this is only the first page of the tabular data.
const expectedChartData = [ 'September 20th 2015, 00:00:00.000', '37',
'September 20th 2015, 03:00:00.000', '202',
'September 20th 2015, 06:00:00.000', '740',
'September 20th 2015, 09:00:00.000', '1,437',
'September 20th 2015, 12:00:00.000', '1,371',
'September 20th 2015, 15:00:00.000', '751',
'September 20th 2015, 18:00:00.000', '188',
'September 20th 2015, 21:00:00.000', '31',
'September 21st 2015, 00:00:00.000', '42',
'September 21st 2015, 03:00:00.000', '202'
const expectedChartData = [ '2015-09-20 00:00', '37',
'2015-09-20 03:00', '202',
'2015-09-20 06:00', '740',
'2015-09-20 09:00', '1,437',
'2015-09-20 12:00', '1,371',
'2015-09-20 15:00', '751',
'2015-09-20 18:00', '188',
'2015-09-20 21:00', '31',
'2015-09-21 00:00', '42',
'2015-09-21 03:00', '202'
];

return PageObjects.visualize.collapseChart()
Expand Down
20 changes: 10 additions & 10 deletions test/functional/apps/visualize/_vertical_bar_chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,16 @@ export default function ({ getService, getPageObjects }) {

it('should show correct data', function () {
// this is only the first page of the tabular data.
const expectedChartData = [ 'September 20th 2015, 00:00:00.000', '37',
'September 20th 2015, 03:00:00.000', '202',
'September 20th 2015, 06:00:00.000', '740',
'September 20th 2015, 09:00:00.000', '1,437',
'September 20th 2015, 12:00:00.000', '1,371',
'September 20th 2015, 15:00:00.000', '751',
'September 20th 2015, 18:00:00.000', '188',
'September 20th 2015, 21:00:00.000', '31',
'September 21st 2015, 00:00:00.000', '42',
'September 21st 2015, 03:00:00.000', '202'
const expectedChartData = [ '2015-09-20 00:00', '37',
'2015-09-20 03:00', '202',
'2015-09-20 06:00', '740',
'2015-09-20 09:00', '1,437',
'2015-09-20 12:00', '1,371',
'2015-09-20 15:00', '751',
'2015-09-20 18:00', '188',
'2015-09-20 21:00', '31',
'2015-09-21 00:00', '42',
'2015-09-21 03:00', '202'
];

return PageObjects.visualize.collapseChart()
Expand Down