Skip to content

Commit afa0d29

Browse files
authored
adding markdown docs for metrics_sql, update metrics_sql docs, change icon from Text to Text/Markdown (#8620)
1 parent 1a76c71 commit afa0d29

File tree

5 files changed

+217
-78
lines changed

5 files changed

+217
-78
lines changed

docs/docs/build/custom-apis/custom-apis.md

Lines changed: 15 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,21 @@ sidebar_label: Custom APIs
77

88
Rill allows you to create custom APIs to pull data out in a flexible manner. You can write custom SQL queries and expose them as API endpoints.
99

10-
To create a custom API, create a new YAML file under the `apis` directory in your Rill project. Currently, we support two types of custom APIs: SQL and Metrics SQL.
10+
To create a custom API, create a new YAML file under the `apis` directory in your Rill project. Currently, we support two types of custom APIs: Metrics SQL and SQL.
11+
12+
## Metrics SQL API
13+
14+
You can write a SQL query referring to metrics definitions and dimensions defined in a [metrics view](/build/metrics-view).
15+
It should have the following structure:
16+
17+
```yaml
18+
type: api
19+
metrics_sql: SELECT publisher, domain, total_records FROM ad_bids_metrics
20+
```
21+
22+
For complete documentation on Metrics SQL API, including querying fundamentals, examples, supported SQL features, templating, and OpenAPI specs, see the [Metrics SQL API documentation](/build/metrics-view/metrics-sql).
23+
24+
1125
1226
## SQL API
1327
@@ -105,76 +119,6 @@ To minimize costs:
105119
- You need the fastest possible query response times
106120

107121

108-
## Metrics SQL API
109-
110-
You can write a SQL query referring to metrics definitions and dimensions defined in a [metrics view](/build/metrics-view).
111-
It should have the following structure:
112-
113-
```yaml
114-
type: api
115-
metrics_sql: SELECT publisher, domain, total_records FROM ad_bids_metrics
116-
```
117-
118-
119-
### Querying Fundamentals
120-
121-
Metrics SQL transforms queries that reference `dimensions` and `measures` within a `metrics view` into their corresponding database columns or expressions. This transformation is based on the mappings defined in a metrics view YAML configuration, enabling reuse of dimension or measure definitions. Additionally, any security policies defined in the metrics view are also inherited.
122-
123-
### Example: Crafting a Metrics SQL Query
124-
125-
Consider a metrics view configured as follows:
126-
```yaml
127-
#metrics/ad_bids_metrics.yaml
128-
type: metrics_view
129-
title: Ad Bids
130-
model: ad_bids
131-
timeseries: timestamp
132-
dimensions:
133-
- name: publisher
134-
expression: toUpper(publisher)
135-
- name: domain
136-
column: domain
137-
measures:
138-
- name: total_records
139-
display_name: Total records
140-
expression: COUNT(*)
141-
```
142-
143-
To query this view, a user might write a Metrics SQL query like:
144-
```sql
145-
SELECT publisher, domain, total_records FROM ad_bids_metrics
146-
```
147-
148-
This Metrics SQL is internally translated to a standard SQL query as follows:
149-
```sql
150-
SELECT toUpper(publisher) AS publisher, domain AS domain, COUNT(*) AS total_records FROM ad_bids_metrics GROUP BY publisher, domain
151-
```
152-
153-
### Security and Compliance
154-
155-
Queries executed via Metrics SQL are subject to the security policies and access controls defined in the metrics view YAML configuration, ensuring data security and compliance.
156-
157-
### Limitations
158-
159-
Metrics SQL is specifically designed for querying metrics views and may not support all features found in standard SQL. Its primary focus is on providing an efficient and easy way to extract data within the constraints of metrics view configurations.
160-
161-
162-
### Supported SQL Features
163-
164-
- **SELECT** statements with plain `dimension` and `measure` references.
165-
- A single **FROM** clause referencing a `metrics view`.
166-
- **WHERE** clause that can reference selected `dimensions` only.
167-
- Operators in **WHERE** and **HAVING** clauses include `=`, `!=`, `>`, `>=`, `<`, `<=`, IN, LIKE, AND, OR, and parentheses for structuring the expression.
168-
- **HAVING** clause for filtering on aggregated results, referencing selected dimension and measure names. Supports the same expression capabilities as the WHERE clause.
169-
- **ORDER BY** clause for sorting the results.
170-
- **LIMIT** and **OFFSET** clauses for controlling the result set size and pagination.
171-
172-
:::warning
173-
The Metrics SQL feature is currently evolving. We are dedicated to enhancing the syntax by introducing additional SQL features, while striving to maintain support for existing syntax. However, please be advised that backward compatibility cannot be guaranteed at all times. Additionally, users should be aware that there may be untested edge cases in the current implementation. We appreciate your understanding as we work to refine and improve this feature.
174-
:::
175-
176-
177-
178122
## SQL Templating
179123

180124
You can use templating to make your SQL query dynamic. We support:
@@ -221,10 +165,6 @@ sql: |
221165
HTTP GET `.../runtime/api/my-api` would return `total_records` for all `publisher`s.
222166
HTTP GET `.../runtime/api/my-api?publisher=Google` would return `total_records` for `Google`.
223167

224-
225-
226-
227-
228168
## Add an OpenAPI spec
229169

230170
You can optionally provide OpenAPI annotations for the request and response schema in your custom API definition. These will automatically be incorporated in the OpenAPI spec for your project (see [Custom API Integration](/integrate/custom-api) for details).

docs/docs/build/dashboards/canvas-widgets/misc.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Miscellaneous widgets in Rill Canvas provide additional functionality for text,
1010

1111
## Text/Markdown
1212

13-
Text widgets allow you to add formatted text, markdown content, and documentation directly to your dashboards.
13+
Text widgets allow you to add formatted text, markdown content, and documentation directly to your dashboards. You can also use `metrics_sql` and `metrics_sql_rows` to display data from Metrics SQL queries.
1414

1515
<ImageCodeToggle
1616
image="/img/build/dashboard/canvas/components/text.png"
@@ -37,6 +37,45 @@ Text widgets allow you to add formatted text, markdown content, and documentatio
3737
codeLanguage="yaml"
3838
/>
3939

40+
### Dynamic Markdown
41+
42+
The `metrics_sql` template function allows you to execute a Metrics SQL query and display the results as formatted text in your dashboard. This is useful for displaying aggregated data or custom metrics.
43+
44+
45+
The `metrics_sql_rows` template function allows you to execute a Metrics SQL query and iterate over the results, displaying each row with custom formatting. This is useful for displaying multiple data points in a structured format.
46+
47+
Use `metrics_sql_rows` to get query results and then iterate over them using Go template syntax. You can access columns using dot notation (`.column_name`) or by assigning a variable name in the range:
48+
49+
<ImageCodeToggle
50+
image="/img/build/dashboard/canvas/components/text.png"
51+
imageAlt="Metrics SQL rows component showing multiple data rows"
52+
code={`- markdown:
53+
content: |-
54+
{{ $rows := metrics_sql_rows "SELECT publisher, domain, total_records FROM ad_bids_metrics ORDER BY total_records DESC LIMIT 3" }}
55+
{{ range $index, $row := $rows }}
56+
<div style="font-weight: 600;">{{ $row.publisher }}</div>
57+
<div>Domain: {{ $row.domain }}</div>
58+
<div>Total: {{ $row.total_records }}</div>
59+
{{ end }}
60+
width: 6`}
61+
codeLanguage="yaml"
62+
/>
63+
64+
Alternatively, you can use dot notation directly without assigning a variable:
65+
66+
```yaml
67+
- markdown:
68+
content: |-
69+
{{ $rows := metrics_sql_rows "SELECT publisher, total_records FROM ad_bids_metrics LIMIT 5" }}
70+
{{ range $rows }}
71+
<div>{{ .publisher }}: {{ .total_records }}</div>
72+
{{ end }}
73+
```
74+
75+
The `metrics_sql_rows` function returns an array of rows that you can iterate over with `{{ range }}`. Access columns using either `{{ $row.column_name }}` (when using named variables) or `{{ .column_name }}` (when using dot notation).
76+
77+
For more information on Metrics SQL syntax and capabilities, see the [Metrics SQL API documentation](/build/metrics-view/metrics-sql).
78+
4079
## Image
4180

4281
Image widgets let you embed images, logos, and visual elements into your dashboards. Put files in your public/ folder and reference them directly in the `url` as `public/image.png`.
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
---
2+
title: Metrics SQL
3+
description: Query metrics views using SQL syntax
4+
sidebar_label: Metrics SQL
5+
---
6+
7+
You can write a SQL query referring to metrics definitions and dimensions defined in a metrics view.
8+
It should have the following structure:
9+
10+
```yaml
11+
type: api
12+
metrics_sql: SELECT publisher, domain, total_records FROM ad_bids_metrics
13+
```
14+
15+
## Querying Fundamentals
16+
17+
Metrics SQL transforms queries that reference `dimensions` and `measures` within a `metrics view` into their corresponding database columns or expressions. This transformation is based on the mappings defined in a metrics view YAML configuration, enabling reuse of dimension or measure definitions. Additionally, any security policies defined in the metrics view are also inherited.
18+
19+
## Example: Crafting a Metrics SQL Query
20+
21+
Consider a metrics view configured as follows:
22+
```yaml
23+
#metrics/ad_bids_metrics.yaml
24+
type: metrics_view
25+
title: Ad Bids
26+
model: ad_bids
27+
timeseries: timestamp
28+
dimensions:
29+
- name: publisher
30+
expression: toUpper(publisher)
31+
- name: domain
32+
column: domain
33+
measures:
34+
- name: total_records
35+
display_name: Total records
36+
expression: COUNT(*)
37+
```
38+
39+
To query this view, a user might write a Metrics SQL query like:
40+
```sql
41+
SELECT publisher, domain, total_records FROM ad_bids_metrics
42+
```
43+
44+
This Metrics SQL is internally translated to a standard SQL query as follows:
45+
```sql
46+
SELECT toUpper(publisher) AS publisher, domain AS domain, COUNT(*) AS total_records FROM ad_bids_metrics GROUP BY publisher, domain
47+
```
48+
49+
## Security and Compliance
50+
51+
Queries executed via Metrics SQL are subject to the security policies and access controls defined in the metrics view YAML configuration, ensuring data security and compliance.
52+
53+
## Limitations
54+
55+
Metrics SQL is specifically designed for querying metrics views and may not support all features found in standard SQL. Its primary focus is on providing an efficient and easy way to extract data within the constraints of metrics view configurations.
56+
57+
## Supported SQL Features
58+
59+
- **SELECT** statements with plain `dimension` and `measure` references.
60+
- A single **FROM** clause referencing a `metrics view`.
61+
- **WHERE** clause that can reference selected `dimensions` only.
62+
- Operators in **WHERE** and **HAVING** clauses include `=`, `!=`, `>`, `>=`, `<`, `<=`, IN, LIKE, AND, OR, and parentheses for structuring the expression.
63+
- **HAVING** clause for filtering on aggregated results, referencing selected dimension and measure names. Supports the same expression capabilities as the WHERE clause.
64+
- **ORDER BY** clause for sorting the results.
65+
- **LIMIT** and **OFFSET** clauses for controlling the result set size and pagination.
66+
67+
:::warning
68+
The Metrics SQL feature is currently evolving. We are dedicated to enhancing the syntax by introducing additional SQL features, while striving to maintain support for existing syntax. However, please be advised that backward compatibility cannot be guaranteed at all times. Additionally, users should be aware that there may be untested edge cases in the current implementation. We appreciate your understanding as we work to refine and improve this feature.
69+
:::
70+
71+
## SQL Templating
72+
73+
You can use templating to make your Metrics SQL query dynamic. We support:
74+
- Dynamic arguments that can be passed in as query parameters during the API call using `{{ .args.<param-name> }}`
75+
- User attributes like email, domain, and admin if available using `{{ .user.<attr> }}` (see integration docs [here](/integrate/custom-api) for when user attributes are available)
76+
- Conditional statements
77+
- Optional parameters paired with conditional statements.
78+
79+
See integration docs [here](/integrate/custom-api) to learn how these are passed in when calling the API.
80+
81+
### Conditional statements
82+
83+
Assume an API endpoint defined as `my-api.yaml`:
84+
```yaml
85+
type: api
86+
metrics_sql: |
87+
SELECT publisher, total_records
88+
{{ if ( .user.admin ) }} ,domain {{ end }}
89+
FROM ad_bids_metrics WHERE timestamp::DATE = '{{ .args.date }}'
90+
{{ if ( .user.admin ) }} GROUP BY publisher, domain {{ else }} GROUP BY publisher {{ end }}
91+
```
92+
If the user is an admin, the API will return the count of records by `publisher` and `domain` for the given date. If the user is not an admin, the API will return the total count of records by `publisher` for the given date.
93+
94+
### Optional parameters
95+
96+
Rill utilizes standard Go templating together with [Sprig](http://masterminds.github.io/sprig/), which adds a number of useful utility functions.
97+
One of those functions is `hasKey`, which in the example below enables optional parameters to be passed to the Custom API endpoint. This allows you to build API endpoints that can handle a wider range of parameters and logic, reducing the need to duplicate API endpoints.
98+
99+
Assume an API endpoint defined as `my-api.yaml`:
100+
```yaml
101+
type: api
102+
metrics_sql: |
103+
SELECT
104+
publisher,
105+
total_records
106+
FROM ad_bids_metrics
107+
{{ if hasKey .args "publisher" }} WHERE publisher = '{{ .args.publisher }}' {{ end }}
108+
GROUP BY publisher
109+
```
110+
111+
HTTP GET `.../runtime/api/my-api` would return `total_records` for all `publisher`s.
112+
HTTP GET `.../runtime/api/my-api?publisher=Google` would return `total_records` for `Google`.
113+
114+
## Add an OpenAPI spec
115+
116+
You can optionally provide OpenAPI annotations for the request and response schema in your custom API definition. These will automatically be incorporated in the OpenAPI spec for your project (see [Custom API Integration](/integrate/custom-api) for details).
117+
118+
Example custom API with request and response schema:
119+
120+
```yaml
121+
type: api
122+
123+
metrics_sql: >
124+
SELECT publisher, total_records
125+
FROM ad_bids_metrics
126+
WHERE domain = '{{ .args.domain }}'
127+
{{ if hasKey .args "limit" }} LIMIT {{ .args.limit }} {{ end }}
128+
{{ if hasKey .args "offset" }} OFFSET {{ .args.offset }} {{ end }}
129+
130+
openapi:
131+
request_schema:
132+
type: object
133+
required:
134+
- domain
135+
properties:
136+
domain:
137+
type: string
138+
description: Domain to filter sales by
139+
limit:
140+
type: integer
141+
description: Optional limit for pagination
142+
offset:
143+
type: integer
144+
description: Optional offset for pagination
145+
146+
response_schema:
147+
type: object
148+
properties:
149+
publisher:
150+
type: string
151+
description: Publisher name
152+
total_records:
153+
type: number
154+
description: Total records for the publisher
155+
```
156+
157+
## How to use Metrics SQL APIs
158+
159+
Refer to the integration docs [here](/integrate/custom-api) to learn how to use Metrics SQL APIs in your application.
160+

web-common/src/features/canvas/AddComponentDropdown.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
export const menuItems: MenuItem[] = [
3030
{ id: "bar_chart", label: "Chart", icon: ChartIcon }, // Default value, will be replaced with random type when clicked
3131
{ id: "table", label: "Table", icon: TableIcon },
32-
{ id: "markdown", label: "Text", icon: TextIcon },
32+
{ id: "markdown", label: "Text/Markdown", icon: TextIcon },
3333
{ id: "kpi_grid", label: "KPI", icon: BigNumberIcon },
3434
{ id: "leaderboard", label: "Leaderboard", icon: LeaderboardIcon },
3535
{ id: "image", label: "Image", icon: ChartIcon },

web-common/src/features/canvas/components/menu-items.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
export const menuItems: MenuItem[] = [
1616
{ id: "stacked_bar", label: "Chart", icon: ChartIcon },
1717
{ id: "table", label: "Table", icon: TableIcon },
18-
{ id: "markdown", label: "Text", icon: TextIcon },
18+
{ id: "markdown", label: "Text/Markdown", icon: TextIcon },
1919
{ id: "kpi_grid", label: "KPI", icon: BigNumberIcon },
2020
{ id: "image", label: "Image", icon: ChartIcon },
2121
{ id: "leaderboard", label: "Leaderboard", icon: TableIcon },

0 commit comments

Comments
 (0)