Skip to content

AdHocFilters: Show reason for non-applicable filters #1181

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

Merged
merged 9 commits into from
Jul 23, 2025

Conversation

mdvictor
Copy link
Collaborator

@mdvictor mdvictor commented Jul 8, 2025

Screenshot 2025-07-23 at 16 31 10

Builds on top of #1152 and refactors things a bit, mainly calls a different DS method from getApplicableFilters to getFiltersApplicability

Non-applicable filters can now also show a reason why the filter pill is invalid. It will also re-evaluate applicability on filter removal.

@mdvictor mdvictor requested a review from dprokop July 8, 2025 12:47
@mdvictor mdvictor marked this pull request as ready for review July 11, 2025 06:58
@@ -172,7 +172,7 @@ export function AdHocFilterPill({ filter, model, readOnly, focusOnWipInputRef }:
</Tooltip>
)}

{filter.origin && !filter.restorable && !filter.readOnly && (
{filter.origin && !filter.restorable && !filter.readOnly && !filter.nonApplicable && (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we extract this condition evaluation to either a function or a variable with a comment that describes what this condition is actually doing?

Comment on lines 617 to +618
update.originFilters?.forEach((f) => {
const isApplicable = response.includes(f.key);
const filter = responseMap.get(`${f.key}-${f.origin}`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this code depend on #1185?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, this is totally independent although the key is similar

Comment on lines 579 to 582
if (!ds || !ds.getFiltersApplicability()) {
if (setStateOnCheckFail) {
this.setState(update);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I understand the setStateOnCheckFail flag. Isn't this an equivalent of !ds || !ds.getFiltersApplicability really? IIUC what you want to do here is to allow immediate setState in case ds does not support the getFiltersApplicability.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is a bit confusing, I refactored it a bit, hoping it makes more sense. Basically that extra flag is for not setting state in the activation handler. So in most cases I replace a setState with this new call, but in the activation handler I am not replacing any setState: if there is no filters applicability call then I simply want to return because there is for sure no state update. That is what this flag does

const ds = await this._dataSourceSrv.get(this.state.datasource, this._scopedVars);
// @ts-expect-error (temporary till we update grafana/data)
if (!ds || !ds.getApplicableFilters) {
return [];
if (!ds || !ds.getFiltersApplicability()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you calling getFiltersApplicability method here? This should be just a check whether or not the data source has this API implemented. The actual execution is happening after that check.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yess, this is a typo 😞 thank you for catching this!

@@ -837,6 +837,8 @@ describe.each(['11.1.2', '11.1.1'])('AdHocFiltersVariable', (v) => {

scopesVariable.update();

await new Promise((r) => setTimeout(r, 1));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to add this to all scopes updating tests since now it goes through _setStateWithFiltersApplicabilityCheck which awaits for the ds query

@@ -325,7 +336,10 @@ export class AdHocFiltersVariable
];

// maintain other originFilters in the array, only update scopes ones
this.setState({ originFilters: [...finalFilters, ...remainingFilters] });
this._setStateWithFiltersApplicabilityCheck({
filters: this.state.filters,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reckon we do not need to clone this as scenes deals nicely with it and keeps things cleaner imo in the adhocs setState call when checking update state changes.


public async _setStateWithFiltersApplicabilityCheck(
update: Partial<AdHocFiltersVariableState>,
returnWithoutStateUpdate?: boolean
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mdvictor please add a verbose explanation of this flage.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed flag alltogether, the code management overhead is quite large for this small of a performance optimization imo

@samjewell
Copy link

This sounds like a nice improvement 👏

Small request from me - might you be able to put a screenshot on the PR description, so we can easily see what this will look like? 🙏

@mdvictor mdvictor merged commit 112b240 into main Jul 23, 2025
10 checks passed
@mdvictor mdvictor deleted the mdvictor/applicability-refactor branch July 23, 2025 17:03
@mdvictor mdvictor added minor Increment the minor version when merged release Create a release when this pr is merged labels Jul 23, 2025
@scenes-repo-bot-access-token
Copy link

🚀 PR was released in v6.28.0 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
minor Increment the minor version when merged release Create a release when this pr is merged released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants