forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ES|QL] add AST inspector to examples (elastic#182720)
## Summary Something to get us started on the AST fun. At least until we contribute ES|QL support to https://github.com/fkling/astexplorer :) https://github.com/elastic/kibana/assets/315764/82c482b7-cd61-4440-b723-84f863c1b596 --------- Co-authored-by: kibanamachine <[email protected]>
- Loading branch information
1 parent
c2861c1
commit 73864cc
Showing
12 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# esql_ast_inspector | ||
|
||
ES|QL AST inspector tool | ||
|
||
To run this example, start kibana with the `--run-examples` flag. | ||
|
||
```bash | ||
yarn start --run-examples | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"type": "plugin", | ||
"id": "@kbn/esql-ast-inspector-plugin", | ||
"owner": "@elastic/kibana-esql", | ||
"plugin": { | ||
"id": "esqlASTInspector", | ||
"server": false, | ||
"browser": true, | ||
"configPath": [ | ||
"esql_ast_inspector" | ||
], | ||
"requiredPlugins": [ | ||
"dataViews", | ||
"developerExamples" | ||
] | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import React, { useRef, useState } from 'react'; | ||
import { | ||
EuiPage, | ||
EuiPageBody, | ||
EuiPageSection, | ||
EuiPageHeader, | ||
EuiSpacer, | ||
EuiForm, | ||
EuiTextArea, | ||
EuiFormRow, | ||
EuiButton, | ||
} from '@elastic/eui'; | ||
|
||
import type { CoreStart } from '@kbn/core/public'; | ||
|
||
import { EditorError, ESQLAst, getAstAndSyntaxErrors } from '@kbn/esql-ast'; | ||
import { CodeEditor } from '@kbn/code-editor'; | ||
import type { StartDependencies } from './plugin'; | ||
|
||
export const App = (props: { core: CoreStart; plugins: StartDependencies }) => { | ||
const [currentErrors, setErrors] = useState<EditorError[]>([]); | ||
const [currentQuery, setQuery] = useState( | ||
'from index1 | eval var0 = round(numberField, 2) | stats by stringField' | ||
); | ||
|
||
const inputRef = useRef<HTMLTextAreaElement | null>(null); | ||
|
||
const [ast, setAST] = useState<ESQLAst>(getAstAndSyntaxErrors(currentQuery).ast); | ||
|
||
const parseQuery = (query: string) => { | ||
const { ast: _ast, errors } = getAstAndSyntaxErrors(query); | ||
setErrors(errors); | ||
setAST(_ast); | ||
}; | ||
|
||
return ( | ||
<EuiPage> | ||
<EuiPageBody style={{ maxWidth: 800, margin: '0 auto' }}> | ||
<EuiPageHeader paddingSize="s" bottomBorder={true} pageTitle="ES|QL AST Inspector" /> | ||
<EuiPageSection paddingSize="s"> | ||
<p>This app gives you the AST for a particular ES|QL query.</p> | ||
|
||
<EuiSpacer /> | ||
|
||
<EuiForm> | ||
<EuiFormRow | ||
fullWidth | ||
label="Query" | ||
isInvalid={Boolean(currentErrors.length)} | ||
error={currentErrors.map((error) => error.message)} | ||
> | ||
<EuiTextArea | ||
inputRef={(node) => { | ||
inputRef.current = node; | ||
}} | ||
isInvalid={Boolean(currentErrors.length)} | ||
fullWidth | ||
value={currentQuery} | ||
onChange={(e) => setQuery(e.target.value)} | ||
css={{ | ||
height: '5em', | ||
}} | ||
/> | ||
</EuiFormRow> | ||
<EuiFormRow fullWidth> | ||
<EuiButton fullWidth onClick={() => parseQuery(inputRef.current?.value ?? '')}> | ||
Parse | ||
</EuiButton> | ||
</EuiFormRow> | ||
</EuiForm> | ||
<EuiSpacer /> | ||
<CodeEditor | ||
allowFullScreen={true} | ||
languageId={'json'} | ||
value={JSON.stringify(ast, null, 2)} | ||
/> | ||
</EuiPageSection> | ||
</EuiPageBody> | ||
</EuiPage> | ||
); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { ESQLASTInspectorPlugin } from './plugin'; | ||
|
||
export const plugin = () => new ESQLASTInspectorPlugin(); |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import * as React from 'react'; | ||
import { render, unmountComponentAtNode } from 'react-dom'; | ||
|
||
import type { CoreSetup, AppMountParameters } from '@kbn/core/public'; | ||
import type { StartDependencies } from './plugin'; | ||
|
||
export const mount = | ||
(coreSetup: CoreSetup<StartDependencies>) => | ||
async ({ element }: AppMountParameters) => { | ||
const [core, plugins] = await coreSetup.getStartServices(); | ||
const { App } = await import('./app'); | ||
|
||
const i18nCore = core.i18n; | ||
|
||
const reactElement = ( | ||
<i18nCore.Context> | ||
<App core={core} plugins={plugins} /> | ||
</i18nCore.Context> | ||
); | ||
|
||
render(reactElement, element); | ||
return () => { | ||
unmountComponentAtNode(element); | ||
plugins.data.search.session.clear(); | ||
}; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { Plugin, CoreSetup } from '@kbn/core/public'; | ||
import { DataPublicPluginStart } from '@kbn/data-plugin/public'; | ||
import { DataViewsPublicPluginStart } from '@kbn/data-views-plugin/public'; | ||
import { DeveloperExamplesSetup } from '@kbn/developer-examples-plugin/public'; | ||
import { mount } from './mount'; | ||
import image from './esql_ast_inspector.png'; | ||
|
||
export interface SetupDependencies { | ||
developerExamples: DeveloperExamplesSetup; | ||
} | ||
|
||
export interface StartDependencies { | ||
data: DataPublicPluginStart; | ||
dataViews: DataViewsPublicPluginStart; | ||
} | ||
|
||
export class ESQLASTInspectorPlugin | ||
implements Plugin<void, void, SetupDependencies, StartDependencies> | ||
{ | ||
public setup(core: CoreSetup<StartDependencies>, { developerExamples }: SetupDependencies) { | ||
core.application.register({ | ||
id: 'esql_ast_inspector', | ||
title: 'ES|QL AST Inspector', | ||
visibleIn: [], | ||
mount: mount(core), | ||
}); | ||
|
||
developerExamples.register({ | ||
appId: 'esql_ast_inspector', | ||
title: 'ES|QL AST Inspector', | ||
description: 'Inspect the ES|QL AST.', | ||
image, | ||
links: [ | ||
{ | ||
label: 'README', | ||
href: 'https://github.com/elastic/kibana/tree/main/packages/kbn-esql-validation-autocomplete/README.md', | ||
iconType: 'logoGithub', | ||
size: 's', | ||
target: '_blank', | ||
}, | ||
], | ||
}); | ||
} | ||
|
||
public start() {} | ||
|
||
public stop() {} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"outDir": "target/types" | ||
}, | ||
"include": [ | ||
"index.ts", | ||
"public/**/*.ts", | ||
"public/**/*.tsx", | ||
"server/**/*.ts", | ||
"../../../typings/**/*" | ||
], | ||
"exclude": [ | ||
"target/**/*", | ||
], | ||
"kbn_references": [ | ||
"@kbn/core", | ||
"@kbn/data-plugin", | ||
"@kbn/data-views-plugin", | ||
"@kbn/developer-examples-plugin", | ||
"@kbn/esql-ast", | ||
"@kbn/code-editor", | ||
] | ||
} |
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