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 all 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
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',
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
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