Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reporting: Fix _index and _id columns in CSV export #96097

Merged
merged 15 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { ISearchSource, SearchSourceFields } from '../../../../data/common';
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 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 @@ -20,13 +20,9 @@ export async function getSharingData(
currentSearchSource: ISearchSource,
state: AppState | SavedSearch,
config: IUiSettingsClient
) {
): Promise<{ columns: string[]; searchSource: SearchSourceFields }> {
tsullivan marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -64,14 +64,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 +93,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 @@ -9,6 +9,7 @@ import { i18n } from '@kbn/i18n';
import moment from 'moment-timezone';
import React from 'react';
import { IUiSettingsClient, ToastsSetup } from 'src/core/public';
import { SearchSourceFields } from 'src/plugins/data/common';
import { ShareContext } from '../../../../../src/plugins/share/public';
import { LicensingPluginSetup } from '../../../licensing/public';
import { CSV_JOB_TYPE } from '../../common/constants';
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

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

Loading