Skip to content

Commit

Permalink
[Lens] Implement time scaling function (#82104)
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 authored Nov 6, 2020
1 parent dae2851 commit 94d0e60
Show file tree
Hide file tree
Showing 8 changed files with 601 additions and 17 deletions.
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
41 changes: 39 additions & 2 deletions src/plugins/data/common/search/aggs/utils/time_column_meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,43 @@ describe('getDateMetaByDatatableColumn', () => {
});
});

it('throws if unable to resolve interval', async () => {
await expect(
getDateMetaByDatatableColumn(params)({
id: 'test',
name: 'test',
meta: {
type: 'date',
source: 'esaggs',
sourceParams: {
type: BUCKET_TYPES.DATE_HISTOGRAM,
params: {
time_zone: 'UTC',
interval: 'auto',
},
},
},
})
).rejects.toBeDefined();

await expect(
getDateMetaByDatatableColumn(params)({
id: 'test',
name: 'test',
meta: {
type: 'date',
source: 'esaggs',
sourceParams: {
type: BUCKET_TYPES.DATE_HISTOGRAM,
params: {
time_zone: 'UTC',
},
},
},
})
).rejects.toBeDefined();
});

it('returns resolved auto interval', async () => {
expect(
await getDateMetaByDatatableColumn(params)({
Expand All @@ -106,8 +143,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',
},
},
},
Expand Down
10 changes: 6 additions & 4 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,9 +52,11 @@ export const getDateMetaByDatatableColumn = ({
);

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

if (!interval) {
if (!interval || interval === 'auto') {
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 @@ -76,6 +76,7 @@ export function columnToOperation(column: IndexPatternColumn, uniqueLabel?: stri

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

export function getIndexPatternDatasource({
core,
Expand Down
Loading

0 comments on commit 94d0e60

Please sign in to comment.