Skip to content

Commit

Permalink
[8.9] fix 'Download CSV' returns no data when panel has custom time r…
Browse files Browse the repository at this point in the history
…ange outside timerange of global time picker (#163887) (#164238)

# Backport

This will backport the following commits from `main` to `8.9`:
- [fix 'Download CSV' returns no data when panel has custom time range
outside timerange of global time picker
(#163887)](#163887)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Nathan
Reese","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-08-17T22:37:12Z","message":"fix
'Download CSV' returns no data when panel has custom time range outside
timerange of global time picker (#163887)\n\nCloses
https://github.com/elastic/kibana/issues/163614\r\n\r\nPR resolves issue
by only adding global time filter to CSV export body\r\nwhen saved
search embeddable does not have time
range.\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine
<[email protected]>","sha":"53e803e42c5c9de73e60ae95cff13d17bab09f26","branchLabelMapping":{"^v8.10.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:Presentation","Team:DataDiscovery","Team:SharedUX","v8.10.0","v8.11.0","v8.9.2"],"number":163887,"url":"https://github.com/elastic/kibana/pull/163887","mergeCommit":{"message":"fix
'Download CSV' returns no data when panel has custom time range outside
timerange of global time picker (#163887)\n\nCloses
https://github.com/elastic/kibana/issues/163614\r\n\r\nPR resolves issue
by only adding global time filter to CSV export body\r\nwhen saved
search embeddable does not have time
range.\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine
<[email protected]>","sha":"53e803e42c5c9de73e60ae95cff13d17bab09f26"}},"sourceBranch":"main","suggestedTargetBranches":["8.11","8.9"],"targetPullRequestStates":[{"branch":"main","label":"v8.10.0","labelRegex":"^v8.10.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/163887","number":163887,"mergeCommit":{"message":"fix
'Download CSV' returns no data when panel has custom time range outside
timerange of global time picker (#163887)\n\nCloses
https://github.com/elastic/kibana/issues/163614\r\n\r\nPR resolves issue
by only adding global time filter to CSV export body\r\nwhen saved
search embeddable does not have time
range.\r\n\r\n---------\r\n\r\nCo-authored-by: kibanamachine
<[email protected]>","sha":"53e803e42c5c9de73e60ae95cff13d17bab09f26"}},{"branch":"8.11","label":"v8.11.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.9","label":"v8.9.2","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Nathan Reese <[email protected]>
  • Loading branch information
kibanamachine and nreese authored Aug 18, 2023
1 parent 566a0f4 commit 2774922
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -644,4 +644,8 @@ export class SavedSearchEmbeddable

if (this.abortController) this.abortController.abort();
}

public hasTimeRange() {
return this.getTimeRange() !== undefined;
}
}
1 change: 1 addition & 0 deletions src/plugins/discover/public/embeddable/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface SearchOutput extends EmbeddableOutput {

export interface ISearchEmbeddable extends IEmbeddable<SearchInput, SearchOutput> {
getSavedSearch(): SavedSearch;
hasTimeRange(): boolean;
}

export interface SearchEmbeddable extends Embeddable<SearchInput, SearchOutput> {
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/discover/public/utils/get_sharing_data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('getSharingData', () => {
index.timeFieldName = 'cool-timefield';
const searchSourceMock = createSearchSourceMock({ index });
const { getSearchSource } = await getSharingData(searchSourceMock, {}, services);
expect(getSearchSource()).toMatchInlineSnapshot(`
expect(getSearchSource({})).toMatchInlineSnapshot(`
Object {
"fields": Array [
Object {
Expand Down Expand Up @@ -121,7 +121,7 @@ describe('getSharingData', () => {
},
services
);
expect(getSearchSource()).toMatchInlineSnapshot(`
expect(getSearchSource({})).toMatchInlineSnapshot(`
Object {
"index": "the-data-view-id",
"sort": Array [
Expand Down Expand Up @@ -151,7 +151,7 @@ describe('getSharingData', () => {
},
services
);
expect(getSearchSource().fields).toStrictEqual([
expect(getSearchSource({}).fields).toStrictEqual([
{ field: 'cool-timefield', include_unmapped: 'true' },
{ field: 'cool-field-1', include_unmapped: 'true' },
{ field: 'cool-field-2', include_unmapped: 'true' },
Expand Down
43 changes: 25 additions & 18 deletions src/plugins/discover/public/utils/get_sharing_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,35 @@ export async function getSharingData(
const absoluteTimeFilter = data.query.timefilter.timefilter.createFilter(index);
const relativeTimeFilter = data.query.timefilter.timefilter.createRelativeFilter(index);
return {
getSearchSource: (absoluteTime?: boolean): SerializedSearchSourceFields => {
getSearchSource: ({
addGlobalTimeFilter,
absoluteTime,
}: {
addGlobalTimeFilter?: boolean;
absoluteTime?: boolean;
}): SerializedSearchSourceFields => {
const timeFilter = absoluteTime ? absoluteTimeFilter : relativeTimeFilter;
if (addGlobalTimeFilter && timeFilter) {
// remove timeFilter from existing filter
if (Array.isArray(existingFilter)) {
existingFilter = existingFilter.filter(
(current) => !isEqualFilters(current, absoluteTimeFilter)
);
} else if (isEqualFilters(existingFilter, absoluteTimeFilter)) {
existingFilter = undefined;
}

// remove timeFilter from existing filter
if (Array.isArray(existingFilter)) {
existingFilter = existingFilter.filter(
(current) => !isEqualFilters(current, absoluteTimeFilter)
);
} else if (isEqualFilters(existingFilter, absoluteTimeFilter)) {
existingFilter = undefined;
if (existingFilter) {
existingFilter = Array.isArray(existingFilter)
? [timeFilter, ...existingFilter]
: ([timeFilter, existingFilter] as Filter[]);
} else {
existingFilter = timeFilter;
}
}

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

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ describe('GetCsvReportPanelAction', () => {
from: 'now-7d',
},
}),
hasTimeRange: () => true,
},
} as unknown as ActionContext;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class ReportingCsvPanelAction implements ActionDefinition<ActionContext>
});
}

public async getSearchSource(savedSearch: SavedSearch, _embeddable: ISearchEmbeddable) {
public async getSharingData(savedSearch: SavedSearch) {
const [{ uiSettings }, { data }] = await Rx.firstValueFrom(this.startServices$);
const { getSharingData } = await loadSharingDataHelpers();
return await getSharingData(savedSearch.searchSource, savedSearch, { uiSettings, data });
Expand Down Expand Up @@ -126,10 +126,13 @@ export class ReportingCsvPanelAction implements ActionDefinition<ActionContext>
}

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

const immediateJobParams = this.apiClient.getDecoratedJobParams({
searchSource: getSearchSource(true),
searchSource: getSearchSource({
addGlobalTimeFilter: !embeddable.hasTimeRange(),
absoluteTime: true,
}),
columns,
title: savedSearch.title || '',
objectType: 'downloadCsv', // FIXME: added for typescript, but immediate download job does not need objectType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ export const reportingCsvShareProvider = ({
return [];
}

const getSearchSource = sharingData.getSearchSource as (
absoluteTime?: boolean
) => SearchSourceFields;
const getSearchSource = sharingData.getSearchSource as ({
addGlobalTimeFilter,
absoluteTime,
}: {
addGlobalTimeFilter?: boolean;
absoluteTime?: boolean;
}) => SearchSourceFields;

const jobParams = {
title: sharingData.title as string,
Expand All @@ -39,10 +43,12 @@ export const reportingCsvShareProvider = ({
};

const getJobParams = (forShareUrl?: boolean) => {
const absoluteTime = !forShareUrl;
return {
...jobParams,
searchSource: getSearchSource(absoluteTime),
searchSource: getSearchSource({
addGlobalTimeFilter: true,
absoluteTime: !forShareUrl,
}),
};
};

Expand Down
Loading

0 comments on commit 2774922

Please sign in to comment.