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

AdHocFiltersVariable: provide updateFilters method to allow updating filters without emitting SceneVariableValueChangedEvent #1004

Merged
merged 10 commits into from
Dec 20, 2024

Conversation

gtk-grafana
Copy link
Contributor

@gtk-grafana gtk-grafana commented Dec 16, 2024

Use case: We have 2 ad-hoc variables at different scopes in the application, an "editing" variable at a lower scope, and a "submitted" variable at a higher scope. Both variables are interpolated by certain query providers. Our un-interpolated expression would look like this: ${AD_HOC_SUBMITTED} ${AD_HOC_EDITING}. The submitted variable is rendered on the top scene, and on every route, but the "editing" variable only exists for a single route. Both variables are rendered in different areas of the UI.

When users are done configuring the lower scope variable, it is submitted, and moved to the higher scope variable. The value in the editing variable is deleted.

However since the SceneVariableValueChangedEvent is emitted whenever an individual ad-hoc variable's expression has changed, this triggers duplicate queries in our application, even though the final interpolated expression doesn't change.

Usage:

// Move filter up
submittedVariable.updateFilters([ {key: 'key', operator: '=', value: 'val'} ], {skipPublish: true});

// Delete lower variable filters
editingVariable.updateFilters([], {
  skipPublish: true
});

Why this change:
Otherwise applications will either need to live with running duplicate queries, extend the base AdHocFiltersVariable class, or convert every query provider that consumes these variables to manual execution.

TL;DR:
This PR will allow the caller of updateFilters to override the default behavior and skip sending the SceneVariableValueChangedEvent, to allow setting the ad-hoc filters state without triggering queries.

Release notes

New AdHocFiltersVariable method updateFilters to allow updating filters state. Allows skipping emit of SceneVariableValueChangedEvent to prevent filter changes from notifying dependent scene objects.

📦 Published PR as canary version: 5.36.0--canary.1004.12431956361.0

✨ Test out this PR locally via:

npm install @grafana/[email protected]
npm install @grafana/[email protected]
# or 
yarn add @grafana/[email protected]
yarn add @grafana/[email protected]

@gtk-grafana gtk-grafana self-assigned this Dec 16, 2024
@gtk-grafana gtk-grafana added release Create a release when this pr is merged minor Increment the minor version when merged labels Dec 16, 2024
@gtk-grafana gtk-grafana changed the title getAdhocFilters: optionally allow setState to avoid emitting SceneVariableValueChangedEvent getAdhocFilters: optionally allow setState to skip emitting SceneVariableValueChangedEvent Dec 16, 2024
@gtk-grafana gtk-grafana marked this pull request as ready for review December 16, 2024 20:03
@gtk-grafana gtk-grafana requested a review from torkelo December 16, 2024 20:03
@dprokop dprokop requested review from a team, Sergej-Vlasov and kaydelaney and removed request for a team December 18, 2024 09:44
@gtk-grafana gtk-grafana requested a review from dprokop December 18, 2024 12:58
@gtk-grafana gtk-grafana changed the title getAdhocFilters: optionally allow setState to skip emitting SceneVariableValueChangedEvent AdHocFiltersVariable: add updateFilters method to allow updating filters without emitting SceneVariableValueChangedEvent Dec 18, 2024
@gtk-grafana gtk-grafana changed the title AdHocFiltersVariable: add updateFilters method to allow updating filters without emitting SceneVariableValueChangedEvent AdHocFiltersVariable: provide updateFilters method to allow updating filters without emitting SceneVariableValueChangedEvent Dec 18, 2024
* If `skipPublish` option is true, this will not emit the `SceneVariableValueChangedEvent`,
* allowing consumers to update the filters without triggering dependent data providers.
*/
public updateFilters(
Copy link
Contributor

Choose a reason for hiding this comment

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

Wondering if we should adjust updateFilters to:

 public updateFilters(
    update: Partial<AdHocFiltersVariableState>,
    options?: {
      skipPublish?: boolean;
    }
  )....

and then reuse that in the setState so we don't get out of sync on functions and have a single source of truth for updating

  public setState(update: Partial<AdHocFiltersVariableState>): void {
    this.updateFilters(update);
  }

Copy link
Contributor Author

@gtk-grafana gtk-grafana Dec 19, 2024

Choose a reason for hiding this comment

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

Good catch, this change is implemented in cdffe34

Copy link
Member

@dprokop dprokop Dec 20, 2024

Choose a reason for hiding this comment

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

I disagree with this. updateFilters should update filters, not entire state of the object. What i think is needed here is to remove the setState overload all together. If someone want's to update filters (and control behavior of such updatr), then we have an API for that. If someone wants to update the state, then they can rely on the base class's setState (without side effect of the event being published). This is a breaking change, hence i suggested adding release notes :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @dprokop, the setState overload has been reverted, but I kept the option to force the publish in the updateFilters.

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.

LGTM. I think the setState method overload in filters should be completely removed, it currently duplicates the updateFilters, but without the options.

Since we have the updateFilters function, it's this one that should be one for updating that particular state. The setState is inherited by default from SceneObjectBase and it should simply update the state with no effects.

I approve this work to not block it during xmas, but I think we can do the removal in this PR given that we are notifying about this change in the release notes.

@gtk-grafana
Copy link
Contributor Author

gtk-grafana commented Dec 20, 2024

@dprokop if we want to update and use this PR to introduce that breaking change after the holidays that's fine with me, I'm personally not really blocked as I extended the base AdHocFiltersVariable in Explore Logs to add this new updateFilters method until we can upgrade to a version of scenes that supports this functionality.

To make sure I'm understanding your request, the setState method on AdHocFiltersVariable should be completely removed, and everything should be handled in updateFilters? Then updateFilters can call this.setState instead of calling the SceneObjectBase super.setState?

@gtk-grafana
Copy link
Contributor Author

I lied, I do need this to get unblocked, we can follow this up in another PR.

@gtk-grafana gtk-grafana merged commit 0788102 into main Dec 20, 2024
5 checks passed
@gtk-grafana gtk-grafana deleted the gtk-grafana/ad-hoc-variable-skip-publish-on-setstate branch December 20, 2024 20:40
@scenes-repo-bot-access-token
Copy link

🚀 PR was released in v5.36.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.

4 participants