-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Lens] Pass used histogram interval to chart #91370
Changes from 5 commits
d4837cf
14242b2
b75a99c
e26b534
812ef89
37fc9ce
22dfa5d
32250aa
dd5025a
35ebddc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,10 @@ import { aggHistogramFnName } from './histogram_fn'; | |
import { ExtendedBounds } from './lib/extended_bounds'; | ||
import { isAutoInterval, autoInterval } from './_interval_options'; | ||
import { calculateHistogramInterval } from './lib/histogram_calculate_interval'; | ||
import { | ||
buildSerializedAutoInterval, | ||
isSerializedAutoInterval, | ||
} from '../utils/get_number_histogram_interval'; | ||
|
||
export interface AutoBounds { | ||
min: number; | ||
|
@@ -152,6 +156,29 @@ export const getHistogramBucketAgg = ({ | |
esTypes: aggConfig.params.field?.spec?.esTypes || [], | ||
}); | ||
}, | ||
serialize(val, aggConfig) { | ||
// store actually used auto interval in serialized agg config to be able to read it from the result data table meta information | ||
const autoBounds = aggConfig?.getAutoBounds(); | ||
if (val === autoInterval && aggConfig && autoBounds) { | ||
const usedInterval = calculateHistogramInterval({ | ||
values: autoBounds, | ||
interval: aggConfig.params.interval, | ||
maxBucketsUiSettings: getConfig(UI_SETTINGS.HISTOGRAM_MAX_BARS), | ||
maxBucketsUserInput: aggConfig.params.maxBars, | ||
intervalBase: aggConfig.params.intervalBase, | ||
esTypes: aggConfig.params.field?.spec?.esTypes || [], | ||
}); | ||
return buildSerializedAutoInterval(usedInterval); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could create a new property on this aggConfig
we don't need any deserialize method for it, write should be _.noop |
||
} else { | ||
return val; | ||
} | ||
}, | ||
deserialize(val) { | ||
if (isSerializedAutoInterval(val)) { | ||
return autoInterval; | ||
} | ||
return val; | ||
}, | ||
}, | ||
{ | ||
name: 'maxBars', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { getNumberHistogramIntervalByDatatableColumn } from '.'; | ||
import { BUCKET_TYPES } from '../buckets'; | ||
|
||
describe('getNumberHistogramIntervalByDatatableColumn', () => { | ||
it('returns nothing on column from other data source', () => { | ||
expect( | ||
getNumberHistogramIntervalByDatatableColumn({ | ||
id: 'test', | ||
name: 'test', | ||
meta: { | ||
type: 'date', | ||
source: 'essql', | ||
}, | ||
}) | ||
).toEqual(undefined); | ||
}); | ||
|
||
it('returns nothing on non histogram column', () => { | ||
expect( | ||
getNumberHistogramIntervalByDatatableColumn({ | ||
id: 'test', | ||
name: 'test', | ||
meta: { | ||
type: 'date', | ||
source: 'esaggs', | ||
sourceParams: { | ||
type: BUCKET_TYPES.TERMS, | ||
}, | ||
}, | ||
}) | ||
).toEqual(undefined); | ||
}); | ||
|
||
it('returns interval on resolved auto interval', () => { | ||
expect( | ||
getNumberHistogramIntervalByDatatableColumn({ | ||
id: 'test', | ||
name: 'test', | ||
meta: { | ||
type: 'date', | ||
source: 'esaggs', | ||
sourceParams: { | ||
type: BUCKET_TYPES.HISTOGRAM, | ||
params: { | ||
interval: 'auto$$$20', | ||
}, | ||
}, | ||
}, | ||
}) | ||
).toEqual(20); | ||
}); | ||
|
||
it('returns interval on fixed interval', () => { | ||
expect( | ||
getNumberHistogramIntervalByDatatableColumn({ | ||
id: 'test', | ||
name: 'test', | ||
meta: { | ||
type: 'date', | ||
source: 'esaggs', | ||
sourceParams: { | ||
type: BUCKET_TYPES.HISTOGRAM, | ||
params: { | ||
interval: 7, | ||
}, | ||
}, | ||
}, | ||
}) | ||
).toEqual(7); | ||
}); | ||
|
||
it('returns undefined if information is not available', () => { | ||
expect( | ||
getNumberHistogramIntervalByDatatableColumn({ | ||
id: 'test', | ||
name: 'test', | ||
meta: { | ||
type: 'date', | ||
source: 'esaggs', | ||
sourceParams: { | ||
type: BUCKET_TYPES.HISTOGRAM, | ||
params: {}, | ||
}, | ||
}, | ||
}) | ||
).toEqual(undefined); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { DatatableColumn } from 'src/plugins/expressions/common'; | ||
import type { AggParamsHistogram } from '../buckets'; | ||
import { BUCKET_TYPES } from '../buckets/bucket_agg_types'; | ||
|
||
const SEPARATOR = '$$$'; | ||
|
||
function parseSerializedInterval(interval: string | number) { | ||
if (typeof interval === 'number') { | ||
return interval; | ||
} | ||
if (interval === 'auto') { | ||
return 'auto'; | ||
} | ||
return Number(interval.split(SEPARATOR)[1]); | ||
} | ||
|
||
export function buildSerializedAutoInterval(usedInterval: number) { | ||
return `auto${SEPARATOR}${usedInterval}`; | ||
} | ||
|
||
export function isSerializedAutoInterval(interval: string | number) { | ||
return typeof interval === 'string' && interval.startsWith('auto'); | ||
} | ||
|
||
/** | ||
* Helper function returning the used interval for data table column created by the histogramm agg type. | ||
* "auto" will get expanded to the actually used interval. | ||
* If the column is not a column created by a histogram aggregation of the esaggs data source, | ||
* this function will return undefined. | ||
*/ | ||
export const getNumberHistogramIntervalByDatatableColumn = (column: DatatableColumn) => { | ||
if (column.meta.source !== 'esaggs') return; | ||
if (column.meta.sourceParams?.type !== BUCKET_TYPES.HISTOGRAM) return; | ||
const params = (column.meta.sourceParams.params as unknown) as AggParamsHistogram; | ||
|
||
if (!params.interval) { | ||
return undefined; | ||
} | ||
return parseSerializedInterval(params.interval); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic is duplicated with the
write
andserialize
functions. I can't think of a better way, but maybe you can?