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

es query rule - get time field from data view instead of rule #182883

Merged
merged 5 commits into from
May 8, 2024
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 @@ -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 @@ -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