Skip to content

Commit

Permalink
Reporting: Fix _index and _id columns in CSV export (#96097) (#97009)
Browse files Browse the repository at this point in the history
* Reporting: Fix _index and _id columns in CSV export

* optimize - cache _columns and run getColumns once per execution

* Update x-pack/plugins/reporting/server/export_types/csv_searchsource/generate_csv/generate_csv.ts

Co-authored-by: Michael Dokolin <dokmic@gmail.com>

* feedback

* fix typescripts

* fix plugin list test

* fix plugin list

* take away the export interface to test CI build stats

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Michael Dokolin <dokmic@gmail.com>

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Michael Dokolin <dokmic@gmail.com>
  • Loading branch information
3 people authored Apr 13, 2021
1 parent d7a832f commit 2f85297
Show file tree
Hide file tree
Showing 14 changed files with 342 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
* Side Public License, v 1.
*/

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

describe('getSharingData', () => {
let mockConfig: IUiSettingsClient;
Expand All @@ -36,6 +35,32 @@ describe('getSharingData', () => {
const result = await getSharingData(searchSourceMock, { columns: [] }, mockConfig);
expect(result).toMatchInlineSnapshot(`
Object {
"columns": Array [],
"searchSource": Object {
"index": "the-index-pattern-id",
"sort": Array [
Object {
"_score": "desc",
},
],
},
}
`);
});

test('returns valid data for sharing when columns are selected', async () => {
const searchSourceMock = createSearchSourceMock({ index: indexPatternMock });
const result = await getSharingData(
searchSourceMock,
{ columns: ['column_a', 'column_b'] },
mockConfig
);
expect(result).toMatchInlineSnapshot(`
Object {
"columns": Array [
"column_a",
"column_b",
],
"searchSource": Object {
"index": "the-index-pattern-id",
"sort": Array [
Expand Down Expand Up @@ -69,16 +94,16 @@ describe('getSharingData', () => {
);
expect(result).toMatchInlineSnapshot(`
Object {
"columns": Array [
"cool-timefield",
"cool-field-1",
"cool-field-2",
"cool-field-3",
"cool-field-4",
"cool-field-5",
"cool-field-6",
],
"searchSource": Object {
"fields": Array [
"cool-timefield",
"cool-field-1",
"cool-field-2",
"cool-field-3",
"cool-field-4",
"cool-field-5",
"cool-field-6",
],
"index": "the-index-pattern-id",
"sort": Array [
Object {
Expand Down Expand Up @@ -120,15 +145,15 @@ describe('getSharingData', () => {
);
expect(result).toMatchInlineSnapshot(`
Object {
"columns": Array [
"cool-field-1",
"cool-field-2",
"cool-field-3",
"cool-field-4",
"cool-field-5",
"cool-field-6",
],
"searchSource": Object {
"fields": Array [
"cool-field-1",
"cool-field-2",
"cool-field-3",
"cool-field-4",
"cool-field-5",
"cool-field-6",
],
"index": "the-index-pattern-id",
"sort": Array [
Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
*/

import type { Capabilities, IUiSettingsClient } from 'kibana/public';
import { DOC_HIDE_TIME_COLUMN_SETTING, SORT_DEFAULT_ORDER_SETTING } from '../../../common';
import { getSortForSearchSource } from '../angular/doc_table';
import { ISearchSource } from '../../../../data/common';
import { AppState } from '../angular/discover_state';
import { DOC_HIDE_TIME_COLUMN_SETTING, SORT_DEFAULT_ORDER_SETTING } from '../../../common';
import type { SavedSearch, SortOrder } from '../../saved_searches/types';
import { AppState } from '../angular/discover_state';
import { getSortForSearchSource } from '../angular/doc_table';

/**
* Preparing data to share the current state as link or CSV/Report
Expand All @@ -23,10 +23,6 @@ export async function getSharingData(
) {
const searchSource = currentSearchSource.createCopy();
const index = searchSource.getField('index')!;
const fields = {
fields: searchSource.getField('fields'),
fieldsFromSource: searchSource.getField('fieldsFromSource'),
};

searchSource.setField(
'sort',
Expand All @@ -37,7 +33,7 @@ export async function getSharingData(
searchSource.removeField('aggs');
searchSource.removeField('size');

// fields get re-set to match the saved search columns
// Columns that the user has selected in the saved search
let columns = state.columns || [];

if (columns && columns.length > 0) {
Expand All @@ -50,14 +46,11 @@ export async function getSharingData(
if (timeFieldName && !columns.includes(timeFieldName)) {
columns = [timeFieldName, ...columns];
}

// if columns were selected in the saved search, use them for the searchSource's fields
const fieldsKey = fields.fieldsFromSource ? 'fieldsFromSource' : 'fields';
searchSource.setField(fieldsKey, columns);
}

return {
searchSource: searchSource.getSerializedFields(true),
columns,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('GetCsvReportPanelAction', () => {
let core: any;
let context: any;
let mockLicense$: any;
let mockSearchSource: any;

beforeAll(() => {
if (typeof window.URL.revokeObjectURL === 'undefined') {
Expand Down Expand Up @@ -49,22 +50,19 @@ describe('GetCsvReportPanelAction', () => {
},
} as any;

mockSearchSource = {
createCopy: () => mockSearchSource,
removeField: jest.fn(),
setField: jest.fn(),
getField: jest.fn(),
getSerializedFields: jest.fn().mockImplementation(() => ({})),
};

context = {
embeddable: {
type: 'search',
getSavedSearch: () => {
const searchSource = {
createCopy: () => searchSource,
removeField: jest.fn(),
setField: jest.fn(),
getField: jest.fn().mockImplementation((key: string) => {
if (key === 'index') {
return 'my-test-index-*';
}
}),
getSerializedFields: jest.fn().mockImplementation(() => ({})),
};
return { searchSource };
return { searchSource: mockSearchSource };
},
getTitle: () => `The Dude`,
getInspectorAdapters: () => null,
Expand All @@ -79,6 +77,49 @@ describe('GetCsvReportPanelAction', () => {
} as any;
});

it('translates empty embeddable context into job params', async () => {
const panel = new GetCsvReportPanelAction(core, mockLicense$());

await panel.execute(context);

expect(core.http.post).toHaveBeenCalledWith(
'/api/reporting/v1/generate/immediate/csv_searchsource',
{
body: '{"searchSource":{},"columns":[],"browserTimezone":"America/New_York"}',
}
);
});

it('translates embeddable context into job params', async () => {
// setup
mockSearchSource = {
createCopy: () => mockSearchSource,
removeField: jest.fn(),
setField: jest.fn(),
getField: jest.fn(),
getSerializedFields: jest.fn().mockImplementation(() => ({ testData: 'testDataValue' })),
};
context.embeddable.getSavedSearch = () => {
return {
searchSource: mockSearchSource,
columns: ['column_a', 'column_b'],
};
};

const panel = new GetCsvReportPanelAction(core, mockLicense$());

// test
await panel.execute(context);

expect(core.http.post).toHaveBeenCalledWith(
'/api/reporting/v1/generate/immediate/csv_searchsource',
{
body:
'{"searchSource":{"testData":"testDataValue"},"columns":["column_a","column_b"],"browserTimezone":"America/New_York"}',
}
);
});

it('allows downloading for valid licenses', async () => {
const panel = new GetCsvReportPanelAction(core, mockLicense$());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@

import { i18n } from '@kbn/i18n';
import moment from 'moment-timezone';
import { CoreSetup } from 'src/core/public';
import type { CoreSetup } from 'src/core/public';
import type { ISearchEmbeddable, SavedSearch } from '../../../../../src/plugins/discover/public';
import {
loadSharingDataHelpers,
ISearchEmbeddable,
SavedSearch,
SEARCH_EMBEDDABLE_TYPE,
} from '../../../../../src/plugins/discover/public';
import { IEmbeddable, ViewMode } from '../../../../../src/plugins/embeddable/public';
import {
IncompatibleActionError,
UiActionsActionDefinition as ActionDefinition,
} from '../../../../../src/plugins/ui_actions/public';
import { LicensingPluginSetup } from '../../../licensing/public';
import type { IEmbeddable } from '../../../../../src/plugins/embeddable/public';
import { ViewMode } from '../../../../../src/plugins/embeddable/public';
import type { UiActionsActionDefinition as ActionDefinition } from '../../../../../src/plugins/ui_actions/public';
import { IncompatibleActionError } from '../../../../../src/plugins/ui_actions/public';
import type { LicensingPluginSetup } from '../../../licensing/public';
import { API_GENERATE_IMMEDIATE, CSV_REPORTING_ACTION } from '../../common/constants';
import { JobParamsDownloadCSV } from '../../server/export_types/csv_searchsource_immediate/types';
import type { JobParamsDownloadCSV } from '../../server/export_types/csv_searchsource_immediate/types';
import { checkLicense } from '../lib/license_check';

function isSavedSearchEmbeddable(
Expand Down Expand Up @@ -64,14 +62,11 @@ export class GetCsvReportPanelAction implements ActionDefinition<ActionContext>

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

return serializedSearchSource;
}

public isCompatible = async (context: ActionContext) => {
Expand All @@ -96,12 +91,13 @@ export class GetCsvReportPanelAction implements ActionDefinition<ActionContext>
}

const savedSearch = embeddable.getSavedSearch();
const searchSource = await this.getSearchSource(savedSearch, embeddable);
const { columns, searchSource } = await this.getSearchSource(savedSearch, embeddable);

const kibanaTimezone = this.core.uiSettings.get('dateFormat:tz');
const browserTimezone = kibanaTimezone === 'Browser' ? moment.tz.guess() : kibanaTimezone;
const immediateJobParams: JobParamsDownloadCSV = {
searchSource,
columns,
browserTimezone,
title: savedSearch.title,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
import { i18n } from '@kbn/i18n';
import moment from 'moment-timezone';
import React from 'react';
import { IUiSettingsClient, ToastsSetup } from 'src/core/public';
import { ShareContext } from '../../../../../src/plugins/share/public';
import { LicensingPluginSetup } from '../../../licensing/public';
import type { IUiSettingsClient, ToastsSetup } from 'src/core/public';
import type { SearchSourceFields } from 'src/plugins/data/common';
import type { ShareContext } from '../../../../../src/plugins/share/public';
import type { LicensingPluginSetup } from '../../../licensing/public';
import { CSV_JOB_TYPE } from '../../common/constants';
import { JobParamsCSV } from '../../server/export_types/csv_searchsource/types';
import type { JobParamsCSV } from '../../server/export_types/csv_searchsource/types';
import { ReportingPanelContent } from '../components/reporting_panel_content_lazy';
import { checkLicense } from '../lib/license_check';
import { ReportingAPIClient } from '../lib/reporting_api_client';
import type { ReportingAPIClient } from '../lib/reporting_api_client';

interface ReportingProvider {
apiClient: ReportingAPIClient;
Expand Down Expand Up @@ -65,7 +66,8 @@ export const csvReportingProvider = ({
browserTimezone,
title: sharingData.title as string,
objectType,
searchSource: sharingData.searchSource,
searchSource: sharingData.searchSource as SearchSourceFields,
columns: sharingData.columns as string[] | undefined,
};

const getJobParams = () => jobParams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import { i18n } from '@kbn/i18n';
import moment from 'moment-timezone';
import React from 'react';
import { IUiSettingsClient, ToastsSetup } from 'src/core/public';
import { ShareContext } from '../../../../../src/plugins/share/public';
import { LicensingPluginSetup } from '../../../licensing/public';
import { LayoutParams } from '../../common/types';
import { JobParamsPNG } from '../../server/export_types/png/types';
import { JobParamsPDF } from '../../server/export_types/printable_pdf/types';
import type { IUiSettingsClient, ToastsSetup } from 'src/core/public';
import type { ShareContext } from '../../../../../src/plugins/share/public';
import type { LicensingPluginSetup } from '../../../licensing/public';
import type { LayoutParams } from '../../common/types';
import type { JobParamsPNG } from '../../server/export_types/png/types';
import type { JobParamsPDF } from '../../server/export_types/printable_pdf/types';
import { ScreenCapturePanelContent } from '../components/screen_capture_panel_content_lazy';
import { checkLicense } from '../lib/license_check';
import { ReportingAPIClient } from '../lib/reporting_api_client';
import type { ReportingAPIClient } from '../lib/reporting_api_client';

interface ReportingPDFPNGProvider {
apiClient: ReportingAPIClient;
Expand Down

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

Loading

0 comments on commit 2f85297

Please sign in to comment.