Skip to content

Commit

Permalink
[Discover] Prevent agg based visualizations of Discover saved objects…
Browse files Browse the repository at this point in the history
… with adhoc data views (#145583)

## Summary

Fixes #141812

This PR preventing using Discover saved objects with adhoc data views in
aggregation based visualisations.

### Test notes 
- Create Saved search based on adhoc data view in Discover
- Open new Agg based visualisations list and choose one
- Created Saved search shouldn't appear in the list.   

### Checklist


- [ ] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
  • Loading branch information
dimaanj authored Nov 25, 2022
1 parent 2728ee3 commit 2c9043a
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('checking migration metadata changes on all registered SO types', () =>
"osquery-saved-query": "7b213b4b7a3e59350e99c50e8df9948662ed493a",
"query": "4640ef356321500a678869f24117b7091a911cb6",
"sample-data-telemetry": "8b10336d9efae6f3d5593c4cc89fb4abcdf84e04",
"search": "e7ba25ea37cb36b622db42c9590c6d8dfc838801",
"search": "d26771bcf7cd271162aab3a610b75249631ef6b1",
"search-session": "ba383309da68a15be3765977f7a44c84f0ec7964",
"search-telemetry": "beb3fc25488c753f2a6dcff1845d667558712b66",
"security-rule": "e0dfdba5d66139d0300723b2e6672993cd4a11f3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export async function persistSavedSearch(
savedSearch.isTextBasedQuery = isTextBasedQuery;
}

savedSearch.usesAdHocDataView = !dataView.isPersisted();

const { from, to } = services.timefilter.getTime();
const refreshInterval = services.timefilter.getRefreshInterval();
savedSearch.timeRange =
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/saved_objects/public/finder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
* Side Public License, v 1.
*/

export type { SavedObjectMetaData, SavedObjectFinderUiProps } from './saved_object_finder';
export type {
SavedObjectMetaData,
SavedObjectFinderUiProps,
FinderAttributes,
} from './saved_object_finder';
export { SavedObjectFinderUi, getSavedObjectFinder } from './saved_object_finder';
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface SavedObjectMetaData<T = unknown> {
defaultSearchField?: string;
}

interface FinderAttributes {
export interface FinderAttributes {
title?: string;
name?: string;
type: string;
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/saved_objects/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { SavedObjectsPublicPlugin } from './plugin';

export type { OnSaveProps, OriginSaveModalProps, SaveModalState, SaveResult } from './save_modal';
export { SavedObjectSaveModal, SavedObjectSaveModalOrigin, showSaveModal } from './save_modal';
export type { SavedObjectFinderUiProps, SavedObjectMetaData } from './finder';
export type { SavedObjectFinderUiProps, SavedObjectMetaData, FinderAttributes } from './finder';
export { getSavedObjectFinder, SavedObjectFinderUi } from './finder';
export type {
SavedObjectDecorator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ describe('getSavedSearch', () => {
"timeRange": undefined,
"timeRestore": undefined,
"title": "test1",
"usesAdHocDataView": undefined,
"viewMode": undefined,
}
`);
Expand Down Expand Up @@ -251,6 +252,7 @@ describe('getSavedSearch', () => {
"timeRange": undefined,
"timeRestore": undefined,
"title": "test2",
"usesAdHocDataView": undefined,
"viewMode": undefined,
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('saved_searches_utils', () => {
grid: {},
hideChart: true,
isTextBasedQuery: false,
usesAdHocDataView: false,
};

expect(
Expand Down Expand Up @@ -81,6 +82,7 @@ describe('saved_searches_utils', () => {
"timeRange": undefined,
"timeRestore": undefined,
"title": "saved search",
"usesAdHocDataView": false,
"viewMode": undefined,
}
`);
Expand Down Expand Up @@ -121,6 +123,7 @@ describe('saved_searches_utils', () => {
grid: {},
hideChart: true,
isTextBasedQuery: true,
usesAdHocDataView: false,
};

expect(toSavedSearchAttributes(savedSearch, '{}')).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -149,6 +152,7 @@ describe('saved_searches_utils', () => {
"timeRange": undefined,
"timeRestore": false,
"title": "title",
"usesAdHocDataView": false,
"viewMode": undefined,
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const fromSavedSearchAttributes = (
hideAggregatedPreview: attributes.hideAggregatedPreview,
rowHeight: attributes.rowHeight,
isTextBasedQuery: attributes.isTextBasedQuery,
usesAdHocDataView: attributes.usesAdHocDataView,
timeRestore: attributes.timeRestore,
timeRange: attributes.timeRange,
refreshInterval: attributes.refreshInterval,
Expand All @@ -66,6 +67,7 @@ export const toSavedSearchAttributes = (
hideAggregatedPreview: savedSearch.hideAggregatedPreview,
rowHeight: savedSearch.rowHeight,
isTextBasedQuery: savedSearch.isTextBasedQuery ?? false,
usesAdHocDataView: savedSearch.usesAdHocDataView,
timeRestore: savedSearch.timeRestore ?? false,
timeRange: savedSearch.timeRange,
refreshInterval: savedSearch.refreshInterval,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface SavedSearchAttributes {
};
hideChart: boolean;
isTextBasedQuery: boolean;
usesAdHocDataView?: boolean;
kibanaSavedObjectMeta: {
searchSourceJSON: string;
};
Expand Down Expand Up @@ -73,6 +74,7 @@ export interface SavedSearch {
hideAggregatedPreview?: boolean;
rowHeight?: number;
isTextBasedQuery?: boolean;
usesAdHocDataView?: boolean;

// for restoring time range with a saved search
timeRestore?: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/plugins/saved_search/server/saved_objects/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export function getSavedSearchObjectType(
viewMode: { type: 'keyword', index: false, doc_values: false },
hideChart: { type: 'boolean', index: false, doc_values: false },
isTextBasedQuery: { type: 'boolean', index: false, doc_values: false },
usesAdHocDataView: { type: 'boolean', index: false, doc_values: false },
hideAggregatedPreview: { type: 'boolean', index: false, doc_values: false },
hits: { type: 'integer', index: false, doc_values: false },
kibanaSavedObjectMeta: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
* Side Public License, v 1.
*/

import React from 'react';
import { EuiModalBody, EuiModalHeader, EuiModalHeaderTitle } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import type { SimpleSavedObject, SavedObjectAttributes } from '@kbn/core/public';
import React from 'react';
import { IUiSettingsClient, SavedObjectsStart } from '@kbn/core/public';

import { SavedObjectFinderUi } from '@kbn/saved-objects-plugin/public';
import type { BaseVisType } from '../../vis_types';
import { DialogNavigation } from '../dialog_navigation';
import { showSavedObject } from './show_saved_object';

interface SearchSelectionProps {
onSearchSelected: (searchId: string, searchType: string) => void;
Expand All @@ -24,9 +24,6 @@ interface SearchSelectionProps {
savedObjects: SavedObjectsStart;
goBack: () => void;
}
interface SavedSearchesAttributes extends SavedObjectAttributes {
isTextBasedQuery: boolean;
}

export class SearchSelection extends React.Component<SearchSelectionProps> {
private fixedPageSize: number = 8;
Expand Down Expand Up @@ -71,11 +68,8 @@ export class SearchSelection extends React.Component<SearchSelectionProps> {
}
),
// ignore the saved searches that have text-based languages queries
includeFields: ['isTextBasedQuery'],
showSavedObject: (savedObject) => {
const so = savedObject as unknown as SimpleSavedObject<SavedSearchesAttributes>;
return !so.attributes.isTextBasedQuery;
},
includeFields: ['isTextBasedQuery', 'usesAdHocDataView'],
showSavedObject,
},
{
type: 'index-pattern',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { SimpleSavedObject } from '@kbn/core/public';
import type { FinderAttributes } from '@kbn/saved-objects-plugin/public';
import { showSavedObject } from './show_saved_object';

describe('showSavedObject', () => {
it('should return true if the saved object is not a text based query', () => {
const savedObject = {
attributes: { isTextBasedQuery: false },
};
expect(showSavedObject(savedObject as unknown as SimpleSavedObject<FinderAttributes>)).toBe(
true
);
});

it('should return false if the saved object is a text based query', () => {
const savedObject = {
attributes: { isTextBasedQuery: true },
};
expect(showSavedObject(savedObject as unknown as SimpleSavedObject<FinderAttributes>)).toBe(
false
);
});

it('should return true if the saved object is not of an adhoc data view', () => {
const savedObject = {
attributes: { usesAdHocDataView: false },
};
expect(showSavedObject(savedObject as unknown as SimpleSavedObject<FinderAttributes>)).toBe(
true
);
});

it('should return false if the saved object is of an adhoc data view', () => {
const savedObject = {
attributes: { usesAdHocDataView: true },
};
expect(showSavedObject(savedObject as unknown as SimpleSavedObject<FinderAttributes>)).toBe(
false
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { SimpleSavedObject, SavedObjectAttributes } from '@kbn/core/public';
import type { FinderAttributes } from '@kbn/saved-objects-plugin/public';

export interface SavedSearchesAttributes extends SavedObjectAttributes {
isTextBasedQuery: boolean;
usesAdHocDataView?: boolean;
}

export const showSavedObject = (savedObject: SimpleSavedObject<FinderAttributes>) => {
const so = savedObject as unknown as SimpleSavedObject<SavedSearchesAttributes>;
return !so.attributes.isTextBasedQuery && !so.attributes.usesAdHocDataView;
};

0 comments on commit 2c9043a

Please sign in to comment.