-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add full text sql query editor
- Loading branch information
1 parent
c25f683
commit 4e02aaa
Showing
7 changed files
with
183 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,125 @@ | ||
import { defaults } from 'lodash'; | ||
|
||
import React, { PureComponent, FormEvent } from 'react'; | ||
import { AutoSizeInput, InlineFieldRow, InlineField } from '@grafana/ui'; | ||
import React, {FormEvent, useState} from 'react'; | ||
import { AutoSizeInput, InlineFieldRow, InlineField, useTheme, InlineSwitch } from '@grafana/ui'; | ||
import { QueryEditorProps } from '@grafana/data'; | ||
import { DataSource } from './datasource'; | ||
import Editor from "@monaco-editor/react"; | ||
import { defaultQuery, MyDataSourceOptions, MyQuery } from './types'; | ||
import * as monaco from "monaco-editor"; | ||
|
||
type Props = QueryEditorProps<DataSource, MyQuery, MyDataSourceOptions>; | ||
|
||
export class QueryEditor extends PureComponent<Props> { | ||
export function QueryEditor(props: Props) { | ||
|
||
ontableNameChange = (event: FormEvent<HTMLInputElement>) => { | ||
const [, setQueryText] = useState("") | ||
const [sqlEditorSelected, setSqlEditorSelected] = useState(false) | ||
const onQuerryChange = (value: string | undefined, ev: monaco.editor.IModelContentChangedEvent) => { | ||
setQueryText(value || "") | ||
const { onChange, query } = props; | ||
onChange({ ...query, rawSqlQuery: value }); | ||
}; | ||
|
||
const onSQLSwitchChange = (event: any) => { | ||
const { onChange, query } = props; | ||
onChange({ ...query, rawSqlSelected: !sqlEditorSelected }); | ||
setSqlEditorSelected(!sqlEditorSelected); | ||
}; | ||
const ontableNameChange = (event: FormEvent<HTMLInputElement>) => { | ||
console.log(event); | ||
const { onChange, query } = this.props; | ||
const { onChange, query } = props; | ||
onChange({ ...query, tableName: event.currentTarget.value }); | ||
}; | ||
|
||
|
||
ontimeColumnNameTextChange = (event: FormEvent<HTMLInputElement>) => { | ||
const ontimeColumnNameTextChange = (event: FormEvent<HTMLInputElement>) => { | ||
console.log(event); | ||
const { onChange, query } = this.props; | ||
const { onChange, query } = props; | ||
onChange({ ...query, timeColumnName: event.currentTarget.value }); | ||
}; | ||
|
||
|
||
onvalueColumnNameChange = (event: FormEvent<HTMLInputElement>) => { | ||
const onvalueColumnNameChange = (event: FormEvent<HTMLInputElement>) => { | ||
console.log(event); | ||
const { onChange, query } = this.props; | ||
const { onChange, query } = props; | ||
onChange({ ...query, valueColumnName: event.currentTarget.value }); | ||
}; | ||
|
||
|
||
onwhereQueryChange = (event: FormEvent<HTMLInputElement>) => { | ||
const onwhereQueryChange = (event: FormEvent<HTMLInputElement>) => { | ||
console.log(event); | ||
const { onChange, query } = this.props; | ||
const { onChange, query } = props; | ||
onChange({ ...query, whereQuery: event.currentTarget.value }); | ||
}; | ||
|
||
render() { | ||
const query = defaults(this.props.query, defaultQuery); | ||
const query = defaults(props.query, defaultQuery); | ||
const { timeColumnName, valueColumnName, whereQuery, tableName } = query; | ||
|
||
const theme = useTheme() | ||
return ( | ||
<div className="gf-form" style={{ flexDirection: "column", rowGap: "8px"}}> | ||
{sqlEditorSelected ? ( | ||
<Editor | ||
height="200px" | ||
theme={theme.isDark ? "vs-dark" : "vs-light"} | ||
defaultLanguage="sql" | ||
defaultValue="SELECT $__time(time_column), $__value(value_column) FROM catalog.default.table_name WHERE $__timeFilter(time_column) GROUP BY $__timeWindow(time_column)" | ||
onChange={onQuerryChange} | ||
/> | ||
) : ( | ||
<> | ||
<InlineFieldRow> | ||
<InlineField label="Time Column" labelWidth={16} tooltip="The column name of the time column"> | ||
<AutoSizeInput | ||
value={timeColumnName || ''} | ||
onCommitChange={ontimeColumnNameTextChange} | ||
minWidth={32} | ||
defaultValue="timestamp" | ||
required | ||
/> | ||
</InlineField> | ||
</InlineFieldRow> | ||
<InlineFieldRow> | ||
<InlineField label="Value Column" labelWidth={16} tooltip="The column name of the value to query"> | ||
<AutoSizeInput | ||
value={valueColumnName || ''} | ||
onCommitChange={onvalueColumnNameChange} | ||
minWidth={32} | ||
defaultValue="value" | ||
required | ||
/> | ||
</InlineField> | ||
</InlineFieldRow> | ||
<InlineFieldRow> | ||
<InlineField label="Table Name" labelWidth={16} tooltip="(Catalog), Schema and Table Name in the format (<catalog>).<schema>.<table_name>"> | ||
<AutoSizeInput | ||
value={tableName || ''} | ||
onCommitChange={ontableNameChange} | ||
minWidth={32} | ||
placeholder="catalog.default.table_name" | ||
required | ||
/> | ||
</InlineField> | ||
</InlineFieldRow> | ||
<InlineFieldRow> | ||
<InlineField label="Where SQL" labelWidth={16} tooltip="Additional WHERE conditions to filter the queried data (Databricks SQL)"> | ||
<AutoSizeInput | ||
value={whereQuery || ''} | ||
onCommitChange={onwhereQueryChange} | ||
minWidth={32} | ||
placeholder="id = 1 AND name = 'Peter'" | ||
/> | ||
</InlineField> | ||
</InlineFieldRow> | ||
</> | ||
)} | ||
<InlineFieldRow> | ||
<InlineField label="Time Column" labelWidth={16} tooltip="The column name of the time column"> | ||
<AutoSizeInput | ||
value={timeColumnName || ''} | ||
onCommitChange={this.ontimeColumnNameTextChange} | ||
minWidth={32} | ||
defaultValue="timestamp" | ||
required | ||
/> | ||
</InlineField> | ||
</InlineFieldRow> | ||
<InlineFieldRow> | ||
<InlineField label="Value Column" labelWidth={16} tooltip="The column name of the value to query"> | ||
<AutoSizeInput | ||
value={valueColumnName || ''} | ||
onCommitChange={this.onvalueColumnNameChange} | ||
minWidth={32} | ||
defaultValue="value" | ||
required | ||
/> | ||
</InlineField> | ||
</InlineFieldRow> | ||
<InlineFieldRow> | ||
<InlineField label="Table Name" labelWidth={16} tooltip="Schema and Table Name in the format <schema>.<table_name>"> | ||
<AutoSizeInput | ||
value={tableName || ''} | ||
onCommitChange={this.ontableNameChange} | ||
minWidth={32} | ||
placeholder="default.table_name" | ||
required | ||
/> | ||
</InlineField> | ||
</InlineFieldRow> | ||
<InlineFieldRow> | ||
<InlineField label="Where SQL" labelWidth={16} tooltip="Additional WHERE conditions to filter the queried data (Databricks SQL)"> | ||
<AutoSizeInput | ||
value={whereQuery || ''} | ||
onCommitChange={this.onwhereQueryChange} | ||
minWidth={32} | ||
placeholder="id = 1 AND name = 'Peter'" | ||
<InlineField label="SQL Editor" labelWidth={16}> | ||
<InlineSwitch | ||
value={sqlEditorSelected} | ||
onChange={onSQLSwitchChange} | ||
/> | ||
</InlineField> | ||
</InlineFieldRow> | ||
</div> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1873,7 +1873,7 @@ | |
resolved "https://registry.yarnpkg.com/@mapbox/unitbezier/-/unitbezier-0.0.0.tgz#15651bd553a67b8581fb398810c98ad86a34524e" | ||
integrity sha512-HPnRdYO0WjFjRTSwO3frz1wKaU649OBFPX3Zo/2WZvuRi6zMiRGui8SnPQiQABgqCf8YikDe5t3HViTVw1WUzA== | ||
|
||
"@monaco-editor/loader@^1.2.0": | ||
"@monaco-editor/loader@^1.2.0", "@monaco-editor/loader@^1.3.2": | ||
version "1.3.2" | ||
resolved "https://registry.yarnpkg.com/@monaco-editor/loader/-/loader-1.3.2.tgz#04effbb87052d19cd7d3c9d81c0635490f9bb6d8" | ||
integrity sha512-BTDbpHl3e47r3AAtpfVFTlAi7WXv4UQ/xZmz8atKl4q7epQV5e7+JbigFDViWF71VBi4IIBdcWP57Hj+OWuc9g== | ||
|
@@ -1888,6 +1888,14 @@ | |
"@monaco-editor/loader" "^1.2.0" | ||
prop-types "^15.7.2" | ||
|
||
"@monaco-editor/react@^4.4.6": | ||
version "4.4.6" | ||
resolved "https://registry.yarnpkg.com/@monaco-editor/react/-/react-4.4.6.tgz#8ae500b0edf85276d860ed702e7056c316548218" | ||
integrity sha512-Gr3uz3LYf33wlFE3eRnta4RxP5FSNxiIV9ENn2D2/rN8KgGAD8ecvcITRtsbbyuOuNkwbuHYxfeaz2Vr+CtyFA== | ||
dependencies: | ||
"@monaco-editor/loader" "^1.3.2" | ||
prop-types "^15.7.2" | ||
|
||
"@nodelib/[email protected]": | ||
version "2.1.5" | ||
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5" | ||
|