CritasWang commented on code in PR #134:
URL: https://github.com/apache/iotdb-extras/pull/134#discussion_r4229388563


##########
connectors/grafana-plugin/src/componments/TablePicker.tsx:
##########
@@ -0,0 +1,176 @@
+/*
+ * 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 React, { useEffect, useState } from 'react';
+import { SelectableValue } from '@grafana/data';
+import { Button, Select } from '@grafana/ui';
+import { DataSource } from '../datasource';
+import { starterTableQuery } from '../tableQuery';
+import { TableColumn } from '../types';
+import { QueryInlineField } from './Form';
+
+interface Props {
+  datasource: DataSource;
+  database: string;
+  table: string;
+  onDatabaseChange: (database: string) => void;
+  onTableChange: (table: string) => void;
+  onSqlChange: (sql: string) => void;
+}
+
+interface Loaded<T> {
+  value?: T;
+  loading: boolean;
+  error?: string;
+}
+
+const categories = ['TAG', 'FIELD', 'ATTRIBUTE'];
+
+function toOptions(names: string[] | undefined): 
Array<SelectableValue<string>> {
+  return (names ?? []).map((name) => ({ label: name, value: name }));
+}
+
+function toValue(name: string): SelectableValue<string> | null {
+  return name ? { label: name, value: name } : null;
+}
+
+function errorMessage(err: unknown): string {
+  const e = err as { data?: { message?: string }; message?: string };
+  return e?.data?.message ?? e?.message ?? String(err);
+}
+
+// useLoaded runs one metadata lookup whenever its key changes. State is only
+// written when a lookup settles, and a result is only used while its key is
+// current. A failed lookup keeps its message; the pickers still accept a typed
+// name, so the editor stays usable when metadata cannot be read.
+function useLoaded<T>(key: string, load: (() => Promise<T>) | undefined): 
Loaded<T> {
+  const [result, setResult] = useState<{ key: string; value?: T; error?: 
string }>();
+  useEffect(() => {
+    if (!load) {
+      return;
+    }
+    let cancelled = false;
+    load().then(
+      (value) => {
+        if (!cancelled) {
+          setResult({ key, value });
+        }
+      },
+      (err) => {
+        if (!cancelled) {
+          setResult({ key, error: errorMessage(err) });
+        }
+      }
+    );
+    return () => {
+      cancelled = true;
+    };
+    // The key identifies the lookup; `load` is a fresh closure on every 
render.
+    // eslint-disable-next-line react-hooks/exhaustive-deps
+  }, [key]);
+  if (!load) {
+    return { loading: false };
+  }
+  if (result?.key !== key) {
+    return { loading: true };
+  }
+  return { value: result.value, error: result.error, loading: false };
+}
+
+// TablePicker lists the databases, tables and columns of the connected server
+// for the table-model editor, and can fill in a starter query for the chosen
+// table.
+export function TablePicker({ datasource, database, table, onDatabaseChange, 
onTableChange, onSqlChange }: Props) {
+  const databases = useLoaded('databases', () => 
datasource.getTableDatabases());
+  const tables = useLoaded(`tables|${database}`, database ? () => 
datasource.getTableTables(database) : undefined);
+  const columns = useLoaded<TableColumn[]>(
+    `columns|${database}|${table}`,
+    database && table ? () => datasource.getTableColumns(database, table) : 
undefined
+  );
+  const error = databases.error ?? tables.error ?? columns.error;
+
+  return (
+    <>
+      <div className="gf-form">
+        <QueryInlineField label={'DATABASE'}>
+          {/* Select rather than Combobox: with Combobox, picking 
information_schema
+              crashed Explore on Grafana 12.3 with React error 185 raised from 
the
+              popover's floating-ui reference; Select does not. */}
+          <Select
+            inputId="iotdb-table-database"
+            width={30}
+            options={toOptions(databases.value)}
+            value={toValue(database)}

Review Comment:
   **1. Should fix: Tab or Enter can silently switch the database.**
   
   `toOptions()` and `toValue()` build new objects on every render. Grafana's 
`Select` passes an object `value` through unchanged (`cleanValue`), and 
react-select looks up the option to focus by identity 
(`focusableOptions.indexOf(selectValue[0])`). So when the menu opens, focus 
lands on the first option instead of the current database. Since 
`tabSelectsValue` defaults to `true`, clicking DATABASE and then pressing Tab 
(or Enter) selects the first database and clears the table. A re-render while 
the menu is open, such as a lookup settling, also moves focus back to the first 
option (`getNextFocusedOption` compares by identity too). The TABLE picker 
(lines 129-130) has the same problem.
   
   Memoizing the options, adding the current custom value to them, and passing 
the matching option object fixes both:
   
   ```tsx
   const databaseOptions = useMemo(() => {
     const options = toOptions(databases.value);
     return database && !options.some((o) => o.value === database)
       ? [...options, { label: database, value: database }]
       : options;
   }, [databases.value, database]);
   // options={databaseOptions}
   // value={databaseOptions.find((o) => o.value === database) ?? null}
   ```



##########
connectors/grafana-plugin/src/componments/TablePicker.tsx:
##########
@@ -0,0 +1,176 @@
+/*
+ * 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 React, { useEffect, useState } from 'react';
+import { SelectableValue } from '@grafana/data';
+import { Button, Select } from '@grafana/ui';
+import { DataSource } from '../datasource';
+import { starterTableQuery } from '../tableQuery';
+import { TableColumn } from '../types';
+import { QueryInlineField } from './Form';
+
+interface Props {
+  datasource: DataSource;
+  database: string;
+  table: string;
+  onDatabaseChange: (database: string) => void;
+  onTableChange: (table: string) => void;
+  onSqlChange: (sql: string) => void;
+}
+
+interface Loaded<T> {
+  value?: T;
+  loading: boolean;
+  error?: string;
+}
+
+const categories = ['TAG', 'FIELD', 'ATTRIBUTE'];
+
+function toOptions(names: string[] | undefined): 
Array<SelectableValue<string>> {
+  return (names ?? []).map((name) => ({ label: name, value: name }));
+}
+
+function toValue(name: string): SelectableValue<string> | null {
+  return name ? { label: name, value: name } : null;
+}
+
+function errorMessage(err: unknown): string {
+  const e = err as { data?: { message?: string }; message?: string };
+  return e?.data?.message ?? e?.message ?? String(err);
+}
+
+// useLoaded runs one metadata lookup whenever its key changes. State is only
+// written when a lookup settles, and a result is only used while its key is
+// current. A failed lookup keeps its message; the pickers still accept a typed
+// name, so the editor stays usable when metadata cannot be read.
+function useLoaded<T>(key: string, load: (() => Promise<T>) | undefined): 
Loaded<T> {
+  const [result, setResult] = useState<{ key: string; value?: T; error?: 
string }>();
+  useEffect(() => {
+    if (!load) {
+      return;
+    }
+    let cancelled = false;
+    load().then(
+      (value) => {
+        if (!cancelled) {
+          setResult({ key, value });
+        }
+      },
+      (err) => {
+        if (!cancelled) {
+          setResult({ key, error: errorMessage(err) });
+        }
+      }
+    );
+    return () => {
+      cancelled = true;
+    };
+    // The key identifies the lookup; `load` is a fresh closure on every 
render.
+    // eslint-disable-next-line react-hooks/exhaustive-deps
+  }, [key]);
+  if (!load) {
+    return { loading: false };
+  }
+  if (result?.key !== key) {
+    return { loading: true };
+  }
+  return { value: result.value, error: result.error, loading: false };
+}
+
+// TablePicker lists the databases, tables and columns of the connected server
+// for the table-model editor, and can fill in a starter query for the chosen
+// table.
+export function TablePicker({ datasource, database, table, onDatabaseChange, 
onTableChange, onSqlChange }: Props) {
+  const databases = useLoaded('databases', () => 
datasource.getTableDatabases());
+  const tables = useLoaded(`tables|${database}`, database ? () => 
datasource.getTableTables(database) : undefined);
+  const columns = useLoaded<TableColumn[]>(
+    `columns|${database}|${table}`,
+    database && table ? () => datasource.getTableColumns(database, table) : 
undefined
+  );
+  const error = databases.error ?? tables.error ?? columns.error;
+
+  return (
+    <>
+      <div className="gf-form">
+        <QueryInlineField label={'DATABASE'}>
+          {/* Select rather than Combobox: with Combobox, picking 
information_schema
+              crashed Explore on Grafana 12.3 with React error 185 raised from 
the
+              popover's floating-ui reference; Select does not. */}
+          <Select
+            inputId="iotdb-table-database"
+            width={30}
+            options={toOptions(databases.value)}
+            value={toValue(database)}
+            isLoading={databases.loading}
+            allowCustomValue

Review Comment:
   **2. Should fix: typed names are easy to lose.**
   
   The old `Input` committed every keystroke. With a creatable `Select`, typed 
text is committed only on Enter, on Tab, or by clicking the custom-value row. 
On blur, react-select clears it (`input-blur`). So typing `$database` and then 
clicking into the SQL box leaves the database unchanged, or empty, which gives 
"DATABASE is required".
   
   Separately, Grafana's `Select` defaults `allowCreateWhileLoading` to 
`false`. While SHOW DATABASES is pending (up to the 10 s connect timeout, or 
the 60 s pool wait), no custom value can be entered at all, which contradicts 
the comment at lines 57-60.
   
   Since the README now advertises typing `$database`, please pass 
`allowCreateWhileLoading` and commit the pending input on blur. react-select 
calls `onBlur` before it clears the input, so tracking the text with 
`onInputChange` and committing it in `onBlur` works. The same applies to the 
TABLE picker (lines 131-133).



##########
connectors/grafana-plugin/src/datasource.ts:
##########
@@ -181,6 +181,24 @@ export class DataSource extends 
DataSourceWithBackend<IoTDBQuery, IoTDBOptions>
       .then((data) => data.map(toMetricFindValue));
   }
 
+  // Metadata behind the table-model editor's pickers. Template variables are
+  // expanded first, so a dashboard whose database is `$database` still lists
+  // its tables.
+  getTableDatabases(): Promise<string[]> {
+    return this.getResource('tableDatabases');

Review Comment:
   **4. Should fix: failed lookups also raise a global error toast.**
   
   For a local URL that is not a data query, with `showErrorAlert` left 
undefined, backendSrv shows an alert unless the caller sets `err.isHandled` 
within 50 ms. So every failed lookup shows a red toast as well as the inline 
"Could not read metadata" row. When the RPC endpoint is unreachable, that 
happens on every editor mount. `getResource(path, params, { showErrorAlert: 
false })` is supported on 12.3.0; the same applies to the two calls below.



##########
connectors/grafana-plugin/src/tableQuery.ts:
##########
@@ -0,0 +1,45 @@
+/*
+ * 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 { TableColumn } from './types';
+
+// quoteIdentifier wraps a name in double quotes, doubling any embedded quote.
+// The names come from DESC, so quoting them never changes what they refer to,
+// and it keeps the query valid when a name is a reserved word such as "order"
+// or "group", or contains a space.
+export function quoteIdentifier(name: string): string {
+  return `"${name.replace(/"/g, '""')}"`;
+}
+
+// starterTableQuery builds a first query for a table: the time column, every
+// TAG column and every FIELD column, limited to the dashboard's time range.
+// ATTRIBUTE columns are left out because they do not change over time. The
+// TAG columns are what the Time series format uses to split the result into
+// one series per device. A table without a time column, such as the views in
+// information_schema, gets no time filter.
+export function starterTableQuery(table: string, columns: TableColumn[]): 
string {
+  const time = columns.find((c) => c.category === 'TIME')?.name;
+  const selected = [
+    ...(time ? [time] : []),
+    ...columns.filter((c) => c.category === 'TAG').map((c) => c.name),
+    ...columns.filter((c) => c.category === 'FIELD').map((c) => c.name),

Review Comment:
   **5. Should fix: the starter query in the default Time series format.**
   
   Every FIELD is selected whatever its type. In the plugin's `LongToWide` 
path, plugin-sdk-go's `TimeSeriesSchema` treats string and bool fields as 
factors. A STRING/TEXT status or message field therefore splits each device 
into many gappy series (one per row for free text). A table whose fields are 
all non-numeric returns "No data", because `hasPlottableValue` is false. The 
query also reads raw rows over the whole dashboard range with no downsampling, 
which gets heavy on large or wide tables.
   
   Please select only numeric FIELDs (INT32/INT64/FLOAT/DOUBLE); `dataType` is 
already in `TableColumn`. Optionally, generate a `date_bin($__interval, time)` 
aggregation (the plugin already expands `$__interval`), or cap the number of 
fields.



##########
connectors/grafana-plugin/src/QueryEditor.tsx:
##########
@@ -144,20 +145,28 @@ export class QueryEditor extends PureComponent<Props, 
State> {
     onChange({ ...query, groupBy: g });
   };
 
-  onDatabaseChange = (event: ChangeEvent<HTMLInputElement>) => {
+  onDatabaseChange = (database: string) => {
     const { onChange, query } = this.props;
-    const database = event.target.value;
     this.setState({ database });
-    onChange({ ...query, database });
+    // A table belongs to one database, so switching database clears it.
+    onChange({ ...query, database, table: undefined });

Review Comment:
   **Minor:** react-select fires `onChange` when the current option is picked 
again, so re-selecting the same database wipes the table. Clear it only when 
`database !== query.database`.



##########
connectors/grafana-plugin/src/componments/TablePicker.tsx:
##########
@@ -0,0 +1,176 @@
+/*
+ * 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 React, { useEffect, useState } from 'react';
+import { SelectableValue } from '@grafana/data';
+import { Button, Select } from '@grafana/ui';
+import { DataSource } from '../datasource';
+import { starterTableQuery } from '../tableQuery';
+import { TableColumn } from '../types';
+import { QueryInlineField } from './Form';
+
+interface Props {
+  datasource: DataSource;
+  database: string;
+  table: string;
+  onDatabaseChange: (database: string) => void;
+  onTableChange: (table: string) => void;
+  onSqlChange: (sql: string) => void;
+}
+
+interface Loaded<T> {
+  value?: T;
+  loading: boolean;
+  error?: string;
+}
+
+const categories = ['TAG', 'FIELD', 'ATTRIBUTE'];
+
+function toOptions(names: string[] | undefined): 
Array<SelectableValue<string>> {
+  return (names ?? []).map((name) => ({ label: name, value: name }));
+}
+
+function toValue(name: string): SelectableValue<string> | null {
+  return name ? { label: name, value: name } : null;
+}
+
+function errorMessage(err: unknown): string {
+  const e = err as { data?: { message?: string }; message?: string };
+  return e?.data?.message ?? e?.message ?? String(err);
+}
+
+// useLoaded runs one metadata lookup whenever its key changes. State is only
+// written when a lookup settles, and a result is only used while its key is
+// current. A failed lookup keeps its message; the pickers still accept a typed
+// name, so the editor stays usable when metadata cannot be read.
+function useLoaded<T>(key: string, load: (() => Promise<T>) | undefined): 
Loaded<T> {
+  const [result, setResult] = useState<{ key: string; value?: T; error?: 
string }>();
+  useEffect(() => {
+    if (!load) {
+      return;
+    }
+    let cancelled = false;
+    load().then(
+      (value) => {
+        if (!cancelled) {
+          setResult({ key, value });
+        }
+      },
+      (err) => {
+        if (!cancelled) {
+          setResult({ key, error: errorMessage(err) });
+        }
+      }
+    );
+    return () => {
+      cancelled = true;
+    };
+    // The key identifies the lookup; `load` is a fresh closure on every 
render.
+    // eslint-disable-next-line react-hooks/exhaustive-deps
+  }, [key]);
+  if (!load) {
+    return { loading: false };
+  }
+  if (result?.key !== key) {
+    return { loading: true };
+  }
+  return { value: result.value, error: result.error, loading: false };
+}
+
+// TablePicker lists the databases, tables and columns of the connected server
+// for the table-model editor, and can fill in a starter query for the chosen
+// table.
+export function TablePicker({ datasource, database, table, onDatabaseChange, 
onTableChange, onSqlChange }: Props) {
+  const databases = useLoaded('databases', () => 
datasource.getTableDatabases());
+  const tables = useLoaded(`tables|${database}`, database ? () => 
datasource.getTableTables(database) : undefined);
+  const columns = useLoaded<TableColumn[]>(
+    `columns|${database}|${table}`,
+    database && table ? () => datasource.getTableColumns(database, table) : 
undefined
+  );

Review Comment:
   **3. Should fix: metadata is not refreshed when a template variable 
changes.**
   
   The keys are built from the raw strings (`tables|$database`, 
`columns|$database|weather`), and interpolation happens only inside the loaders 
(`datasource.ts:191-198`). When the dashboard variable changes, the key stays 
the same and nothing is refetched. The TABLE options, the COLUMNS row and "Use 
starter query" keep the previous database's schema. Keying on the interpolated 
value fixes it.
   
   Smaller point: the loaders interpolate without `scopedVars`, so in repeated 
panels `$db` resolves differently from `applyTemplateVariables` (a multi-value 
variable becomes `{a,b}`).



##########
connectors/grafana-plugin/src/componments/TablePicker.tsx:
##########
@@ -0,0 +1,176 @@
+/*
+ * 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 React, { useEffect, useState } from 'react';
+import { SelectableValue } from '@grafana/data';
+import { Button, Select } from '@grafana/ui';
+import { DataSource } from '../datasource';
+import { starterTableQuery } from '../tableQuery';
+import { TableColumn } from '../types';
+import { QueryInlineField } from './Form';
+
+interface Props {
+  datasource: DataSource;
+  database: string;
+  table: string;
+  onDatabaseChange: (database: string) => void;
+  onTableChange: (table: string) => void;
+  onSqlChange: (sql: string) => void;
+}
+
+interface Loaded<T> {
+  value?: T;
+  loading: boolean;
+  error?: string;
+}
+
+const categories = ['TAG', 'FIELD', 'ATTRIBUTE'];
+
+function toOptions(names: string[] | undefined): 
Array<SelectableValue<string>> {
+  return (names ?? []).map((name) => ({ label: name, value: name }));
+}
+
+function toValue(name: string): SelectableValue<string> | null {
+  return name ? { label: name, value: name } : null;
+}
+
+function errorMessage(err: unknown): string {
+  const e = err as { data?: { message?: string }; message?: string };
+  return e?.data?.message ?? e?.message ?? String(err);
+}
+
+// useLoaded runs one metadata lookup whenever its key changes. State is only
+// written when a lookup settles, and a result is only used while its key is
+// current. A failed lookup keeps its message; the pickers still accept a typed
+// name, so the editor stays usable when metadata cannot be read.
+function useLoaded<T>(key: string, load: (() => Promise<T>) | undefined): 
Loaded<T> {
+  const [result, setResult] = useState<{ key: string; value?: T; error?: 
string }>();
+  useEffect(() => {
+    if (!load) {
+      return;
+    }
+    let cancelled = false;
+    load().then(
+      (value) => {
+        if (!cancelled) {
+          setResult({ key, value });
+        }
+      },
+      (err) => {
+        if (!cancelled) {
+          setResult({ key, error: errorMessage(err) });
+        }
+      }
+    );
+    return () => {
+      cancelled = true;
+    };
+    // The key identifies the lookup; `load` is a fresh closure on every 
render.
+    // eslint-disable-next-line react-hooks/exhaustive-deps
+  }, [key]);
+  if (!load) {
+    return { loading: false };
+  }
+  if (result?.key !== key) {
+    return { loading: true };
+  }
+  return { value: result.value, error: result.error, loading: false };
+}
+
+// TablePicker lists the databases, tables and columns of the connected server
+// for the table-model editor, and can fill in a starter query for the chosen
+// table.
+export function TablePicker({ datasource, database, table, onDatabaseChange, 
onTableChange, onSqlChange }: Props) {
+  const databases = useLoaded('databases', () => 
datasource.getTableDatabases());

Review Comment:
   **Minor:** the `databases` key never changes. A transient SHOW DATABASES 
failure, or a database created later, is never retried while the editor is 
mounted. Line 105 also lets `databases.error` hide later table and column 
errors. Retrying when the menu opens (`onOpenMenu`) would cover both.



##########
connectors/grafana-plugin/pkg/plugin/table_metadata.go:
##########
@@ -0,0 +1,166 @@
+/*
+ * 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.
+ */
+
+package plugin
+
+import (
+       "fmt"
+       "net/http"
+       "strings"
+
+       "github.com/grafana/grafana-plugin-sdk-go/backend/log"
+)
+
+// Resource handlers behind the table-model query editor's database, table and
+// column pickers. Each one runs a fixed metadata statement on a pooled native
+// session; request parameters only reach the server as quoted identifiers.
+
+// tableColumn describes one column of a table-model table as DESC reports it.
+type tableColumn struct {
+       Name     string `json:"name"`
+       DataType string `json:"dataType"`
+       Category string `json:"category"`
+}
+
+// getTableDatabases lists the databases visible to the datasource user.
+func (d *IoTDBDataSource) getTableDatabases() http.Handler {
+       return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+               dataSet, err := d.executeTableStatement(r.Context(), "", "SHOW 
DATABASES")
+               if err != nil {
+                       writeMetadataError(w, "SHOW DATABASES", err)
+                       return
+               }
+               names, err := columnStrings(dataSet, "Database")
+               if err != nil {
+                       writeMetadataError(w, "SHOW DATABASES", err)
+                       return
+               }
+               writeJSON(w, names)
+       })
+}
+
+// getTableTables lists the tables of the database named by the "database"
+// query parameter.
+func (d *IoTDBDataSource) getTableTables() http.Handler {
+       return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+               database := strings.TrimSpace(r.URL.Query().Get("database"))
+               if database == "" {
+                       writeJSONError(w, http.StatusBadRequest, "the database 
parameter is required")
+                       return
+               }
+               dataSet, err := d.executeTableStatement(r.Context(), database, 
"SHOW TABLES")
+               if err != nil {
+                       writeMetadataError(w, "SHOW TABLES", err)
+                       return
+               }
+               names, err := columnStrings(dataSet, "TableName")
+               if err != nil {
+                       writeMetadataError(w, "SHOW TABLES", err)
+                       return
+               }
+               writeJSON(w, names)
+       })
+}
+
+// getTableColumns describes the table named by the "database" and "table"
+// query parameters, in the server's column order.
+func (d *IoTDBDataSource) getTableColumns() http.Handler {
+       return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+               database := strings.TrimSpace(r.URL.Query().Get("database"))
+               table := strings.TrimSpace(r.URL.Query().Get("table"))

Review Comment:
   **Minor:** the handlers trim `database` and `table` (lines 62 and 85-86), 
but the starter query (`TablePicker.tsx:159`) quotes the untrimmed value. A 
pasted `weather ` lists its columns but generates `FROM "weather "`. Trimming 
in one place, or not at all, keeps them consistent.



##########
connectors/grafana-plugin/tests/tableModelEditor.spec.ts:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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 { expect, test } from '@grafana/plugin-e2e';
+
+const DATASOURCE_NAME = 'IoTDB';
+
+// information_schema exists on every IoTDB instance, so the pickers can be
+// exercised against the real server without seeding any data.
+test('the table-model editor lists databases, tables and columns from the 
server', async ({ page }) => {
+  await page.goto('/explore');
+
+  await page.getByText('SQL: Full Customized', { exact: true 
}).first().click();
+  await page.getByText('SQL: Table Model', { exact: true }).last().click();
+
+  await page.locator('#iotdb-table-database').click();
+  await page.getByRole('option', { name: 'information_schema' }).click();
+
+  await page.locator('#iotdb-table-table').click();
+  await page.getByRole('option', { name: 'tables', exact: true }).click();
+
+  // The COLUMNS row is built from DESC, grouped by category.
+  await expect(page.getByText('table_name', { exact: false })).toBeVisible({ 
timeout: 30_000 });
+
+  await page.getByRole('button', { name: 'Use starter query' }).click();
+  const sql = await page.locator('textarea').first().inputValue();
+  // information_schema.tables has no time column, so no time filter is added.
+  expect(sql).toBe('SELECT "database", "table_name" FROM "tables"');
+
+  // The generated query has to run, not just look right.
+  const datasource = await page.evaluate(async (name) => {
+    const res = await 
fetch(`/api/datasources/name/${encodeURIComponent(name)}`);
+    return res.json();
+  }, DATASOURCE_NAME);
+  const now = Date.now();
+  const result = await page.evaluate(
+    async (payload) => {
+      const res = await fetch('/api/ds/query', {
+        method: 'POST',
+        headers: { 'Content-Type': 'application/json' },
+        body: JSON.stringify(payload),
+      });
+      return { status: res.status, json: await res.json() };
+    },
+    {
+      from: String(now - 3_600_000),
+      to: String(now),
+      queries: [
+        {
+          refId: 'A',
+          datasource: { uid: datasource.uid, type: datasource.type },
+          sqlType: 'SQL: Table Model',
+          database: 'information_schema',
+          sql,
+          format: 'Table',

Review Comment:
   **Minor:** the payload hard-codes `database: 'information_schema'` and 
`format: 'Table'`, so the test never checks that the picker persisted 
`query.database`; running the query from the editor would. Separately, CI 
doesn't run `npm run test:ci` or `go test` today (the "Build and test frontend" 
step only builds), so the new Jest and Go tests run only locally. Adding both 
to the build job could be a follow-up.



##########
connectors/grafana-plugin/pkg/plugin/table_metadata.go:
##########
@@ -0,0 +1,166 @@
+/*
+ * 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.
+ */
+
+package plugin
+
+import (
+       "fmt"
+       "net/http"
+       "strings"
+
+       "github.com/grafana/grafana-plugin-sdk-go/backend/log"
+)
+
+// Resource handlers behind the table-model query editor's database, table and
+// column pickers. Each one runs a fixed metadata statement on a pooled native
+// session; request parameters only reach the server as quoted identifiers.
+
+// tableColumn describes one column of a table-model table as DESC reports it.
+type tableColumn struct {
+       Name     string `json:"name"`
+       DataType string `json:"dataType"`
+       Category string `json:"category"`
+}
+
+// getTableDatabases lists the databases visible to the datasource user.
+func (d *IoTDBDataSource) getTableDatabases() http.Handler {
+       return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+               dataSet, err := d.executeTableStatement(r.Context(), "", "SHOW 
DATABASES")

Review Comment:
   **Follow-up, not blocking:** metadata lookups share the 8-session pool with 
panel queries and can't be cancelled. `pool.GetSession()` ignores the context 
and waits up to 60 s, and the effect cleanup in `TablePicker` only discards the 
result. They also run again on every editor mount. A short-TTL cache on the 
datasource, an abort signal, and `SHOW TABLES FROM <db>` / `DESC <db>.<table>` 
(which avoid the extra `USE`) would reduce the load.



##########
connectors/grafana-plugin/src/componments/TablePicker.tsx:
##########
@@ -0,0 +1,176 @@
+/*
+ * 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 React, { useEffect, useState } from 'react';
+import { SelectableValue } from '@grafana/data';
+import { Button, Select } from '@grafana/ui';
+import { DataSource } from '../datasource';
+import { starterTableQuery } from '../tableQuery';
+import { TableColumn } from '../types';
+import { QueryInlineField } from './Form';
+
+interface Props {
+  datasource: DataSource;
+  database: string;
+  table: string;
+  onDatabaseChange: (database: string) => void;
+  onTableChange: (table: string) => void;
+  onSqlChange: (sql: string) => void;
+}
+
+interface Loaded<T> {
+  value?: T;
+  loading: boolean;
+  error?: string;
+}
+
+const categories = ['TAG', 'FIELD', 'ATTRIBUTE'];
+
+function toOptions(names: string[] | undefined): 
Array<SelectableValue<string>> {
+  return (names ?? []).map((name) => ({ label: name, value: name }));
+}
+
+function toValue(name: string): SelectableValue<string> | null {
+  return name ? { label: name, value: name } : null;
+}
+
+function errorMessage(err: unknown): string {
+  const e = err as { data?: { message?: string }; message?: string };
+  return e?.data?.message ?? e?.message ?? String(err);
+}
+
+// useLoaded runs one metadata lookup whenever its key changes. State is only
+// written when a lookup settles, and a result is only used while its key is
+// current. A failed lookup keeps its message; the pickers still accept a typed
+// name, so the editor stays usable when metadata cannot be read.
+function useLoaded<T>(key: string, load: (() => Promise<T>) | undefined): 
Loaded<T> {
+  const [result, setResult] = useState<{ key: string; value?: T; error?: 
string }>();
+  useEffect(() => {
+    if (!load) {
+      return;
+    }
+    let cancelled = false;
+    load().then(
+      (value) => {
+        if (!cancelled) {
+          setResult({ key, value });
+        }
+      },
+      (err) => {
+        if (!cancelled) {
+          setResult({ key, error: errorMessage(err) });
+        }
+      }
+    );
+    return () => {
+      cancelled = true;
+    };
+    // The key identifies the lookup; `load` is a fresh closure on every 
render.
+    // eslint-disable-next-line react-hooks/exhaustive-deps
+  }, [key]);
+  if (!load) {
+    return { loading: false };
+  }
+  if (result?.key !== key) {
+    return { loading: true };
+  }
+  return { value: result.value, error: result.error, loading: false };
+}
+
+// TablePicker lists the databases, tables and columns of the connected server
+// for the table-model editor, and can fill in a starter query for the chosen
+// table.
+export function TablePicker({ datasource, database, table, onDatabaseChange, 
onTableChange, onSqlChange }: Props) {
+  const databases = useLoaded('databases', () => 
datasource.getTableDatabases());
+  const tables = useLoaded(`tables|${database}`, database ? () => 
datasource.getTableTables(database) : undefined);
+  const columns = useLoaded<TableColumn[]>(
+    `columns|${database}|${table}`,
+    database && table ? () => datasource.getTableColumns(database, table) : 
undefined
+  );
+  const error = databases.error ?? tables.error ?? columns.error;
+
+  return (
+    <>
+      <div className="gf-form">
+        <QueryInlineField label={'DATABASE'}>
+          {/* Select rather than Combobox: with Combobox, picking 
information_schema
+              crashed Explore on Grafana 12.3 with React error 185 raised from 
the
+              popover's floating-ui reference; Select does not. */}
+          <Select
+            inputId="iotdb-table-database"

Review Comment:
   **Minor:** the fixed `inputId`s (here and at line 127) give duplicate DOM 
ids when a panel has more than one table-model query. Deriving them from 
`refId` or `useId()` avoids that.



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