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

[Lens] Implement time scaling function #82104

Merged
merged 8 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/plugins/data/common/search/aggs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export interface AggsCommonStart {
*/
getDateMetaByDatatableColumn: (
column: DatatableColumn
) => Promise<undefined | { timeZone: string; timeRange: TimeRange; interval: string }>;
) => Promise<undefined | { timeZone: string; timeRange?: TimeRange; interval: string }>;
createAggConfigs: (
indexPattern: IndexPattern,
configStates?: CreateAggConfigParams[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ describe('getDateMetaByDatatableColumn', () => {
interval: 'auto',
},
appliedTimeRange: {
from: 'now-5d',
to: 'now',
from: '2020-10-05T00:00:00.000Z',
to: '2020-10-10T00:00:00.000Z',
Comment on lines +146 to +147
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should also add a test for the new case that is introduced in this PR where we fall back to interval if the appliedTimeRange is undefined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point - we always had this case but the types didn't reflect it. Actually it's an error case, I extended the condition to throw an error and added unit tests.

},
},
},
Expand Down
8 changes: 5 additions & 3 deletions src/plugins/data/common/search/aggs/utils/time_column_meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ export const getDateMetaByDatatableColumn = ({
getConfig,
}: DateMetaByColumnDeps) => async (
column: DatatableColumn
): Promise<undefined | { timeZone: string; timeRange: TimeRange; interval: string }> => {
): Promise<undefined | { timeZone: string; timeRange?: TimeRange; interval: string }> => {
if (column.meta.source !== 'esaggs') return;
if (column.meta.sourceParams?.type !== BUCKET_TYPES.DATE_HISTOGRAM) return;
const params = column.meta.sourceParams.params as AggParamsDateHistogram;
const appliedTimeRange = column.meta.sourceParams.appliedTimeRange as TimeRange;
const appliedTimeRange = column.meta.sourceParams.appliedTimeRange as TimeRange | undefined;

const tz = inferTimeZone(
params,
Expand All @@ -52,7 +52,9 @@ export const getDateMetaByDatatableColumn = ({
);

const interval =
params.interval === 'auto' ? calculateAutoTimeExpression(appliedTimeRange) : params.interval;
params.interval === 'auto' && appliedTimeRange
? calculateAutoTimeExpression(appliedTimeRange)
: params.interval;

if (!interval) {
throw new Error('time interval could not be determined');
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/data/public/search/expressions/esaggs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,8 @@ export const esaggs = (): EsaggsExpressionFunctionDefinition => ({
searchSource.setField('index', indexPattern);
searchSource.setField('size', 0);

const resolvedTimeRange = input?.timeRange && calculateBounds(input.timeRange);

const response = await handleCourierRequest({
searchSource,
aggs,
Expand Down Expand Up @@ -303,7 +305,10 @@ export const esaggs = (): EsaggsExpressionFunctionDefinition => ({
input?.timeRange &&
args.timeFields &&
args.timeFields.includes(column.aggConfig.params.field?.name)
? { from: input.timeRange.from, to: input.timeRange.to }
? {
from: resolvedTimeRange?.min?.toISOString(),
to: resolvedTimeRange?.max?.toISOString(),
}
: undefined,
...column.aggConfig.serialize(),
},
Expand Down
22 changes: 13 additions & 9 deletions x-pack/plugins/lens/public/indexpattern_datasource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,23 @@ export class IndexPatternDatasource {
{ expressions, editorFrame, charts }: IndexPatternDatasourceSetupPlugins
) {
editorFrame.registerDatasource(async () => {
const { getIndexPatternDatasource, renameColumns, formatColumn } = await import(
'../async_services'
);
expressions.registerFunction(renameColumns);
expressions.registerFunction(formatColumn);
return core.getStartServices().then(([coreStart, { data }]) =>
getIndexPatternDatasource({
const {
getIndexPatternDatasource,
renameColumns,
formatColumn,
getTimeScaleFunction,
} = await import('../async_services');
return core.getStartServices().then(([coreStart, { data }]) => {
expressions.registerFunction(getTimeScaleFunction(data));
expressions.registerFunction(renameColumns);
expressions.registerFunction(formatColumn);
return getIndexPatternDatasource({
core: coreStart,
storage: new Storage(localStorage),
data,
charts,
})
) as Promise<Datasource>;
});
}) as Promise<Datasource>;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export function uniqueLabels(layers: Record<string, IndexPatternLayer>) {

export * from './rename_columns';
export * from './format_column';
export * from './time_scale';

export function getIndexPatternDatasource({
core,
Expand Down
Loading