-
Notifications
You must be signed in to change notification settings - Fork 21
Implement Grafana Query Builder #329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
archef2000
wants to merge
50
commits into
VictoriaMetrics:main
Choose a base branch
from
archef2000:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
c2a6cdb
Implement Grafana Query Builder
archef2000 201f871
delete unused files
archef2000 66b3d5b
Make Editors compatible with lower versions
archef2000 ffac882
Add error handling for unsupported field types in LogsQlLanguageProvider
archef2000 b96a69c
fix tests
archef2000 52f4b3e
remove unneeded query rendering
archef2000 cbc49bb
combine QueryBuilder and QueryEditor
archef2000 c87894a
fix requested changes
archef2000 aef6be4
simplify FieldAsFieldEditor updateValue function
archef2000 111e28c
fix parseFieldMapList function
archef2000 f28b814
simplify onChange for all editors with a Select component
archef2000 cd5862d
compare with VictoriaLogsOperationId values
archef2000 50bc9e5
remove redundant returning of undefined
archef2000 cd4ca7c
convert value safely
archef2000 2aef4a9
simplify logic in FieldsEditorWithPrefix
archef2000 3476058
remove check for empty expr
archef2000 760a477
simplify to remove all falsy values
archef2000 b9bbcd6
flatten if block for clarity
archef2000 92f195f
simplity SortedFieldsEditor
archef2000 9fd29cf
split state into separate useState calls
archef2000 c8220f8
improve readability for getFieldName
archef2000 a1c20cb
use switch case to parse math expresion
archef2000 a291aec
allow creating values while loading
archef2000 b2d0fbe
correct wording in getValuesFromBrackets function
archef2000 838960f
make the logic of parsing the stream filter less complex
archef2000 8281796
add tests to make shure the parsing is correct
archef2000 238b3b2
turn onOpenMenu handler into function in ExactValueEditor editor
archef2000 a5527ae
remove else branch in FieldsEditorWithPrefix editor
archef2000 12ff850
move menu handlers from components to seperate functions
archef2000 b6ae599
fix comparison operations
archef2000 b45858c
improve SortedFieldsEditor
archef2000 8c2cd63
fix unselection of item in editors using MultiSelect
archef2000 e265f8e
fix variable generation and option queries
archef2000 51eb66f
reorder operations for better searching
archef2000 bd6b7aa
fixed various parsing errors
archef2000 23af02b
fix disapearing of disabled operations
archef2000 db33be0
fix disapearing of disabled operations in editors
archef2000 0e0e0c3
fix math editor and query renderer
archef2000 bbd152c
add time_add pipe
archef2000 7d8a171
add missing options
archef2000 8f1c222
add generate_sequence
archef2000 6efef10
add query_stats
archef2000 ed2fee6
add split
archef2000 7842bc8
add all missing operations
archef2000 3d9de16
Merge branch 'main' into main
archef2000 3270538
fix options generation for subqueries
archef2000 55d2517
fix logic error in subquery editor
archef2000 7f3d219
Merge branch 'VictoriaMetrics:main' into main
archef2000 cc26956
fix stream field parsing and field prediction
archef2000 55f7fbe
remove else
archef2000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
src/components/QueryEditor/QueryBuilder/Editors/ExactValueEditor.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { QueryBuilderOperationParamEditorProps } from '@grafana/plugin-ui'; | ||
| import { Combobox } from '@grafana/ui'; | ||
|
|
||
| import { getFieldValueOptions } from './utils/editorHelper'; | ||
|
|
||
| export default function ExactValueEditor(props: QueryBuilderOperationParamEditorProps) { | ||
| const { onChange, index, value, operation } = props; | ||
|
|
||
| return ( | ||
| <Combobox<string> | ||
| options={()=> getFieldValueOptions(props, operation.params[0] as string)} | ||
| value={value as string} | ||
| onChange={(v) => onChange(index, v.value)} | ||
| width="auto" | ||
| maxWidth={30} | ||
| minWidth={10} | ||
| createCustomValue | ||
| /> | ||
| ); | ||
| } |
80 changes: 80 additions & 0 deletions
80
src/components/QueryEditor/QueryBuilder/Editors/FieldAsFieldEditor.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import React, { useState } from 'react'; | ||
|
|
||
| import { QueryBuilderOperationParamEditorProps } from '@grafana/plugin-ui'; | ||
| import { InlineField, Stack, Combobox } from '@grafana/ui'; | ||
|
|
||
| import { isValue, quoteString, getValue } from '../utils/stringHandler'; | ||
| import { splitString } from '../utils/stringSplitter'; | ||
|
|
||
| import { getFieldNameOptions } from './utils/editorHelper'; | ||
|
|
||
| export default function FieldAsFieldEditor(props: QueryBuilderOperationParamEditorProps) { | ||
| const { value, onChange, index } = props; | ||
|
|
||
| const str = splitString(value as string); | ||
| let parsedFromField = ""; | ||
| let parsedToField = ""; | ||
| if (str.length === 3) { | ||
| if (isValue(str[0])) { | ||
| parsedFromField = getValue(str[0]); | ||
| } | ||
| if (str[1].type === "space" && str[1].value === "as") { | ||
| if (isValue(str[2])) { | ||
| parsedToField = getValue(str[2]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const [fromField, setFromField] = useState<string>(parsedFromField); | ||
| const [toField, setToField] = useState<string>(parsedToField); | ||
|
|
||
| const updateValue = (fromField: string, toField: string) => { | ||
| let value = ""; | ||
| if (fromField.trim() === "") { | ||
| value += "\"\""; | ||
| } else { | ||
| value += quoteString(fromField.trim()); | ||
| } | ||
| value += " as "; | ||
| if (toField.trim() === "") { | ||
| value += "\"\""; | ||
| } else { | ||
| value += quoteString(toField.trim()); | ||
| } | ||
| onChange(index, value); | ||
| }; | ||
archef2000 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return ( | ||
| <Stack> | ||
| <InlineField> | ||
| <Combobox<string> | ||
| createCustomValue | ||
| options={()=>getFieldNameOptions(props)} | ||
| onChange={(value) => { | ||
| setFromField(value.value); | ||
| updateValue(value.value, toField); | ||
| }} | ||
| value={fromField} | ||
| width="auto" | ||
| maxWidth={30} | ||
| minWidth={10} | ||
| /> | ||
| </InlineField> | ||
| <div style={{ padding: '6px 0 8px 0px' }}>as</div> | ||
| <InlineField> | ||
| <Combobox<string> | ||
| createCustomValue | ||
| options={()=>getFieldNameOptions(props)} | ||
| onChange={(value) => { | ||
| setToField(value.value); | ||
| updateValue(fromField, value.value); | ||
| }} | ||
| value={toField} | ||
| width="auto" | ||
| maxWidth={30} | ||
| minWidth={10} | ||
| /> | ||
| </InlineField> | ||
| </Stack> | ||
| ); | ||
| } | ||
25 changes: 25 additions & 0 deletions
25
src/components/QueryEditor/QueryBuilder/Editors/FieldEditor.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import React from 'react'; | ||
|
|
||
| import { QueryBuilderOperationParamEditorProps } from '@grafana/plugin-ui'; | ||
| import { Combobox, InlineField } from '@grafana/ui'; | ||
|
|
||
| import { getFieldNameOptions } from './utils/editorHelper'; | ||
|
|
||
| export default function FieldEditor(props: QueryBuilderOperationParamEditorProps) { | ||
| const { value, onChange, index } = props; | ||
| return ( | ||
| <InlineField> | ||
| <Combobox<string> | ||
| createCustomValue | ||
| options={()=>getFieldNameOptions(props)} | ||
| onChange={(value) => { | ||
| onChange(index, value.value) | ||
| }} | ||
| value={value as string} | ||
| width="auto" | ||
| maxWidth={30} | ||
| minWidth={10} | ||
| /> | ||
| </InlineField> | ||
| ); | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/components/QueryEditor/QueryBuilder/Editors/FieldValueTypeEditor.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import React from "react"; | ||
|
|
||
| import { QueryBuilderOperationParamEditorProps } from "@grafana/plugin-ui"; | ||
| import { Combobox } from "@grafana/ui"; | ||
|
|
||
| import { getValueTypeOptions } from "./utils/editorHelper"; | ||
|
|
||
| export default function FieldValueTypeEditor(props: QueryBuilderOperationParamEditorProps) { | ||
| const { value, onChange, index } = props; | ||
|
|
||
| return ( | ||
| <Combobox<string> | ||
| createCustomValue | ||
| options={()=>getValueTypeOptions(props)} | ||
| onChange={(value) => { | ||
| onChange(index, value.value); | ||
| }} | ||
| value={value as string} | ||
| width="auto" | ||
| maxWidth={30} | ||
| minWidth={10} | ||
| /> | ||
| ) | ||
| } |
46 changes: 46 additions & 0 deletions
46
src/components/QueryEditor/QueryBuilder/Editors/FieldsEditor.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import React, { useState } from 'react'; | ||
|
|
||
| import { SelectableValue } from '@grafana/data'; | ||
| import { QueryBuilderOperationParamEditorProps } from '@grafana/plugin-ui'; | ||
| import { MultiSelect } from '@grafana/ui'; | ||
|
|
||
| import { getValuesFromBrackets } from '../utils/operationParser'; | ||
| import { quoteString } from '../utils/stringHandler'; | ||
| import { splitString } from '../utils/stringSplitter'; | ||
|
|
||
| import { getFieldNameOptions } from './utils/editorHelper'; | ||
|
|
||
| export default function FieldsEditor(props: QueryBuilderOperationParamEditorProps) { | ||
| const { value, onChange, index } = props; | ||
|
|
||
| const setFields = (values: SelectableValue<string>[]) => { | ||
| const rawValues = values.map((v) => v.value?.trim()).filter((v) => v !== undefined && v !== null); | ||
| let value = rawValues.map((v)=> quoteString(v)).join(", "); | ||
| onChange(index, value); | ||
| } | ||
|
|
||
| const [state, setState] = useState<{ | ||
| options?: SelectableValue[]; | ||
| isLoading?: boolean; | ||
| }>({}); | ||
|
|
||
| const handleOpenMenu = async () => { | ||
| setState({ isLoading: true }); | ||
| const options = await getFieldNameOptions(props); | ||
| setState({ options, isLoading: undefined }); | ||
| } | ||
|
|
||
| return ( | ||
| <MultiSelect<string> | ||
| onChange={setFields} | ||
| options={state.options} | ||
| value={getValuesFromBrackets(splitString(value as string))} | ||
| isLoading={state.isLoading} | ||
| allowCustomValue | ||
| noOptionsMessage="No labels found" | ||
| loadingMessage="Loading labels" | ||
| width={20} | ||
| onOpenMenu={handleOpenMenu} | ||
| /> | ||
| ); | ||
| } |
121 changes: 121 additions & 0 deletions
121
src/components/QueryEditor/QueryBuilder/Editors/FieldsEditorWithPrefix.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import React, { useState } from 'react'; | ||
|
|
||
| import { SelectableValue } from '@grafana/data'; | ||
| import { QueryBuilderOperationParamEditorProps } from '@grafana/plugin-ui'; | ||
| import { MultiSelect } from '@grafana/ui'; | ||
|
|
||
| import { quoteString, unquoteString } from '../utils/stringHandler'; | ||
| import { splitByUnescapedChar, SplitString, splitString } from '../utils/stringSplitter'; | ||
|
|
||
| import { getFieldNameOptions } from './utils/editorHelper'; | ||
|
|
||
| interface FieldWithPrefix { | ||
| name: string; | ||
| isPrefix: boolean; | ||
| } | ||
|
|
||
| export default function FieldsEditorWithPrefix(props: QueryBuilderOperationParamEditorProps) { | ||
| const { value, onChange, index } = props; | ||
|
|
||
| const str = splitString(value as string); | ||
| const parsedValues = parseInputValues(str); | ||
| const [values, setValues] = useState<FieldWithPrefix[]>(parsedValues); | ||
|
|
||
| const setFields = (values: FieldWithPrefix[]) => { | ||
| setValues(values); | ||
| const newValue = values.map((field) => { | ||
| if (field.isPrefix !== undefined) { | ||
| return field.isPrefix ? `${quoteString(field.name)}*` : `${quoteString(field.name)}`; | ||
| } else { | ||
archef2000 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return quoteString(field as unknown as string); | ||
| } | ||
| }).join(', '); | ||
| onChange(index, newValue); | ||
| } | ||
|
|
||
| const togglePrefix = (index: number) => { | ||
| const field = values[index]; | ||
| field.isPrefix = !field.isPrefix; | ||
| setFields(values); | ||
| }; | ||
|
|
||
| const [state, setState] = useState<{ | ||
| options?: SelectableValue<FieldWithPrefix>[]; | ||
| isLoading?: boolean; | ||
| }>({}); | ||
|
|
||
| return ( | ||
| <MultiSelect<FieldWithPrefix> | ||
| openMenuOnFocus | ||
| onOpenMenu={async () => { | ||
| setState({ isLoading: true }); | ||
| let options = await getFieldNameOptions(props); | ||
| const selectedNames = values.map(v => v.name); | ||
| options = options.filter((opt: SelectableValue<string>) => opt.value && !selectedNames.includes(opt.value)); | ||
| setState({ options, isLoading: undefined }); | ||
| }} | ||
| isLoading={state.isLoading} | ||
| allowCustomValue | ||
| noOptionsMessage="No labels found" | ||
| loadingMessage="Loading labels" | ||
| options={state.options} | ||
| value={values} | ||
| onChange={(values) => setFields(values.map((v) => v.value || v as FieldWithPrefix))} | ||
| formatOptionLabel={(option, { context }) => { | ||
| if (context === 'value') { | ||
| const field = option as FieldWithPrefix; | ||
| const handleToggle = (e: React.SyntheticEvent) => { | ||
| e.stopPropagation(); | ||
| const idx = values.findIndex((v) => (v).name === field.name); | ||
| if (idx !== -1) { | ||
| togglePrefix(idx); | ||
| } | ||
| }; | ||
| return ( | ||
| <span | ||
| tabIndex={0} | ||
| style={{ cursor: 'pointer' }} | ||
| onMouseDown={handleToggle} | ||
| > | ||
| {formatFieldLabel(field)} | ||
| </span> | ||
| ); | ||
| } | ||
archef2000 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return <>{option.label}</>; | ||
| }} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| const formatFieldLabel = (field: FieldWithPrefix): string => { | ||
| return field.isPrefix ? `${field.name} *` : field.name; | ||
| }; | ||
|
|
||
| const parseValue = (value: SplitString[]): FieldWithPrefix => { | ||
| if (value.length === 0 || value[0].type === "bracket") { | ||
| return { name: '', isPrefix: false }; | ||
| } | ||
| if (value[0].type === "quote") { | ||
| let isPrefix = false; | ||
| if (value.length > 1) { | ||
| isPrefix = (value[1].type === "space" && value[1].value === "*"); | ||
| } | ||
archef2000 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return { name: unquoteString(value[0].value), isPrefix }; | ||
| } else { | ||
archef2000 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let fieldValue = value[0].value; | ||
| if (fieldValue.endsWith('*')) { | ||
| return { name: fieldValue.slice(0, -1), isPrefix: true }; | ||
| } | ||
| return { name: fieldValue, isPrefix: false }; | ||
archef2000 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| const parseInputValues = (str: SplitString[]): FieldWithPrefix[] => { | ||
| let fields: FieldWithPrefix[] = []; | ||
| for (const field of splitByUnescapedChar(str, ',')) { | ||
| if (field.length > 0 && field[0].type !== "bracket") { | ||
| fields.push(parseValue(field)); | ||
| } | ||
| } | ||
| return fields; | ||
| }; | ||
53 changes: 53 additions & 0 deletions
53
src/components/QueryEditor/QueryBuilder/Editors/LogicalFilterEditor.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { css } from "@emotion/css"; | ||
| import React, { useMemo, useState } from "react"; | ||
|
|
||
| import { GrafanaTheme2 } from "@grafana/data"; | ||
| import { OperationList, QueryBuilderOperationParamEditorProps } from "@grafana/plugin-ui"; | ||
| import { InlineField, useStyles2 } from "@grafana/ui"; | ||
|
|
||
| import { VisualQuery } from "../../../../types"; | ||
| import { parseExprToVisualQuery, createQueryModellerWithDefaultField } from "../QueryModeller"; | ||
| import { VictoriaLogsQueryOperationCategory } from "../VictoriaLogsQueryOperationCategory"; | ||
|
|
||
| export default function LogicalFilterEditor(props: QueryBuilderOperationParamEditorProps) { | ||
| const styles = useStyles2(getStyles); | ||
| const { value, onChange, index, datasource, timeRange, onRunQuery, operation } = props; | ||
| const fieldName = operation.params[0] as string; | ||
| const [state, setState] = useState<{ expr: string, visQuery: VisualQuery }>({ | ||
| expr: value as string, | ||
| visQuery: parseExprToVisualQuery(value as string, fieldName).query | ||
| }) | ||
|
|
||
| const onVisQueryChange = (visQuery: VisualQuery) => { | ||
| const expr = queryModeller.renderQuery(visQuery); | ||
| setState({ expr, visQuery }) | ||
| onChange(index, expr); | ||
| }; | ||
| const queryModeller = useMemo(() => createQueryModellerWithDefaultField(fieldName, [VictoriaLogsQueryOperationCategory.Filters, VictoriaLogsQueryOperationCategory.Operators]), [fieldName]); | ||
| return ( | ||
| <InlineField> | ||
| <> | ||
| <OperationList | ||
| query={state.visQuery} | ||
| datasource={datasource} | ||
| onChange={onVisQueryChange} | ||
| timeRange={timeRange} | ||
| onRunQuery={onRunQuery} | ||
| queryModeller={queryModeller} | ||
| /> | ||
| <hr /> | ||
| <p className={styles.previewText}> | ||
| {state.expr !== '' && state.expr} | ||
archef2000 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| </p> | ||
| </> | ||
| </InlineField> | ||
| ) | ||
| } | ||
|
|
||
| const getStyles = (theme: GrafanaTheme2) => { | ||
| return { | ||
| previewText: css` | ||
| font-size: ${theme.typography.bodySmall.fontSize}; | ||
| ` | ||
| }; | ||
| }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too much nesting makes this logic harder to read. Consider extracting it into a separate function like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the parseFieldMapList function ensures that there are always 3 parts now so a simple check would be enough
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this would be totally save.