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

AdhocFilters: Show label for selected key #690

Merged
merged 7 commits into from
Apr 22, 2024
Merged

Conversation

ashharrison90
Copy link
Contributor

@ashharrison90 ashharrison90 commented Apr 11, 2024

Ensures we find the selected option from the list and get the full key/value object instead of creating a new object from just the key

📦 Published PR as canary version: 4.11.3--canary.690.8783271240.0

✨ Test out this PR locally via:

npm install @grafana/scenes@4.11.3--canary.690.8783271240.0
# or 
yarn add @grafana/scenes@4.11.3--canary.690.8783271240.0

@ashharrison90 ashharrison90 added patch Increment the patch version when merged release Create a release when this pr is merged labels Apr 11, 2024
@ashharrison90 ashharrison90 self-assigned this Apr 11, 2024
return toSelectableValue(matchingDefaultKey);
}
if (state.keys) {
return state.keys.find(option => option.value === filter.key);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

are we concerned about having to find the option in the (potentially large) list here? 🤔

@dprokop
Copy link
Member

dprokop commented Apr 15, 2024

@ashharrison90 - think this ain't sufficient, as it will still use key.value when it's synced from URL. Same with the dimension values, which are affected by the same issue.

I think we need to trigger value "validation" on acivation when there are filters that were synced from URL. For that we need to fetch the keys from the data source and upate the available options accordingly.

@torkelo - any reason why we don't do that already?

@torkelo
Copy link
Member

torkelo commented Apr 16, 2024

AdHocFilters are not really well designed for "display labels" for keys, as we only include the value in URL and loading the filter state from URL should be all that is needed before queries are issued and state sync

Do not want the AdHocFilterRenderer to have to issue a query for every filter to lookup the label, or would be nice to avoid that, just so messy

@bfmatei
Copy link
Contributor

bfmatei commented Apr 17, 2024

@torkelo @dprokop @ashharrison90

I just pushed an iteration of syncing the label via the URL. It works like this:

  • the label is added to the URL by using a comma separator between the value and label
  • we escape the commas in the label/value using __gfc__ similarly to what we do for the pipe
  • if the key and label are different, we just omit the duplication

One thing to have in mind: extending AdHocFilter internally and adding the labels there seemed the best solution. Alternatively we can have some mappings in the AdHocFiltersVariableState. Let me know if you prefer the latter or you have another idea on how to achieve it.

Also a small demo:

Screen.Recording.2024-04-17.at.15.47.05.mov

@bfmatei bfmatei assigned bfmatei and unassigned ashharrison90 Apr 17, 2024
@dprokop dprokop requested a review from torkelo April 17, 2024 16:14
);

act(() => {
locationService.push('/?var-filters=newKey|=|newValue,New Value&var-filters=newKey2|=~|newValue2,New Value 2');
Copy link
Member

Choose a reason for hiding this comment

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

Can we add a test where key and value have a "," in them? btw, can't we use | (pipe) character as separator so we can use one split operation?

Copy link
Member

Choose a reason for hiding this comment

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

ah, I see now why we need two delimiters (as both key and value can have an optional label)

Copy link
Contributor

Choose a reason for hiding this comment

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

The optional label was more of an afterthought. The initial idea was to offer backwards compatibility with the existing URLs.

I also pushed changes to the comma test where new the value also contains a comma.

Copy link
Member

@torkelo torkelo Apr 18, 2024

Choose a reason for hiding this comment

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

this would not be backward compatible with old URLs (in the sense new URL would break on old Grafana/scenes)

Copy link
Contributor

@bfmatei bfmatei Apr 18, 2024

Choose a reason for hiding this comment

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

Ah, the idea was the other way around: offer compatibility for the existing URLs.

In the new implementation you can still load an URL that was bookmarked in an older Grafana/scenes version. There's only one exception to this though: values saved in the past and containing commas would break the functionality in the new implementation as they were not encoded to __gfc__. Changing the key name would overcome this but I think it's way too much hassle for such edge case.

});

expect(locationService.getLocation().search).toBe(
'?var-filters=newKey%7C%3D%7CnewValue,New%20Value&var-filters=key2%7C%3D%7Cval2'
Copy link
Member

Choose a reason for hiding this comment

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

Think we should encode , shouldn't we?

Copy link
Contributor

Choose a reason for hiding this comment

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

Good question. I would have encoded the comma in the tests but the problem is that Scenes does not encode it for some reason. In the demo you can see that when adding it to the URL it is not encoded.

@bfmatei bfmatei merged commit 7c95363 into main Apr 22, 2024
3 checks passed
@bfmatei bfmatei deleted the ash/adhoc-filter-label branch April 22, 2024 13:05
@grafanabot
Copy link
Contributor

🚀 PR was released in v4.11.3 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patch Increment the patch 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.

None yet

5 participants