-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Discover] Improve warn feedback when data view has changed after rule creation #144908
Changes from all commits
c76129b
746a286
6e929ae
5139706
317db09
bf17668
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,13 @@ | |
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { sha256 } from 'js-sha256'; | ||
import { buildRangeFilter, Filter } from '@kbn/es-query'; | ||
import { Logger } from '@kbn/core/server'; | ||
import { | ||
DataView, | ||
DataViewSpec, | ||
getTime, | ||
ISearchSource, | ||
ISearchStartSearchSource, | ||
|
@@ -18,6 +22,8 @@ export async function fetchSearchSourceQuery( | |
ruleId: string, | ||
params: OnlySearchSourceRuleParams, | ||
latestTimestamp: string | undefined, | ||
publicBaseUrl: string, | ||
spacePrefix: string, | ||
services: { | ||
logger: Logger; | ||
searchSourceClient: ISearchStartSearchSource; | ||
|
@@ -27,8 +33,14 @@ export async function fetchSearchSourceQuery( | |
|
||
const initialSearchSource = await searchSourceClient.create(params.searchConfiguration); | ||
|
||
const index = initialSearchSource.getField('index') as DataView; | ||
if (!isTimeBasedDataView(index)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there another issue to prevent rules from being created with non time based data views? Typically we like to prevent situations where users can create rules that error with every execution. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, there is another issue for this case #135806 |
||
throw new Error('Invalid data view without timeFieldName.'); | ||
} | ||
|
||
const { searchSource, dateStart, dateEnd } = updateSearchSource( | ||
initialSearchSource, | ||
index, | ||
params, | ||
latestTimestamp | ||
); | ||
|
@@ -41,7 +53,12 @@ export async function fetchSearchSourceQuery( | |
|
||
const searchResult = await searchSource.fetch(); | ||
|
||
const dataViewChecksum = getDataViewChecksum(index.toSpec(false)); | ||
const ruleParamsChecksum = getRuleParamsChecksum(params as OnlySearchSourceRuleParams); | ||
const link = `${publicBaseUrl}${spacePrefix}/app/discover#/viewAlert/${ruleId}?from=${dateStart}&to=${dateEnd}&ruleParamsChecksum=${ruleParamsChecksum}&dataViewChecksum=${dataViewChecksum}`; | ||
|
||
return { | ||
link, | ||
numMatches: Number(searchResult.hits.total), | ||
searchResult, | ||
dateStart, | ||
|
@@ -51,16 +68,11 @@ export async function fetchSearchSourceQuery( | |
|
||
export function updateSearchSource( | ||
searchSource: ISearchSource, | ||
index: DataView, | ||
params: OnlySearchSourceRuleParams, | ||
latestTimestamp: string | undefined | ||
latestTimestamp?: string | ||
) { | ||
const index = searchSource.getField('index'); | ||
|
||
const timeFieldName = index?.timeFieldName; | ||
if (!timeFieldName) { | ||
throw new Error('Invalid data view without timeFieldName.'); | ||
} | ||
|
||
const timeFieldName = index.timeFieldName!; | ||
searchSource.setField('size', params.size); | ||
|
||
const timerangeFilter = getTime(index, { | ||
|
@@ -84,9 +96,35 @@ export function updateSearchSource( | |
const searchSourceChild = searchSource.createChild(); | ||
searchSourceChild.setField('filter', filters as Filter[]); | ||
searchSourceChild.setField('sort', [{ [timeFieldName]: SortDirection.desc }]); | ||
|
||
return { | ||
searchSource: searchSourceChild, | ||
dateStart, | ||
dateEnd, | ||
}; | ||
} | ||
|
||
function isTimeBasedDataView(index?: DataView) { | ||
return index?.timeFieldName; | ||
} | ||
|
||
function getDataViewChecksum(index: DataViewSpec) { | ||
const { title, timeFieldName, sourceFilters, runtimeFieldMap } = index; | ||
const orderedParams = Object.values({ title, timeFieldName, sourceFilters, runtimeFieldMap }); | ||
return sha256.create().update(JSON.stringify(orderedParams)).hex(); | ||
} | ||
|
||
/** | ||
* Get rule params checksum skipping serialized data view object | ||
*/ | ||
function getRuleParamsChecksum(params: OnlySearchSourceRuleParams) { | ||
const orderedParams = Object.values(params); | ||
return sha256 | ||
.create() | ||
.update( | ||
JSON.stringify(orderedParams, (key: string, value: string) => | ||
key === 'index' ? undefined : value | ||
) | ||
) | ||
.hex(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think changing the source filters changes the output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually we can filter out some field, then it will be unavailable on Discover. It first was found here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but it won't alter the output of the alert.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but here we are deciding whether to show
Data View has changed
toast message. When he/she opens the alert results and data view has changed in the meantime, we detect it by comparing checksum.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dimaanj
Yes, I understand that, but maybe there's something else I'm missing. Can you describe the steps a user might take where a change to source filters deserves this toast?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I've updated description