Skip to content

Commit

Permalink
[Discover] Fix csv report for filtered discover result on dashboard (#…
Browse files Browse the repository at this point in the history
…119583) (#120419)

* [Discover] fix csv report for filtered discover result on dashboard

* [Discover] fix tests

* [Discover] add functional test

* [Discover] fix the case when timeFilter was not applied, but others filters were applied for non time based data views

Co-authored-by: Kibana Machine <[email protected]>
# Conflicts:
#	src/plugins/discover/public/utils/get_sharing_data.ts

Co-authored-by: Dmitry Tomashevich <[email protected]>
  • Loading branch information
tsullivan and dimaanj authored Dec 3, 2021
1 parent fdbdbef commit ee8aa25
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import type { Capabilities } from 'kibana/public';
import type { IUiSettingsClient } from 'src/core/public';
import type { DataPublicPluginStart } from 'src/plugins/data/public';
import type { ISearchSource, SearchSourceFields } from 'src/plugins/data/common';
import type { Filter, ISearchSource, SearchSourceFields } from 'src/plugins/data/common';
import { DOC_HIDE_TIME_COLUMN_SETTING, SORT_DEFAULT_ORDER_SETTING } from '../../../../../common';
import type { SavedSearch, SortOrder } from '../../../../saved_searches';
import { getSortForSearchSource } from '../components/doc_table';
Expand All @@ -26,6 +26,7 @@ export async function getSharingData(
const { uiSettings: config, data } = services;
const searchSource = currentSearchSource.createCopy();
const index = searchSource.getField('index')!;
const existingFilter = searchSource.getField('filter');

searchSource.setField(
'sort',
Expand Down Expand Up @@ -55,11 +56,21 @@ export async function getSharingData(

return {
getSearchSource: (absoluteTime?: boolean): SearchSourceFields => {
const filter = absoluteTime
const timeFilter = absoluteTime
? data.query.timefilter.timefilter.createFilter(index)
: data.query.timefilter.timefilter.createRelativeFilter(index);

searchSource.setField('filter', filter);
if (existingFilter && timeFilter) {
searchSource.setField(
'filter',
Array.isArray(existingFilter)
? [timeFilter, ...existingFilter]
: ([timeFilter, existingFilter] as Filter[])
);
} else {
const filter = timeFilter || existingFilter;
searchSource.setField('filter', filter);
}

return searchSource.getSerializedFields(true);
},
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 41 additions & 1 deletion x-pack/test/functional/apps/dashboard/reporting/download_csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
const filterBar = getService('filterBar');
const find = getService('find');
const retry = getService('retry');
const PageObjects = getPageObjects(['reporting', 'common', 'dashboard', 'timePicker']);
const PageObjects = getPageObjects([
'reporting',
'common',
'dashboard',
'timePicker',
'discover',
]);
const dashboardAddPanel = getService('dashboardAddPanel');

const ecommerceSOPath = 'x-pack/test/functional/fixtures/kbn_archiver/reporting/ecommerce.json';
const ecommerceDataPath = 'x-pack/test/functional/es_archives/reporting/ecommerce';
Expand Down Expand Up @@ -120,6 +127,39 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
const csvFile = await getDownload(getCsvPath('Ecommerce Data')); // file exists with proper name
expect(csvFile).to.not.be(null);
});

it('Downloads filtered Discover saved search report', async () => {
const setTimeRange = async () => {
const fromTime = 'Jun 20, 2019 @ 23:56:51.374';
const toTime = 'Jun 25, 2019 @ 16:18:51.821';
await PageObjects.timePicker.setAbsoluteRange(fromTime, toTime);
};

PageObjects.common.navigateToApp('discover');
await setTimeRange();
await PageObjects.discover.selectIndexPattern('ecommerce');
await PageObjects.discover.clickNewSearchButton();

await PageObjects.discover.clickFieldListItemAdd('customer_first_name');
await PageObjects.discover.clickFieldListItemAdd('customer_last_name');
await PageObjects.discover.clickFieldListItemAdd('customer_full_name');
await filterBar.addFilter('customer_first_name', 'is', 'Betty');
await PageObjects.discover.saveSearch('search-by-name');

await PageObjects.common.navigateToApp('dashboard');
await PageObjects.dashboard.gotoDashboardLandingPage();
await PageObjects.dashboard.clickNewDashboard();
await setTimeRange();
await dashboardAddPanel.addSavedSearch('search-by-name');
await PageObjects.dashboard.saveDashboard('filtered-result');

await PageObjects.dashboard.clickCancelOutOfEditMode();
await clickActionsMenu('search-by-name');
await clickDownloadCsv();

const csvFile = await getDownload(getCsvPath('search-by-name'));
expectSnapshot(csvFile).toMatch();
});
});

describe('Field Formatters and Scripted Fields', () => {
Expand Down

0 comments on commit ee8aa25

Please sign in to comment.