Skip to content

Commit

Permalink
Merge branch 'master' of github.com:elastic/kibana into feat/init-osq…
Browse files Browse the repository at this point in the history
…uery
  • Loading branch information
patrykkopycinski committed Jan 26, 2021
2 parents 3ded958 + 7bb8d3a commit c19334a
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ export const WorkplaceSearchConfigured: React.FC<InitialAppData> = (props) => {
<Route exact path="/">
{errorConnecting ? <ErrorState /> : <Overview />}
</Route>
<Route path={PERSONAL_SOURCES_PATH}>
{/* TODO: replace Layout with PrivateSourcesLayout (needs to be created) */}
<Layout navigation={<></>} restrictWidth readOnlyMode={readOnlyMode}>
<SourcesRouter />
</Layout>
</Route>
<Route path={SOURCES_PATH}>
<Layout
navigation={<WorkplaceSearchNav sourcesSubNav={showSourcesSubnav && <SourceSubNav />} />}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { EuiCallOut, EuiEmptyPrompt, EuiSpacer, EuiPanel } from '@elastic/eui';

import { LicensingLogic } from '../../../../applications/shared/licensing';

import { ADD_SOURCE_PATH } from '../../routes';
import { ADD_SOURCE_PATH, getSourcesPath } from '../../routes';

import noSharedSourcesIcon from '../../assets/share_circle.svg';

Expand Down Expand Up @@ -74,12 +74,17 @@ export const PrivateSources: React.FC = () => {
sidebarLinks.push({
title: PRIVATE_LINK_TITLE,
iconType: 'plusInCircle',
path: ADD_SOURCE_PATH,
path: getSourcesPath(ADD_SOURCE_PATH, false),
});
}

const headerAction = (
<EuiButtonTo to={ADD_SOURCE_PATH} fill color="primary" data-test-subj="AddSourceButton">
<EuiButtonTo
to={getSourcesPath(ADD_SOURCE_PATH, false)}
fill
color="primary"
data-test-subj="AddSourceButton"
>
{PRIVATE_LINK_TITLE}
</EuiButtonTo>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,12 @@ describe('editor_frame', () => {
/>
);
});
expect(mockDatasource.initialize).toHaveBeenCalledWith(datasource1State, [], undefined);
expect(mockDatasource2.initialize).toHaveBeenCalledWith(datasource2State, [], undefined);
expect(mockDatasource.initialize).toHaveBeenCalledWith(datasource1State, [], undefined, {
isFullEditor: true,
});
expect(mockDatasource2.initialize).toHaveBeenCalledWith(datasource2State, [], undefined, {
isFullEditor: true,
});
expect(mockDatasource3.initialize).not.toHaveBeenCalled();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ export function EditorFrame(props: EditorFrameProps) {
props.datasourceMap,
state.datasourceStates,
props.doc?.references,
visualizeTriggerFieldContext
visualizeTriggerFieldContext,
{ isFullEditor: true }
)
.then((result) => {
if (!isUnmounted) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Datasource,
DatasourcePublicAPI,
FramePublicAPI,
InitializationOptions,
Visualization,
VisualizationDimensionGroupConfig,
} from '../../types';
Expand All @@ -21,14 +22,20 @@ export async function initializeDatasources(
datasourceMap: Record<string, Datasource>,
datasourceStates: Record<string, { state: unknown; isLoading: boolean }>,
references?: SavedObjectReference[],
initialContext?: VisualizeFieldContext
initialContext?: VisualizeFieldContext,
options?: InitializationOptions
) {
const states: Record<string, { isLoading: boolean; state: unknown }> = {};
await Promise.all(
Object.entries(datasourceMap).map(([datasourceId, datasource]) => {
if (datasourceStates[datasourceId]) {
return datasource
.initialize(datasourceStates[datasourceId].state || undefined, references, initialContext)
.initialize(
datasourceStates[datasourceId].state || undefined,
references,
initialContext,
options
)
.then((datasourceState) => {
states[datasourceId] = { isLoading: false, state: datasourceState };
});
Expand Down Expand Up @@ -82,7 +89,9 @@ export async function persistedStateToExpression(
{ isLoading: false, state },
])
),
references
references,
undefined,
{ isFullEditor: false }
);

const datasourceLayers = createDatasourceLayers(datasources, datasourceStates);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
Operation,
DatasourceLayerPanelProps,
PublicAPIProps,
InitializationOptions,
} from '../types';
import {
loadInitialState,
Expand Down Expand Up @@ -104,7 +105,8 @@ export function getIndexPatternDatasource({
async initialize(
persistedState?: IndexPatternPersistedState,
references?: SavedObjectReference[],
initialContext?: VisualizeFieldContext
initialContext?: VisualizeFieldContext,
options?: InitializationOptions
) {
return loadInitialState({
persistedState,
Expand All @@ -114,6 +116,7 @@ export function getIndexPatternDatasource({
storage,
indexPatternsService,
initialContext,
options,
});
},

Expand Down
28 changes: 28 additions & 0 deletions x-pack/plugins/lens/public/indexpattern_datasource/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ describe('loader', () => {
savedObjectsClient: mockClient(),
indexPatternsService: mockIndexPatternsService(),
storage,
options: { isFullEditor: true },
});

expect(state).toMatchObject({
Expand All @@ -364,12 +365,35 @@ describe('loader', () => {
});
});

it('should load a default state without loading the indexPatterns when embedded', async () => {
const storage = createMockStorage();
const savedObjectsClient = mockClient();
const state = await loadInitialState({
savedObjectsClient,
indexPatternsService: mockIndexPatternsService(),
storage,
options: { isFullEditor: false },
});

expect(state).toMatchObject({
currentIndexPatternId: undefined,
indexPatternRefs: [],
indexPatterns: {},
layers: {},
});

expect(storage.set).not.toHaveBeenCalled();

expect(savedObjectsClient.find).not.toHaveBeenCalled();
});

it('should load a default state when lastUsedIndexPatternId is not found in indexPatternRefs', async () => {
const storage = createMockStorage({ indexPatternId: 'c' });
const state = await loadInitialState({
savedObjectsClient: mockClient(),
indexPatternsService: mockIndexPatternsService(),
storage,
options: { isFullEditor: true },
});

expect(state).toMatchObject({
Expand All @@ -393,6 +417,7 @@ describe('loader', () => {
savedObjectsClient: mockClient(),
indexPatternsService: mockIndexPatternsService(),
storage: createMockStorage({ indexPatternId: '2' }),
options: { isFullEditor: true },
});

expect(state).toMatchObject({
Expand All @@ -415,6 +440,7 @@ describe('loader', () => {
savedObjectsClient: mockClient(),
indexPatternsService: mockIndexPatternsService(),
storage,
options: { isFullEditor: true },
});

expect(state).toMatchObject({
Expand Down Expand Up @@ -443,6 +469,7 @@ describe('loader', () => {
indexPatternId: '1',
fieldName: '',
},
options: { isFullEditor: true },
});

expect(state).toMatchObject({
Expand Down Expand Up @@ -499,6 +526,7 @@ describe('loader', () => {
savedObjectsClient: mockClient(),
indexPatternsService: mockIndexPatternsService(),
storage,
options: { isFullEditor: true },
});

expect(state).toMatchObject({
Expand Down
17 changes: 12 additions & 5 deletions x-pack/plugins/lens/public/indexpattern_datasource/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import _ from 'lodash';
import { IStorageWrapper } from 'src/plugins/kibana_utils/public';
import { SavedObjectsClientContract, HttpSetup, SavedObjectReference } from 'kibana/public';
import { StateSetter } from '../types';
import { InitializationOptions, StateSetter } from '../types';
import {
IndexPattern,
IndexPatternRef,
Expand Down Expand Up @@ -190,6 +190,7 @@ export async function loadInitialState({
storage,
indexPatternsService,
initialContext,
options,
}: {
persistedState?: IndexPatternPersistedState;
references?: SavedObjectReference[];
Expand All @@ -198,8 +199,10 @@ export async function loadInitialState({
storage: IStorageWrapper;
indexPatternsService: IndexPatternsService;
initialContext?: VisualizeFieldContext;
options?: InitializationOptions;
}): Promise<IndexPatternPrivateState> {
const indexPatternRefs = await loadIndexPatternRefs(savedObjectsClient);
const { isFullEditor } = options ?? {};
const indexPatternRefs = await (isFullEditor ? loadIndexPatternRefs(savedObjectsClient) : []);
const lastUsedIndexPatternId = getLastUsedIndexPatternId(storage, indexPatternRefs);

const state =
Expand All @@ -210,11 +213,15 @@ export async function loadInitialState({
? Object.values(state.layers)
.map((l) => l.indexPatternId)
.concat(state.currentIndexPatternId)
: [lastUsedIndexPatternId || defaultIndexPatternId || indexPatternRefs[0].id]
);
: [lastUsedIndexPatternId || defaultIndexPatternId || indexPatternRefs[0]?.id]
)
// take out the undefined from the list
.filter(Boolean);

const currentIndexPatternId = initialContext?.indexPatternId ?? requiredPatterns[0];
setLastUsedIndexPatternId(storage, currentIndexPatternId);
if (currentIndexPatternId) {
setLastUsedIndexPatternId(storage, currentIndexPatternId);
}

const indexPatterns = await loadIndexPatterns({
indexPatternsService,
Expand Down
7 changes: 6 additions & 1 deletion x-pack/plugins/lens/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ export interface DatasourceSuggestion<T = unknown> {

export type StateSetter<T> = (newState: T | ((prevState: T) => T)) => void;

export interface InitializationOptions {
isFullEditor?: boolean;
}

/**
* Interface for the datasource registry
*/
Expand All @@ -151,7 +155,8 @@ export interface Datasource<T = unknown, P = unknown> {
initialize: (
state?: P,
savedObjectReferences?: SavedObjectReference[],
initialContext?: VisualizeFieldContext
initialContext?: VisualizeFieldContext,
options?: InitializationOptions
) => Promise<T>;

// Given the current state, which parts should be saved?
Expand Down

0 comments on commit c19334a

Please sign in to comment.