-
Notifications
You must be signed in to change notification settings - Fork 23
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
refactor: Update alerting DSL verify mechanism #359
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,9 +29,10 @@ import { SUMMARY_ASSISTANT_API } from '../../../common/constants/llm'; | |
import shiny_sparkle from '../../assets/shiny_sparkle.svg'; | ||
import { UsageCollectionSetup } from '../../../../../src/plugins/usage_collection/public'; | ||
import { reportMetric } from '../../utils/report_metric'; | ||
import { buildUrlQuery, createIndexPatterns } from '../../utils'; | ||
import { buildUrlQuery, createIndexPatterns, extractTimeRangeDSL } from '../../utils'; | ||
import { AssistantPluginStartDependencies } from '../../types'; | ||
import { UI_SETTINGS } from '../../../../../src/plugins/data/public'; | ||
import { formatUrlWithWorkspaceId } from '../../../../../src/core/public/utils'; | ||
|
||
export const GeneratePopoverBody: React.FC<{ | ||
incontextInsight: IncontextInsightInput; | ||
|
@@ -55,10 +56,19 @@ export const GeneratePopoverBody: React.FC<{ | |
const getMonitorType = async () => { | ||
const context = await incontextInsight.contextProvider?.(); | ||
const monitorType = context?.additionalInfo?.monitorType; | ||
const dsl = context?.additionalInfo?.dsl; | ||
// Only this two types from alerting contain DSL and index. | ||
const shoudDisplayDiscoverButton = | ||
const isSupportedMonitorType = | ||
monitorType === 'query_level_monitor' || monitorType === 'bucket_level_monitor'; | ||
setDisplayDiscoverButton(shoudDisplayDiscoverButton); | ||
let hasTimeRangeFilter = false; | ||
if (dsl) { | ||
const dslObject = JSON.parse(dsl); | ||
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. add try catch for JSON.parse? |
||
const filters = dslObject?.query?.bool?.filter; | ||
// Filters contains time range filter,if no filters, return. | ||
if (!filters?.length) return; | ||
hasTimeRangeFilter = !!extractTimeRangeDSL(filters).timeRangeDSL; | ||
} | ||
setDisplayDiscoverButton(isSupportedMonitorType && hasTimeRangeFilter); | ||
}; | ||
getMonitorType(); | ||
}, [incontextInsight, setDisplayDiscoverButton]); | ||
|
@@ -195,12 +205,11 @@ export const GeneratePopoverBody: React.FC<{ | |
if (!dsl || !indexName) return; | ||
const dslObject = JSON.parse(dsl); | ||
const filters = dslObject?.query?.bool?.filter; | ||
if (!filters) return; | ||
const timeDslIndex = filters?.findIndex((filter: Record<string, string>) => filter?.range); | ||
const timeDsl = filters[timeDslIndex]?.range; | ||
const timeFieldName = Object.keys(timeDsl)[0]; | ||
if (!timeFieldName) return; | ||
filters?.splice(timeDslIndex, 1); | ||
if (!filters?.length) return; | ||
const { timeRangeDSL, newFilters, timeFieldName } = extractTimeRangeDSL(filters); | ||
// Filter out time range DSL and use this result to build filter query. | ||
if (!timeFieldName || !timeRangeDSL) return; | ||
dslObject.query.bool.filter = newFilters; | ||
|
||
if (getStartServices) { | ||
const [coreStart, startDeps] = await getStartServices(); | ||
|
@@ -222,11 +231,18 @@ export const GeneratePopoverBody: React.FC<{ | |
coreStart.savedObjects, | ||
indexPattern, | ||
dslObject, | ||
timeDsl[timeFieldName], | ||
timeRangeDSL, | ||
context?.dataSourceId | ||
); | ||
// Navigate to new discover with query built to populate | ||
coreStart.application.navigateToUrl(`data-explorer/discover#?${query}`); | ||
// Navigate to new discover with query built to populate, use new window to avoid discover search failed. | ||
const discoverUrl = `data-explorer/discover#?${query}`; | ||
const currentWorkspace = coreStart.workspaces.currentWorkspace$.getValue(); | ||
const url = formatUrlWithWorkspaceId( | ||
discoverUrl, | ||
currentWorkspace?.id ?? '', | ||
coreStart.http.basePath | ||
); | ||
window.open(url, '_blank'); | ||
} | ||
} finally { | ||
setDiscoverLoading(false); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,16 +5,19 @@ | |
|
||
import rison from 'rison-node'; | ||
import { stringify } from 'query-string'; | ||
import moment from 'moment'; | ||
import { buildCustomFilter } from '../../../../src/plugins/data/common'; | ||
import { url } from '../../../../src/plugins/opensearch_dashboards_utils/public'; | ||
import { | ||
DataPublicPluginStart, | ||
opensearchFilters, | ||
IndexPattern, | ||
Filter, | ||
} from '../../../../src/plugins/data/public'; | ||
import { CoreStart } from '../../../../src/core/public'; | ||
import { NestedRecord, DSL } from '../types'; | ||
|
||
export const buildFilter = (indexPatternId: string, dsl: Record<string, unknown>) => { | ||
export const buildFilter = (indexPatternId: string, dsl: DSL) => { | ||
const filterAlias = 'Alerting-filters'; | ||
return buildCustomFilter( | ||
indexPatternId, | ||
|
@@ -69,16 +72,19 @@ export const buildUrlQuery = async ( | |
dataStart: DataPublicPluginStart, | ||
savedObjects: CoreStart['savedObjects'], | ||
indexPattern: IndexPattern, | ||
dsl: Record<string, unknown>, | ||
dsl: DSL, | ||
timeDsl: Record<'from' | 'to', string>, | ||
dataSourceId?: string | ||
) => { | ||
const filter = buildFilter(indexPattern.id!, dsl); | ||
|
||
const filterManager = dataStart.query.filterManager; | ||
// There are some map and flatten operations to filters in filterManager, use this to keep aligned with discover. | ||
filterManager.setAppFilters([filter]); | ||
const filters = filterManager.getAppFilters(); | ||
let filters: Filter[] = []; | ||
// If there is none filter after filtering timeRange filter, skip to build filter query. | ||
if ((dsl?.query?.bool?.filter?.length ?? 0) > 0) { | ||
const filter = buildFilter(indexPattern.id!, dsl); | ||
const filterManager = dataStart.query.filterManager; | ||
// There are some map and flatten operations to filters in filterManager, use this to keep aligned with discover. | ||
filterManager.setAppFilters([filter]); | ||
filters = filterManager.getAppFilters(); | ||
} | ||
|
||
const refreshInterval = { | ||
pause: true, | ||
|
@@ -142,3 +148,35 @@ export const buildUrlQuery = async ( | |
); | ||
return hash; | ||
}; | ||
|
||
export const validateToTimeRange = (time: string) => { | ||
// Alerting uses this format in to field of time range filter. | ||
const TO_TIME_FORMAT = 'YYYY-MM-DDTHH:mm:ssZ'; | ||
return moment.utc(time, TO_TIME_FORMAT, true).isValid(); | ||
}; | ||
|
||
export const extractTimeRangeDSL = (filters: NestedRecord[]) => { | ||
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. could we add a unit test for these two functions? |
||
let timeRangeDSL; | ||
let timeFieldName; | ||
const newFilters = filters.filter((filter) => { | ||
if (filter?.range && typeof filter.range === 'object') { | ||
for (const key of Object.keys(filter.range)) { | ||
const rangeValue = filter.range[key]; | ||
if (typeof rangeValue === 'object' && 'to' in rangeValue) { | ||
const toValue = rangeValue.to; | ||
if (typeof toValue === 'string' && validateToTimeRange(toValue)) { | ||
timeRangeDSL = filter.range[key]; | ||
timeFieldName = key; | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
return true; | ||
}); | ||
return { | ||
newFilters, | ||
timeRangeDSL, | ||
timeFieldName, | ||
}; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not related to this PR, could you store this context in a variable so we don't need to invoke this provider multiple times?(It got 3 invoking for now.) contextProvider for alerting is a heavy operation, it will execute a dsl and a ppl each time.
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, let me see how to redesign this and do it in a separate PR in order not to have any influence on upcoming release.