Skip to content

Commit

Permalink
[8.14] es query rule - get time field from data view instead of rule (#…
Browse files Browse the repository at this point in the history
…182883) (#183001)

# Backport

This will backport the following commits from `main` to `8.14`:
- [es query rule - get time field from data view instead of rule
(#182883)](#182883)

<!--- Backport version: 9.4.3 -->

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

<!--BACKPORT [{"author":{"name":"Matthew
Kime","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-05-08T22:00:24Z","message":"es
query rule - get time field from data view instead of rule
(#182883)\n\n## Summary\r\n\r\nPreviously it was possible to create a
rule with a data view and change\r\nthe data view but the previous time
field would still be referenced. Now\r\nthe time field is always pulled
from the current data view.\r\n\r\n\r\nCloses
https://github.com/elastic/kibana/issues/182879\r\n\r\n#### Release
note\r\n\r\nFixed issue where an ES query rule could be created with a
data view,\r\nthen the data view is changed but there's still a
reference to the\r\nprevious data view's timestamp field. Now the
timestamp field is always\r\ntaken from the currently configured data
view.\r\n\r\n---------\r\n\r\nCo-authored-by: Davis McPhee
<[email protected]>","sha":"bc103c7016245901a04fc4921c1b213a4fbe2695","branchLabelMapping":{"^v8.15.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:DataDiscovery","backport:prev-minor","v8.15.0"],"title":"es
query rule - get time field from data view instead of
rule","number":182883,"url":"https://github.com/elastic/kibana/pull/182883","mergeCommit":{"message":"es
query rule - get time field from data view instead of rule
(#182883)\n\n## Summary\r\n\r\nPreviously it was possible to create a
rule with a data view and change\r\nthe data view but the previous time
field would still be referenced. Now\r\nthe time field is always pulled
from the current data view.\r\n\r\n\r\nCloses
https://github.com/elastic/kibana/issues/182879\r\n\r\n#### Release
note\r\n\r\nFixed issue where an ES query rule could be created with a
data view,\r\nthen the data view is changed but there's still a
reference to the\r\nprevious data view's timestamp field. Now the
timestamp field is always\r\ntaken from the currently configured data
view.\r\n\r\n---------\r\n\r\nCo-authored-by: Davis McPhee
<[email protected]>","sha":"bc103c7016245901a04fc4921c1b213a4fbe2695"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.15.0","branchLabelMappingKey":"^v8.15.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/182883","number":182883,"mergeCommit":{"message":"es
query rule - get time field from data view instead of rule
(#182883)\n\n## Summary\r\n\r\nPreviously it was possible to create a
rule with a data view and change\r\nthe data view but the previous time
field would still be referenced. Now\r\nthe time field is always pulled
from the current data view.\r\n\r\n\r\nCloses
https://github.com/elastic/kibana/issues/182879\r\n\r\n#### Release
note\r\n\r\nFixed issue where an ES query rule could be created with a
data view,\r\nthen the data view is changed but there's still a
reference to the\r\nprevious data view's timestamp field. Now the
timestamp field is always\r\ntaken from the currently configured data
view.\r\n\r\n---------\r\n\r\nCo-authored-by: Davis McPhee
<[email protected]>","sha":"bc103c7016245901a04fc4921c1b213a4fbe2695"}}]}]
BACKPORT-->

Co-authored-by: Matthew Kime <[email protected]>
  • Loading branch information
kibanamachine and mattkime authored May 8, 2024
1 parent c7f1657 commit 9ea601f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ const defaultParams: OnlySearchSourceRuleParams = {
excludeHitsFromPreviousRun: true,
aggType: 'count',
groupBy: 'all',
timeField: 'time',
// this should be ignored when using a data view
timeField: 'timeFieldNotFromDataView',
};

describe('fetchSearchSourceQuery', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,17 @@ export function updateSearchSource(
alertLimit?: number
): { searchSource: ISearchSource; filterToExcludeHitsFromPreviousRun: Filter | null } {
const isGroupAgg = isGroupAggregation(params.termField);
const timeFieldName = params.timeField || index.timeFieldName;
const timeField = index.getTimeField();

if (!timeFieldName) {
throw new Error('Invalid data view without timeFieldName.');
if (!timeField) {
throw new Error(`Data view with ID ${index.id} no longer contains a time field.`);
}

searchSource.setField('size', isGroupAgg ? 0 : params.size);

const field = index.fields.find((f) => f.name === timeFieldName);
const filters = [
buildRangeFilter(
field!,
timeField,
{ lte: dateEnd, gte: dateStart, format: 'strict_date_optional_time' },
index
),
Expand All @@ -135,7 +134,7 @@ export function updateSearchSource(
// add additional filter for documents with a timestamp greater than
// the timestamp of the previous run, so that those documents are not counted twice
filterToExcludeHitsFromPreviousRun = buildRangeFilter(
field!,
timeField,
{ gt: latestTimestamp, format: 'strict_date_optional_time' },
index
);
Expand All @@ -150,7 +149,7 @@ export function updateSearchSource(
searchSourceChild.setField('filter', filters as Filter[]);
searchSourceChild.setField('sort', [
{
[timeFieldName]: {
[timeField.name]: {
order: SortDirection.desc,
format: 'strict_date_optional_time||epoch_millis',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ describe('ruleType', () => {
toSpec: () => {
return { id: 'test-id', title: 'test-title', timeFieldName: 'timestamp', fields: [] };
},
getTimeField: () => dataViewMock.fields[1],
};
const defaultParams: OnlySearchSourceRuleParams = {
size: 100,
Expand Down Expand Up @@ -701,12 +702,12 @@ describe('ruleType', () => {

(searchSourceInstanceMock.getField as jest.Mock).mockImplementationOnce((name: string) => {
if (name === 'index') {
return { dataViewMock, timeFieldName: undefined };
return { dataViewMock, getTimeField: () => undefined, id: 1234 };
}
});

await expect(invokeExecutor({ params, ruleServices })).rejects.toThrow(
'Invalid data view without timeFieldName.'
'Data view with ID 1234 no longer contains a time field.'
);
});

Expand All @@ -717,6 +718,7 @@ describe('ruleType', () => {
(ruleServices.dataViews.create as jest.Mock).mockResolvedValueOnce({
...dataViewMock.toSpec(),
toSpec: () => dataViewMock.toSpec(),
getTimeField: () => dataViewMock.fields[1],
toMinimalSpec: () => dataViewMock.toSpec(),
});
(searchSourceInstanceMock.getField as jest.Mock).mockImplementation((name: string) => {
Expand Down

0 comments on commit 9ea601f

Please sign in to comment.