-
Notifications
You must be signed in to change notification settings - Fork 11.6k
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
Alerting: Fix changing datasource and creating new query not using defaults. #63092
Conversation
da3f8d0
to
53c6d27
Compare
3a1cf72
to
504b66e
Compare
504b66e
to
284321c
Compare
Hello @soniaAguilarPeiron!
Please, if the current pull request addresses a bug fix, label it with the |
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.
Is there a way we can split this PR in to two distinct problems:
- clearing the model when switching DS
- applying
getDefaultQuery
I feel like the former is more important than the latter and I think there might be some opportunities to improve on the latter while not blocking a bug fix for 1.
@@ -199,9 +202,46 @@ export function recordingRulerRuleToRuleForm( | |||
}; | |||
} | |||
|
|||
export const getDefaultQueries = (): AlertQuery[] => { | |||
export const getDefaultQueriesAsync = async (): Promise<{ | |||
queries: AlertQuery[] | null; |
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.
Why would we return null
here instead of an empty array?
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.
we use null to differentiate between the resolved value that can be an empty array or the null that indicates in some parts of the code that we don't have the value yet.
}, | ||
"datasourceUid": "", |
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.
This diff seems weird, why did the type
and uid
change? And why is the datasourceUid
in the model
not the same as at the root (some uid
)?
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.
now in the test we are using this default query in the payload (we didn't have this payload before)
const defaultQuery: AlertQuery = {
refId: 'A',
queryType: '', =>>>>>>>>>>
datasourceUid: '',=>>>>>>>>>>
model: { refId: 'A', expression: 'THIS IS THE DEFAULT EXPRESSION' },
relativeTimeRange: getDefaultRelativeTimeRange(),
};
where the queryType is empty and also the datasource. That's why we get this snapshot.
@@ -73,6 +89,26 @@ type Props = { | |||
prefill?: Partial<RuleFormValues>; // Existing implies we modify existing rule. Prefill only provides default form values | |||
}; | |||
|
|||
export const useGetDefaults = (queryParams: UrlQueryMap, existing: RuleWithLocation<RulerRuleDTO> | undefined) => { | |||
const [defaultDsAndQueries, setDefaultDsAndQueries] = useState<{ | |||
queries: AlertQuery[] | null; |
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.
Dito null
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.
we use null to differentiate between the resolved value that can be an empty array or the null that indicates in some parts of the code that we don't have the value yet
}>({ queries: null }); | ||
|
||
useEffect(() => { | ||
const getDefaultQueries_ = async () => { |
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.
We can turn this in to an iife, but I prefer using useAsync
if we can
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.
done!
prefill={!!prefill} | ||
onDataChange={checkAlertCondition} | ||
asyncDefaultQueries={defaultDsAndQueries.queries} | ||
asyncDataSource={defaultDsAndQueries.ds} |
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'm a bit confused here why we need to pass these separately since getDefaultQueries
is a function we can call on the DataSourceApi
.
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.
this defaultDsAndQueries.queries
is not the same as we get from DataSourceApi
.getDefaultQuery
. This defaultDsAndQueries.queries
is:
queries: [
{
refId: 'A',
datasourceUid: dataSource.uid,
queryType: '',
relativeTimeRange,
model: {
refId: 'A',
hide: false,
...ds?.getDefaultQuery?.(CoreApp.UnifiedAlerting),
},
},
...getDefaultExpressions('B', 'C'),
],
<QueryAndExpressionsStep editingExistingRule={!!existing} onDataChange={checkAlertCondition} /> | ||
<QueryAndExpressionsStep | ||
editingExistingRule={!!existing} | ||
prefill={!!prefill} |
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'm not sure if I understand why these props have to be passed all the way down in to the QueryAndExpressionsStep
, it looks like we're using them to make some assertions on when to dispatch setDataQueries
?
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.
We need to pass all the way down because:
- we need default queries for initialising the form with the default values. (when creating a new alert rule)
- we need also these default queries as you said in the
QueryAndExpressionsStep
to set queries once this async value is set, and also for theonAddNewQuery
(to set the default) - at the same time we need to keep updated this async default value in the
ExpressionEditor
when we are creating a new 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.
Gotcha, that makes sense!
d398a58
to
66fc7ed
Compare
@gillesdemey I updated the description of the PR, because this PR is actually setting the defaults. What I see is that the problem of not clearing the model is not happening. I was testing it and I didn't find any issues on that. @konrad147 mentioned the same. Maybe something related to data source plugins changed and this error is gone? |
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.
Everything looks good, let's ship it!
…faults. (#63092) * Set default query when changing data source or adding new alert query * Set default query when creating new alert rule * Set fefault query for cloud and recording rules * Create hook for getting defaults in AlertRuleForm * fixing tests * Use conditionals with 'if' for more clarity and rename lazy to async * Add loading indicator for default queries * Fix tests * Make newModel a sync method and fix tests error * Use useAsync instead of useEffect for an async call (cherry picked from commit b42f973)
… using defaults. (#63720) Alerting: Fix changing datasource and creating new query not using defaults. (#63092) * Set default query when changing data source or adding new alert query * Set default query when creating new alert rule * Set fefault query for cloud and recording rules * Create hook for getting defaults in AlertRuleForm * fixing tests * Use conditionals with 'if' for more clarity and rename lazy to async * Add loading indicator for default queries * Fix tests * Make newModel a sync method and fix tests error * Use useAsync instead of useEffect for an async call (cherry picked from commit b42f973) Co-authored-by: Sonia Aguilar <33540275+soniaAguilarPeiron@users.noreply.github.com>
…faults. (#63092) * Set default query when changing data source or adding new alert query * Set default query when creating new alert rule * Set fefault query for cloud and recording rules * Create hook for getting defaults in AlertRuleForm * fixing tests * Use conditionals with 'if' for more clarity and rename lazy to async * Add loading indicator for default queries * Fix tests * Make newModel a sync method and fix tests error * Use useAsync instead of useEffect for an async call
What is this feature?
This PR sets default query from the data source when creating new alert rule, changing data source or adding a new query in the alert rule form.
Why do we need this feature?
Data sources can include a default value for query that should be used in the cases described before.
Who is this feature for?
All users
Which issue(s) does this PR fix?:
Fixes #61280