Skip to content

Commit 56e9d41

Browse files
authored
Update query project when switching datasources (#41)
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.
1 parent e17f877 commit 56e9d41

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

src/components/QueryEditor.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ export class QueryEditor extends PureComponent<Props, State> {
1818
isOptionsOpen: false,
1919
};
2020

21+
componentDidMount(): void {
22+
const { query, datasource, onChange, onRunQuery } = this.props;
23+
const projects = datasource.projects();
24+
25+
// onMount the editor must validate the configured query projectName is part
26+
// of the configured datasource's configured projects
27+
// nb: This is a required check for users with multiple instances of the
28+
// plugin installed, switching between them needs to also update the query
29+
// project name value
30+
if (!projects.includes(query.projectName)) {
31+
onChange({ ...query, projectName: datasource.defaultProjectName() });
32+
onRunQuery();
33+
}
34+
}
35+
2136
onQueryChange = (value: string) => {
2237
const { onChange, query } = this.props;
2338

src/datasource.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ export class DataSource extends DataSourceApi<LightstepQuery, LightstepDataSourc
3737
async query(request: DataQueryRequest<LightstepQuery>): Promise<DataQueryResponse> {
3838
try {
3939
const hashedEmail = await hashEmail(config.bootData.user.email);
40+
const projects = this.projects();
4041

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

0 commit comments

Comments
 (0)