Skip to content

Commit

Permalink
Update query project when switching datasources (#41)
Browse files Browse the repository at this point in the history
Adds checks that the configured query projectName matches the current datasource instance projectName configuration. This resolves two current issues:

1. Switching between instances of the plugin when editing a query currently fails to update the query's project name to match.
2. Using a template variable for the query datasource currently fails to update the query's project name to match.
  • Loading branch information
DHedgecock authored Sep 18, 2023
1 parent e17f877 commit 56e9d41
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/components/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ export class QueryEditor extends PureComponent<Props, State> {
isOptionsOpen: false,
};

componentDidMount(): void {
const { query, datasource, onChange, onRunQuery } = this.props;
const projects = datasource.projects();

// onMount the editor must validate the configured query projectName is part
// of the configured datasource's configured projects
// nb: This is a required check for users with multiple instances of the
// plugin installed, switching between them needs to also update the query
// project name value
if (!projects.includes(query.projectName)) {
onChange({ ...query, projectName: datasource.defaultProjectName() });
onRunQuery();
}
}

onQueryChange = (value: string) => {
const { onChange, query } = this.props;

Expand Down
7 changes: 5 additions & 2 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ export class DataSource extends DataSourceApi<LightstepQuery, LightstepDataSourc
async query(request: DataQueryRequest<LightstepQuery>): Promise<DataQueryResponse> {
try {
const hashedEmail = await hashEmail(config.bootData.user.email);
const projects = this.projects();

// All queries _should_ have a project name, this decoration _ensures_ it
// Project name check: Ensure that every query has a projectName defined, and that
// the defined value is in the current datasource's configured set of projects.
// nb: This is a required check when users have setup a datasource template variable
request.targets.forEach((target) => {
if (!target.projectName) {
if (!projects.includes(target.projectName)) {
target.projectName = this.defaultProjectName();
}
});
Expand Down

0 comments on commit 56e9d41

Please sign in to comment.