jixuan1989 commented on code in PR #109:
URL: https://github.com/apache/iotdb-extras/pull/109#discussion_r3292441092
##########
connectors/grafana-plugin/src/datasource.ts:
##########
@@ -30,16 +30,70 @@ export class DataSource extends
DataSourceWithBackend<IoTDBQuery, IoTDBOptions>
this.username = instanceSettings.jsonData.username;
}
applyTemplateVariables(query: IoTDBQuery, scopedVars: ScopedVars) {
- if (query.sqlType === 'SQL: Full Customized') {
+ if (!query.sqlType || query.sqlType === 'SQL: Full Customized') {
if (query.expression) {
query.expression.map(
(_, index) => (query.expression[index] =
getTemplateSrv().replace(query.expression[index], scopedVars))
);
}
if (query.prefixPath) {
- query.prefixPath.map(
- (_, index) => (query.prefixPath[index] =
getTemplateSrv().replace(query.prefixPath[index], scopedVars))
- );
+ const expanded: string[] = [];
+ const templateSrv = getTemplateSrv();
+ const varPattern = /\$\{(\w+)(?::[^}]*)?\}|\$(\w+)\b/;
+ for (const path of query.prefixPath) {
+ if (varPattern.test(path)) {
+ const varMatch = path.match(/\$\{(\w+)(?::[^}]*)?\}|\$(\w+)\b/);
+ if (varMatch) {
+ const varName = varMatch[1] || varMatch[2];
+ const idx = varMatch.index!;
+ const prefix = path.substring(0, idx);
+ const suffix = path.substring(idx + varMatch[0].length);
+ let values: string[] = [];
+ if (scopedVars && scopedVars[varName]) {
+ const val = scopedVars[varName].value;
+ values = Array.isArray(val) ? val : [String(val)];
+ } else {
+ const allVars = templateSrv.getVariables() as any[];
Review Comment:
Fixed in commit `04db426`. When `scopedVars[varName].value === '$__all'`, we
now look up the variable's options list via `getVariables()` and expand to all
non-$__all values. Added test: "should expand $__all from scopedVars using
options list".
##########
connectors/grafana-plugin/src/datasource.ts:
##########
@@ -30,16 +30,70 @@ export class DataSource extends
DataSourceWithBackend<IoTDBQuery, IoTDBOptions>
this.username = instanceSettings.jsonData.username;
}
applyTemplateVariables(query: IoTDBQuery, scopedVars: ScopedVars) {
- if (query.sqlType === 'SQL: Full Customized') {
+ if (!query.sqlType || query.sqlType === 'SQL: Full Customized') {
if (query.expression) {
query.expression.map(
(_, index) => (query.expression[index] =
getTemplateSrv().replace(query.expression[index], scopedVars))
);
}
if (query.prefixPath) {
- query.prefixPath.map(
- (_, index) => (query.prefixPath[index] =
getTemplateSrv().replace(query.prefixPath[index], scopedVars))
- );
+ const expanded: string[] = [];
+ const templateSrv = getTemplateSrv();
+ const varPattern = /\$\{(\w+)(?::[^}]*)?\}|\$(\w+)\b/;
+ for (const path of query.prefixPath) {
+ if (varPattern.test(path)) {
+ const varMatch = path.match(/\$\{(\w+)(?::[^}]*)?\}|\$(\w+)\b/);
+ if (varMatch) {
+ const varName = varMatch[1] || varMatch[2];
+ const idx = varMatch.index!;
+ const prefix = path.substring(0, idx);
+ const suffix = path.substring(idx + varMatch[0].length);
+ let values: string[] = [];
+ if (scopedVars && scopedVars[varName]) {
+ const val = scopedVars[varName].value;
+ values = Array.isArray(val) ? val : [String(val)];
+ } else {
+ const allVars = templateSrv.getVariables() as any[];
+ const found = allVars.find((v: any) => v.name === varName);
+ if (found) {
+ const current = found.current;
+ if (current) {
+ if (Array.isArray(current.value)) {
+ values = current.value.filter((v: string) => v !==
'$__all');
+ if (values.length === 0 && found.options) {
+ values = found.options
+ .filter((o: any) => o.value !== '$__all')
+ .map((o: any) => o.value);
+ }
+ } else if (current.value === '$__all') {
+ if (found.options) {
+ values = found.options
+ .filter((o: any) => o.value !== '$__all')
+ .map((o: any) => o.value);
+ }
+ } else {
+ values = [current.value];
+ }
+ }
+ }
+ if (values.length === 0) {
+ values = [templateSrv.replace(varMatch[0], scopedVars)];
+ }
Review Comment:
Fixed in commit `04db426`. The fallback now uses `templateSrv.replace(path,
scopedVars)` on the whole path (not just the token), producing a single entry
that preserves prior behavior when the variable cannot be resolved.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]