-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hogql): HogQLQuery node and data table support (#14265)
- Loading branch information
1 parent
1d7afd3
commit 1456301
Showing
24 changed files
with
486 additions
and
62 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
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
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
55 changes: 55 additions & 0 deletions
55
frontend/src/queries/nodes/HogQLQuery/HogQLQueryEditor.tsx
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,55 @@ | ||
import { useActions, useValues } from 'kea' | ||
import { HogQLQuery } from '~/queries/schema' | ||
import { useState } from 'react' | ||
import { hogQLQueryEditorLogic } from './hogQLQueryEditorLogic' | ||
import MonacoEditor from '@monaco-editor/react' | ||
import { AutoSizer } from 'react-virtualized/dist/es/AutoSizer' | ||
import { LemonButton } from 'lib/lemon-ui/LemonButton' | ||
|
||
export interface HogQLQueryEditorProps { | ||
query: HogQLQuery | ||
setQuery?: (query: HogQLQuery) => void | ||
} | ||
|
||
let uniqueNode = 0 | ||
export function HogQLQueryEditor(props: HogQLQueryEditorProps): JSX.Element { | ||
const [key] = useState(() => uniqueNode++) | ||
const hogQLQueryEditorLogicProps = { query: props.query, setQuery: props.setQuery, key } | ||
const { queryInput } = useValues(hogQLQueryEditorLogic(hogQLQueryEditorLogicProps)) | ||
const { setQueryInput, saveQuery } = useActions(hogQLQueryEditorLogic(hogQLQueryEditorLogicProps)) | ||
|
||
return ( | ||
<div className={'flex flex-col p-2 bg-border space-y-2 resize-y overflow-auto h-80 w-full'}> | ||
<div className="flex-1"> | ||
<AutoSizer disableWidth> | ||
{({ height }) => ( | ||
<MonacoEditor | ||
theme="vs-light" | ||
className="border" | ||
language="mysql" | ||
value={queryInput} | ||
onChange={(v) => setQueryInput(v ?? '')} | ||
height={height} | ||
options={{ | ||
minimap: { | ||
enabled: false, | ||
}, | ||
wordWrap: 'on', | ||
}} | ||
/> | ||
)} | ||
</AutoSizer> | ||
</div> | ||
<LemonButton | ||
onClick={saveQuery} | ||
type="primary" | ||
status={'muted-alt'} | ||
disabledReason={!props.setQuery ? 'No permission to update' : undefined} | ||
fullWidth | ||
center | ||
> | ||
{!props.setQuery ? 'No permission to update' : 'Update'} | ||
</LemonButton> | ||
</div> | ||
) | ||
} |
45 changes: 45 additions & 0 deletions
45
frontend/src/queries/nodes/HogQLQuery/hogQLQueryEditorLogic.ts
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,45 @@ | ||
import { actions, kea, key, listeners, path, props, propsChanged, reducers } from 'kea' | ||
import { format } from 'sql-formatter' | ||
import { HogQLQuery } from '~/queries/schema' | ||
|
||
import type { hogQLQueryEditorLogicType } from './hogQLQueryEditorLogicType' | ||
|
||
function formatSQL(sql: string): string { | ||
return format(sql, { | ||
language: 'mysql', | ||
tabWidth: 2, | ||
keywordCase: 'preserve', | ||
linesBetweenQueries: 2, | ||
indentStyle: 'tabularRight', | ||
}) | ||
} | ||
export interface HogQLQueryEditorLogicProps { | ||
key: number | ||
query: HogQLQuery | ||
setQuery?: (query: HogQLQuery) => void | ||
} | ||
|
||
export const hogQLQueryEditorLogic = kea<hogQLQueryEditorLogicType>([ | ||
path(['queries', 'nodes', 'HogQLQuery', 'hogQLQueryEditorLogic']), | ||
props({} as HogQLQueryEditorLogicProps), | ||
key((props) => props.key), | ||
propsChanged(({ actions, props }, oldProps) => { | ||
if (props.query.query !== oldProps.query.query) { | ||
actions.setQueryInput(formatSQL(props.query.query)) | ||
} | ||
}), | ||
actions({ | ||
saveQuery: true, | ||
setQueryInput: (queryInput: string) => ({ queryInput }), | ||
}), | ||
reducers(({ props }) => ({ | ||
queryInput: [formatSQL(props.query.query), { setQueryInput: (_, { queryInput }) => queryInput }], | ||
})), | ||
listeners(({ actions, props, values }) => ({ | ||
saveQuery: () => { | ||
const formattedQuery = formatSQL(values.queryInput) | ||
actions.setQueryInput(formattedQuery) | ||
props.setQuery?.({ ...props.query, query: formattedQuery }) | ||
}, | ||
})), | ||
]) |
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
Oops, something went wrong.