Skip to content

Commit

Permalink
Merge branch 'main' into skip-failing-test-fleet
Browse files Browse the repository at this point in the history
  • Loading branch information
MiriamAparicio authored Nov 9, 2022
2 parents 113498a + a9162f7 commit eb12e88
Show file tree
Hide file tree
Showing 122 changed files with 2,639 additions and 1,482 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -983,14 +983,19 @@ describe('SearchSource', () => {
},
];

const indexPattern123 = { id: '123', isPersisted: () => true } as DataView;
const indexPattern123 = {
id: '123',
isPersisted: jest.fn(() => true),
toSpec: jest.fn(),
} as unknown as DataView;

test('should return serialized fields', () => {
searchSource.setField('index', indexPattern123);
searchSource.setField('filter', () => {
return filter;
});
const serializedFields = searchSource.getSerializedFields();
expect(indexPattern123.toSpec).toHaveBeenCalledTimes(0);
expect(serializedFields).toMatchSnapshot();
});

Expand All @@ -1000,11 +1005,19 @@ describe('SearchSource', () => {
const childSearchSource = searchSource.createChild();
childSearchSource.setField('timeout', '100');
const serializedFields = childSearchSource.getSerializedFields(true);
expect(indexPattern123.toSpec).toHaveBeenCalledTimes(0);
expect(serializedFields).toMatchObject({
timeout: '100',
parent: { index: '123', from: 123 },
});
});

test('should use spec', () => {
indexPattern123.isPersisted = jest.fn(() => false);
searchSource.setField('index', indexPattern123);
searchSource.getSerializedFields(true, false);
expect(indexPattern123.toSpec).toHaveBeenCalledWith(false);
});
});

describe('fetch$', () => {
Expand Down
6 changes: 4 additions & 2 deletions src/plugins/data/common/search/search_source/search_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,7 @@ export class SearchSource {
/**
* serializes search source fields (which can later be passed to {@link ISearchStartSearchSource})
*/
public getSerializedFields(recurse = false): SerializedSearchSourceFields {
public getSerializedFields(recurse = false, includeFields = true): SerializedSearchSourceFields {
const {
filter: originalFilters,
aggs: searchSourceAggs,
Expand All @@ -938,7 +938,9 @@ export class SearchSource {
...searchSourceFields,
};
if (index) {
serializedSearchSourceFields.index = index.isPersisted() ? index.id : index.toSpec();
serializedSearchSourceFields.index = index.isPersisted()
? index.id
: index.toSpec(includeFields);
}
if (sort) {
serializedSearchSourceFields.sort = !Array.isArray(sort) ? [sort] : sort;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ async function mountComponent(
persistDataView: jest.fn(),
updateAdHocDataViewId: jest.fn(),
adHocDataViewList: [],
savedDataViewList: [],
updateDataViewList: jest.fn(),
};

const component = mountWithIntl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export function DiscoverLayout({
persistDataView,
updateAdHocDataViewId,
adHocDataViewList,
savedDataViewList,
updateDataViewList,
}: DiscoverLayoutProps) {
const {
trackUiMetric,
Expand Down Expand Up @@ -233,6 +235,8 @@ export function DiscoverLayout({
persistDataView={persistDataView}
updateAdHocDataViewId={updateAdHocDataViewId}
adHocDataViewList={adHocDataViewList}
savedDataViewList={savedDataViewList}
updateDataViewList={updateDataViewList}
/>
<EuiPageBody className="dscPageBody" aria-describedby="savedSearchTitle">
<SavedSearchURLConflictCallout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import type { Query, TimeRange, AggregateQuery } from '@kbn/es-query';
import type { DataView } from '@kbn/data-views-plugin/public';
import { DataViewListItem, ISearchSource } from '@kbn/data-plugin/public';
import type { DataViewListItem, ISearchSource } from '@kbn/data-plugin/public';
import { RequestAdapter } from '@kbn/inspector-plugin/common';
import { SavedSearch } from '@kbn/saved-search-plugin/public';
import { DataTableRecord } from '../../../../types';
Expand All @@ -35,6 +35,8 @@ export interface DiscoverLayoutProps {
state: AppState;
stateContainer: GetStateReturn;
persistDataView: (dataView: DataView) => Promise<DataView | undefined>;
updateDataViewList: (dataViews: DataView[]) => Promise<void>;
updateAdHocDataViewId: (dataView: DataView) => Promise<DataView>;
adHocDataViewList: DataView[];
savedDataViewList: DataViewListItem[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ function getProps(savePermissions = true): DiscoverTopNavProps {
persistDataView: jest.fn(),
updateAdHocDataViewId: jest.fn(),
adHocDataViewList: [],
savedDataViewList: [],
updateDataViewList: jest.fn(),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
import { useHistory } from 'react-router-dom';
import type { Query, TimeRange, AggregateQuery } from '@kbn/es-query';
import { DataViewType, type DataView } from '@kbn/data-views-plugin/public';
import { DataViewListItem, DataViewType, type DataView } from '@kbn/data-views-plugin/public';
import type { DataViewPickerProps } from '@kbn/unified-search-plugin/public';
import { ENABLE_SQL } from '../../../../../common';
import { useDiscoverServices } from '../../../../hooks/use_discover_services';
Expand Down Expand Up @@ -38,6 +38,8 @@ export type DiscoverTopNavProps = Pick<
persistDataView: (dataView: DataView) => Promise<DataView | undefined>;
updateAdHocDataViewId: (dataView: DataView) => Promise<DataView>;
adHocDataViewList: DataView[];
savedDataViewList: DataViewListItem[];
updateDataViewList: (DataViewEditorStart: DataView[]) => Promise<void>;
};

export const DiscoverTopNav = ({
Expand All @@ -58,6 +60,8 @@ export const DiscoverTopNav = ({
persistDataView,
updateAdHocDataViewId,
adHocDataViewList,
savedDataViewList,
updateDataViewList,
}: DiscoverTopNavProps) => {
const history = useHistory();

Expand Down Expand Up @@ -161,6 +165,8 @@ export const DiscoverTopNav = ({
searchSource,
onOpenSavedSearch,
isPlainRecord,
adHocDataViews: adHocDataViewList,
updateDataViewList,
persistDataView,
updateAdHocDataViewId,
}),
Expand All @@ -174,8 +180,10 @@ export const DiscoverTopNav = ({
searchSource,
onOpenSavedSearch,
isPlainRecord,
adHocDataViewList,
persistDataView,
updateAdHocDataViewId,
updateDataViewList,
]
);

Expand Down Expand Up @@ -213,6 +221,7 @@ export const DiscoverTopNav = ({
onChangeDataView,
textBasedLanguages: supportedTextBasedLanguages as DataViewPickerProps['textBasedLanguages'],
adHocDataViews: adHocDataViewList,
savedDataViewList,
};

const onTextBasedSavedAndExit = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ test('getTopNavLinks result', () => {
onOpenSavedSearch: () => {},
isPlainRecord: false,
persistDataView: jest.fn(),
updateDataViewList: jest.fn(),
adHocDataViews: [],
updateAdHocDataViewId: jest.fn(),
});
expect(topNavLinks).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -102,6 +104,8 @@ test('getTopNavLinks result for sql mode', () => {
onOpenSavedSearch: () => {},
isPlainRecord: true,
persistDataView: jest.fn(),
updateDataViewList: jest.fn(),
adHocDataViews: [],
updateAdHocDataViewId: jest.fn(),
});
expect(topNavLinks).toMatchInlineSnapshot(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export const getTopNavLinks = ({
onOpenSavedSearch,
isPlainRecord,
persistDataView,
adHocDataViews,
updateDataViewList,
updateAdHocDataViewId,
}: {
dataView: DataView;
Expand All @@ -45,6 +47,8 @@ export const getTopNavLinks = ({
searchSource: ISearchSource;
onOpenSavedSearch: (id: string) => void;
isPlainRecord: boolean;
adHocDataViews: DataView[];
updateDataViewList: (dataView: DataView[]) => Promise<void>;
persistDataView: (dataView: DataView) => Promise<DataView | undefined>;
updateAdHocDataViewId: (dataView: DataView) => Promise<DataView>;
}): TopNavMenuData[] => {
Expand Down Expand Up @@ -75,16 +79,15 @@ export const getTopNavLinks = ({
defaultMessage: 'Alerts',
}),
run: async (anchorElement: HTMLElement) => {
const updatedDataView = await persistDataView(dataView);
if (updatedDataView) {
openAlertsPopover({
I18nContext: services.core.i18n.Context,
anchorElement,
searchSource: savedSearch.searchSource,
services,
savedQueryId: state.appStateContainer.getState().savedQuery,
});
}
openAlertsPopover({
I18nContext: services.core.i18n.Context,
anchorElement,
searchSource: savedSearch.searchSource,
services,
adHocDataViews,
updateDataViewList,
savedQueryId: state.appStateContainer.getState().savedQuery,
});
},
testId: 'discoverAlertsButton',
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import React from 'react';
import React, { ReactNode } from 'react';
import { mountWithIntl } from '@kbn/test-jest-helpers';
import { findTestSubject } from '@elastic/eui/lib/test';
import { createSearchSourceMock } from '@kbn/data-plugin/public/mocks';
Expand All @@ -16,14 +16,20 @@ import { discoverServiceMock } from '../../../../__mocks__/services';
import { dataViewWithTimefieldMock } from '../../../../__mocks__/data_view_with_timefield';
import { dataViewMock } from '../../../../__mocks__/data_view';

const Context = ({ children }: { children: ReactNode }) => <>{children}</>;

const mount = (dataView = dataViewMock) =>
mountWithIntl(
<KibanaContextProvider services={discoverServiceMock}>
<AlertsPopover
searchSource={createSearchSourceMock({ index: dataView })}
anchorElement={document.createElement('div')}
savedQueryId={undefined}
onClose={() => {}}
adHocDataViews={[]}
services={discoverServiceMock}
updateDataViewList={jest.fn()}
onClose={jest.fn()}
I18nContext={Context}
/>
</KibanaContextProvider>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import ReactDOM from 'react-dom';
import { I18nStart } from '@kbn/core/public';
import { EuiWrappingPopover, EuiContextMenu } from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';
import { ISearchSource } from '@kbn/data-plugin/common';
import type { DataView, ISearchSource } from '@kbn/data-plugin/common';
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public';
import { DiscoverServices } from '../../../../build_services';
import { updateSearchSource } from '../../utils/update_search_source';
import { useDiscoverServices } from '../../../../hooks/use_discover_services';

const container = document.createElement('div');
let isOpen = false;
Expand All @@ -27,16 +26,27 @@ interface AlertsPopoverProps {
anchorElement: HTMLElement;
searchSource: ISearchSource;
savedQueryId?: string;
adHocDataViews: DataView[];
I18nContext: I18nStart['Context'];
services: DiscoverServices;
updateDataViewList: (dataViews: DataView[]) => Promise<void>;
}

interface EsQueryAlertMetaData {
isManagementPage?: boolean;
adHocDataViewList: DataView[];
}

export function AlertsPopover({
searchSource,
anchorElement,
savedQueryId,
adHocDataViews,
services,
onClose: originalOnClose,
updateDataViewList,
}: AlertsPopoverProps) {
const dataView = searchSource.getField('index')!;
const services = useDiscoverServices();
const { triggersActionsUi } = services;
const [alertFlyoutVisible, setAlertFlyoutVisibility] = useState(false);
const onClose = useCallback(() => {
Expand All @@ -63,20 +73,45 @@ export function AlertsPopover({
};
}, [savedQueryId, searchSource, services]);

const discoverMetadata: EsQueryAlertMetaData = useMemo(
() => ({
isManagementPage: false,
adHocDataViewList: adHocDataViews,
}),
[adHocDataViews]
);

const SearchThresholdAlertFlyout = useMemo(() => {
if (!alertFlyoutVisible) {
return;
}

const onFinishFlyoutInteraction = (metadata: EsQueryAlertMetaData) => {
updateDataViewList(metadata.adHocDataViewList);
};

return triggersActionsUi?.getAddAlertFlyout({
metadata: discoverMetadata,
consumer: 'discover',
onClose,
onClose: (_, metadata) => {
onFinishFlyoutInteraction(metadata as EsQueryAlertMetaData);
onClose();
},
onSave: async (metadata) => {
onFinishFlyoutInteraction(metadata as EsQueryAlertMetaData);
},
canChangeTrigger: false,
ruleTypeId: ALERT_TYPE_ID,
initialValues: {
params: getParams(),
},
initialValues: { params: getParams() },
});
}, [getParams, onClose, triggersActionsUi, alertFlyoutVisible]);
}, [
alertFlyoutVisible,
triggersActionsUi,
discoverMetadata,
getParams,
updateDataViewList,
onClose,
]);

const hasTimeFieldName = dataView.timeFieldName;
const panels = [
Expand Down Expand Up @@ -145,13 +180,17 @@ export function openAlertsPopover({
anchorElement,
searchSource,
services,
adHocDataViews,
savedQueryId,
updateDataViewList,
}: {
I18nContext: I18nStart['Context'];
anchorElement: HTMLElement;
searchSource: ISearchSource;
services: DiscoverServices;
adHocDataViews: DataView[];
savedQueryId?: string;
updateDataViewList: (dataViews: DataView[]) => Promise<void>;
}) {
if (isOpen) {
closeAlertsPopover();
Expand All @@ -169,6 +208,10 @@ export function openAlertsPopover({
anchorElement={anchorElement}
searchSource={searchSource}
savedQueryId={savedQueryId}
adHocDataViews={adHocDataViews}
I18nContext={I18nContext}
services={services}
updateDataViewList={updateDataViewList}
/>
</KibanaContextProvider>
</I18nContext>
Expand Down
Loading

0 comments on commit eb12e88

Please sign in to comment.