Skip to content

Commit

Permalink
[7.14] [Reporting/Discover/Search Sessions] Only use relative time fi…
Browse files Browse the repository at this point in the history
…lter when generating share data (elastic#112588) (elastic#112740)

* [Reporting/Discover/Search Sessions] Only use relative time filter when generating share data (elastic#112588)

* only use relative time filter when generating share data

* Added comment on absolute time filter.

Co-authored-by: Tim Sullivan <[email protected]>

* improve discover search session relative time range test

* update discover tests and types for passing in data plugin to getSharingData function

* updated reporting to pass in data plugin to getSharingData, also updates jest tests

Co-authored-by: Kibana Machine <[email protected]>
Co-authored-by: Tim Sullivan <[email protected]>
Co-authored-by: Anton Dosov <[email protected]>

* fix bad merge

* fixes

* fix

Co-authored-by: Jean-Louis Leysens <[email protected]>
Co-authored-by: Kibana Machine <[email protected]>
Co-authored-by: Anton Dosov <[email protected]>
  • Loading branch information
4 people authored Sep 22, 2021
1 parent 951b700 commit fbb661e
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const getTopNavLinks = ({
const sharingData = await getSharingData(
searchSource,
state.appStateContainer.getState(),
services.uiSettings
services
);

services.share.toggleShareContextMenu({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,37 @@
*/

import { Capabilities, IUiSettingsClient } from 'kibana/public';
import { IndexPattern } from 'src/plugins/data/public';
import type { IndexPattern } from 'src/plugins/data/public';
import type { DiscoverServices } from '../../../../build_services';
import { dataPluginMock } from '../../../../../../data/public/mocks';
import { createSearchSourceMock } from '../../../../../../data/common/search/search_source/mocks';
import { DOC_HIDE_TIME_COLUMN_SETTING, SORT_DEFAULT_ORDER_SETTING } from '../../../../../common';
import { indexPatternMock } from '../../../../__mocks__/index_pattern';
import { getSharingData, showPublicUrlSwitch } from './get_sharing_data';

describe('getSharingData', () => {
let mockConfig: IUiSettingsClient;
let services: DiscoverServices;

beforeEach(() => {
mockConfig = ({
get: (key: string) => {
if (key === SORT_DEFAULT_ORDER_SETTING) {
return 'desc';
}
if (key === DOC_HIDE_TIME_COLUMN_SETTING) {
services = {
data: dataPluginMock.createStartContract(),
uiSettings: {
get: (key: string) => {
if (key === SORT_DEFAULT_ORDER_SETTING) {
return 'desc';
}
if (key === DOC_HIDE_TIME_COLUMN_SETTING) {
return false;
}
return false;
}
return false;
},
},
} as unknown) as IUiSettingsClient;
} as DiscoverServices;
});

test('returns valid data for sharing', async () => {
const searchSourceMock = createSearchSourceMock({ index: indexPatternMock });
const result = await getSharingData(searchSourceMock, { columns: [] }, mockConfig);
const result = await getSharingData(searchSourceMock, { columns: [] }, services);
expect(result).toMatchInlineSnapshot(`
Object {
"columns": Array [],
Expand All @@ -53,7 +58,7 @@ describe('getSharingData', () => {
const result = await getSharingData(
searchSourceMock,
{ columns: ['column_a', 'column_b'] },
mockConfig
services
);
expect(result).toMatchInlineSnapshot(`
Object {
Expand Down Expand Up @@ -90,7 +95,7 @@ describe('getSharingData', () => {
'cool-field-6',
],
},
mockConfig
services
);
expect(result).toMatchInlineSnapshot(`
Object {
Expand All @@ -116,7 +121,7 @@ describe('getSharingData', () => {
});

test('fields conditionally do not have prepended timeField', async () => {
mockConfig = ({
services.uiSettings = ({
get: (key: string) => {
if (key === DOC_HIDE_TIME_COLUMN_SETTING) {
return true;
Expand All @@ -141,7 +146,7 @@ describe('getSharingData', () => {
'cool-field-6',
],
},
mockConfig
services
);
expect(result).toMatchInlineSnapshot(`
Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
* Side Public License, v 1.
*/

import type { Capabilities, IUiSettingsClient } from 'kibana/public';
import { ISearchSource } from '../../../../../../data/common';
import type { Capabilities } from 'kibana/public';
import type { IUiSettingsClient } from 'src/core/public';
import type { DataPublicPluginStart } from 'src/plugins/data/public';
import type { ISearchSource } from 'src/plugins/data/common';
import { DOC_HIDE_TIME_COLUMN_SETTING, SORT_DEFAULT_ORDER_SETTING } from '../../../../../common';
import type { SavedSearch, SortOrder } from '../../../../saved_searches/types';
import { AppState } from '../services/discover_state';
Expand All @@ -19,15 +21,18 @@ import { getSortForSearchSource } from '../../../angular/doc_table';
export async function getSharingData(
currentSearchSource: ISearchSource,
state: AppState | SavedSearch,
config: IUiSettingsClient
services: { uiSettings: IUiSettingsClient; data: DataPublicPluginStart }
) {
const { uiSettings: config, data } = services;
const searchSource = currentSearchSource.createCopy();
const index = searchSource.getField('index')!;

searchSource.setField(
'sort',
getSortForSearchSource(state.sort as SortOrder[], index, config.get(SORT_DEFAULT_ORDER_SETTING))
);
// When sharing externally we preserve relative time values
searchSource.setField('filter', data.query.timefilter.timefilter.createRelativeFilter(index));
searchSource.removeField('highlight');
searchSource.removeField('highlightAll');
searchSource.removeField('aggs');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ export function updateSearchSource(

// this is not the default index pattern, it determines that it's not of type rollup
if (indexPatternsUtils.isDefault(indexPattern)) {
searchSource.setField(
'filter',
data.query.timefilter.timefilter.createRelativeFilter(indexPattern)
);
// Set the date range filter fields from timeFilter using the absolute format. Search sessions requires that it be converted from a relative range
searchSource.setField('filter', data.query.timefilter.timefilter.createFilter(indexPattern));
}

if (useNewFieldsApi) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import * as Rx from 'rxjs';
import { first } from 'rxjs/operators';
import { CoreStart } from 'src/core/public';
import { dataPluginMock } from '../../../../../src/plugins/data/public/mocks';
import { LicensingPluginSetup } from '../../../licensing/public';
import type { ReportingPublicPluginStartDendencies } from '../plugin';
import { ReportingCsvPanelAction } from './get_csv_panel_action';

type LicenseResults = 'valid' | 'invalid' | 'unavailable' | 'expired';
Expand All @@ -18,8 +20,8 @@ describe('GetCsvReportPanelAction', () => {
let context: any;
let mockLicense$: any;
let mockSearchSource: any;
let mockStartServicesPayload: [CoreStart, object, unknown];
let mockStartServices$: Rx.Subject<typeof mockStartServicesPayload>;
let mockStartServicesPayload: [CoreStart, ReportingPublicPluginStartDendencies, unknown];
let mockStartServices$: Rx.Observable<typeof mockStartServicesPayload>;

beforeAll(() => {
if (typeof window.URL.revokeObjectURL === 'undefined') {
Expand All @@ -38,15 +40,6 @@ describe('GetCsvReportPanelAction', () => {
}) as unknown) as LicensingPluginSetup['license$'];
};

mockStartServices$ = new Rx.Subject<[CoreStart, object, unknown]>();
mockStartServicesPayload = [
({
application: { capabilities: { dashboard: { downloadCsv: true } } },
} as unknown) as CoreStart,
{},
null,
];

core = {
http: {
post: jest.fn().mockImplementation(() => Promise.resolve(true)),
Expand All @@ -62,6 +55,18 @@ describe('GetCsvReportPanelAction', () => {
},
} as any;

mockStartServicesPayload = [
({
...core,
application: { capabilities: { dashboard: { downloadCsv: true } } },
} as unknown) as CoreStart,
{
data: dataPluginMock.createStartContract(),
} as ReportingPublicPluginStartDendencies,
null,
];
mockStartServices$ = Rx.from(Promise.resolve(mockStartServicesPayload));

mockSearchSource = {
createCopy: () => mockSearchSource,
removeField: jest.fn(),
Expand Down Expand Up @@ -97,7 +102,7 @@ describe('GetCsvReportPanelAction', () => {
usesUiCapabilities: true,
});

mockStartServices$.next(mockStartServicesPayload);
await mockStartServices$.pipe(first()).toPromise();

await panel.execute(context);

Expand Down Expand Up @@ -131,7 +136,7 @@ describe('GetCsvReportPanelAction', () => {
usesUiCapabilities: true,
});

mockStartServices$.next(mockStartServicesPayload);
await mockStartServices$.pipe(first()).toPromise();

await panel.execute(context);

Expand All @@ -152,7 +157,7 @@ describe('GetCsvReportPanelAction', () => {
usesUiCapabilities: true,
});

mockStartServices$.next(mockStartServicesPayload);
await mockStartServices$.pipe(first()).toPromise();

await panel.execute(context);

Expand All @@ -167,7 +172,7 @@ describe('GetCsvReportPanelAction', () => {
usesUiCapabilities: true,
});

mockStartServices$.next(mockStartServicesPayload);
await mockStartServices$.pipe(first()).toPromise();

await panel.execute(context);

Expand All @@ -189,7 +194,7 @@ describe('GetCsvReportPanelAction', () => {
usesUiCapabilities: true,
});

mockStartServices$.next(mockStartServicesPayload);
await mockStartServices$.pipe(first()).toPromise();

await panel.execute(context);

Expand All @@ -205,55 +210,58 @@ describe('GetCsvReportPanelAction', () => {
usesUiCapabilities: true,
});

mockStartServices$.next(mockStartServicesPayload);
await mockStartServices$.pipe(first()).toPromise();

await licenseMock$.pipe(first()).toPromise();

expect(await plugin.isCompatible(context)).toEqual(false);
});

it('sets a display and icon type', () => {
it('sets a display and icon type', async () => {
const panel = new ReportingCsvPanelAction({
core,
license$: mockLicense$(),
startServices$: mockStartServices$,
usesUiCapabilities: true,
});

mockStartServices$.next(mockStartServicesPayload);
await mockStartServices$.pipe(first()).toPromise();

expect(panel.getIconType()).toMatchInlineSnapshot(`"document"`);
expect(panel.getDisplayName()).toMatchInlineSnapshot(`"Download CSV"`);
});

describe('Application UI Capabilities', () => {
it(`doesn't allow downloads when UI capability is not enabled`, async () => {
mockStartServicesPayload = [
({ application: { capabilities: {} } } as unknown) as CoreStart,
{
data: dataPluginMock.createStartContract(),
} as ReportingPublicPluginStartDendencies,
null,
];
const startServices$ = Rx.from(Promise.resolve(mockStartServicesPayload));
const plugin = new ReportingCsvPanelAction({
core,
license$: mockLicense$(),
startServices$: mockStartServices$,
startServices$,
usesUiCapabilities: true,
});

mockStartServices$.next([
({ application: { capabilities: {} } } as unknown) as CoreStart,
{},
null,
]);
await startServices$.pipe(first()).toPromise();

expect(await plugin.isCompatible(context)).toEqual(false);
});

it(`allows downloads when license is valid and UI capability is enabled`, async () => {
mockStartServices$ = new Rx.Subject();
const plugin = new ReportingCsvPanelAction({
core,
license$: mockLicense$(),
startServices$: mockStartServices$,
usesUiCapabilities: true,
});

mockStartServices$.next(mockStartServicesPayload);
await mockStartServices$.pipe(first()).toPromise();

expect(await plugin.isCompatible(context)).toEqual(true);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { i18n } from '@kbn/i18n';
import moment from 'moment-timezone';
import * as Rx from 'rxjs';
import { first } from 'rxjs/operators';
import type { CoreSetup } from 'src/core/public';
import { CoreStart } from 'src/core/public';
import type { ISearchEmbeddable, SavedSearch } from '../../../../../src/plugins/discover/public';
Expand All @@ -23,6 +24,7 @@ import type { LicensingPluginSetup } from '../../../licensing/public';
import { API_GENERATE_IMMEDIATE, CSV_REPORTING_ACTION } from '../../common/constants';
import type { JobParamsDownloadCSV } from '../../server/export_types/csv_searchsource_immediate/types';
import { checkLicense } from '../lib/license_check';
import type { ReportingPublicPluginStartDendencies } from '../plugin';

function isSavedSearchEmbeddable(
embeddable: IEmbeddable | ISearchEmbeddable
Expand All @@ -36,7 +38,7 @@ interface ActionContext {

interface Params {
core: CoreSetup;
startServices$: Rx.Observable<[CoreStart, object, unknown]>;
startServices$: Rx.Observable<[CoreStart, ReportingPublicPluginStartDendencies, unknown]>;
license$: LicensingPluginSetup['license$'];
usesUiCapabilities: boolean;
}
Expand All @@ -48,19 +50,22 @@ export class ReportingCsvPanelAction implements ActionDefinition<ActionContext>
private licenseHasDownloadCsv: boolean = false;
private capabilityHasDownloadCsv: boolean = false;
private core: CoreSetup;
private startServices$: Params['startServices$'];

constructor({ core, startServices$, license$, usesUiCapabilities }: Params) {
this.isDownloading = false;
this.core = core;

this.startServices$ = startServices$;

license$.subscribe((license) => {
const results = license.check('reporting', 'basic');
const { showLinks } = checkLicense(results);
this.licenseHasDownloadCsv = showLinks;
});

if (usesUiCapabilities) {
startServices$.subscribe(([{ application }]) => {
this.startServices$.subscribe(([{ application }]) => {
this.capabilityHasDownloadCsv = application.capabilities.dashboard?.downloadCsv === true;
});
} else {
Expand All @@ -79,11 +84,12 @@ export class ReportingCsvPanelAction implements ActionDefinition<ActionContext>
}

public async getSearchSource(savedSearch: SavedSearch, embeddable: ISearchEmbeddable) {
const [{ uiSettings }, { data }] = await this.startServices$.pipe(first()).toPromise();
const { getSharingData } = await loadSharingDataHelpers();
return await getSharingData(
savedSearch.searchSource,
savedSearch, // TODO: get unsaved state (using embeddale.searchScope): https://github.com/elastic/kibana/issues/43977
this.core.uiSettings
savedSearch, // TODO: get unsaved state (using embeddable.searchScope): https://github.com/elastic/kibana/issues/43977
{ uiSettings, data }
);
}

Expand Down
Loading

0 comments on commit fbb661e

Please sign in to comment.