PDGGK commented on code in PR #134: URL: https://github.com/apache/iotdb-extras/pull/134#discussion_r4231722013
########## 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: The database list is now fetched again each time its menu opens, and the previous list stays visible while that lookup runs. Errors from all three lookups are now shown together, so the database error no longer hides the others. ########## 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: They are now `iotdb-table-database-${refId}` and `iotdb-table-table-${refId}`. A component test checks them for refId `B`. ########## 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: The editor now trims a name once, when it is committed. The handlers no longer trim; they only reject a blank name. A Go test checks that `weather ` is described as `"weather "`, so the listed columns and the starter query refer to the same table. ########## 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: The new e2e test runs the query from Explore and checks that the request carries the picked `database` and `table`. I will add `npm run test:ci` and `go test` to the build job in a separate PR. ########## 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: Agreed. I will take the cache, the abort signal and the `SHOW TABLES FROM` / `DESC <db>.<table>` forms in a follow-up PR. -- 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]
