Skip to content

Commit

Permalink
Merge branch 'master' into fix/88786
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Jan 25, 2021
2 parents 5281956 + f15a1e6 commit 92cbb3b
Show file tree
Hide file tree
Showing 45 changed files with 1,792 additions and 165 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* and the Server Side Public License, v 1; you may not use this file except in
* compliance with, at your election, the Elastic License or the Server Side
* Public License, v 1.
*/

import { dataPluginMock } from '../../../../data/public/mocks';
import { createSessionRestorationDataProvider } from './session_restoration';
import { getAppStateDefaults } from './get_app_state_defaults';
import { getSavedDashboardMock } from '../test_helpers';
import { SavedObjectTagDecoratorTypeGuard } from '../../../../saved_objects_tagging_oss/public';

describe('createSessionRestorationDataProvider', () => {
const mockDataPlugin = dataPluginMock.createStartContract();
const searchSessionInfoProvider = createSessionRestorationDataProvider({
data: mockDataPlugin,
getAppState: () =>
getAppStateDefaults(
getSavedDashboardMock(),
false,
((() => false) as unknown) as SavedObjectTagDecoratorTypeGuard
),
getDashboardTitle: () => 'Dashboard',
getDashboardId: () => 'Id',
});

describe('session state', () => {
test('restoreState has sessionId and initialState has not', async () => {
const searchSessionId = 'id';
(mockDataPlugin.search.session.getSessionId as jest.Mock).mockImplementation(
() => searchSessionId
);
const { initialState, restoreState } = await searchSessionInfoProvider.getUrlGeneratorData();
expect(initialState.searchSessionId).toBeUndefined();
expect(restoreState.searchSessionId).toBe(searchSessionId);
});

test('restoreState has absoluteTimeRange', async () => {
const relativeTime = 'relativeTime';
const absoluteTime = 'absoluteTime';
(mockDataPlugin.query.timefilter.timefilter.getTime as jest.Mock).mockImplementation(
() => relativeTime
);
(mockDataPlugin.query.timefilter.timefilter.getAbsoluteTime as jest.Mock).mockImplementation(
() => absoluteTime
);
const { initialState, restoreState } = await searchSessionInfoProvider.getUrlGeneratorData();
expect(initialState.timeRange).toBe(relativeTime);
expect(restoreState.timeRange).toBe(absoluteTime);
});

test('restoreState has refreshInterval paused', async () => {
const { initialState, restoreState } = await searchSessionInfoProvider.getUrlGeneratorData();
expect(initialState.refreshInterval).toBeUndefined();
expect(restoreState.refreshInterval?.pause).toBe(true);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export function createSessionRestorationDataProvider(deps: {
getUrlGeneratorData: async () => {
return {
urlGeneratorId: DASHBOARD_APP_URL_GENERATOR,
initialState: getUrlGeneratorState({ ...deps, forceAbsoluteTime: false }),
restoreState: getUrlGeneratorState({ ...deps, forceAbsoluteTime: true }),
initialState: getUrlGeneratorState({ ...deps, shouldRestoreSearchSession: false }),
restoreState: getUrlGeneratorState({ ...deps, shouldRestoreSearchSession: true }),
};
},
};
Expand All @@ -32,20 +32,17 @@ function getUrlGeneratorState({
data,
getAppState,
getDashboardId,
forceAbsoluteTime,
shouldRestoreSearchSession,
}: {
data: DataPublicPluginStart;
getAppState: () => DashboardAppState;
getDashboardId: () => string;
/**
* Can force time range from time filter to convert from relative to absolute time range
*/
forceAbsoluteTime: boolean;
shouldRestoreSearchSession: boolean;
}): DashboardUrlGeneratorState {
const appState = getAppState();
return {
dashboardId: getDashboardId(),
timeRange: forceAbsoluteTime
timeRange: shouldRestoreSearchSession
? data.query.timefilter.timefilter.getAbsoluteTime()
: data.query.timefilter.timefilter.getTime(),
filters: data.query.filterManager.getFilters(),
Expand All @@ -55,6 +52,12 @@ function getUrlGeneratorState({
preserveSavedFilters: false,
viewMode: appState.viewMode,
panels: getDashboardId() ? undefined : appState.panels,
searchSessionId: data.search.session.getSessionId(),
searchSessionId: shouldRestoreSearchSession ? data.search.session.getSessionId() : undefined,
refreshInterval: shouldRestoreSearchSession
? {
pause: true, // force pause refresh interval when restoring a session
value: 0,
}
: undefined,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ describe('Test discover state with legacy migration', () => {

describe('createSearchSessionRestorationDataProvider', () => {
let mockSavedSearch: SavedSearch = ({} as unknown) as SavedSearch;
const mockDataPlugin = dataPluginMock.createStartContract();
const searchSessionInfoProvider = createSearchSessionRestorationDataProvider({
data: dataPluginMock.createStartContract(),
data: mockDataPlugin,
appStateContainer: getState({
history: createBrowserHistory(),
}).appStateContainer,
Expand All @@ -124,4 +125,30 @@ describe('createSearchSessionRestorationDataProvider', () => {
expect(await searchSessionInfoProvider.getName()).toBe('Discover');
});
});

describe('session state', () => {
test('restoreState has sessionId and initialState has not', async () => {
const searchSessionId = 'id';
(mockDataPlugin.search.session.getSessionId as jest.Mock).mockImplementation(
() => searchSessionId
);
const { initialState, restoreState } = await searchSessionInfoProvider.getUrlGeneratorData();
expect(initialState.searchSessionId).toBeUndefined();
expect(restoreState.searchSessionId).toBe(searchSessionId);
});

test('restoreState has absoluteTimeRange', async () => {
const relativeTime = 'relativeTime';
const absoluteTime = 'absoluteTime';
(mockDataPlugin.query.timefilter.timefilter.getTime as jest.Mock).mockImplementation(
() => relativeTime
);
(mockDataPlugin.query.timefilter.timefilter.getAbsoluteTime as jest.Mock).mockImplementation(
() => absoluteTime
);
const { initialState, restoreState } = await searchSessionInfoProvider.getUrlGeneratorData();
expect(initialState.timeRange).toBe(relativeTime);
expect(restoreState.timeRange).toBe(absoluteTime);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,12 @@ export function createSearchSessionRestorationDataProvider(deps: {
initialState: createUrlGeneratorState({
...deps,
getSavedSearchId,
forceAbsoluteTime: false,
shouldRestoreSearchSession: false,
}),
restoreState: createUrlGeneratorState({
...deps,
getSavedSearchId,
forceAbsoluteTime: true,
shouldRestoreSearchSession: true,
}),
};
},
Expand All @@ -291,26 +291,23 @@ function createUrlGeneratorState({
appStateContainer,
data,
getSavedSearchId,
forceAbsoluteTime,
shouldRestoreSearchSession,
}: {
appStateContainer: StateContainer<AppState>;
data: DataPublicPluginStart;
getSavedSearchId: () => string | undefined;
/**
* Can force time range from time filter to convert from relative to absolute time range
*/
forceAbsoluteTime: boolean;
shouldRestoreSearchSession: boolean;
}): DiscoverUrlGeneratorState {
const appState = appStateContainer.get();
return {
filters: data.query.filterManager.getFilters(),
indexPatternId: appState.index,
query: appState.query,
savedSearchId: getSavedSearchId(),
timeRange: forceAbsoluteTime
timeRange: shouldRestoreSearchSession
? data.query.timefilter.timefilter.getAbsoluteTime()
: data.query.timefilter.timefilter.getTime(),
searchSessionId: data.search.session.getSessionId(),
searchSessionId: shouldRestoreSearchSession ? data.search.session.getSessionId() : undefined,
columns: appState.columns,
sort: appState.sort,
savedQuery: appState.savedQuery,
Expand Down
30 changes: 30 additions & 0 deletions src/plugins/telemetry/schema/oss_plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -5247,6 +5247,36 @@
}
}
},
"vis_type_table": {
"properties": {
"total": {
"type": "long"
},
"total_split": {
"type": "long"
},
"split_columns": {
"properties": {
"total": {
"type": "long"
},
"enabled": {
"type": "long"
}
}
},
"split_rows": {
"properties": {
"total": {
"type": "long"
},
"enabled": {
"type": "long"
}
}
}
}
},
"vis_type_vega": {
"properties": {
"vega_lib_specs_total": {
Expand Down
9 changes: 9 additions & 0 deletions src/plugins/vis_type_table/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* and the Server Side Public License, v 1; you may not use this file except in
* compliance with, at your election, the Elastic License or the Server Side
* Public License, v 1.
*/

export * from './types';
28 changes: 28 additions & 0 deletions src/plugins/vis_type_table/common/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* and the Server Side Public License, v 1; you may not use this file except in
* compliance with, at your election, the Elastic License or the Server Side
* Public License, v 1.
*/

export const VIS_TYPE_TABLE = 'table';

export enum AggTypes {
SUM = 'sum',
AVG = 'avg',
MIN = 'min',
MAX = 'max',
COUNT = 'count',
}

export interface TableVisParams {
perPage: number | '';
showPartialRows: boolean;
showMetricsAtAllLevels: boolean;
showToolbar: boolean;
showTotal: boolean;
totalFunc: AggTypes;
percentageCol: string;
row?: boolean;
}
1 change: 1 addition & 0 deletions src/plugins/vis_type_table/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ module.exports = {
rootDir: '../../..',
roots: ['<rootDir>/src/plugins/vis_type_table'],
testRunner: 'jasmine2',
collectCoverageFrom: ['<rootDir>/src/plugins/vis_type_table/**/*.{js,ts,tsx}'],
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
NumberInputOption,
VisOptionsProps,
} from '../../../vis_default_editor/public';
import { TableVisParams } from '../types';
import { TableVisParams } from '../../common';
import { totalAggregations } from './utils';

const { tabifyGetColumns } = search;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import React, { lazy, Suspense } from 'react';
import { EuiLoadingSpinner } from '@elastic/eui';
import { VisOptionsProps } from 'src/plugins/vis_default_editor/public';
import { TableVisParams } from '../types';
import { TableVisParams } from '../../common';

const TableOptionsComponent = lazy(() => import('./table_vis_options'));

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/vis_type_table/public/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { i18n } from '@kbn/i18n';
import { AggTypes } from '../types';
import { AggTypes } from '../../common';

const totalAggregations = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { i18n } from '@kbn/i18n';
import { ExpressionFunctionDefinition, Datatable, Render } from 'src/plugins/expressions/public';
import { tableVisLegacyResponseHandler, TableContext } from './table_vis_legacy_response_handler';
import { TableVisConfig } from '../types';
import { VIS_TYPE_TABLE } from '../../common';

export type Input = Datatable;

Expand All @@ -19,7 +20,7 @@ interface Arguments {

export interface TableVisRenderValue {
visData: TableContext;
visType: 'table';
visType: typeof VIS_TYPE_TABLE;
visConfig: TableVisConfig;
}

Expand Down Expand Up @@ -53,7 +54,7 @@ export const createTableVisLegacyFn = (): TableExpressionFunctionDefinition => (
as: 'table_vis',
value: {
visData: convertedData,
visType: 'table',
visType: VIS_TYPE_TABLE,
visConfig,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import { BaseVisTypeOptions } from '../../../visualizations/public';

import { TableOptions } from '../components/table_vis_options_lazy';
import { VIS_EVENT_TO_TRIGGER } from '../../../visualizations/public';
import { TableVisParams, VIS_TYPE_TABLE } from '../../common';
import { toExpressionAst } from '../to_ast';
import { TableVisParams } from '../types';

export const tableVisLegacyTypeDefinition: BaseVisTypeOptions<TableVisParams> = {
name: 'table',
name: VIS_TYPE_TABLE,
title: i18n.translate('visTypeTable.tableVisTitle', {
defaultMessage: 'Data table',
}),
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/vis_type_table/public/table_vis_fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { i18n } from '@kbn/i18n';
import { tableVisResponseHandler, TableContext } from './table_vis_response_handler';
import { ExpressionFunctionDefinition, Datatable, Render } from '../../expressions/public';
import { TableVisConfig } from './types';
import { VIS_TYPE_TABLE } from '../common';

export type Input = Datatable;

Expand All @@ -19,7 +20,7 @@ interface Arguments {

export interface TableVisRenderValue {
visData: TableContext;
visType: 'table';
visType: typeof VIS_TYPE_TABLE;
visConfig: TableVisConfig;
}

Expand Down Expand Up @@ -56,7 +57,7 @@ export const createTableVisFn = (): TableExpressionFunctionDefinition => ({
as: 'table_vis',
value: {
visData: convertedData,
visType: 'table',
visType: VIS_TYPE_TABLE,
visConfig,
},
};
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/vis_type_table/public/table_vis_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import { i18n } from '@kbn/i18n';
import { AggGroupNames } from '../../data/public';
import { BaseVisTypeOptions } from '../../visualizations/public';

import { TableVisParams, VIS_TYPE_TABLE } from '../common';
import { TableOptions } from './components/table_vis_options_lazy';
import { VIS_EVENT_TO_TRIGGER } from '../../../plugins/visualizations/public';
import { toExpressionAst } from './to_ast';
import { TableVisParams } from './types';

export const tableVisTypeDefinition: BaseVisTypeOptions<TableVisParams> = {
name: 'table',
name: VIS_TYPE_TABLE,
title: i18n.translate('visTypeTable.tableVisTitle', {
defaultMessage: 'Data table',
}),
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/vis_type_table/public/to_ast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { Vis } from 'src/plugins/visualizations/public';
import { toExpressionAst } from './to_ast';
import { AggTypes, TableVisParams } from './types';
import { AggTypes, TableVisParams } from '../common';

const mockSchemas = {
metric: [{ accessor: 1, format: { id: 'number' }, params: {}, label: 'Count', aggType: 'count' }],
Expand Down
Loading

0 comments on commit 92cbb3b

Please sign in to comment.