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: Add support for new isOneOf multi value operator #868

Merged
merged 18 commits into from
Aug 29, 2024

Conversation

ashharrison90
Copy link
Contributor

@ashharrison90 ashharrison90 commented Aug 13, 2024

  • adds new supportsMultiValueOperator property to AdHocFiltersVariable
  • adds new isMultiValueOperator method
  • adjusts _getOperators to conditionally return the new multi value operators depending on the value of supportsMultiValueOperator
  • modifies _updateFilter to take in a generic partial filter update
    • this allows for updating multiple keys at once! 🥳
  • adds supports for rendering a <MultiSelect> when a multi value operator is selected in AdHocFilterRenderer
  • adjusts the default renderFilter expression (which should produce a prometheus-compatible expression) to handle =| and !=| by converting to regex and joining with |
  • extends AdHocFiltersVariableUrlSyncHandler to handle multiple value/label pairs
    • modifies valueLabel to valueLabels so it can hold multiple labels
    • this bit feels a little messy/brittle imo 😕 any suggestions?

For https://github.com/grafana/hyperion-planning/issues/1
For grafana/grafana#85074

📦 Published PR as canary version: 5.11.0--canary.868.10613389161.0

✨ Test out this PR locally via:

npm install @grafana/scenes-react@5.11.0--canary.868.10613389161.0
npm install @grafana/scenes@5.11.0--canary.868.10613389161.0
# or 
yarn add @grafana/scenes-react@5.11.0--canary.868.10613389161.0
yarn add @grafana/scenes@5.11.0--canary.868.10613389161.0

@ashharrison90 ashharrison90 marked this pull request as ready for review August 14, 2024 10:00
@@ -40,6 +40,7 @@ export function getAdhocFiltersDemo(defaults: SceneAppPageState) {
// Only want keys for this series
baseFilters: [{ key: '__name__', operator: '=', value: 'ALERTS', condition: '' }],
datasource: { uid: 'gdev-prometheus' },
supportsMultiValueOperators: true,
Copy link
Member

@torkelo torkelo Aug 15, 2024

Choose a reason for hiding this comment

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

won't support for this be dependent on each data source, so not sure how we can set this in core dashboards. would have to update it's value after we load the datasource plugin and check if it supports it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hmmmmm, good point. i think we were trying to avoid putting something like that in the datasource since then we'd have to wait for the datasource request to return.

we could just put all this logic behind the feature toggle, and implement support in the other datasources before enabling the toggle? 🤔

looking at the docs, it looks like adhoc filters are only supported in Prometheus, Loki, InfluxDB, and Elasticsearch.
prometheus support is added in grafana#91837, can't imagine it would be much work to add support in loki/influx/elastic.

Copy link
Member

@dprokop dprokop Aug 19, 2024

Choose a reason for hiding this comment

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

Given that we are going to introduce this behind a feature toggle I think we could assume it's only Prometheus that supports multi value filters by default from day one. Tho I agree with @ashharrison90 that adding support for this in the data sources that already support ad hoc filters shouldn't be too much of an effort.

IMO whether or not data source should allow this operator would eventually depend on a different API (type-aware filters) where the availability of operators would be driven by getTagKeys call results, so that each dimension would define what type it is (string, number, enum) and whether or not it supports multi value selection.

WDYT?

Copy link
Contributor Author

@ashharrison90 ashharrison90 Aug 20, 2024

Choose a reason for hiding this comment

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

i think that's fine - the question then becomes what should we do as part of this PR/iteration? 🤔

e.g. is the implementation in this PR and grafana/grafana#91837 ok? with the understanding that i can add support in loki/influx/elastic datasources in follow up PRs. given they all should support the regex operator it should be trivial to do something similar as what we've done for prometheus.

Copy link
Member

@dprokop dprokop left a comment

Choose a reason for hiding this comment

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

@ashharrison90 - really great progress on this. I've left couple of comments in the code, and here are some more UX/UI related things:

  1. With multi value filter the filter is applied as soon as you choose an individual value currently. This results in queries being performed as you build the filter. I think we need to align this behavior with multi value variable so that the queries are executed when the user selected all desired values. That would be on filter value input blur.
  2. Removing individual value by clicking (x) on the pill results in the filter value popover being shown - i think we need to stop the events propagation.

@Sergej-Vlasov - please keep an eyer and review this PR as we need to integrate this work with the work you are doing in #830

},
closeMenuOnSelect: false,
onChange: (v: SelectableValue) => {
const updatedValue = v.map((option: SelectableValue<string>) => option.value).join('__gfp__');
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about keeping the value as a regex string rather than joining individual values with a special set of characters? I think that would be more intuitive.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

tbf i don't think this even really gets used at all, since the datasource looks for the presence of values...

we could just set it to the first value in the values array? 🤔

we could also make it the regex string. i guess that would mean we have to join the values with | but also regex escape the individual values

@ashharrison90 ashharrison90 added minor Increment the minor version when merged release Create a release when this pr is merged labels Aug 27, 2024
Copy link
Contributor

@bfmatei bfmatei left a comment

Choose a reason for hiding this comment

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

I tested this through grafana/grafana#91837 and it seems to work fine

Copy link
Collaborator

@mdvictor mdvictor left a comment

Choose a reason for hiding this comment

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

LGTM!

@ashharrison90 ashharrison90 merged commit 1eacb00 into main Aug 29, 2024
3 checks passed
@ashharrison90 ashharrison90 deleted the ash/one-of-operator branch August 29, 2024 15:16
@grafanabot
Copy link
Contributor

🚀 PR was released in v5.11.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 type/enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants