Skip to content

Commit

Permalink
[Discover/Reporting] Fix potential time drift with relative time when…
Browse files Browse the repository at this point in the history
… requesting a report (elastic#114274) (elastic#114602)

* updated Discover getSharingData to accept flag for absolute time

* update snapshots

* simplify fallback title

* more appropriate title?

* remove old translations

* implement PR feedback

Co-authored-by: Kibana Machine <[email protected]>

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
jloleysens and kibanamachine authored Oct 12, 2021
1 parent 7bdfcf1 commit 0f15cce
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import { i18n } from '@kbn/i18n';
import moment from 'moment';
import type { IndexPattern, ISearchSource } from 'src/plugins/data/common';
import { showOpenSearchPanel } from './show_open_search_panel';
import { getSharingData, showPublicUrlSwitch } from '../../utils/get_sharing_data';
Expand Down Expand Up @@ -128,8 +127,7 @@ export const getTopNavLinks = ({
title:
savedSearch.title ||
i18n.translate('discover.localMenu.fallbackReportTitle', {
defaultMessage: 'Discover search [{date}]',
values: { date: moment().toISOString(true) },
defaultMessage: 'Untitled discover search',
}),
},
isDirty: !savedSearch.id || state.isAppStateDirty(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,7 @@ describe('getSharingData', () => {
expect(result).toMatchInlineSnapshot(`
Object {
"columns": Array [],
"searchSource": Object {
"index": "the-index-pattern-id",
"sort": Array [
Object {
"_score": "desc",
},
],
},
"getSearchSource": [Function],
}
`);
});
Expand All @@ -66,14 +59,7 @@ describe('getSharingData', () => {
"column_a",
"column_b",
],
"searchSource": Object {
"index": "the-index-pattern-id",
"sort": Array [
Object {
"_score": "desc",
},
],
},
"getSearchSource": [Function],
}
`);
});
Expand Down Expand Up @@ -108,14 +94,7 @@ describe('getSharingData', () => {
"cool-field-5",
"cool-field-6",
],
"searchSource": Object {
"index": "the-index-pattern-id",
"sort": Array [
Object {
"_doc": "desc",
},
],
},
"getSearchSource": [Function],
}
`);
});
Expand Down Expand Up @@ -158,14 +137,7 @@ describe('getSharingData', () => {
"cool-field-5",
"cool-field-6",
],
"searchSource": Object {
"index": "the-index-pattern-id",
"sort": Array [
Object {
"_doc": false,
},
],
},
"getSearchSource": [Function],
}
`);
});
Expand Down
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 } from 'src/plugins/data/common';
import type { 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/types';
import { getSortForSearchSource } from '../components/doc_table';
Expand All @@ -31,8 +31,8 @@ export async function getSharingData(
'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('filter');
searchSource.removeField('highlight');
searchSource.removeField('highlightAll');
searchSource.removeField('aggs');
Expand All @@ -54,7 +54,15 @@ export async function getSharingData(
}

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

searchSource.setField('filter', filter);

return searchSource.getSerializedFields(true);
},
columns,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ export class ReportingCsvPanelAction implements ActionDefinition<ActionContext>
}

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

const immediateJobParams = this.apiClient.getDecoratedJobParams({
searchSource,
searchSource: getSearchSource(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 @@ -48,14 +48,23 @@ export const ReportingCsvShareProvider = ({
return [];
}

const getSearchSource = sharingData.getSearchSource as (
absoluteTime?: boolean
) => SearchSourceFields;

const jobParams = {
title: sharingData.title as string,
objectType,
searchSource: sharingData.searchSource as SearchSourceFields,
columns: sharingData.columns as string[] | undefined,
};

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

const shareActions = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface ReportingPanelProps {
requiresSavedState: boolean; // Whether the report to be generated requires saved state that is not captured in the URL submitted to the report generator.
layoutId?: string;
objectId?: string;
getJobParams: () => Omit<BaseParams, 'browserTimezone' | 'version'>;
getJobParams: (forShareUrl?: boolean) => Omit<BaseParams, 'browserTimezone' | 'version'>;
options?: ReactElement | null;
isDirty?: boolean;
onClose?: () => void;
Expand Down Expand Up @@ -75,7 +75,7 @@ class ReportingPanelContentUi extends Component<Props, State> {
private getAbsoluteReportGenerationUrl = (props: Props) => {
const relativePath = this.props.apiClient.getReportingJobPath(
props.reportType,
this.props.apiClient.getDecoratedJobParams(this.props.getJobParams())
this.props.apiClient.getDecoratedJobParams(this.props.getJobParams(true))
);
return url.resolve(window.location.href, relativePath); // FIXME: '(from: string, to: string): string' is deprecated
};
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -2528,7 +2528,6 @@
"discover.loadingDocuments": "ドキュメントを読み込み中",
"discover.loadingJSON": "JSONを読み込んでいます",
"discover.loadingResults": "結果を読み込み中",
"discover.localMenu.fallbackReportTitle": "Discover検索[{date}]",
"discover.localMenu.inspectTitle": "検査",
"discover.localMenu.localMenu.newSearchTitle": "新規",
"discover.localMenu.localMenu.optionsTitle": "オプション",
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -2554,7 +2554,6 @@
"discover.loadingDocuments": "正在加载文档",
"discover.loadingJSON": "正在加载 JSON",
"discover.loadingResults": "正在加载结果",
"discover.localMenu.fallbackReportTitle": "Discover 搜索 [{date}]",
"discover.localMenu.inspectTitle": "检查",
"discover.localMenu.localMenu.newSearchTitle": "新建",
"discover.localMenu.localMenu.optionsTitle": "选项",
Expand Down

0 comments on commit 0f15cce

Please sign in to comment.