Skip to content

Commit

Permalink
Refactoring and start adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Oct 30, 2020
1 parent 30b2e5c commit b385e33
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 22 deletions.
5 changes: 2 additions & 3 deletions src/plugins/data/common/search/aggs/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import { Assign } from '@kbn/utility-types';
import { DatatableColumn } from 'src/plugins/expressions';
import { IndexPattern } from '../../index_patterns/index_patterns/index_pattern';
import { TimeRange } from '../../query';
import {
AggConfigSerialized,
AggConfigs,
Expand Down Expand Up @@ -93,9 +94,7 @@ export interface AggsCommonStart {
*/
getDateMetaByDatatableColumn: (
column: DatatableColumn
) => Promise<
undefined | { timeZone: string; timeRange?: { min?: number; max?: number }; interval: string }
>;
) => Promise<undefined | { timeZone: string; timeRange?: TimeRange; interval: string }>;
createAggConfigs: (
indexPattern: IndexPattern,
configStates?: CreateAggConfigParams[],
Expand Down
14 changes: 3 additions & 11 deletions src/plugins/data/common/search/aggs/utils/time_column_meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* under the License.
*/

import moment from 'moment';
import { DatatableColumn } from 'src/plugins/expressions/common';
import { IndexPattern } from '../../../index_patterns';

Expand All @@ -39,15 +38,11 @@ export const getDateMetaByDatatableColumn = ({
getConfig,
}: DateMetaByColumnDeps) => async (
column: DatatableColumn
): Promise<
undefined | { timeZone: string; timeRange?: { min?: number; max?: number }; 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
| { min?: number; max?: number }
| undefined;
const appliedTimeRange = column.meta.sourceParams.appliedTimeRange as TimeRange | undefined;

const tz = inferTimeZone(
params,
Expand All @@ -58,10 +53,7 @@ export const getDateMetaByDatatableColumn = ({

const interval =
params.interval === 'auto' && appliedTimeRange
? calculateAutoTimeExpression({
from: moment(appliedTimeRange.min).toISOString(),
to: moment(appliedTimeRange.max).toISOString(),
})
? calculateAutoTimeExpression(appliedTimeRange)
: params.interval;

if (!interval) {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/public/search/expressions/esaggs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,8 @@ export const esaggs = (): EsaggsExpressionFunctionDefinition => ({
args.timeFields &&
args.timeFields.includes(column.aggConfig.params.field?.name)
? {
min: resolvedTimeRange?.min?.valueOf(),
max: resolvedTimeRange?.max?.valueOf(),
from: resolvedTimeRange?.min?.toISOString(),
to: resolvedTimeRange?.max?.toISOString(),
}
: undefined,
...column.aggConfig.serialize(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import moment from 'moment';
import { Datatable } from 'src/plugins/expressions/public';
import { DataPublicPluginStart } from 'src/plugins/data/public';
import { dataPluginMock } from '../../../../../src/plugins/data/public/mocks';
import { functionWrapper } from 'src/plugins/expressions/common/expression_functions/specs/tests/utils';
import { getTimeScaleFunction, TimeScaleArgs } from './time_scale';

describe('time_scale', () => {
let timeScale: (input: Datatable, args: TimeScaleArgs) => Promise<Datatable>;
let dataMock: jest.Mocked<DataPublicPluginStart>;

const emptyTable: Datatable = {
type: 'datatable',
columns: [
{
id: 'date',
name: 'date',
meta: {
type: 'date',
},
},
{
id: 'metric',
name: 'metric',
meta: {
type: 'number',
},
},
],
rows: [],
};

const defaultArgs: TimeScaleArgs = {
dateColumnId: 'date',
inputColumnId: 'metric',
outputColumnId: 'scaledMetric',
targetUnit: 'h',
};

beforeEach(() => {
dataMock = dataPluginMock.createStartContract();
(dataMock.search.aggs.getDateMetaByDatatableColumn as jest.Mock).mockReturnValue({
timeZone: 'UTC',
timeRange: {
from: '2020-10-05T00:00:00.000Z',
to: '2020-10-10T00:00:00.000Z',
},
interval: '1d',
});
(dataMock.query.timefilter.timefilter.calculateBounds as jest.Mock).mockReturnValue({
min: moment('2020-10-05T00:00:00.000Z'),
max: moment('2020-10-10T00:00:00.000Z'),
});
timeScale = functionWrapper(getTimeScaleFunction(dataMock));
});

it('should apply time scale factor to each row', async () => {
const result = await timeScale(
{
...emptyTable,
rows: [
{
date: moment('2020-10-05T00:00:00.000Z').valueOf(),
metric: 24,
},
{
date: moment('2020-10-06T00:00:00.000Z').valueOf(),
metric: 24,
},
{
date: moment('2020-10-07T00:00:00.000Z').valueOf(),
metric: 24,
},
{
date: moment('2020-10-08T00:00:00.000Z').valueOf(),
metric: 24,
},
{
date: moment('2020-10-09T00:00:00.000Z').valueOf(),
metric: 24,
},
],
},
{
...defaultArgs,
}
);

expect(result.rows.map(({ scaledMetric }) => scaledMetric)).toEqual([1, 1, 1, 1, 1]);
});
});
10 changes: 4 additions & 6 deletions x-pack/plugins/lens/public/indexpattern_datasource/time_scale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { parseInterval } from 'src/plugins/data/common';

type TimeScaleUnit = 's' | 'm' | 'h' | 'd';

interface TimeScaleArgs {
export interface TimeScaleArgs {
dateColumnId: string;
inputColumnId: string;
outputColumnId: string;
Expand Down Expand Up @@ -129,10 +129,8 @@ export function getTimeScaleFunction(data: DataPublicPluginStart) {
const defaultTimezone = moment().zoneName();
moment.tz.setDefault(timeInfo.timeZone);

const timeBounds = timeInfo.timeRange && {
min: moment(timeInfo.timeRange.min),
max: moment(timeInfo.timeRange.max),
};
const timeBounds =
timeInfo.timeRange && data.query.timefilter.timefilter.calculateBounds(timeInfo.timeRange);

const result = {
...input,
Expand All @@ -144,7 +142,7 @@ export function getTimeScaleFunction(data: DataPublicPluginStart) {
if (timeBounds && timeBounds.min) {
startOfBucket = moment.max(startOfBucket, timeBounds.min);
}
let endOfBucket = startOfBucket.add(intervalDuration);
let endOfBucket = startOfBucket.clone().add(intervalDuration);
if (timeBounds && timeBounds.max) {
endOfBucket = moment.min(endOfBucket, timeBounds.max);
}
Expand Down

0 comments on commit b385e33

Please sign in to comment.