-
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): sparkline tags and title (#23273)
- Loading branch information
1 parent
2735d98
commit 90b2ee3
Showing
15 changed files
with
387 additions
and
34 deletions.
There are no files selected for viewing
Binary file modified
BIN
-3.88 KB
(86%)
frontend/__snapshots__/exporter-exporter--dashboard--light.png
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
54 changes: 54 additions & 0 deletions
54
frontend/src/queries/nodes/HogQLX/__snapshots__/render.test.tsx.snap
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,54 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`HogQLX render should render Sparkline 1`] = ` | ||
<ErrorBoundary> | ||
<Sparkline | ||
data={ | ||
[ | ||
1, | ||
2, | ||
3, | ||
] | ||
} | ||
type={ | ||
[ | ||
"line", | ||
] | ||
} | ||
/> | ||
</ErrorBoundary> | ||
`; | ||
|
||
exports[`HogQLX render should render array 1`] = ` | ||
<JSONViewer | ||
collapsed={1} | ||
name={null} | ||
src={ | ||
[ | ||
1, | ||
2, | ||
3, | ||
] | ||
} | ||
/> | ||
`; | ||
|
||
exports[`HogQLX render should render object 1`] = ` | ||
<JSONViewer | ||
collapsed={1} | ||
name={null} | ||
src={ | ||
{ | ||
"a": 1, | ||
"b": 2, | ||
} | ||
} | ||
/> | ||
`; | ||
|
||
exports[`HogQLX render should render unknown tag 1`] = ` | ||
<div> | ||
Unknown tag: | ||
Unknown | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { parseHogQLX, renderHogQLX } from '~/queries/nodes/HogQLX/render' | ||
|
||
describe('HogQLX', () => { | ||
describe('parse', () => { | ||
it('should parse tags', () => { | ||
const value = parseHogQLX(['__hx_tag', 'Sparkline', 'data', [1, 2, 3], 'type', ['line']]) | ||
expect(value).toEqual({ | ||
__hx_tag: 'Sparkline', | ||
data: [1, 2, 3], | ||
type: ['line'], | ||
}) | ||
}) | ||
|
||
it('should parse empty tags', () => { | ||
const value = parseHogQLX(['__hx_tag', 'Sparkline']) | ||
expect(value).toEqual({ | ||
__hx_tag: 'Sparkline', | ||
}) | ||
}) | ||
|
||
it('should parse objects', () => { | ||
const value = parseHogQLX(['__hx_tag', '__hx_obj', 'a', 1, 'b', 2]) | ||
expect(value).toEqual({ | ||
a: 1, | ||
b: 2, | ||
}) | ||
}) | ||
|
||
it('should handle arrays', () => { | ||
const value = parseHogQLX(['a', 'b', 'c']) | ||
expect(value).toEqual(['a', 'b', 'c']) | ||
}) | ||
|
||
it('should handle nested arrays', () => { | ||
const value = parseHogQLX(['a', ['b', 'c']]) | ||
expect(value).toEqual(['a', ['b', 'c']]) | ||
}) | ||
|
||
it('should handle nested objects', () => { | ||
const value = parseHogQLX(['__hx_tag', '__hx_obj', 'a', ['b', 'c']]) | ||
expect(value).toEqual({ | ||
a: ['b', 'c'], | ||
}) | ||
}) | ||
|
||
it('should handle nested objects with tags', () => { | ||
const value = parseHogQLX([ | ||
'__hx_tag', | ||
'__hx_obj', | ||
'a', | ||
['__hx_tag', 'Sparkline', 'data', [1, 2, 3], 'type', ['line']], | ||
]) | ||
expect(value).toEqual({ | ||
a: { | ||
__hx_tag: 'Sparkline', | ||
data: [1, 2, 3], | ||
type: ['line'], | ||
}, | ||
}) | ||
}) | ||
}) | ||
|
||
describe('render', () => { | ||
it('should render Sparkline', () => { | ||
const value = { | ||
__hx_tag: 'Sparkline', | ||
data: [1, 2, 3], | ||
type: ['line'], | ||
} | ||
const element = renderHogQLX(value) | ||
expect(element).toMatchSnapshot() | ||
}) | ||
|
||
it('should render object', () => { | ||
const value = { | ||
a: 1, | ||
b: 2, | ||
} | ||
const element = renderHogQLX(value) | ||
expect(element).toMatchSnapshot() | ||
}) | ||
|
||
it('should render unknown tag', () => { | ||
const value = { | ||
__hx_tag: 'Unknown', | ||
} | ||
const element = renderHogQLX(value) | ||
expect(element).toMatchSnapshot() | ||
}) | ||
|
||
it('should render array', () => { | ||
const value = [1, 2, 3] | ||
const element = renderHogQLX(value) | ||
expect(element).toMatchSnapshot() | ||
}) | ||
}) | ||
}) |
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,44 @@ | ||
import { JSONViewer } from 'lib/components/JSONViewer' | ||
import { Sparkline } from 'lib/lemon-ui/Sparkline' | ||
|
||
import { ErrorBoundary } from '~/layout/ErrorBoundary' | ||
|
||
export function parseHogQLX(value: any): any { | ||
if (!Array.isArray(value)) { | ||
return value | ||
} | ||
if (value[0] === '__hx_tag') { | ||
const object: Record<string, any> = {} | ||
const start = value[1] === '__hx_obj' ? 2 : 0 | ||
for (let i = start; i < value.length; i += 2) { | ||
const key = parseHogQLX(value[i]) | ||
object[key] = parseHogQLX(value[i + 1]) | ||
} | ||
return object | ||
} | ||
return value.map((v) => parseHogQLX(v)) | ||
} | ||
|
||
export function renderHogQLX(value: any): JSX.Element { | ||
const object = parseHogQLX(value) | ||
|
||
if (typeof object === 'object') { | ||
if (Array.isArray(object)) { | ||
return <JSONViewer src={object} name={null} collapsed={object.length > 10 ? 0 : 1} /> | ||
} | ||
|
||
const { __hx_tag: tag, ...rest } = object | ||
if (!tag) { | ||
return <JSONViewer src={rest} name={null} collapsed={Object.keys(rest).length > 10 ? 0 : 1} /> | ||
} else if (tag === 'Sparkline') { | ||
return ( | ||
<ErrorBoundary> | ||
<Sparkline {...rest} data={rest.data ?? []} type={rest.type ?? []} /> | ||
</ErrorBoundary> | ||
) | ||
} | ||
return <div>Unknown tag: {String(tag)}</div> | ||
} | ||
|
||
return <>{String(value)}</> | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from typing import Any | ||
|
||
from posthog.hogql import ast | ||
|
||
HOGQLX_COMPONENTS = ["Sparkline"] | ||
|
||
|
||
def convert_tag_to_hx(node: ast.HogQLXTag) -> ast.Tuple: | ||
attrs: list[ast.Expr] = [ | ||
ast.Constant(value="__hx_tag"), | ||
ast.Constant(value=node.kind), | ||
] | ||
for attribute in node.attributes: | ||
attrs.append(convert_to_hx(attribute.name)) | ||
attrs.append(convert_to_hx(attribute.value)) | ||
return ast.Tuple(exprs=attrs) | ||
|
||
|
||
def convert_dict_to_hx(node: ast.Dict) -> ast.Tuple: | ||
attrs: list[ast.Expr] = [ast.Constant(value="__hx_tag"), ast.Constant(value="__hx_obj")] | ||
for attribute in node.items: | ||
attrs.append(convert_to_hx(attribute[0])) | ||
attrs.append(convert_to_hx(attribute[1])) | ||
return ast.Tuple(exprs=attrs) | ||
|
||
|
||
def convert_to_hx(node: Any) -> ast.Expr: | ||
if isinstance(node, ast.HogQLXTag): | ||
return convert_tag_to_hx(node) | ||
if isinstance(node, ast.Dict): | ||
return convert_dict_to_hx(node) | ||
if isinstance(node, ast.Array) or isinstance(node, ast.Tuple): | ||
return ast.Tuple(exprs=[convert_to_hx(x) for x in node.exprs]) | ||
if isinstance(node, ast.Expr): | ||
return node | ||
if isinstance(node, list) or isinstance(node, tuple): | ||
return ast.Tuple(exprs=[convert_to_hx(x) for x in node]) | ||
return ast.Constant(value=node) |
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
Oops, something went wrong.