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

GroupBy: Fetch options when opening menu #687

Merged
merged 8 commits into from
Apr 22, 2024
Merged

GroupBy: Fetch options when opening menu #687

merged 8 commits into from
Apr 22, 2024

Conversation

ashharrison90
Copy link
Contributor

@ashharrison90 ashharrison90 commented Apr 10, 2024

i think this does what we want, but leaving as draft for now to check the approach before adding tests etc

what this does:

  • takes the rendering logic from VariableValueSelectMulti
  • enhances with isFetchingOptions and isOptionsOpen logic controlled by onOpenMenu/onCloseMenu
  • fetches and waits for new options when opening the menu via model.validateAndUpdate()

some questions:

  • does this logic only apply to group by, or would we want it for any multi value variable?
    • if so, could move this new logic back into VariableValueSelectMulti 🤔

Fixes #686

📦 Published PR as canary version: 4.11.2--canary.687.8737546972.0

✨ Test out this PR locally via:

npm install @grafana/scenes@4.11.2--canary.687.8737546972.0
# or 
yarn add @grafana/scenes@4.11.2--canary.687.8737546972.0

@ashharrison90 ashharrison90 self-assigned this Apr 10, 2024
@dprokop dprokop requested a review from torkelo April 15, 2024 10:49
@dprokop
Copy link
Member

dprokop commented Apr 17, 2024

@ashharrison90 - I've made a couple of changes to this PR. In particular:

  • I've added isLazy to SceneVariableState. Thanks to that and the change in SceneVariableSet the GroupBy values are not fetched when the picker renders. Overall this property needs to be added to other variables as well, but this is a bit bigger endeavour and won't address it here. We are tracking it in Variables: Allow lazy loading of variable values #214
  • I've added tests to the code.
  • I've also included a followup fix to Filters/GroupBy: Resolve queries only from active query runners #685 as that one did not respect the fact, that there may be inactive runners that we still want the queries from- i.e. for panels below the fold. Overall, I have implemented sort of core dashboard specific code, that ignres inactive runners with the same key. This reflects a scenario when we clone a panel in panel edit - the key of the SceneQueryRunner is the same, but the original one is not active. It's probably not the most elegant solution, but nothing else came to my mind - I'm open for any other ideas.

@torkelo - I would appreciate your eyes on this too, as I'm touching on the isLazy we discussed and introducing it in the interface for the future use.

@dprokop dprokop marked this pull request as ready for review April 17, 2024 14:51
@dprokop dprokop requested a review from bfmatei April 17, 2024 14:51
@dprokop dprokop added patch Increment the patch version when merged release Create a release when this pr is merged labels Apr 17, 2024
@@ -14,6 +14,7 @@ export interface SceneVariableState extends SceneObjectState {
loading?: boolean;
error?: any | null;
description?: string | null;
isLazy?: boolean;
Copy link
Member

Choose a reason for hiding this comment

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

Thinking about putting this in the SceneVariable interface rather than in the SceneVariableState, don't think that would ever be needed to dynamically modify the nature of a variable during runtime. Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

@dprokop would we not need it in state (to make it an persisted & UI option for QueryVariable?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it would, but... I think we can lift it to the public variable API (SceneVariableState) when we work on the general problem of variables lazy loading (ref #214). I'm afraid putting it in the state right now might be confusing for consumers, as this is now an exclusive feature of Group By. I.e. configuring Query variable as lazy now makes it not usable.

@@ -67,7 +67,7 @@ export class SceneVariableSet extends SceneObjectBase<SceneVariableSetState> imp

// Add all variables that need updating to queue
for (const variable of this.state.variables) {
if (this._variableNeedsUpdate(variable)) {
if (!variable.state.isLazy && this._variableNeedsUpdate(variable)) {
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 move this !variable.state.isLazy condition to _variableNeedsUpdate ?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I think we can. I initially thought this is called in update code paths, but it's called only for variables initialisation so it's OK to move it there. Addressed in 4a5f0db

Comment on lines +185 to +208
const { value, key, maxVisibleValues, noValueOnClear } = model.useState();
const arrayValue = useMemo(() => (isArray(value) ? value : [value]), [value]);
const [isFetchingOptions, setIsFetchingOptions] = useState(false);
const [isOptionsOpen, setIsOptionsOpen] = useState(false);

// To not trigger queries on every selection we store this state locally here and only update the variable onBlur
const [uncommittedValue, setUncommittedValue] = useState(arrayValue);

// Detect value changes outside
useEffect(() => {
setUncommittedValue(arrayValue);
}, [arrayValue]);

const onInputChange = model.onSearchChange
? (value: string, meta: InputActionMeta) => {
if (meta.action === 'input-change') {
model.onSearchChange!(value);
}
}
: undefined;

const placeholder = 'Select value';

return (
Copy link
Member

Choose a reason for hiding this comment

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

Any specific reason not doing this in the renderSelectForVariable? limit potential for bugs?

Copy link
Member

Choose a reason for hiding this comment

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

Similar to my comment above - i think we need to tackle this with #214 as currently the selects rendered for variables do not have onOpenMenu handler implemented. I've tried to change it so thast it uses the renderSelectForVariable but this is a bigger effort that we need to address both in SceneVariableSet and the UI for variables.

Copy link
Member

Choose a reason for hiding this comment

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

but this is a bigger effort that we need to address both in SceneVariableSet and the UI for variables.

what more changes are needed in SceneVariableSet and UI for variables?

Copy link
Contributor Author

@ashharrison90 ashharrison90 left a comment

Choose a reason for hiding this comment

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

feels weird reviewing a PR where i'm marked as author 😂

think this looks good 👍

@dprokop dprokop requested a review from torkelo April 18, 2024 12:06
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.

lgtm 🚀

@ashharrison90 ashharrison90 merged commit f0214ee into main Apr 22, 2024
3 checks passed
@ashharrison90 ashharrison90 deleted the ash/686 branch April 22, 2024 09:12
@grafanabot
Copy link
Contributor

🚀 PR was released in v4.11.2 🚀

@ashharrison90
Copy link
Contributor Author

delivered in core with grafana/grafana#86671

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.

GroupBy variable: It may not receive the latest set of queries available in a scene
5 participants