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

Datasources: Propagate TestDataDB default selection to PanelModel #57703

Closed
wants to merge 10 commits into from

Conversation

mmandrus
Copy link
Contributor

What is this feature?
Currently, when a TestDataDB datasource is selected, it will automatically create a Random Walk scenario based on a frontend. However, this doesn't get set in the dashboard JSON itself, and actually relies on the backend to have the same default. As a result, in public dashboard mode, there is no query json and the panel can't render anything.

Why do we need this feature?
Not only does this fix a bug in public dashboards, it ensures that we don't have to have matching default behaviors on the front and back ends of the app.

Which issue(s) does this PR fix?:

Fixes #57702

@mmandrus mmandrus added no-backport Skip backport of PR team/operator-experience no-changelog Skip including change in changelog/release notes area/public-dashboards labels Oct 26, 2022
@mmandrus mmandrus added this to the 9.3.0 milestone Oct 26, 2022
Copy link
Contributor

@jalevin jalevin left a comment

Choose a reason for hiding this comment

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

Hard won fix! Way to be persistent. Nice work

@mmandrus
Copy link
Contributor Author

Hard won fix! Way to be persistent. Nice work

😩

@grafanabot
Copy link
Contributor

@grafanabot
Copy link
Contributor

@mmandrus mmandrus requested a review from a team as a code owner October 27, 2022 01:34
@mmandrus mmandrus requested review from joshhunt and JoaoSilvaGrafana and removed request for a team October 27, 2022 01:34
@grafanabot
Copy link
Contributor

@grafanabot
Copy link
Contributor

Copy link
Member

@torkelo torkelo left a comment

Choose a reason for hiding this comment

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

Calling onChange onMount is not great (it has caused various issues in the past) so are strongly discouraged, ideally, we should have a better way to set query defaults. But this ability has been lacking for a long time :(

@sunker did you explore ways to do this before? I know you have hit this problem many times

@sunker
Copy link
Contributor

sunker commented Oct 27, 2022

@mmandrus you can specify a default query using the getDefaultQuery method in the datasource api. That way you don't have to call onChange when the editor is mounted the first time you select the data source.

@mmandrus
Copy link
Contributor Author

mmandrus commented Oct 27, 2022

@torkelo @sunker I am glad it is not just me who struggled with this! Thank you very much for the pointer. There were no examples to go off of, but this should do the trick. Please have a look and let me know if this is what you had in mind (ignore the e2e tests for now please).

@mmandrus mmandrus requested a review from torkelo October 27, 2022 18:30
if (!query.datasource && this.datasource) {
query.datasource = this.datasource;
}
if (Object.keys(query).length === 2) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is based on the combination of the above if statement, plus the ensureQueryIds() function. If the only two properties on the query are the ones we are hard-coding in, the query is not well-formed. If there is a better way of inferring this, let me know.

@grafanabot
Copy link
Contributor

@mmandrus
Copy link
Contributor Author

mmandrus commented Oct 27, 2022

@torkelo one more try... now it takes care of all the default query handling at query creation time.

Thanks for your patience as I learn my way around the front-end codebase!

@grafanabot
Copy link
Contributor

@mmandrus
Copy link
Contributor Author

Awaiting approval before I address build issues.

@mmandrus mmandrus requested a review from torkelo October 28, 2022 14:43
Comment on lines +264 to +277
getDefaultQuery(app: CoreApp): TestDataQuery {
switch (app) {
// Ignore the app input for now, always use the same default query
case CoreApp.CloudAlerting:
case CoreApp.UnifiedAlerting:
case CoreApp.Dashboard:
case CoreApp.Explore:
case CoreApp.PanelEditor:
case CoreApp.PanelViewer:
case CoreApp.Unknown:
default:
return defaultQuery;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
getDefaultQuery(app: CoreApp): TestDataQuery {
switch (app) {
// Ignore the app input for now, always use the same default query
case CoreApp.CloudAlerting:
case CoreApp.UnifiedAlerting:
case CoreApp.Dashboard:
case CoreApp.Explore:
case CoreApp.PanelEditor:
case CoreApp.PanelViewer:
case CoreApp.Unknown:
default:
return defaultQuery;
}
}
getDefaultQuery(app: CoreApp): TestDataQuery {
return defaultQuery;
}

Comment on lines +99 to +103
if (changedQueries) {
this.onChange({
queries
});
}
Copy link
Member

Choose a reason for hiding this comment

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

This is basically the same as calling onChange onMount in the query editor, you could maybe refactor out to a function that can also be used from runRequest? Then we can detect and perform this logic before issuing requests as well. And then we don't need to call this.onChange (which should be preserved for use-initiated change)

Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need to call onChange? If not I think this can be simplified a bit

  const queries = options.queries.map((q) => {
        if (!q.datasource) {
          q = { ...q, datasource };
        }
        if (Object.keys(q).length === 2) {
          q = { ...this.newQuery(ds, dsSettings), ...q };
        }
        return q;
      });
      this.setState({ queries, dataSource: ds, dsSettings, defaultDataSource });

Think we may also need to set default query in [updateQueries](https://github.com/grafana/grafana/blob/mmandrus/fix-testdatadb-default-selection/public/app/features/query/state/updateQueries.ts#L30-L36) too. Otherwise, this won't work when changing the data source.

@leandro-deveikis leandro-deveikis modified the milestones: 9.3.0, 9.3.0-beta1 Nov 14, 2022
@grafanabot grafanabot removed this from the 9.3.0-beta1 milestone Nov 15, 2022
@grafanabot
Copy link
Contributor

This pull request was removed from the 9.3.0-beta1 milestone because 9.3.0-beta1 is currently being released.

@mmandrus
Copy link
Contributor Author

mmandrus commented Dec 6, 2022

Closing, fixed by #59625

@mmandrus mmandrus closed this Dec 6, 2022
@mmandrus mmandrus deleted the mmandrus/fix-testdatadb-default-selection branch April 14, 2023 02:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix TestDataDB query initialization so that it works with public dashboards
7 participants