Copilot commented on code in PR #109:
URL: https://github.com/apache/iotdb-extras/pull/109#discussion_r3286060124


##########
connectors/grafana-plugin/src/datasource.ts:
##########
@@ -37,9 +37,19 @@ export class DataSource extends 
DataSourceWithBackend<IoTDBQuery, IoTDBOptions>
         );
       }
       if (query.prefixPath) {
-        query.prefixPath.map(
-          (_, index) => (query.prefixPath[index] = 
getTemplateSrv().replace(query.prefixPath[index], scopedVars))
-        );
+        const expanded: string[] = [];
+        for (const path of query.prefixPath) {
+          if (getTemplateSrv().containsTemplate(path)) {
+            const replaced = getTemplateSrv().replace(path, scopedVars, 
'pipe');
+            const values = replaced.split('|');
+            for (const val of values) {
+              expanded.push(val);

Review Comment:
   Current prefixPath expansion splits the fully replaced string on '|'. With 
Grafana's `pipe` formatter, only the *variable* portion is pipe-joined, so e.g. 
`root.app.${device}` commonly becomes `root.app.device1|device2`, and this 
logic would incorrectly produce `['root.app.device1', 'device2']` (invalid 
path). Expand by substituting the variable values back into the original 
template instead of splitting the whole path string.



##########
connectors/grafana-plugin/src/datasource.test.ts:
##########
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+import { DataSource } from './datasource';
+import { IoTDBQuery } from './types';
+import { ScopedVars } from '@grafana/data';
+
+const mockReplace = jest.fn();
+const mockContainsTemplate = jest.fn();
+
+jest.mock('@grafana/runtime', () => ({
+  DataSourceWithBackend: class {},
+  getTemplateSrv: () => ({
+    replace: mockReplace,
+    containsTemplate: mockContainsTemplate,
+  }),
+}));
+
+describe('DataSource', () => {
+  let ds: DataSource;
+
+  beforeEach(() => {
+    ds = new DataSource({ jsonData: { url: 'http://localhost:6667', username: 
'root' } } as any);
+    mockReplace.mockReset();
+    mockContainsTemplate.mockReset();
+  });
+
+  describe('applyTemplateVariables - prefixPath expansion', () => {
+    const baseQuery: Partial<IoTDBQuery> = {
+      sqlType: 'SQL: Full Customized',
+      expression: [],
+      prefixPath: [],
+      condition: '',
+      control: '',
+    };
+    const scopedVars: ScopedVars = {};
+
+    it('should pass through literal paths without variables', () => {
+      mockContainsTemplate.mockReturnValue(false);
+      const query = { ...baseQuery, prefixPath: ['root.app.device1', 
'root.app.device2'] } as IoTDBQuery;
+
+      const result = ds.applyTemplateVariables(query, scopedVars);
+
+      expect(result.prefixPath).toEqual(['root.app.device1', 
'root.app.device2']);
+      expect(mockReplace).not.toHaveBeenCalled();
+    });
+
+    it('should handle single-value variable without expansion', () => {
+      mockContainsTemplate.mockReturnValue(true);
+      mockReplace.mockReturnValue('root.app.device1');
+      const query = { ...baseQuery, prefixPath: ['root.app.${device}'] } as 
IoTDBQuery;
+
+      const result = ds.applyTemplateVariables(query, scopedVars);
+
+      expect(result.prefixPath).toEqual(['root.app.device1']);
+      expect(mockReplace).toHaveBeenCalledWith('root.app.${device}', 
scopedVars, 'pipe');
+    });
+
+    it('should expand multi-value variable into multiple paths', () => {
+      mockContainsTemplate.mockReturnValue(true);
+      
mockReplace.mockReturnValue('root.app.device1|root.app.device2|root.app.device3');
+      const query = { ...baseQuery, prefixPath: ['root.app.${device}'] } as 
IoTDBQuery;
+
+      const result = ds.applyTemplateVariables(query, scopedVars);
+
+      expect(result.prefixPath).toEqual(['root.app.device1', 
'root.app.device2', 'root.app.device3']);
+    });
+
+    it('should handle mixed literal and template paths', () => {
+      mockContainsTemplate.mockImplementation((path: string) => 
path.includes('${'));
+      mockReplace.mockReturnValue('root.app.device1|root.app.device2');
+      const query = {
+        ...baseQuery,
+        prefixPath: ['root.static.path', 'root.app.${device}'],
+      } as IoTDBQuery;
+
+      const result = ds.applyTemplateVariables(query, scopedVars);
+
+      expect(result.prefixPath).toEqual(['root.static.path', 
'root.app.device1', 'root.app.device2']);
+    });

Review Comment:
   Same issue as above: this test's mocked `pipe` output 
(`root.app.device1|root.app.device2`) doesn't match how Grafana typically 
formats multi-values inside a larger string. Using a realistic mock 
(`root.app.device1|device2`) will prevent the test from passing with an invalid 
expanded path list.
   



##########
connectors/grafana-plugin/src/datasource.test.ts:
##########
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+import { DataSource } from './datasource';
+import { IoTDBQuery } from './types';
+import { ScopedVars } from '@grafana/data';
+
+const mockReplace = jest.fn();
+const mockContainsTemplate = jest.fn();
+
+jest.mock('@grafana/runtime', () => ({
+  DataSourceWithBackend: class {},
+  getTemplateSrv: () => ({
+    replace: mockReplace,
+    containsTemplate: mockContainsTemplate,
+  }),
+}));
+
+describe('DataSource', () => {
+  let ds: DataSource;
+
+  beforeEach(() => {
+    ds = new DataSource({ jsonData: { url: 'http://localhost:6667', username: 
'root' } } as any);
+    mockReplace.mockReset();
+    mockContainsTemplate.mockReset();
+  });
+
+  describe('applyTemplateVariables - prefixPath expansion', () => {
+    const baseQuery: Partial<IoTDBQuery> = {
+      sqlType: 'SQL: Full Customized',
+      expression: [],
+      prefixPath: [],
+      condition: '',
+      control: '',
+    };
+    const scopedVars: ScopedVars = {};
+
+    it('should pass through literal paths without variables', () => {
+      mockContainsTemplate.mockReturnValue(false);
+      const query = { ...baseQuery, prefixPath: ['root.app.device1', 
'root.app.device2'] } as IoTDBQuery;
+
+      const result = ds.applyTemplateVariables(query, scopedVars);
+
+      expect(result.prefixPath).toEqual(['root.app.device1', 
'root.app.device2']);
+      expect(mockReplace).not.toHaveBeenCalled();
+    });
+
+    it('should handle single-value variable without expansion', () => {
+      mockContainsTemplate.mockReturnValue(true);
+      mockReplace.mockReturnValue('root.app.device1');
+      const query = { ...baseQuery, prefixPath: ['root.app.${device}'] } as 
IoTDBQuery;
+
+      const result = ds.applyTemplateVariables(query, scopedVars);
+
+      expect(result.prefixPath).toEqual(['root.app.device1']);
+      expect(mockReplace).toHaveBeenCalledWith('root.app.${device}', 
scopedVars, 'pipe');
+    });
+
+    it('should expand multi-value variable into multiple paths', () => {
+      mockContainsTemplate.mockReturnValue(true);
+      
mockReplace.mockReturnValue('root.app.device1|root.app.device2|root.app.device3');
+      const query = { ...baseQuery, prefixPath: ['root.app.${device}'] } as 
IoTDBQuery;
+
+      const result = ds.applyTemplateVariables(query, scopedVars);
+
+      expect(result.prefixPath).toEqual(['root.app.device1', 
'root.app.device2', 'root.app.device3']);
+    });
+
+    it('should handle mixed literal and template paths', () => {
+      mockContainsTemplate.mockImplementation((path: string) => 
path.includes('${'));
+      mockReplace.mockReturnValue('root.app.device1|root.app.device2');
+      const query = {
+        ...baseQuery,
+        prefixPath: ['root.static.path', 'root.app.${device}'],
+      } as IoTDBQuery;
+
+      const result = ds.applyTemplateVariables(query, scopedVars);
+
+      expect(result.prefixPath).toEqual(['root.static.path', 
'root.app.device1', 'root.app.device2']);
+    });
+
+    it('should handle multiple template paths each with multi-value 
variables', () => {
+      mockContainsTemplate.mockReturnValue(true);
+      mockReplace
+        .mockReturnValueOnce('root.a.d1|root.a.d2')
+        .mockReturnValueOnce('root.b.d3|root.b.d4');
+      const query = {
+        ...baseQuery,
+        prefixPath: ['root.a.${var1}', 'root.b.${var2}'],
+      } as IoTDBQuery;
+
+      const result = ds.applyTemplateVariables(query, scopedVars);
+
+      expect(result.prefixPath).toEqual(['root.a.d1', 'root.a.d2', 
'root.b.d3', 'root.b.d4']);
+    });

Review Comment:
   Same issue as above for multiple template entries: `pipe` formatting inside 
`root.a.${var1}` would typically yield `root.a.d1|d2`, not 
`root.a.d1|root.a.d2`. Adjusting the mock makes this test validate correct 
prefixPath expansion rather than an unrealistic replace result.
   



##########
connectors/grafana-plugin/src/datasource.test.ts:
##########
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+import { DataSource } from './datasource';
+import { IoTDBQuery } from './types';
+import { ScopedVars } from '@grafana/data';
+
+const mockReplace = jest.fn();
+const mockContainsTemplate = jest.fn();
+
+jest.mock('@grafana/runtime', () => ({
+  DataSourceWithBackend: class {},
+  getTemplateSrv: () => ({
+    replace: mockReplace,
+    containsTemplate: mockContainsTemplate,
+  }),
+}));
+
+describe('DataSource', () => {
+  let ds: DataSource;
+
+  beforeEach(() => {
+    ds = new DataSource({ jsonData: { url: 'http://localhost:6667', username: 
'root' } } as any);
+    mockReplace.mockReset();
+    mockContainsTemplate.mockReset();
+  });
+
+  describe('applyTemplateVariables - prefixPath expansion', () => {
+    const baseQuery: Partial<IoTDBQuery> = {
+      sqlType: 'SQL: Full Customized',
+      expression: [],
+      prefixPath: [],
+      condition: '',
+      control: '',
+    };
+    const scopedVars: ScopedVars = {};
+
+    it('should pass through literal paths without variables', () => {
+      mockContainsTemplate.mockReturnValue(false);
+      const query = { ...baseQuery, prefixPath: ['root.app.device1', 
'root.app.device2'] } as IoTDBQuery;
+
+      const result = ds.applyTemplateVariables(query, scopedVars);
+
+      expect(result.prefixPath).toEqual(['root.app.device1', 
'root.app.device2']);
+      expect(mockReplace).not.toHaveBeenCalled();
+    });
+
+    it('should handle single-value variable without expansion', () => {
+      mockContainsTemplate.mockReturnValue(true);
+      mockReplace.mockReturnValue('root.app.device1');
+      const query = { ...baseQuery, prefixPath: ['root.app.${device}'] } as 
IoTDBQuery;
+
+      const result = ds.applyTemplateVariables(query, scopedVars);
+
+      expect(result.prefixPath).toEqual(['root.app.device1']);
+      expect(mockReplace).toHaveBeenCalledWith('root.app.${device}', 
scopedVars, 'pipe');
+    });
+
+    it('should expand multi-value variable into multiple paths', () => {
+      mockContainsTemplate.mockReturnValue(true);
+      
mockReplace.mockReturnValue('root.app.device1|root.app.device2|root.app.device3');
+      const query = { ...baseQuery, prefixPath: ['root.app.${device}'] } as 
IoTDBQuery;
+
+      const result = ds.applyTemplateVariables(query, scopedVars);
+
+      expect(result.prefixPath).toEqual(['root.app.device1', 
'root.app.device2', 'root.app.device3']);
+    });

Review Comment:
   This test mocks `replace(..., 'pipe')` as returning full paths separated by 
`|` (e.g. `root.app.device1|root.app.device2`). In Grafana, the `pipe` 
formatter typically only pipe-joins the variable values, so 
`root.app.${device}` becomes `root.app.device1|device2`. Update the mock to 
reflect this so the test would catch the current incorrect split-based 
expansion.
   



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