CloudWise-Lukemiao commented on code in PR #109:
URL: https://github.com/apache/iotdb-extras/pull/109#discussion_r3329060878


##########
connectors/grafana-plugin/src/datasource.ts:
##########
@@ -30,16 +30,81 @@ 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;
+                if (val === '$__all') {
+                  const allVars = templateSrv.getVariables() as any[];
+                  const found = allVars.find((v: any) => v.name === varName);
+                  if (found && found.options) {

Review Comment:
   Missing array type check for found.options.



##########
connectors/grafana-plugin/src/datasource.ts:
##########
@@ -30,16 +30,81 @@ 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;
+                if (val === '$__all') {
+                  const allVars = templateSrv.getVariables() as any[];
+                  const found = allVars.find((v: any) => v.name === varName);
+                  if (found && found.options) {
+                    values = found.options
+                      .filter((o: any) => o.value !== '$__all')
+                      .map((o: any) => o.value);
+                  }
+                } else {
+                  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')

Review Comment:
   suggest:o?.value



##########
connectors/grafana-plugin/src/datasource.ts:
##########
@@ -30,16 +30,81 @@ 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;

Review Comment:
   Missing null/undefined checks,suggest:scopedVars[varName]?.value;



##########
connectors/grafana-plugin/src/datasource.ts:
##########
@@ -30,16 +30,81 @@ 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;
+                if (val === '$__all') {
+                  const allVars = templateSrv.getVariables() as any[];
+                  const found = allVars.find((v: any) => v.name === varName);
+                  if (found && found.options) {
+                    values = found.options
+                      .filter((o: any) => o.value !== '$__all')
+                      .map((o: any) => o.value);
+                  }
+                } else {
+                  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

Review Comment:
   Missing array type check for found.options.



##########
connectors/grafana-plugin/src/datasource.ts:
##########
@@ -30,16 +30,81 @@ 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;
+                if (val === '$__all') {
+                  const allVars = templateSrv.getVariables() as any[];
+                  const found = allVars.find((v: any) => v.name === varName);
+                  if (found && found.options) {
+                    values = found.options
+                      .filter((o: any) => o.value !== '$__all')
+                      .map((o: any) => o.value);
+                  }
+                } else {
+                  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

Review Comment:
    Missing array type check for found.options. suggest:const options = 
found.options || [];



##########
connectors/grafana-plugin/src/datasource.ts:
##########
@@ -30,16 +30,81 @@ 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;
+                if (val === '$__all') {
+                  const allVars = templateSrv.getVariables() as any[];
+                  const found = allVars.find((v: any) => v.name === varName);
+                  if (found && found.options) {
+                    values = found.options
+                      .filter((o: any) => o.value !== '$__all')

Review Comment:
   Missing null/undefined checks for o.value.suggest:o?.value



##########
connectors/grafana-plugin/src/datasource.ts:
##########
@@ -30,16 +30,81 @@ 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;
+                if (val === '$__all') {
+                  const allVars = templateSrv.getVariables() as any[];
+                  const found = allVars.find((v: any) => v.name === varName);
+                  if (found && found.options) {
+                    values = found.options
+                      .filter((o: any) => o.value !== '$__all')
+                      .map((o: any) => o.value);
+                  }
+                } else {
+                  values = Array.isArray(val) ? val : [String(val)];
+                }
+              } else {
+                const allVars = templateSrv.getVariables() as any[];
+                const found = allVars.find((v: any) => v.name === varName);

Review Comment:
   suggest:v?.name===varName



-- 
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]

Reply via email to