merge with main
This commit is contained in:
+3
-15
@@ -540,9 +540,6 @@ exports[`better eslint`] = {
|
||||
[0, 0, 0, "No untranslated strings in text props. Wrap text with <Trans /> or use t()", "0"],
|
||||
[0, 0, 0, "No untranslated strings in text props. Wrap text with <Trans /> or use t()", "1"]
|
||||
],
|
||||
"packages/grafana-ui/src/components/Combobox/Combobox.tsx:5381": [
|
||||
[0, 0, 0, "Do not use any type assertions.", "0"]
|
||||
],
|
||||
"packages/grafana-ui/src/components/Combobox/MultiCombobox.tsx:5381": [
|
||||
[0, 0, 0, "Do not use any type assertions.", "0"]
|
||||
],
|
||||
@@ -635,9 +632,6 @@ exports[`better eslint`] = {
|
||||
[0, 0, 0, "No untranslated strings in text props. Wrap text with <Trans /> or use t()", "1"],
|
||||
[0, 0, 0, "No untranslated strings in text props. Wrap text with <Trans /> or use t()", "2"]
|
||||
],
|
||||
"packages/grafana-ui/src/components/DateTimePickers/WeekStartPicker.tsx:5381": [
|
||||
[0, 0, 0, "Do not use any type assertions.", "0"]
|
||||
],
|
||||
"packages/grafana-ui/src/components/FileDropzone/FileDropzone.tsx:5381": [
|
||||
[0, 0, 0, "No untranslated strings in text props. Wrap text with <Trans /> or use t()", "0"]
|
||||
],
|
||||
@@ -3661,10 +3655,9 @@ exports[`better eslint`] = {
|
||||
[0, 0, 0, "Do not use any type assertions.", "1"],
|
||||
[0, 0, 0, "Do not use any type assertions.", "2"],
|
||||
[0, 0, 0, "Do not use any type assertions.", "3"],
|
||||
[0, 0, 0, "Do not use any type assertions.", "4"],
|
||||
[0, 0, 0, "Unexpected any. Specify a different type.", "4"],
|
||||
[0, 0, 0, "Unexpected any. Specify a different type.", "5"],
|
||||
[0, 0, 0, "Unexpected any. Specify a different type.", "6"],
|
||||
[0, 0, 0, "Unexpected any. Specify a different type.", "7"]
|
||||
[0, 0, 0, "Unexpected any. Specify a different type.", "6"]
|
||||
],
|
||||
"public/app/features/dashboard/api/v1.ts:5381": [
|
||||
[0, 0, 0, "Do not use any type assertions.", "0"]
|
||||
@@ -4991,12 +4984,7 @@ exports[`better eslint`] = {
|
||||
[0, 0, 0, "No untranslated strings in text props. Wrap text with <Trans /> or use t()", "4"]
|
||||
],
|
||||
"public/app/features/expressions/components/Threshold.tsx:5381": [
|
||||
[0, 0, 0, "No untranslated strings in text props. Wrap text with <Trans /> or use t()", "0"],
|
||||
[0, 0, 0, "No untranslated strings in text props. Wrap text with <Trans /> or use t()", "1"],
|
||||
[0, 0, 0, "No untranslated strings in text props. Wrap text with <Trans /> or use t()", "2"],
|
||||
[0, 0, 0, "No untranslated strings in text props. Wrap text with <Trans /> or use t()", "3"],
|
||||
[0, 0, 0, "No untranslated strings in text props. Wrap text with <Trans /> or use t()", "4"],
|
||||
[0, 0, 0, "No untranslated strings in text props. Wrap text with <Trans /> or use t()", "5"]
|
||||
[0, 0, 0, "No untranslated strings in text props. Wrap text with <Trans /> or use t()", "0"]
|
||||
],
|
||||
"public/app/features/expressions/guards.ts:5381": [
|
||||
[0, 0, 0, "\'@grafana/runtime/src/utils/DataSourceWithBackend\' import is restricted from being used by a pattern. Import from the public export instead.", "0"],
|
||||
|
||||
@@ -757,6 +757,7 @@ embed.go @grafana/grafana-as-code
|
||||
/.github/workflows/add-to-whats-new.yml @grafana/docs-tooling
|
||||
/.github/workflows/auto-triager/ @grafana/plugins-platform-frontend
|
||||
/.github/workflows/alerting-swagger-gen.yml @grafana/alerting-backend
|
||||
/.github/workflows/alerting-update-module.yml @grafana/alerting-backend
|
||||
/.github/workflows/auto-milestone.yml @grafana/grafana-developer-enablement-squad
|
||||
/.github/workflows/backport.yml @grafana/grafana-developer-enablement-squad
|
||||
/.github/workflows/bump-version.yml @grafana/grafana-developer-enablement-squad
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
name: Update Alerting Module
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
update-grafana:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: write
|
||||
pull-requests: write
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # 4.2.2
|
||||
|
||||
- name: Check if update branch exists
|
||||
run: |
|
||||
if git ls-remote --heads origin update-alerting-module | grep -q 'update-alerting-module'; then
|
||||
echo "Branch 'update-alerting-module' already exists. There might be an open PR with Grafana updates."
|
||||
echo "Please review and merge/close the existing PR before running this workflow again."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- name: Setup Go
|
||||
uses: actions/setup-go@f111f3307d8850f501ac008e886eec1fd1932a34 # 5.3.0
|
||||
with:
|
||||
"go-version-file": "go.mod"
|
||||
|
||||
- name: Extract current commit hash of alerting module
|
||||
id: current-commit
|
||||
run: |
|
||||
FROM_COMMIT=$(go list -m -json github.com/grafana/alerting | jq -r '.Version' | grep -oP '(?<=-)[a-f0-9]+$')
|
||||
echo "from_commit=$FROM_COMMIT" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Get latest commit
|
||||
id: latest-commit
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
run: |
|
||||
TO_COMMIT=$(gh api repos/grafana/alerting/commits/main --jq '.sha')
|
||||
if [ -z "$TO_COMMIT" ]; then
|
||||
echo "Failed to fetch latest commit"
|
||||
exit 1
|
||||
fi
|
||||
echo "to_commit=$TO_COMMIT" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Compare commit hashes
|
||||
run: |
|
||||
FROM_COMMIT="${{ steps.current-commit.outputs.from_commit }}"
|
||||
TO_COMMIT="${{ steps.latest-commit.outputs.to_commit }}"
|
||||
|
||||
# Compare just the length of the shorter hash
|
||||
SHORT_TO_COMMIT="${TO_COMMIT:0:${#FROM_COMMIT}}"
|
||||
|
||||
if [ "$FROM_COMMIT" = "$SHORT_TO_COMMIT" ]; then
|
||||
echo "Current version ($FROM_COMMIT) is already at latest ($SHORT_TO_COMMIT). No update needed."
|
||||
exit 0
|
||||
fi
|
||||
echo "Updates available: $FROM_COMMIT -> $TO_COMMIT"
|
||||
|
||||
- name: Check for commit history
|
||||
id: check-commits
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
run: |
|
||||
# get all commits that contains 'Alerting:' in the message
|
||||
ALERTING_COMMITS=$(gh api repos/grafana/alerting/compare/${{ steps.current-commit.outputs.from_commit }}...${{ steps.latest-commit.outputs.to_commit }} \
|
||||
--jq '.commits[].commit.message | split("\n")[0]') || true
|
||||
|
||||
# Use printf instead of echo -e for better multiline handling
|
||||
printf "%s\n" "$ALERTING_COMMITS"
|
||||
|
||||
# make the list for markdown and replace PR numbers with links
|
||||
ALERTING_COMMITS_FORMATTED=$(echo "$ALERTING_COMMITS" | while read -r line; do echo "- $line" | sed -E 's/\(#([0-9]+)\)/[#\1](https:\/\/github.com\/grafana\/grafana\/pull\/\1)/g'; done)
|
||||
|
||||
echo "alerting_commits<<EOF" >> $GITHUB_OUTPUT
|
||||
echo "$ALERTING_COMMITS_FORMATTED" >> $GITHUB_OUTPUT
|
||||
echo "EOF" >> $GITHUB_OUTPUT
|
||||
|
||||
- name: Update alerting module
|
||||
env:
|
||||
GOSUMDB: off
|
||||
run: |
|
||||
go get github.com/grafana/alerting@${{ steps.latest-commit.outputs.to_commit }}
|
||||
make update-workspace
|
||||
|
||||
- id: get-secrets
|
||||
uses: grafana/shared-workflows/actions/get-vault-secrets@28361cdb22223e5f1e34358c86c20908e7248760 # 1.1.0
|
||||
with:
|
||||
repo_secrets: |
|
||||
GITHUB_APP_ID=github-app:app-id
|
||||
GITHUB_APP_PRIVATE_KEY=github-app:private-key
|
||||
|
||||
- name: "Generate token"
|
||||
id: generate_token
|
||||
uses: actions/create-github-app-token@0d564482f06ca65fa9e77e2510873638c82206f2 # 1.11.5
|
||||
with:
|
||||
app-id: ${{ env.GITHUB_APP_ID }}
|
||||
private-key: ${{ env.GITHUB_APP_PRIVATE_KEY }}
|
||||
|
||||
- name: Create Pull Request
|
||||
uses: peter-evans/create-pull-request@67ccf781d68cd99b580ae25a5c18a1cc84ffff1f # 7.0.6
|
||||
id: create-pr
|
||||
with:
|
||||
token: '${{ steps.generate_token.outputs.token }}'
|
||||
title: 'Alerting: Update alerting module to ${{ steps.latest-commit.outputs.to_commit }}'
|
||||
branch: alerting/update-alerting-module
|
||||
delete-branch: true
|
||||
body: |
|
||||
Updates Grafana Alerting module to latest version.
|
||||
|
||||
Compare changes: https://github.com/grafana/alerting/compare/${{ steps.current-commit.outputs.from_commit }}...${{ steps.latest-commit.outputs.to_commit }}
|
||||
<details>
|
||||
<summary>Commits</summary>
|
||||
|
||||
${{ steps.check-commits.outputs.alerting_commits }}
|
||||
|
||||
</details>
|
||||
|
||||
Created by: [GitHub Action Job](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})
|
||||
- name: Add PR URL to Summary
|
||||
if: steps.create-pr.outputs.pull-request-url != ''
|
||||
run: |
|
||||
echo "## Pull Request Created" >> $GITHUB_STEP_SUMMARY
|
||||
echo "🔗 [View Pull Request](${{ steps.create-pr.outputs.pull-request-url }})" >> $GITHUB_STEP_SUMMARY
|
||||
@@ -2,7 +2,7 @@
|
||||
aliases:
|
||||
- ../data-sources/postgres/
|
||||
- ../features/datasources/postgres/
|
||||
description: Guide for using PostgreSQL in Grafana
|
||||
description: Introduction to the PostgreSQL data source in Grafana.
|
||||
keywords:
|
||||
- grafana
|
||||
- postgresql
|
||||
@@ -16,506 +16,58 @@ menuTitle: PostgreSQL
|
||||
title: PostgreSQL data source
|
||||
weight: 1200
|
||||
refs:
|
||||
provisioning-data-sources:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/administration/provisioning/#datasources
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/administration/provisioning/#datasources
|
||||
variables:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/
|
||||
add-template-variables-interval-ms:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/add-template-variables/#__interval_ms
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/add-template-variables/#__interval_ms
|
||||
add-template-variables-interval:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/add-template-variables/#__interval
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/add-template-variables/#__interval
|
||||
annotate-visualizations:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/build-dashboards/annotate-visualizations/
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/build-dashboards/annotate-visualizations/
|
||||
configure-standard-options-display-name:
|
||||
configure-postgres-data-source:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/panels-visualizations/configure-standard-options/#display-name
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/datasources/postgres/configure/
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/panels-visualizations/configure-standard-options/#display-name
|
||||
data-source-management:
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/datasources/postgres/configure/
|
||||
postgres-query-editor:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/administration/data-source-management/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/datasources/postgres/query-editor/
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/administration/data-source-management/
|
||||
variable-syntax-advanced-variable-format-options:
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/datasources/postgres/query-editor/
|
||||
alerting:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/variable-syntax/#advanced-variable-format-options
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/alerting/
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/variable-syntax/#advanced-variable-format-options
|
||||
destination: /docs/grafana-cloud/alerting-and-irm/alerting/
|
||||
transformations:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/panels-visualizations/query-transform-data/transform-data/
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana-cloud/visualizations/panels-visualizations/query-transform-data/transform-data/
|
||||
visualizations:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/panels-visualizations/visualizations/
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana-cloud/visualizations/panels-visualizations/visualizations/
|
||||
---
|
||||
|
||||
# PostgreSQL data source
|
||||
|
||||
Grafana ships with a built-in PostgreSQL data source plugin that allows you to query and visualize data from a PostgreSQL compatible database.
|
||||
Grafana includes a built-in PostgreSQL data source plugin, enabling you to query and visualize data from any PostgreSQL-compatible database. You don't need to install a plugin to add the PostgreSQL data source to your Grafana instance.
|
||||
|
||||
For instructions on how to add a data source to Grafana, refer to the [administration documentation](ref:data-source-management).
|
||||
Only users with the organization administrator role can add data sources.
|
||||
Administrators can also [configure the data source via YAML](#provision-the-data-source) with Grafana's provisioning system.
|
||||
Grafana offers several configuration options for this data source as well as a visual and code-based query editor.
|
||||
|
||||
## Get started with the PostgreSQL data source
|
||||
|
||||
The following documents will help you get started with the PostgreSQL data source in Grafana:
|
||||
|
||||
- [Configure the PostgreSQL data source](ref:configure-postgres-data-source)
|
||||
- [PostgreSQL query editor](ref:postgres-query-editor)
|
||||
|
||||
After you have configured the data source you can:
|
||||
|
||||
- Create a variety of [visualizations](ref:visualizations)
|
||||
- Add [annotations](ref:annotate-visualizations)
|
||||
- Set up [alerting](ref:alerting)
|
||||
- Add [transformations](ref:transformations)
|
||||
|
||||
View a PostgreSQL overview on Grafana Play:
|
||||
|
||||
{{< docs/play title="PostgreSQL Overview" url="https://play.grafana.org/d/ddvpgdhiwjvuod/postgresql-overview" >}}
|
||||
|
||||
## PostgreSQL settings
|
||||
|
||||
To configure basic settings for the data source, complete the following steps:
|
||||
|
||||
1. Click **Connections** in the left-side menu.
|
||||
1. Under Your connections, click **Data sources**.
|
||||
1. Enter `PostgreSQL` in the search bar.
|
||||
1. Select **PostgreSQL**.
|
||||
|
||||
The **Settings** tab of the data source is displayed.
|
||||
|
||||
1. Set the data source's basic configuration options:
|
||||
|
||||
| Name | Description |
|
||||
| --------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| **Name** | The data source name. This is how you refer to the data source in panels and queries. |
|
||||
| **Default** | Default data source means that it will be pre-selected for new panels. |
|
||||
| **Host** | The IP address/hostname and optional port of your PostgreSQL instance. _Do not_ include the database name. The connection string for connecting to Postgres will not be correct and it may cause errors. |
|
||||
| **Database** | Name of your PostgreSQL database. |
|
||||
| **User** | Database user's login/username |
|
||||
| **Password** | Database user's password |
|
||||
| **SSL Mode** | Determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the server. When SSL Mode is disabled, SSL Method and Auth Details would not be visible. |
|
||||
| **SSL Auth Details Method** | Determines whether the SSL Auth details will be configured as a file path or file content. |
|
||||
| **SSL Auth Details Value** | File path or file content of SSL root certificate, client certificate and client key |
|
||||
| **Max open** | The maximum number of open connections to the database, default `100`. |
|
||||
| **Max idle** | The maximum number of connections in the idle connection pool, default `100`. |
|
||||
| **Auto (max idle)** | If set will set the maximum number of idle connections to the number of maximum open connections. Default is `true`. |
|
||||
| **Max lifetime** | The maximum amount of time in seconds a connection may be reused, default `14400`/4 hours. |
|
||||
| **Version** | Determines which functions are available in the query builder. |
|
||||
| **TimescaleDB** | A time-series database built as a PostgreSQL extension. When enabled, Grafana uses `time_bucket` in the `$__timeGroup` macro to display TimescaleDB specific aggregate functions in the query builder. For more information, see [TimescaleDB documentation](https://docs.timescale.com/timescaledb/latest/tutorials/grafana/grafana-timescalecloud/#connect-timescaledb-and-grafana). |
|
||||
|
||||
### Min time interval
|
||||
|
||||
A lower limit for the [`$__interval`](ref:add-template-variables-interval) and [`$__interval_ms`](ref:add-template-variables-interval-ms) variables.
|
||||
Recommended to be set to write frequency, for example `1m` if your data is written every minute.
|
||||
This option can also be overridden/configured in a dashboard panel under data source options. It's important to note that this value **needs** to be formatted as a
|
||||
number followed by a valid time identifier, e.g. `1m` (1 minute) or `30s` (30 seconds). The following time identifiers are supported:
|
||||
|
||||
| Identifier | Description |
|
||||
| ---------- | ----------- |
|
||||
| `y` | year |
|
||||
| `M` | month |
|
||||
| `w` | week |
|
||||
| `d` | day |
|
||||
| `h` | hour |
|
||||
| `m` | minute |
|
||||
| `s` | second |
|
||||
| `ms` | millisecond |
|
||||
|
||||
### Database user permissions (Important!)
|
||||
|
||||
The database user you specify when you add the data source should only be granted SELECT permissions on
|
||||
the specified database and tables you want to query. Grafana does not validate that the query is safe. The query
|
||||
could include any SQL statement. For example, statements like `DELETE FROM user;` and `DROP TABLE user;` would be
|
||||
executed. To protect against this we **highly** recommend you create a specific PostgreSQL user with restricted permissions.
|
||||
|
||||
Example:
|
||||
|
||||
```sql
|
||||
CREATE USER grafanareader WITH PASSWORD 'password';
|
||||
GRANT USAGE ON SCHEMA schema TO grafanareader;
|
||||
GRANT SELECT ON schema.table TO grafanareader;
|
||||
```
|
||||
|
||||
Make sure the user does not get any unwanted privileges from the public role.
|
||||
|
||||
## Query builder
|
||||
|
||||
{{< figure src="/static/img/docs/screenshot-postgres-query-editor.png" class="docs-image--no-shadow" caption="PostgreSQL query builder" >}}
|
||||
|
||||
The PostgreSQL query builder is available when editing a panel using a PostgreSQL data source. The built query can be run by pressing the `Run query` button in the top right corner of the editor.
|
||||
|
||||
### Format
|
||||
|
||||
The response from PostgreSQL can be formatted as either a table or as a time series. To use the time series format one of the columns must be named `time`.
|
||||
|
||||
### Dataset and table selection
|
||||
|
||||
The dataset dropdown will be populated with the configured database to which the user has access.
|
||||
The table dropdown is populated with the tables that are available within that database.
|
||||
|
||||
### Columns and Aggregation functions (SELECT)
|
||||
|
||||
Using the dropdown, select a column to include in the data. You can also specify an optional aggregation function.
|
||||
|
||||
Add further value columns by clicking the plus button and another column dropdown appears.
|
||||
|
||||
{{< docs/shared source="grafana" lookup="datasources/sql-query-builder-macros.md" version="<GRAFANA_VERSION>" >}}
|
||||
|
||||
### Filter data (WHERE)
|
||||
|
||||
To add a filter, toggle the **Filter** switch at the top of the editor.
|
||||
This reveals a **Filter by column value** section with two dropdown selectors.
|
||||
|
||||
Use the first dropdown to choose whether all of the filters need to match (`AND`), or if only one of the filters needs to match (`OR`).
|
||||
Use the second dropdown to choose a filter.
|
||||
|
||||
To filter on more columns, click the plus (`+`) button to the right of the condition dropdown.
|
||||
|
||||
To remove a filter, click the `x` button next to that filter's dropdown.
|
||||
|
||||
After selecting a date type column, you can choose Macros from the operators list and select timeFilter which will add the $\_\_timeFilter macro to the query with the selected date column.
|
||||
|
||||
### Group By
|
||||
|
||||
To group the results by column, flip the group switch at the top of the editor. You can then choose which column to group the results by. The group by clause can be removed by pressing the X button.
|
||||
|
||||
### Preview
|
||||
|
||||
By flipping the preview switch at the top of the editor, you can get a preview of the SQL query generated by the query builder.
|
||||
|
||||
### Provision the data source
|
||||
|
||||
It's now possible to configure data sources using config files with Grafana's provisioning system. You can read more about how it works and all the settings you can set for data sources on the [provisioning docs page](ref:provisioning-data-sources).
|
||||
|
||||
#### Provisioning example
|
||||
|
||||
```yaml
|
||||
apiVersion: 1
|
||||
|
||||
datasources:
|
||||
- name: Postgres
|
||||
type: postgres
|
||||
url: localhost:5432
|
||||
user: grafana
|
||||
secureJsonData:
|
||||
password: 'Password!'
|
||||
jsonData:
|
||||
database: grafana
|
||||
sslmode: 'disable' # disable/require/verify-ca/verify-full
|
||||
maxOpenConns: 100
|
||||
maxIdleConns: 100
|
||||
maxIdleConnsAuto: true
|
||||
connMaxLifetime: 14400
|
||||
postgresVersion: 903 # 903=9.3, 904=9.4, 905=9.5, 906=9.6, 1000=10
|
||||
timescaledb: false
|
||||
```
|
||||
|
||||
{{% admonition type="note" %}}
|
||||
In the above code, the `postgresVersion` value of `10` refers to version PostgreSQL 10 and above.
|
||||
{{% /admonition %}}
|
||||
|
||||
#### Troubleshoot provisioning
|
||||
|
||||
If you encounter metric request errors or other issues:
|
||||
|
||||
- Make sure your data source YAML file parameters exactly match the example. This includes parameter names and use of quotation marks.
|
||||
- Make sure the `database` name is not included in the `url`.
|
||||
|
||||
## Code editor
|
||||
|
||||
{{< figure src="/static/img/docs/v92/sql_code_editor.png" class="docs-image--no-shadow" >}}
|
||||
|
||||
To make advanced queries, switch to the code editor by clicking `code` in the top right corner of the editor. The code editor support autocompletion of tables, columns, SQL keywords, standard sql functions, Grafana template variables and Grafana macros. Columns cannot be completed before a table has been specified.
|
||||
|
||||
You can expand the code editor by pressing the `chevron` pointing downwards in the lower right corner of the code editor.
|
||||
|
||||
`CTRL/CMD + Return` works as a keyboard shortcut to run the query.
|
||||
|
||||
## Macros
|
||||
|
||||
Macros can be used within a query to simplify syntax and allow for dynamic parts.
|
||||
|
||||
| Macro example | Description |
|
||||
| ----------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `$__time(dateColumn)` | Will be replaced by an expression to convert to a UNIX timestamp and rename the column to `time_sec`. For example, _UNIX_TIMESTAMP(dateColumn) as time_sec_ |
|
||||
| `$__timeEpoch(dateColumn)` | Will be replaced by an expression to convert to a UNIX timestamp and rename the column to `time_sec`. For example, _UNIX_TIMESTAMP(dateColumn) as time_sec_ |
|
||||
| `$__timeFilter(dateColumn)` | Will be replaced by a time range filter using the specified column name. For example, _dateColumn BETWEEN FROM_UNIXTIME(1494410783) AND FROM_UNIXTIME(1494410983)_ |
|
||||
| `$__timeFrom()` | Will be replaced by the start of the currently active time selection. For example, _FROM_UNIXTIME(1494410783)_ |
|
||||
| `$__timeTo()` | Will be replaced by the end of the currently active time selection. For example, _FROM_UNIXTIME(1494410983)_ |
|
||||
| `$__timeGroup(dateColumn,'5m')` | Will be replaced by an expression usable in GROUP BY clause. For example, *cast(cast(UNIX_TIMESTAMP(dateColumn)/(300) as signed)*300 as signed),\* |
|
||||
| `$__timeGroup(dateColumn,'5m', 0)` | Same as above but with a fill parameter so missing points in that series will be added by grafana and 0 will be used as value (only works with time series queries). |
|
||||
| `$__timeGroup(dateColumn,'5m', NULL)` | Same as above but NULL will be used as value for missing points (only works with time series queries). |
|
||||
| `$__timeGroup(dateColumn,'5m', previous)` | Same as above but the previous value in that series will be used as fill value if no value has been seen yet NULL will be used (only works with time series queries). |
|
||||
| `$__timeGroupAlias(dateColumn,'5m')` | Will be replaced identical to $\_\_timeGroup but with an added column alias. |
|
||||
| `$__unixEpochFilter(dateColumn)` | Will be replaced by a time range filter using the specified column name with times represented as Unix timestamp. For example, _dateColumn > 1494410783 AND dateColumn < 1494497183_ |
|
||||
| `$__unixEpochFrom()` | Will be replaced by the start of the currently active time selection as Unix timestamp. For example, _1494410783_ |
|
||||
| `$__unixEpochTo()` | Will be replaced by the end of the currently active time selection as Unix timestamp. For example, _1494497183_ |
|
||||
| `$__unixEpochNanoFilter(dateColumn)` | Will be replaced by a time range filter using the specified column name with times represented as nanosecond timestamp. For example, _dateColumn > 1494410783152415214 AND dateColumn < 1494497183142514872_ |
|
||||
| `$__unixEpochNanoFrom()` | Will be replaced by the start of the currently active time selection as nanosecond timestamp. For example, _1494410783152415214_ |
|
||||
| `$__unixEpochNanoTo()` | Will be replaced by the end of the currently active time selection as nanosecond timestamp. For example, _1494497183142514872_ |
|
||||
| `$__unixEpochGroup(dateColumn,'5m', [fillmode])` | Same as $\_\_timeGroup but for times stored as Unix timestamp (`fillMode` only works with time series queries). |
|
||||
| `$__unixEpochGroupAlias(dateColumn,'5m', [fillmode])` | Same as above but also adds a column alias (`fillMode` only works with time series queries). |
|
||||
|
||||
## Table queries
|
||||
|
||||
If the `Format as` query option is set to `Table` then you can basically do any type of SQL query. The table panel will automatically show the results of whatever columns and rows your query returns.
|
||||
|
||||
Query editor with example query:
|
||||
|
||||

|
||||
|
||||
The query:
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
title as "Title",
|
||||
"user".login as "Created By",
|
||||
dashboard.created as "Created On"
|
||||
FROM dashboard
|
||||
INNER JOIN "user" on "user".id = dashboard.created_by
|
||||
WHERE $__timeFilter(dashboard.created)
|
||||
```
|
||||
|
||||
You can control the name of the Table panel columns by using regular `as ` SQL column selection syntax.
|
||||
|
||||
The resulting table panel:
|
||||
|
||||

|
||||
|
||||
## Time series queries
|
||||
|
||||
If you set Format as to _Time series_, then the query must have a column named time that returns either a SQL datetime or any numeric datatype representing Unix epoch in seconds. In addition, result sets of time series queries must be sorted by time for panels to properly visualize the result.
|
||||
|
||||
A time series query result is returned in a [wide data frame format](https://grafana.com/developers/plugin-tools/key-concepts/data-frames#wide-format). Any column except time or of type string transforms into value fields in the data frame query result. Any string column transforms into field labels in the data frame query result.
|
||||
|
||||
> For backward compatibility, there's an exception to the above rule for queries that return three columns including a string column named metric. Instead of transforming the metric column into field labels, it becomes the field name, and then the series name is formatted as the value of the metric column. See the example with the metric column below.
|
||||
|
||||
To optionally customize the default series name formatting, refer to [Standard options definitions](ref:configure-standard-options-display-name).
|
||||
|
||||
**Example with `metric` column:**
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
$__timeGroupAlias("time_date_time",'5m'),
|
||||
min("value_double"),
|
||||
'min' as metric
|
||||
FROM test_data
|
||||
WHERE $__timeFilter("time_date_time")
|
||||
GROUP BY time
|
||||
ORDER BY time
|
||||
```
|
||||
|
||||
Data frame result:
|
||||
|
||||
```text
|
||||
+---------------------+-----------------+
|
||||
| Name: time | Name: min |
|
||||
| Labels: | Labels: |
|
||||
| Type: []time.Time | Type: []float64 |
|
||||
+---------------------+-----------------+
|
||||
| 2020-01-02 03:05:00 | 3 |
|
||||
| 2020-01-02 03:10:00 | 6 |
|
||||
+---------------------+-----------------+
|
||||
```
|
||||
|
||||
**Example using the fill parameter in the $\_\_timeGroupAlias macro to convert null values to be zero instead:**
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
$__timeGroupAlias("createdAt",'5m',0),
|
||||
sum(value) as value,
|
||||
hostname
|
||||
FROM test_data
|
||||
WHERE
|
||||
$__timeFilter("createdAt")
|
||||
GROUP BY time, hostname
|
||||
ORDER BY time
|
||||
```
|
||||
|
||||
Given the data frame result in the following example and using the graph panel, you will get two series named _value 10.0.1.1_ and _value 10.0.1.2_. To render the series with a name of _10.0.1.1_ and _10.0.1.2_ , use a [Standard options definitions](ref:configure-standard-options-display-name) display value of `${__field.labels.hostname}`.
|
||||
|
||||
Data frame result:
|
||||
|
||||
```text
|
||||
+---------------------+---------------------------+---------------------------+
|
||||
| Name: time | Name: value | Name: value |
|
||||
| Labels: | Labels: hostname=10.0.1.1 | Labels: hostname=10.0.1.2 |
|
||||
| Type: []time.Time | Type: []float64 | Type: []float64 |
|
||||
+---------------------+---------------------------+---------------------------+
|
||||
| 2020-01-02 03:05:00 | 3 | 4 |
|
||||
| 2020-01-02 03:10:00 | 6 | 7 |
|
||||
+---------------------+---------------------------+---------------------------+
|
||||
```
|
||||
|
||||
**Example with multiple columns:**
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
$__timeGroupAlias("time_date_time",'5m'),
|
||||
min("value_double") as "min_value",
|
||||
max("value_double") as "max_value"
|
||||
FROM test_data
|
||||
WHERE $__timeFilter("time_date_time")
|
||||
GROUP BY time
|
||||
ORDER BY time
|
||||
```
|
||||
|
||||
Data frame result:
|
||||
|
||||
```text
|
||||
+---------------------+-----------------+-----------------+
|
||||
| Name: time | Name: min_value | Name: max_value |
|
||||
| Labels: | Labels: | Labels: |
|
||||
| Type: []time.Time | Type: []float64 | Type: []float64 |
|
||||
+---------------------+-----------------+-----------------+
|
||||
| 2020-01-02 03:04:00 | 3 | 4 |
|
||||
| 2020-01-02 03:05:00 | 6 | 7 |
|
||||
+---------------------+-----------------+-----------------+
|
||||
```
|
||||
|
||||
## Templating
|
||||
|
||||
Instead of hard-coding things like server, application and sensor name in your metric queries you can use variables in their place. Variables are shown as dropdown select boxes at the top of the dashboard. These dropdowns make it easy to change the data being displayed in your dashboard.
|
||||
|
||||
Refer to [Templates and variables](ref:variables) for an introduction to the templating feature and the different types of template variables.
|
||||
|
||||
### Query variable
|
||||
|
||||
If you add a template variable of the type `Query`, you can write a PostgreSQL query that can
|
||||
return things like measurement names, key names or key values that are shown as a dropdown select box.
|
||||
|
||||
For example, you can have a variable that contains all values for the `hostname` column in a table if you specify a query like this in the templating variable _Query_ setting.
|
||||
|
||||
```sql
|
||||
SELECT hostname FROM host
|
||||
```
|
||||
|
||||
A query can return multiple columns and Grafana will automatically create a list from them. For example, the query below will return a list with values from `hostname` and `hostname2`.
|
||||
|
||||
```sql
|
||||
SELECT host.hostname, other_host.hostname2 FROM host JOIN other_host ON host.city = other_host.city
|
||||
```
|
||||
|
||||
To use time range dependent macros like `$__timeFilter(column)` in your query the refresh mode of the template variable needs to be set to _On Time Range Change_.
|
||||
|
||||
```sql
|
||||
SELECT event_name FROM event_log WHERE $__timeFilter(time_column)
|
||||
```
|
||||
|
||||
Another option is a query that can create a key/value variable. The query should return two columns that are named `__text` and `__value`. The `__text` column value should be unique (if it is not unique then the first value is used). The options in the dropdown will have a text and value that allows you to have a friendly name as text and an id as the value. An example query with `hostname` as the text and `id` as the value:
|
||||
|
||||
```sql
|
||||
SELECT hostname AS __text, id AS __value FROM host
|
||||
```
|
||||
|
||||
You can also create nested variables. Using a variable named `region`, you could have
|
||||
the hosts variable only show hosts from the current selected region with a query like this (if `region` is a multi-value variable then use the `IN` comparison operator rather than `=` to match against multiple values):
|
||||
|
||||
```sql
|
||||
SELECT hostname FROM host WHERE region IN($region)
|
||||
```
|
||||
|
||||
#### Using `__searchFilter` to filter results in Query Variable
|
||||
|
||||
Using `__searchFilter` in the query field will filter the query result based on what the user types in the dropdown select box.
|
||||
When nothing has been entered by the user the default value for `__searchFilter` is `%`.
|
||||
|
||||
> Important that you surround the `__searchFilter` expression with quotes as Grafana does not do this for you.
|
||||
|
||||
The example below shows how to use `__searchFilter` as part of the query field to enable searching for `hostname` while the user types in the dropdown select box.
|
||||
|
||||
Query
|
||||
|
||||
```sql
|
||||
SELECT hostname FROM my_host WHERE hostname LIKE '$__searchFilter'
|
||||
```
|
||||
|
||||
### Using Variables in Queries
|
||||
|
||||
Template variable values are only quoted when the template variable is a `multi-value`.
|
||||
|
||||
If the variable is a multi-value variable then use the `IN` comparison operator rather than `=` to match against multiple values.
|
||||
|
||||
There are two syntaxes:
|
||||
|
||||
`$<varname>` Example with a template variable named `hostname`:
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
atimestamp as time,
|
||||
aint as value
|
||||
FROM table
|
||||
WHERE $__timeFilter(atimestamp) and hostname in($hostname)
|
||||
ORDER BY atimestamp ASC
|
||||
```
|
||||
|
||||
`[[varname]]` Example with a template variable named `hostname`:
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
atimestamp as time,
|
||||
aint as value
|
||||
FROM table
|
||||
WHERE $__timeFilter(atimestamp) and hostname in([[hostname]])
|
||||
ORDER BY atimestamp ASC
|
||||
```
|
||||
|
||||
#### Disabling quoting for multi-value variables
|
||||
|
||||
Grafana automatically creates a quoted, comma-separated string for multi-value variables. For example: if `server01` and `server02` are selected then it will be formatted as: `'server01', 'server02'`. To disable quoting, use the csv formatting option for variables:
|
||||
|
||||
`${servers:csv}`
|
||||
|
||||
Read more about variable formatting options in the [Variables](ref:variable-syntax-advanced-variable-format-options) documentation.
|
||||
|
||||
## Annotations
|
||||
|
||||
[Annotations](ref:annotate-visualizations) allow you to overlay rich event information on top of graphs. You add annotation queries via the Dashboard menu / Annotations view.
|
||||
|
||||
**Example query using time column with epoch values:**
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
epoch_time as time,
|
||||
metric1 as text,
|
||||
concat_ws(', ', metric1::text, metric2::text) as tags
|
||||
FROM
|
||||
public.test_data
|
||||
WHERE
|
||||
$__unixEpochFilter(epoch_time)
|
||||
```
|
||||
|
||||
**Example region query using time and timeend columns with epoch values:**
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
epoch_time as time,
|
||||
epoch_time_end as timeend,
|
||||
metric1 as text,
|
||||
concat_ws(', ', metric1::text, metric2::text) as tags
|
||||
FROM
|
||||
public.test_data
|
||||
WHERE
|
||||
$__unixEpochFilter(epoch_time)
|
||||
```
|
||||
|
||||
**Example query using time column of native SQL date/time data type:**
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
native_date_time as time,
|
||||
metric1 as text,
|
||||
concat_ws(', ', metric1::text, metric2::text) as tags
|
||||
FROM
|
||||
public.test_data
|
||||
WHERE
|
||||
$__timeFilter(native_date_time)
|
||||
```
|
||||
|
||||
| Name | Description |
|
||||
| --------- | ----------------------------------------------------------------------------------------------------------------- |
|
||||
| `time` | The name of the date/time field. Could be a column with a native SQL date/time data type or epoch value. |
|
||||
| `timeend` | Optional name of the end date/time field. Could be a column with a native SQL date/time data type or epoch value. |
|
||||
| `text` | Event description field. |
|
||||
| `tags` | Optional field name to use for event tags as a comma separated string. |
|
||||
|
||||
## Alerting
|
||||
|
||||
Time series queries should work in alerting conditions. Table formatted queries are not yet supported in alert rule
|
||||
conditions.
|
||||
|
||||
@@ -0,0 +1,194 @@
|
||||
---
|
||||
description: This document provides instructions for configuring the PostgreSQL data source.
|
||||
keywords:
|
||||
- grafana
|
||||
- postgresql
|
||||
- guide
|
||||
labels:
|
||||
products:
|
||||
- cloud
|
||||
- enterprise
|
||||
- oss
|
||||
menuTitle: Configure the PostgreSQL data source
|
||||
title: Configure the PostgreSQL data source
|
||||
weight: 10
|
||||
refs:
|
||||
provisioning-data-sources:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/administration/provisioning/#datasources
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/administration/provisioning/#datasources
|
||||
variables:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/
|
||||
add-template-variables-interval-ms:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/add-template-variables/#__interval_ms
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/add-template-variables/#__interval_ms
|
||||
add-template-variables-interval:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/add-template-variables/#__interval
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/add-template-variables/#__interval
|
||||
data-source-management:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/administration/data-source-management/
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/administration/data-source-management/
|
||||
variable-syntax-advanced-variable-format-options:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/variable-syntax/#advanced-variable-format-options
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/variable-syntax/#advanced-variable-format-options
|
||||
---
|
||||
|
||||
# Configure the PostgreSQL data source
|
||||
|
||||
This document provides instructions for configuring the PostgreSQL data source and explains available configuration options. For general information on managing data sources refer to [Data source management](ref:data-source-management).
|
||||
|
||||
## Before you begin
|
||||
|
||||
You must have the `Organization administrator` role to configure the Postgres data source.
|
||||
Organization administrators can also [configure the data source via YAML](#provision-the-data-source) with the Grafana provisioning system.
|
||||
|
||||
Grafana comes with a built-in PostgreSQL data source plugin, eliminating the need to install a plugin.
|
||||
|
||||
{{< admonition type="note" >}}
|
||||
When adding a data source, the database user you specify should have only `SELECT` permissions on the relevant database and tables. Grafana does not validate the safety of queries, which means they can include potentially harmful SQL statements, such as `USE otherdb;` or `DROP TABLE user;`, that could be executed. To mitigate this risk, Grafana strongly recommends creating a dedicated PostgreSQL user with restricted permissions.
|
||||
{{< /admonition >}}
|
||||
|
||||
Example:
|
||||
|
||||
```sql
|
||||
CREATE USER grafanareader WITH PASSWORD 'password';
|
||||
GRANT USAGE ON SCHEMA schema TO grafanareader;
|
||||
GRANT SELECT ON schema.table TO grafanareader;
|
||||
```
|
||||
|
||||
## Add the PostgreSQL data source
|
||||
|
||||
Complete the following steps to set up a new PostgreSQL data source:
|
||||
|
||||
1. Click **Connections** in the left-side menu.
|
||||
1. Click **Add new connection**
|
||||
1. Type `PostgreSQL` in the search bar.
|
||||
1. Select the **PostgreSQL data source**.
|
||||
1. Click **Add new data source** in the upper right.
|
||||
|
||||
You are taken to the **Settings** tab where you will configure the data source.
|
||||
|
||||
## PostgreSQL configuration options
|
||||
|
||||
Following is a list of PostgreSQL configuration options:
|
||||
|
||||
- **Name** - Sets the name you use to refer to the data source in panels and queries. Examples: `PostgreSQL-DB-1`.
|
||||
- **Default** - Toggle to set this specific PostgreSQL data source as the default pre-selected data source in panels and visualizations.
|
||||
|
||||
**Connection section:**
|
||||
|
||||
- **Host URL** - The IP address/hostname and optional port of your PostgreSQL instance.
|
||||
- **Database name** - The name of your PostgreSQL database.
|
||||
|
||||
**Authentication section:**
|
||||
|
||||
- **Username** - Enter the username used to connect to your PostgreSQL database.
|
||||
- **Password** - Enter the password used to connect to the PostgreSQL database.
|
||||
- **TLS/SSL Mode** - Determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the server. When **TLS/SSL Mode** is disabled, **TLS/SSL Method** and **TLS/SSL Auth Details** aren't visible options.
|
||||
- **TLS/SSL Method** - Determines how TLS/SSL certificates are configured.
|
||||
- **File system path** - This option allows you to configure certificates by specifying paths to existing certificates on the local file system where Grafana is running. Ensure this file is readable by the user executing the Grafana process.
|
||||
- **Certificate content** - This option allows you to configure certificate by specifying their content. The content is stored and encrypted in the Grafana database. When connecting to the database, the certificates are saved as files, on the local filesystem, in the Grafana data path.
|
||||
|
||||
**TLS/SSL Auth Details**
|
||||
|
||||
If you select the TLS/SSL Mode options **require**, **verify-ca** or **verify-full** and **file system path** the following are required:
|
||||
|
||||
- **TLS/SSL Root Certificate** - Specify the path to the root certificate file.
|
||||
- **TLS/SSL Client Certificate** - Specify the path to the client certificate and ensure the file is accessible to the user running the Grafana process.
|
||||
- **TLS/SSL Client Key** - Specify the path to the client key file and ensure the file is accessible to the user running the Grafana process.
|
||||
|
||||
If you select the TLS/SSL Mode option **require** and TLS/SSL Method certificate content the following are required:
|
||||
|
||||
- **TLS/SSL Client Certificate** - Provide the client certificate.
|
||||
- **TLS/SSL Client Key** - Provide the client key.
|
||||
|
||||
If you select the TLS/SSL Mode options **verify-ca** or **verify-full** with the TLS/SSL Method certificate content the following are required:
|
||||
|
||||
- **TLS/SSL Client Certificate** - Provide the client certificate.
|
||||
- **TLS/SSL Root Certificate** - Provide the root certificate.
|
||||
- **TLS/SSL Client Key** - Provide the client key.
|
||||
|
||||
**PostgreSQL Options:**
|
||||
|
||||
- **Version** - Determines which functions are available in the query builder. The default is the current version.
|
||||
- **Min time interval** - Defines a lower limit for the auto group by by time interval. Grafana recommends aligning this setting with the data write frequency. For example, set it to `1m` if your data is written every minute. Refer to [Min time interval](#min-time-interval) for format examples.
|
||||
- **TimescaleDB** - A time-series database built as a PostgreSQL extension. When enabled, Grafana uses `time_bucket` in the `$__timeGroup` macro to display TimescaleDB specific aggregate functions in the query builder. For more information, refer to [TimescaleDB documentation](https://docs.timescale.com/timescaledb/latest/tutorials/grafana/grafana-timescalecloud/#connect-timescaledb-and-grafana).
|
||||
|
||||
**Connection limits:**
|
||||
|
||||
- **Max open** - The maximum number of open connections to the database. The default `100`.
|
||||
- **Auto max idle** - Toggle to set the maximum number of idle connections to the number of maximum open connections. This setting is toggled on by default.
|
||||
- **Max idle** - The maximum number of connections in the idle connection pool. The default `100`.
|
||||
- **Max lifetime** - The maximum amount of time in seconds a connection may be reused. The default is `14400`, or 4 hours.
|
||||
|
||||
**Private data source connect** - _Only for Grafana Cloud users._ Private data source connect, or PDC, allows you to establish a private, secured connection between a Grafana Cloud instance, or stack, and data sources secured within a private network. Click the drop-down to locate the URL for PDC. For more information regarding Grafana PDC refer to [Private data source connect (PDC)](https://grafana.com/docs/grafana-cloud/connect-externally-hosted/private-data-source-connect/).
|
||||
|
||||
Click **Manage private data source connect** to be taken to your PDC connection page, where you’ll find your PDC configuration details.
|
||||
|
||||
After you have added your PostgreSQL connection settings, click **Save & test** to test and save the data source connection.
|
||||
|
||||
### Min time interval
|
||||
|
||||
The **Min time interval** setting defines a lower limit for the [`$__interval`](ref:add-template-variables-interval) and [`$__interval_ms`](ref:add-template-variables-interval-ms) variables.
|
||||
|
||||
This option can also be configured or overridden in the dashboard panel under the data source settings.
|
||||
|
||||
This value must be formatted as a number followed by a valid time identifier:
|
||||
|
||||
| Identifier | Description |
|
||||
| ---------- | ----------- |
|
||||
| `y` | year |
|
||||
| `M` | month |
|
||||
| `w` | week |
|
||||
| `d` | day |
|
||||
| `h` | hour |
|
||||
| `m` | minute |
|
||||
| `s` | second |
|
||||
| `ms` | millisecond |
|
||||
|
||||
## Provision the data source
|
||||
|
||||
You can define and configure the data source in YAML files with [provisioning](/docs/grafana/<GRAFANA_VERSION>/administration/provisioning/#data-sources).
|
||||
For more information about provisioning, and available configuration options, refer to [Provision Grafana](ref:provisioning-data-sources).
|
||||
|
||||
### PostgreSQL provisioning example
|
||||
|
||||
```yaml
|
||||
apiVersion: 1
|
||||
|
||||
datasources:
|
||||
- name: Postgres
|
||||
type: postgres
|
||||
url: localhost:5432
|
||||
user: grafana
|
||||
secureJsonData:
|
||||
password: 'Password!'
|
||||
jsonData:
|
||||
database: grafana
|
||||
sslmode: 'disable' # disable/require/verify-ca/verify-full
|
||||
maxOpenConns: 100
|
||||
maxIdleConns: 100
|
||||
maxIdleConnsAuto: true
|
||||
connMaxLifetime: 14400
|
||||
postgresVersion: 903 # 903=9.3, 904=9.4, 905=9.5, 906=9.6, 1000=10
|
||||
timescaledb: false
|
||||
```
|
||||
|
||||
#### Troubleshoot provisioning issues
|
||||
|
||||
If you encounter metric request errors or other issues:
|
||||
|
||||
- Ensure that the parameters in your data source YAML file precisely match the example provided, including parameter names and the correct use of quotation marks.
|
||||
- Verify that the database name _isn't_ included in the URL.
|
||||
@@ -0,0 +1,410 @@
|
||||
---
|
||||
description: This document describes the PostgreSQL query editor in Grafana.
|
||||
keywords:
|
||||
- grafana
|
||||
- postgresql
|
||||
- guide
|
||||
labels:
|
||||
products:
|
||||
- cloud
|
||||
- enterprise
|
||||
- oss
|
||||
menuTitle: PostgreSQL query editor
|
||||
title: PostgreSQL query editor
|
||||
weight: 20
|
||||
refs:
|
||||
variables:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/
|
||||
add-template-variables-interval:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/add-template-variables/#__interval
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/add-template-variables/#__interval
|
||||
explore:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/explore/
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/explore/
|
||||
query-transform-data:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/panels-visualizations/query-transform-data/
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana-cloud/visualizations/panels-visualizations/query-transform-data/
|
||||
query-editor:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/panels-visualizations/query-transform-data/#query-editors
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana-cloud/visualizations/panels-visualizations/query-transform-data/#query-editors
|
||||
alert-rules:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/alerting/fundamentals/alert-rules/
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana-cloud/alerting-and-irm/alerting/alerting-rules/
|
||||
template-annotations-and-labels:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/alerting/alerting-rules/templates/
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana-cloud/alerting-and-irm/alerting/alerting-rules/templates/
|
||||
templates:
|
||||
- pattern: /docs/grafana/
|
||||
destination: /docs/grafana/<GRAFANA_VERSION>/dashboards/variables/#templates
|
||||
- pattern: /docs/grafana-cloud/
|
||||
destination: /docs/grafana-cloud/visualizations/dashboards/variables/#templates
|
||||
---
|
||||
|
||||
# PostgreSQL query editor
|
||||
|
||||
Grafana query editors are unique for each data source.
|
||||
|
||||
For general information on Grafana query editors, refer to [Query editors](ref:query-editor).
|
||||
|
||||
For general information on querying data sources in Grafana, refer to [Query and transform data](ref:query-transform-data).
|
||||
|
||||
The PostgreSQL query editor is located on the [Explore page](ref:explore). You can also access the PostgreSQL query editor from a dashboard panel. Click the ellipsis in the upper right of the panel and select **Edit**.
|
||||
|
||||
{{< figure src="/static/img/docs/screenshot-postgres-query-editor.png" class="docs-image--no-shadow" caption="PostgreSQL query builder" >}}
|
||||
|
||||
## PostgreSQL query editor components
|
||||
|
||||
The PostgreSQL query editor has two modes: **Builder** and **Code**.
|
||||
|
||||
Builder mode helps you build a query using a visual interface. Code mode allows for advanced querying and offers support for complex SQL query writing.
|
||||
|
||||
### PostgreSQL Builder mode
|
||||
|
||||
The following components will help you build a PostgreSQL query:
|
||||
|
||||
- **Format** - Select a format response from the drop-down for the PostgreSQL query. The default is **Table**. If you use the **Time series** format option, one of the columns must be `time`. Refer to [Time series queries](#time-series-queries) for more information.
|
||||
- **Table** - Select a table from the drop-down. Tables correspond to the chosen database.
|
||||
- **Data operations** - _Optional_ Select an aggregation from the drop-down. You can add multiple data operations by clicking the **+ sign**. Click the **garbage can icon** to remove data operations.
|
||||
- **Column** - Select a column on which to run the aggregation.
|
||||
- **Alias** - _Optional_ Add an alias from the drop-down. You can also add your own alias by typing it in the box and clicking **Enter**. Remove an alias by clicking the **X**.
|
||||
- **Filter** - Toggle to add filters.
|
||||
- **Filter by column value** - _Optional_ If you toggle **Filter** you can add a column to filter by from the drop-down. To filter on more columns, click the **+ sign** to the right of the condition drop-down. You can choose a variety of operators from the drop-down next to the condition. When multiple filters are added you can add an `AND` operator to display all true conditions or an `OR` operator to display any true conditions. Use the second drop-down to choose a filter. To remove a filter, click the `X` button next to that filter's drop-down. After selecting a date type column, you can choose **Macros** from the operators list and select `timeFilter` which will add the `$\_\_timeFilter` macro to the query with the selected date column.
|
||||
- **Group** - Toggle to add **Group by column**.
|
||||
- **Group by column** - Select a column to filter by from the drop-down. Click the **+sign** to filter by multiple columns. Click the **X** to remove a filter.
|
||||
- **Order** - Toggle to add an `ORDER BY` statement.
|
||||
- **Order by** - Select a column to order by from the drop-down. Select ascending (`ASC`) or descending (`DESC`) order.
|
||||
- **Limit** - You can add an optional limit on the number of retrieved results. Default is 50.
|
||||
- **Preview** - Toggle for a preview of the SQL query generated by the query builder. Preview is toggled on by default.
|
||||
|
||||
## PostgreSQL Code mode
|
||||
|
||||
To create advanced queries, switch to **Code mode** by clicking **Code** in the upper right of the editor window. Code mode supports the auto-completion of tables, columns, SQL keywords, standard SQL functions, Grafana template variables, and Grafana macros. Columns cannot be completed before a table has been specified.
|
||||
|
||||
{{< figure src="/static/img/docs/v92/sql_code_editor.png" class="docs-image--no-shadow" >}}
|
||||
|
||||
Select **Table** or **Time Series** as the format. Click the **{}** in the bottom right to format the query. Click the **downward caret** to expand the Code mode editor. **CTRL/CMD + Return** serves as a keyboard shortcut to execute the query.
|
||||
|
||||
{{< admonition type="warning" >}}
|
||||
Changes made to a query in Code mode will not transfer to Builder mode and will be discarded. You will be prompted to copy your code to the clipboard to save any changes.
|
||||
{{< /admonition >}}
|
||||
|
||||
## Macros
|
||||
|
||||
You can add macros to your queries to simplify the syntax and enable dynamic elements, such as date range filters.
|
||||
|
||||
| Macro example | Description |
|
||||
| ----------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `$__time(dateColumn)` | Replaces the value with an expression to convert to a UNIX timestamp and renames the column to `time_sec`. Example: `UNIX_TIMESTAMP(dateColumn) AS time_sec`. |
|
||||
| `$__timeEpoch(dateColumn)` | Replaces the value with an expression to convert to a UNIX Epoch timestamp and renames the column to `time_sec`. Example: `UNIX_TIMESTAMP(dateColumn) AS time_sec`. |
|
||||
| `$__timeFilter(dateColumn)` | Replaces the value a time range filter using the specified column name. Example: `dateColumn BETWEEN FROM_UNIXTIME(1494410783) AND FROM_UNIXTIME(1494410983)` |
|
||||
| `$__timeFrom()` | Replaces the value with the start of the currently active time selection. Example: `FROM_UNIXTIME(1494410783)` |
|
||||
| `$__timeTo()` | Replaces the value with the end of the currently active time selection. Example: `FROM_UNIXTIME(1494410983)` |
|
||||
| `$__timeGroup(dateColumn,'5m')` | Replaces the value with an expression suitable for use in a `GROUP BY` clause. Example: `cast(cast(UNIX_TIMESTAMP(dateColumn)/(300) AS signed)*300 AS signed)` |
|
||||
| `$__timeGroup(dateColumn,'5m', 0)` | Same as the `$__timeGroup(dateColumn,'5m')` macro, but includes a fill parameter to ensure missing points in the series are added by Grafana, using 0 as the default value. **This applies only to time series queries.** |
|
||||
| `$__timeGroup(dateColumn,'5m', NULL)` | Same as the `$__timeGroup(dateColumn,'5m', 0)` but `NULL` is used as the value for missing points. _This applies only to time series queries._ |
|
||||
| `$__timeGroup(dateColumn,'5m', previous)` | Same as the `$__timeGroup(dateColumn,'5m', previous)` macro, but uses the previous value in the series as the fill value. If no previous value exists, it uses `NULL`. _This applies only to time series queries._ |
|
||||
| `$__timeGroupAlias(dateColumn,'5m')` | Replaces the value identical to `$__timeGroup` but with an added column alias. |
|
||||
| `$__unixEpochFilter(dateColumn)` | Replaces the value by a time range filter using the specified column name with times represented as a UNIX timestamp. Example: `dateColumn > 1494410783 AND dateColumn < 1494497183` |
|
||||
| `$__unixEpochFrom()` | Replaces the value with the start of the currently active time selection as a UNIX timestamp. Example: `1494410783` |
|
||||
| `$__unixEpochTo()` | Replaces the value with the end of the currently active time selection as a UNIX timestamp. Example: `1494497183` |
|
||||
| `$__unixEpochNanoFilter(dateColumn)` | Replaces the value with a time range filter using the specified column name with time represented as a nanosecond timestamp. Example: `dateColumn > 1494410783152415214 AND dateColumn < 1494497183142514872` |
|
||||
| `$__unixEpochNanoFrom()` | Replaces the value with the start of the currently active time selection as a nanosecond timestamp. Example: `1494410783152415214` |
|
||||
| `$__unixEpochNanoTo()` | Replaces the value with the end of the currently active time selection as nanosecond timestamp. Example: `1494497183142514872` |
|
||||
| `$__unixEpochGroup(dateColumn,'5m', [fillmode])` | Same as `$__timeGroup` but for times stored as Unix timestamp. `fillMode` only works with time series queries. |
|
||||
| `$__unixEpochGroupAlias(dateColumn,'5m', [fillmode])` | Same as `$__timeGroup` but also adds a column alias. `fillMode` only works with time series queries. |
|
||||
|
||||
## Table SQL queries
|
||||
|
||||
If the **Format** option is set to **Table**, you can execute virtually any type of SQL query. The Table panel will automatically display the resulting columns and rows from your query.
|
||||
|
||||

|
||||
|
||||
You can change or customize the name of a Table panel column by using the SQL keyword `AS` syntax.
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
title as "Title",
|
||||
"user".login as "Created By",
|
||||
dashboard.created as "Created On"
|
||||
FROM dashboard
|
||||
INNER JOIN "user" on "user".id = dashboard.created_by
|
||||
WHERE $__timeFilter(dashboard.created)
|
||||
```
|
||||
|
||||
## Time series queries
|
||||
|
||||
Set the **Format** option to **Time series** to create and run time series queries.
|
||||
|
||||
{{< admonition type="note" >}}
|
||||
To run a time series query you must include a column named `time` that returns either a SQL `datetime` value or a numeric datatype representing the UNIX epoch time in seconds. Additionally, the query results must be sorted by the `time` column for proper visualization in panels.
|
||||
{{< /admonition >}}
|
||||
|
||||
The examples in this section refer to the data in the following table:
|
||||
|
||||
```text
|
||||
+---------------------+--------------+---------------------+----------+
|
||||
| time_date_time | value_double | CreatedAt | hostname |
|
||||
+---------------------+--------------+---------------------+----------+
|
||||
| 2020-01-02 03:05:00 | 3.0 | 2020-01-02 03:05:00 | 10.0.1.1 |
|
||||
| 2020-01-02 03:06:00 | 4.0 | 2020-01-02 03:06:00 | 10.0.1.2 |
|
||||
| 2020-01-02 03:10:00 | 6.0 | 2020-01-02 03:10:00 | 10.0.1.1 |
|
||||
| 2020-01-02 03:11:00 | 7.0 | 2020-01-02 03:11:00 | 10.0.1.2 |
|
||||
| 2020-01-02 03:20:00 | 5.0 | 2020-01-02 03:20:00 | 10.0.1.2 |
|
||||
+---------------------+--------------+---------------------+----------+
|
||||
```
|
||||
|
||||
Time series query results are returned in [wide data frame format](https://grafana.com/developers/plugin-tools/key-concepts/data-frames#wide-format). In the data frame query result, any column, except for time or string-type columns, transforms into value fields. String columns, on the other hand, become field labels.
|
||||
|
||||
{{< admonition type="note" >}}
|
||||
For backward compatibility, an exception to this rule applies to queries that return three columns, one of which is a string column named `metric`. Instead of converting the metric column into field labels, it is used as the field name, while the series name is set to its value. See the following example for reference.
|
||||
{{< /admonition >}}
|
||||
|
||||
**Example with `metric` column:**
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
$__timeGroupAlias("time_date_time",'5m'),
|
||||
min("value_double"),
|
||||
'min' as metric
|
||||
FROM test_data
|
||||
WHERE $__timeFilter("time_date_time")
|
||||
GROUP BY time
|
||||
ORDER BY time
|
||||
```
|
||||
|
||||
Data frame result:
|
||||
|
||||
```text
|
||||
+---------------------+-----------------+
|
||||
| Name: time | Name: min |
|
||||
| Labels: | Labels: |
|
||||
| Type: []time.Time | Type: []float64 |
|
||||
+---------------------+-----------------+
|
||||
| 2020-01-02 03:05:00 | 3 |
|
||||
| 2020-01-02 03:10:00 | 6 |
|
||||
+---------------------+-----------------+
|
||||
```
|
||||
|
||||
To customize default series name formatting, refer to [Standard options definitions](ref:configure-standard-options-display-name).
|
||||
|
||||
Following are time series query examples.
|
||||
|
||||
**Example using the fill parameter in the $\_\_timeGroupAlias macro to convert null values to be zero instead:**
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
$__timeGroupAlias("createdAt",'5m',0),
|
||||
sum(value) as value,
|
||||
hostname
|
||||
FROM test_data
|
||||
WHERE
|
||||
$__timeFilter("createdAt")
|
||||
GROUP BY time, hostname
|
||||
ORDER BY time
|
||||
```
|
||||
|
||||
Based on the data frame result in the following example, the time series panel will generate two series named _value 10.0.1.1_ and _value 10.0.1.2_. To display the series names as _10.0.1.1_ and _10.0.1.2_, use the [Standard options definitions](ref:configure-standard-options-display-name) display value `${__field.labels.hostname}`.
|
||||
|
||||
Data frame result:
|
||||
|
||||
```text
|
||||
+---------------------+---------------------------+---------------------------+
|
||||
| Name: time | Name: value | Name: value |
|
||||
| Labels: | Labels: hostname=10.0.1.1 | Labels: hostname=10.0.1.2 |
|
||||
| Type: []time.Time | Type: []float64 | Type: []float64 |
|
||||
+---------------------+---------------------------+---------------------------+
|
||||
| 2020-01-02 03:05:00 | 3 | 4 |
|
||||
| 2020-01-02 03:10:00 | 6 | 7 |
|
||||
+---------------------+---------------------------+---------------------------+
|
||||
```
|
||||
|
||||
**Example with multiple columns:**
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
$__timeGroupAlias("time_date_time",'5m'),
|
||||
min("value_double") as "min_value",
|
||||
max("value_double") as "max_value"
|
||||
FROM test_data
|
||||
WHERE $__timeFilter("time_date_time")
|
||||
GROUP BY time
|
||||
ORDER BY time
|
||||
```
|
||||
|
||||
Data frame result:
|
||||
|
||||
```text
|
||||
+---------------------+-----------------+-----------------+
|
||||
| Name: time | Name: min_value | Name: max_value |
|
||||
| Labels: | Labels: | Labels: |
|
||||
| Type: []time.Time | Type: []float64 | Type: []float64 |
|
||||
+---------------------+-----------------+-----------------+
|
||||
| 2020-01-02 03:04:00 | 3 | 4 |
|
||||
| 2020-01-02 03:05:00 | 6 | 7 |
|
||||
+---------------------+-----------------+-----------------+
|
||||
```
|
||||
|
||||
## Templating
|
||||
|
||||
Instead of hard coding values like server, application, or sensor names in your metric queries, you can use variables. Variables appear as drop-down select boxes at the top of the dashboard. These drop-downs make it easy to change the data being displayed in your dashboard.
|
||||
|
||||
Refer to [Templates](ref:templates) for an introduction to creating template variables as well as the different types.
|
||||
|
||||
### Query variable
|
||||
|
||||
If you add a `Query` template variable you can write a PostgreSQL query to retrieve items such as measurement names, key names, or key values, which will be displayed in the drop-down menu.
|
||||
|
||||
For example, you can use a variable to retrieve all the values from the `hostname` column in a table by creating the following query in the templating variable _Query_ setting.
|
||||
|
||||
```sql
|
||||
SELECT hostname FROM host
|
||||
```
|
||||
|
||||
A query can return multiple columns, and Grafana will automatically generate a list based on the query results. For example, the following query returns a list with values from `hostname` and `hostname2`.
|
||||
|
||||
```sql
|
||||
SELECT host.hostname, other_host.hostname2 FROM host JOIN other_host ON host.city = other_host.city
|
||||
```
|
||||
|
||||
To use time range dependent macros like `$__timeFilter(column)` in your query, you must set the template variable's refresh mode to _On Time Range Change_.
|
||||
|
||||
```sql
|
||||
SELECT event_name FROM event_log WHERE $__timeFilter(time_column)
|
||||
```
|
||||
|
||||
Another option is a query that can create a key/value variable. The query should return two columns that are named `__text` and `__value`. The `__text` column must contain unique values (if not, only the first value is used). This allows the drop-down options to display a text-friendly name as the text while using an ID as the value. For example, a query could use `hostname` as the text and `id` as the value:
|
||||
|
||||
```sql
|
||||
SELECT hostname AS __text, id AS __value FROM host
|
||||
```
|
||||
|
||||
You can also create nested variables. For example, if you have a variable named `region`, you can configure the `hosts` variable to display only the hosts within the currently selected region as shown in the following example. If `region` is a multi-value variable, use the `IN` operator instead of `=` to match multiple values.
|
||||
|
||||
```sql
|
||||
SELECT hostname FROM host WHERE region IN($region)
|
||||
```
|
||||
|
||||
#### Using `__searchFilter` to filter results in Query Variable
|
||||
|
||||
Using `__searchFilter` in the query field allows the query results to be filtered based on the user’s input in the drop-down selection box. If you do not enter anything, the default value for `__searchFilter` is `%`.
|
||||
|
||||
Note that you must enclose the `__searchFilter` expression in quotes as Grafana does not add them automatically.
|
||||
|
||||
The following example demonstrates how to use `__searchFilter` in the query field to enable real-time searching for `hostname` as the user type in the drop-down selection box.
|
||||
|
||||
```sql
|
||||
SELECT hostname FROM my_host WHERE hostname LIKE '$__searchFilter'
|
||||
```
|
||||
|
||||
### Using Variables in Queries
|
||||
|
||||
Template variable values are only quoted when the template variable is a `multi-value`.
|
||||
|
||||
If the variable is a multi-value variable, use the `IN` comparison operator instead of `=` to match against multiple values.
|
||||
|
||||
You can use two different syntaxes:
|
||||
|
||||
`$<varname>` Example with a template variable named `hostname`:
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
atimestamp as time,
|
||||
aint as value
|
||||
FROM table
|
||||
WHERE $__timeFilter(atimestamp) and hostname in($hostname)
|
||||
ORDER BY atimestamp ASC
|
||||
```
|
||||
|
||||
`[[varname]]` Example with a template variable named `hostname`:
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
atimestamp as time,
|
||||
aint as value
|
||||
FROM table
|
||||
WHERE $__timeFilter(atimestamp) and hostname in([[hostname]])
|
||||
ORDER BY atimestamp ASC
|
||||
```
|
||||
|
||||
#### Disabling quoting for multi-value variables
|
||||
|
||||
Grafana automatically formats multi-value variables as a quoted, comma-separated string. For example, if `server01` and `server02` are selected, they are formatted as `'server01'`, `'server02'`. To remove the quotes, enable the CSV formatting option for the variables:
|
||||
|
||||
`${servers:csv}`
|
||||
|
||||
Read more about variable formatting options in the [Variables](ref:variable-syntax-advanced-variable-format-options) documentation.
|
||||
|
||||
## Annotations
|
||||
|
||||
[Annotations](ref:annotate-visualizations) allow you to overlay rich event information on top of graphs. Add annotation queries via the **Dashboard settings > Annotations view**.
|
||||
|
||||
**Example query using a `time` column with epoch values:**
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
epoch_time as time,
|
||||
metric1 as text,
|
||||
concat_ws(', ', metric1::text, metric2::text) as tags
|
||||
FROM
|
||||
public.test_data
|
||||
WHERE
|
||||
$__unixEpochFilter(epoch_time)
|
||||
```
|
||||
|
||||
**Example region query using `time` and `timeend` columns with epoch values:**
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
epoch_time as time,
|
||||
epoch_time_end as timeend,
|
||||
metric1 as text,
|
||||
concat_ws(', ', metric1::text, metric2::text) as tags
|
||||
FROM
|
||||
public.test_data
|
||||
WHERE
|
||||
$__unixEpochFilter(epoch_time)
|
||||
```
|
||||
|
||||
**Example query using a `time` column with a native SQL date/time data type:**
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
native_date_time as time,
|
||||
metric1 as text,
|
||||
concat_ws(', ', metric1::text, metric2::text) as tags
|
||||
FROM
|
||||
public.test_data
|
||||
WHERE
|
||||
$__timeFilter(native_date_time)
|
||||
```
|
||||
|
||||
| Name | Description |
|
||||
| --------- | --------------------------------------------------------------------------------------------------------------------- |
|
||||
| `time` | The name of the date/time field, which can be a column with a native SQL date/time data type or epoch value. |
|
||||
| `timeend` | Optional name of the end date/time field, which can be a column with a native SQL date/time data type or epoch value. |
|
||||
| `text` | Event description field. |
|
||||
| `tags` | Optional field name to use for event tags as a comma-separated string. |
|
||||
|
||||
## Alerting
|
||||
|
||||
Use time series queries to create alerts. Table formatted queries aren't yet supported in alert rule conditions.
|
||||
|
||||
For more information regarding alerting refer to the following:
|
||||
|
||||
- [Alert rules](ref:alert-rules)
|
||||
- [Template annotations and labels](ref:template-annotations-and-labels)
|
||||
@@ -12,7 +12,7 @@ hero:
|
||||
level: 1
|
||||
width: 100
|
||||
height: 100
|
||||
description: Use the Drilldown apps to investigate and identify issues using telemetry data.
|
||||
description: Use the Grafana Drilldown apps to investigate and identify issues using telemetry data.
|
||||
cards:
|
||||
title_class: pt-0 lh-1
|
||||
items:
|
||||
@@ -40,6 +40,8 @@ The Grafana Drilldown apps are designed for effortless data exploration through
|
||||
|
||||
Easily explore telemetry signals with these specialized tools, tailored specifically for the Grafana databases to provide quick and accurate insights.
|
||||
|
||||
{{< docs/shared source="grafana" lookup="plugins/rename-note.md" version="<GRAFANA_VERSION>" >}}
|
||||
|
||||
To learn more, read:
|
||||
|
||||
- [From multi-line queries to no-code investigations: meeting Grafana users where they are](https://grafana.com/blog/2024/10/22/from-multi-line-queries-to-no-code-investigations-meeting-grafana-users-where-they-are/)
|
||||
|
||||
@@ -16,7 +16,9 @@ weight: 200
|
||||
|
||||
Grafana Metrics Drilldown is a query-less experience for browsing **Prometheus-compatible** metrics. Quickly find related metrics with just a few simple clicks, without needing to write PromQL queries to retrieve metrics.
|
||||
|
||||
With Grafana Metrics Drilldown, you can:
|
||||
{{< docs/shared source="grafana" lookup="plugins/rename-note.md" version="<GRAFANA_VERSION>" >}}
|
||||
|
||||
With Metrics Drilldown, you can:
|
||||
|
||||
- Easily segment metrics based on their labels, so you can immediately spot anomalies and identify issues.
|
||||
- Automatically display the optimal visualization for each metric type (gauge vs. counter, for example) without manual setup.
|
||||
@@ -25,13 +27,13 @@ With Grafana Metrics Drilldown, you can:
|
||||
- View a history of user steps when navigating through metrics and their filters.
|
||||
- Seamlessly pivot to related telemetry, including log data.
|
||||
|
||||
{{< docs/play title="Grafana Metrics Drilldown" url="https://play.grafana.org/explore/metrics/trail?from=now-1h&to=now&var-ds=grafanacloud-demoinfra-prom&var-filters=&refresh=&metricPrefix=all" >}}
|
||||
{{< docs/play title="Metrics Drilldown" url="https://play.grafana.org/explore/metrics/trail?from=now-1h&to=now&var-ds=grafanacloud-demoinfra-prom&var-filters=&refresh=&metricPrefix=all" >}}
|
||||
|
||||
You can access Grafana Metrics Drilldown either as a standalone experience or as part of Grafana dashboards.
|
||||
You can access Metrics Drilldown either as a standalone experience or as part of Grafana dashboards.
|
||||
|
||||
## Standalone experience
|
||||
|
||||
To access Grafana Metrics Drilldown as a standalone experience:
|
||||
To access Metrics Drilldown as a standalone experience:
|
||||
|
||||
1. Click the arrow next to **Drilldown** in the Grafana left-side menu and click **Metrics**. You are taken to an overview page that shows recent metrics, bookmarks, and the option to select a new metric exploration.
|
||||
1. To get started with a new exploration, click **Let's start!**.
|
||||
@@ -63,7 +65,7 @@ After you have gathered your metrics exploration data you can:
|
||||
|
||||
## Dashboard experience
|
||||
|
||||
To access Grafana Metrics Drilldown via a dashboard:
|
||||
To access Metrics Drilldown via a dashboard:
|
||||
|
||||
1. Navigate to your dashboard.
|
||||
1. Select a time series panel.
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
---
|
||||
headless: true
|
||||
labels:
|
||||
products:
|
||||
- enterprise
|
||||
- oss
|
||||
---
|
||||
|
||||
[//]: # 'This file contains a rename note for Explore to Drilldown apps.'
|
||||
[//]: # 'This shared file is included in a lot of files. Check the app docs in'
|
||||
[//]: # 'drilldown-traces, drilldown-logs, drilldown-profiles, grafana, and website/grafana-cloud.'
|
||||
[//]: # 'If you make changes to this file, verify that the meaning and content are not changed in any place where the file is included.'
|
||||
[//]: # 'Any links should be fully qualified and not relative: /docs/grafana/ instead of ../grafana/.'
|
||||
|
||||
{{< admonition type="note" >}}
|
||||
The Grafana Explore apps have changed to Grafana Drilldown apps.
|
||||
For example, Explore Logs is now Logs Drilldown.
|
||||
To learn more, read [Grafana Drilldown apps: the improved queryless experience known as the Explore apps](https://grafana.com/blog/2025/02/20/grafana-drilldown-apps-the-improved-queryless-experience-formerly-known-as-the-explore-apps/).
|
||||
{{< /admonition >}}
|
||||
@@ -48,8 +48,8 @@ export function makeNewDashboardRequestBody(dashboardName: string, folderUid?: s
|
||||
timezone: '',
|
||||
title: dashboardName,
|
||||
version: 0,
|
||||
weekStart: '',
|
||||
uid: '',
|
||||
weekStart: '',
|
||||
},
|
||||
message: '',
|
||||
overwrite: false,
|
||||
|
||||
@@ -54,7 +54,7 @@ require (
|
||||
github.com/go-openapi/runtime v0.28.0 // @grafana/alerting-backend
|
||||
github.com/go-openapi/strfmt v0.23.0 // @grafana/alerting-backend
|
||||
github.com/go-redis/redis/v8 v8.11.5 // @grafana/grafana-backend-group
|
||||
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // @grafana/grafana-backend-group
|
||||
github.com/go-sourcemap/sourcemap v2.1.4+incompatible // @grafana/grafana-backend-group
|
||||
github.com/go-sql-driver/mysql v1.8.1 // @grafana/grafana-search-and-storage
|
||||
github.com/go-stack/stack v1.8.1 // @grafana/grafana-backend-group
|
||||
github.com/gobwas/glob v0.2.3 // @grafana/grafana-backend-group
|
||||
@@ -71,7 +71,7 @@ require (
|
||||
github.com/googleapis/gax-go/v2 v2.14.1 // @grafana/grafana-backend-group
|
||||
github.com/gorilla/mux v1.8.1 // @grafana/grafana-backend-group
|
||||
github.com/gorilla/websocket v1.5.3 // @grafana/grafana-app-platform-squad
|
||||
github.com/grafana/alerting v0.0.0-20250219153626-c475b1a572b0 // @grafana/alerting-backend
|
||||
github.com/grafana/alerting v0.0.0-20250221202230-9d7e00921e44 // @grafana/alerting-backend
|
||||
github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7 // @grafana/identity-access-team
|
||||
github.com/grafana/authlib/types v0.0.0-20250219092154-21ce22b49f31 // @grafana/identity-access-team
|
||||
github.com/grafana/dataplane/examples v0.0.1 // @grafana/observability-metrics
|
||||
@@ -111,7 +111,6 @@ require (
|
||||
github.com/jmoiron/sqlx v1.3.5 // @grafana/grafana-backend-group
|
||||
github.com/json-iterator/go v1.1.12 // @grafana/grafana-backend-group
|
||||
github.com/lib/pq v1.10.9 // @grafana/grafana-backend-group
|
||||
github.com/linkedin/goavro/v2 v2.10.0 // @grafana/grafana-backend-group
|
||||
github.com/m3db/prometheus_remote_client_golang v0.4.4 // @grafana/grafana-backend-group
|
||||
github.com/madflojo/testcerts v1.1.1 // @grafana/alerting-backend
|
||||
github.com/magefile/mage v1.15.0 // @grafana/grafana-developer-enablement-squad
|
||||
|
||||
@@ -1281,8 +1281,8 @@ github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC
|
||||
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
|
||||
github.com/go-resty/resty/v2 v2.15.3 h1:bqff+hcqAflpiF591hhJzNdkRsFhlB96CYfBwSFvql8=
|
||||
github.com/go-resty/resty/v2 v2.15.3/go.mod h1:0fHAoK7JoBy/Ch36N8VFeMsK7xQOHhvWaC3iOktwmIU=
|
||||
github.com/go-sourcemap/sourcemap v2.1.3+incompatible h1:W1iEw64niKVGogNgBN3ePyLFfuisuzeidWPMPWmECqU=
|
||||
github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
|
||||
github.com/go-sourcemap/sourcemap v2.1.4+incompatible h1:a+iTbH5auLKxaNwQFg0B+TCYl6lbukKPc7b5x0n1s6Q=
|
||||
github.com/go-sourcemap/sourcemap v2.1.4+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
|
||||
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
|
||||
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
|
||||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
||||
@@ -1511,8 +1511,8 @@ github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7Fsg
|
||||
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/grafana/alerting v0.0.0-20250219153626-c475b1a572b0 h1:LGH+tVzHCDrR9hsltmkP4jmNRg5IreQw5CNFbJKlnts=
|
||||
github.com/grafana/alerting v0.0.0-20250219153626-c475b1a572b0/go.mod h1:hdGB3dSl8Ma9Rjo2YiAEAjMkZ5HiNJbNDqRKDefRZrM=
|
||||
github.com/grafana/alerting v0.0.0-20250221202230-9d7e00921e44 h1:vboqvbAO0s0CTALHnqfmNvhCP1ziBcZNpYDbORqvOgg=
|
||||
github.com/grafana/alerting v0.0.0-20250221202230-9d7e00921e44/go.mod h1:hdGB3dSl8Ma9Rjo2YiAEAjMkZ5HiNJbNDqRKDefRZrM=
|
||||
github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7 h1:NTMmow+74I3Jb033xhbRgWQS7A//5TDhiM4tl7bsVP4=
|
||||
github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7/go.mod h1:T3X4z0ejGfJOiOmZLFeKCRT/yxWJq/RtclAc/PHj/w4=
|
||||
github.com/grafana/authlib/types v0.0.0-20250219092154-21ce22b49f31 h1:EokLC5grHwLPs4tXW8T6E8187H1e5G9AP0QQ5B60HbA=
|
||||
@@ -1845,8 +1845,6 @@ github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/linkedin/goavro/v2 v2.10.0 h1:eTBIRoInBM88gITGXYtUSqqxLTFXfOsJBiX8ZMW0o4U=
|
||||
github.com/linkedin/goavro/v2 v2.10.0/go.mod h1:UgQUb2N/pmueQYH9bfqFioWxzYCZXSfF8Jw03O5sjqA=
|
||||
github.com/linode/linodego v1.43.0 h1:sGeBB3caZt7vKBoPS5p4AVzmlG4JoqQOdigIibx3egk=
|
||||
github.com/linode/linodego v1.43.0/go.mod h1:n4TMFu1UVNala+icHqrTEFFaicYSF74cSAUG5zkTwfA=
|
||||
github.com/lyft/protoc-gen-star v0.6.0/go.mod h1:TGAoBVkt8w7MPG72TrKIu85MIdXwDuzJYeZuUPFPNwA=
|
||||
|
||||
+27
-1266
File diff suppressed because it is too large
Load Diff
@@ -1,8 +1,14 @@
|
||||
import { toDataFrame } from '../../dataframe/processDataFrame';
|
||||
import { FieldType } from '../../types/dataFrame';
|
||||
import { Field, FieldType } from '../../types/dataFrame';
|
||||
import { mockTransformationsRegistry } from '../../utils/tests/mockTransformationsRegistry';
|
||||
|
||||
import { histogramTransformer, buildHistogram, histogramFieldsToFrame } from './histogram';
|
||||
import {
|
||||
histogramTransformer,
|
||||
buildHistogram,
|
||||
histogramFieldsToFrame,
|
||||
HistogramFields,
|
||||
joinHistograms,
|
||||
} from './histogram';
|
||||
|
||||
describe('histogram frames frames', () => {
|
||||
beforeAll(() => {
|
||||
@@ -280,3 +286,110 @@ describe('histogram frames frames', () => {
|
||||
`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('joinHistograms', () => {
|
||||
type TestHistogram = {
|
||||
xMin: number[];
|
||||
xMax: number[];
|
||||
counts: number[][];
|
||||
};
|
||||
|
||||
function toField(name: string, values: number[]): Field {
|
||||
return {
|
||||
config: {},
|
||||
name,
|
||||
type: FieldType.number,
|
||||
values,
|
||||
};
|
||||
}
|
||||
|
||||
function testHistogramToHistogram(test: TestHistogram): HistogramFields {
|
||||
return {
|
||||
xMin: toField('xMin', test.xMin),
|
||||
xMax: toField('xMax', test.xMax),
|
||||
counts: test.counts.map((values) => toField(`count`, values)),
|
||||
};
|
||||
}
|
||||
|
||||
type TestCase = {
|
||||
name: string;
|
||||
histograms: TestHistogram[];
|
||||
expected: TestHistogram;
|
||||
};
|
||||
|
||||
const testCases: TestCase[] = [
|
||||
{
|
||||
name: 'just one histogram',
|
||||
histograms: [
|
||||
{
|
||||
xMin: [1, 2, 3],
|
||||
xMax: [2, 3, 4],
|
||||
counts: [[1, 2, 3]],
|
||||
},
|
||||
],
|
||||
expected: {
|
||||
xMin: [1, 2, 3],
|
||||
xMax: [2, 3, 4],
|
||||
counts: [[1, 2, 3]],
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'two histograms with same bucket sizes',
|
||||
histograms: [
|
||||
{
|
||||
xMin: [1, 3, 4],
|
||||
xMax: [2, 4, 5],
|
||||
counts: [[1, 2, 3]],
|
||||
},
|
||||
{
|
||||
xMin: [1, 3, 4],
|
||||
xMax: [2, 4, 5],
|
||||
counts: [[4, 5, 6]],
|
||||
},
|
||||
],
|
||||
expected: {
|
||||
xMin: [1, 3, 4],
|
||||
xMax: [2, 4, 5],
|
||||
counts: [
|
||||
[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
],
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'two histograms with same bucket sizes but counts in some different buckets',
|
||||
histograms: [
|
||||
{
|
||||
xMin: [1, 3, 4],
|
||||
xMax: [2, 4, 5],
|
||||
counts: [[1, 2, 3]],
|
||||
},
|
||||
{
|
||||
xMin: [2, 3, 6],
|
||||
xMax: [3, 4, 7],
|
||||
counts: [[4, 5, 6]],
|
||||
},
|
||||
],
|
||||
expected: {
|
||||
xMin: [1, 2, 3, 4, 6],
|
||||
xMax: [2, 3, 4, 5, 7],
|
||||
counts: [
|
||||
[1, 0, 2, 3, 0],
|
||||
[0, 4, 5, 0, 6],
|
||||
],
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
testCases.forEach((tc) => {
|
||||
it(tc.name, () => {
|
||||
const result = joinHistograms(tc.histograms.map(testHistogramToHistogram));
|
||||
|
||||
expect({
|
||||
xMin: result.xMin.values,
|
||||
xMax: result.xMax.values,
|
||||
counts: result.counts.map((f) => f.values),
|
||||
}).toEqual(tc.expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { map } from 'rxjs/operators';
|
||||
|
||||
import { outerJoinDataFrames } from '../..';
|
||||
import { getDisplayProcessor } from '../../field/displayProcessor';
|
||||
import { createTheme } from '../../themes/createTheme';
|
||||
import { GrafanaTheme2 } from '../../themes/types';
|
||||
@@ -325,7 +326,11 @@ export function getHistogramFields(frame: DataFrame): HistogramFields | undefine
|
||||
/**
|
||||
* @alpha
|
||||
*/
|
||||
export function buildHistogram(frames: DataFrame[], options?: HistogramTransformerOptions): HistogramFields | null {
|
||||
export function buildHistogram(
|
||||
frames: DataFrame[],
|
||||
options?: HistogramTransformerOptions,
|
||||
theme?: GrafanaTheme2
|
||||
): HistogramFields | null {
|
||||
let bucketSize = options?.bucketSize;
|
||||
let bucketCount = options?.bucketCount ?? DEFAULT_BUCKET_COUNT;
|
||||
let bucketOffset = options?.bucketOffset ?? 0;
|
||||
@@ -412,13 +417,20 @@ export function buildHistogram(frames: DataFrame[], options?: HistogramTransform
|
||||
if (field.type === FieldType.number) {
|
||||
let fieldHist = histogram(field.values, getBucket, histFilter, histSort);
|
||||
histograms.push(fieldHist);
|
||||
counts.push({
|
||||
|
||||
const count = {
|
||||
...field,
|
||||
config: {
|
||||
...field.config,
|
||||
unit: field.config.unit === 'short' ? 'short' : undefined,
|
||||
},
|
||||
};
|
||||
|
||||
count.display = getDisplayProcessor({
|
||||
field: count,
|
||||
theme: theme ?? createTheme(),
|
||||
});
|
||||
counts.push(count);
|
||||
if (!config && field.config.unit) {
|
||||
config = field.config;
|
||||
}
|
||||
@@ -573,12 +585,6 @@ export function histogramFieldsToFrame(info: HistogramFields, theme?: GrafanaThe
|
||||
info.xMax.display = display;
|
||||
}
|
||||
|
||||
// ensure updated units are reflected on the count field used for y axis formatting
|
||||
info.counts[0].display = getDisplayProcessor({
|
||||
field: info.counts[0],
|
||||
theme: theme ?? createTheme(),
|
||||
});
|
||||
|
||||
return {
|
||||
length: info.xMin.values.length,
|
||||
meta: {
|
||||
@@ -588,3 +594,63 @@ export function histogramFieldsToFrame(info: HistogramFields, theme?: GrafanaThe
|
||||
refId: `${DataTransformerID.histogram}`,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Join multiple histograms into a histogram with multiple counts.
|
||||
* Useful eg if you want to overlay them for comparison.
|
||||
*
|
||||
* This is needed because histogram results from database
|
||||
* will have buckets omitted for 0 counts, but when joining multiple histograms
|
||||
* we need to fill in the 0 values for missing buckets.
|
||||
*
|
||||
* Returns field configs of the first provided histogram.
|
||||
* @alpha
|
||||
*/
|
||||
|
||||
export function joinHistograms(histograms: HistogramFields[]): HistogramFields {
|
||||
if (histograms.length === 1) {
|
||||
return histograms[0];
|
||||
}
|
||||
|
||||
let joined = outerJoinDataFrames({
|
||||
frames: histograms.map((h) => ({
|
||||
length: h.xMax.values.length,
|
||||
fields: [h.xMax, h.xMin, ...h.counts],
|
||||
})),
|
||||
joinBy: (field) => field.name === 'xMax',
|
||||
})!;
|
||||
|
||||
let xMaxField: Field | null = null;
|
||||
let xMinField: Field | null = null;
|
||||
let countFields: Field[] = [];
|
||||
|
||||
// merge all xMin fields into first xMin field
|
||||
// and default all count fields to 0
|
||||
joined.fields.forEach((f) => {
|
||||
if (f.name === 'xMax') {
|
||||
xMaxField = f;
|
||||
} else if (f.name === 'xMin') {
|
||||
if (xMinField == null) {
|
||||
xMinField = f;
|
||||
} else {
|
||||
for (let i = 0; i < f.values.length; i++) {
|
||||
xMinField.values[i] ??= f.values[i];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
countFields.push({
|
||||
...f,
|
||||
values: f.values.map((v) => v ?? 0),
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const result: HistogramFields = {
|
||||
xMin: xMinField!,
|
||||
xMax: xMaxField!,
|
||||
counts: countFields,
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -256,4 +256,5 @@ export interface FeatureToggles {
|
||||
alertingJiraIntegration?: boolean;
|
||||
alertingRuleVersionHistoryRestore?: boolean;
|
||||
newShareReportDrawer?: boolean;
|
||||
rendererDisableAppPluginsPreload?: boolean;
|
||||
}
|
||||
|
||||
@@ -35,9 +35,13 @@ describe('MetricCombobox', () => {
|
||||
} as unknown as DataSourceInstanceSettings<PromOptions>;
|
||||
|
||||
const mockDatasource = new PrometheusDatasource(instanceSettings);
|
||||
const mockValues = [{ label: 'random_metric' }, { label: 'unique_metric' }, { label: 'more_unique_metric' }];
|
||||
|
||||
// Mock metricFindQuery which will call backend API
|
||||
// Options returned when user first opens the combobox - returned by onGetMetrics
|
||||
const initialMockValues = [{ label: 'top_metric_one' }, { label: 'top_metric_two' }, { label: 'top_metric_three' }];
|
||||
const mockOnGetMetrics = jest.fn(() => Promise.resolve(initialMockValues.map((v) => ({ value: v.label }))));
|
||||
|
||||
// Options returned when user searches for a metric
|
||||
const mockValues = [{ label: 'random_metric' }, { label: 'unique_metric' }, { label: 'more_unique_metric' }];
|
||||
mockDatasource.metricFindQuery = jest.fn((query: string) => {
|
||||
// return Promise.resolve([]);
|
||||
// Use the label values regex to get the values inside the label_values function call
|
||||
@@ -61,7 +65,6 @@ describe('MetricCombobox', () => {
|
||||
});
|
||||
|
||||
const mockOnChange = jest.fn();
|
||||
const mockOnGetMetrics = jest.fn(() => Promise.resolve(mockValues.map((v) => ({ value: v.label }))));
|
||||
|
||||
const defaultProps: MetricComboboxProps = {
|
||||
metricLookupDisabled: false,
|
||||
@@ -92,10 +95,11 @@ describe('MetricCombobox', () => {
|
||||
const combobox = screen.getByPlaceholderText('Select metric');
|
||||
await userEvent.click(combobox);
|
||||
|
||||
expect(mockOnGetMetrics).toHaveBeenCalledTimes(1);
|
||||
|
||||
const item = await screen.findByRole('option', { name: 'random_metric' });
|
||||
const item = await screen.findByRole('option', { name: 'top_metric_one' });
|
||||
expect(item).toBeInTheDocument();
|
||||
|
||||
// This should be asserted by the above check, but double check anyway
|
||||
expect(mockOnGetMetrics).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('fetches metrics for the users query', async () => {
|
||||
@@ -108,8 +112,9 @@ describe('MetricCombobox', () => {
|
||||
const item = await screen.findByRole('option', { name: 'unique_metric' });
|
||||
expect(item).toBeInTheDocument();
|
||||
|
||||
const negativeItem = screen.queryByRole('option', { name: 'random_metric' });
|
||||
expect(negativeItem).not.toBeInTheDocument();
|
||||
// This should be asserted by the above check, but double check anyway
|
||||
// This is the actual argument, created by formatKeyValueStringsForLabelValuesQuery()
|
||||
expect(mockDatasource.metricFindQuery).toHaveBeenCalledWith('label_values({__name__=~".*unique.*"},__name__)');
|
||||
});
|
||||
|
||||
it('calls onChange with the correct value when a metric is selected', async () => {
|
||||
@@ -118,10 +123,10 @@ describe('MetricCombobox', () => {
|
||||
const combobox = screen.getByPlaceholderText('Select metric');
|
||||
await userEvent.click(combobox);
|
||||
|
||||
const item = await screen.findByRole('option', { name: 'random_metric' });
|
||||
const item = await screen.findByRole('option', { name: 'top_metric_two' });
|
||||
await userEvent.click(item);
|
||||
|
||||
expect(mockOnChange).toHaveBeenCalledWith({ metric: 'random_metric', labels: [], operations: [] });
|
||||
expect(mockOnChange).toHaveBeenCalledWith({ metric: 'top_metric_two', labels: [], operations: [] });
|
||||
});
|
||||
|
||||
it('shows the metrics explorer button by default', () => {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { act, render, screen, fireEvent } from '@testing-library/react';
|
||||
import { act, render, screen, fireEvent, waitFor } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import React from 'react';
|
||||
|
||||
@@ -395,7 +395,9 @@ describe('Combobox', () => {
|
||||
const input = screen.getByRole('combobox');
|
||||
await user.click(input);
|
||||
|
||||
expect(asyncSpy).toHaveBeenCalledTimes(1); // Called on open
|
||||
expect(asyncSpy).not.toHaveBeenCalledTimes(1); // Not called yet
|
||||
act(() => jest.advanceTimersByTime(200)); // Add the debounce time
|
||||
expect(asyncSpy).toHaveBeenCalledTimes(1); // Then check if called on open
|
||||
asyncSpy.mockClear();
|
||||
|
||||
await user.keyboard('a');
|
||||
@@ -434,9 +436,9 @@ describe('Combobox', () => {
|
||||
});
|
||||
|
||||
it('should display message when there is an error loading async options', async () => {
|
||||
const asyncOptions = jest.fn(() => {
|
||||
throw new Error('Could not retrieve options');
|
||||
});
|
||||
const fetchData = jest.fn();
|
||||
const asyncOptions = fetchData.mockRejectedValue(new Error('Could not retrieve options'));
|
||||
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
|
||||
|
||||
render(<Combobox options={asyncOptions} value={null} onChange={onChangeHandler} />);
|
||||
|
||||
@@ -445,12 +447,15 @@ describe('Combobox', () => {
|
||||
await user.type(input, 'test');
|
||||
|
||||
await act(async () => {
|
||||
jest.advanceTimersToNextTimer();
|
||||
jest.advanceTimersByTimeAsync(500);
|
||||
});
|
||||
expect(asyncOptions).rejects.toThrow('Could not retrieve options');
|
||||
await waitFor(() => expect(consoleErrorSpy).toHaveBeenCalled());
|
||||
|
||||
const emptyMessage = screen.queryByText('An error occurred while loading options.');
|
||||
|
||||
expect(emptyMessage).toBeInTheDocument();
|
||||
|
||||
asyncOptions.mockClear();
|
||||
});
|
||||
|
||||
describe('with a value already selected', () => {
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import { cx } from '@emotion/css';
|
||||
import { useVirtualizer } from '@tanstack/react-virtual';
|
||||
import { useCombobox } from 'downshift';
|
||||
import { debounce } from 'lodash';
|
||||
import { useCallback, useId, useMemo, useState } from 'react';
|
||||
import { useId, useMemo } from 'react';
|
||||
|
||||
import { useStyles2 } from '../../themes';
|
||||
import { logOptions } from '../../utils';
|
||||
import { t } from '../../utils/i18n';
|
||||
import { Icon } from '../Icon/Icon';
|
||||
import { AutoSizeInput } from '../Input/AutoSizeInput';
|
||||
@@ -14,11 +12,11 @@ import { Portal } from '../Portal/Portal';
|
||||
import { ScrollContainer } from '../ScrollContainer/ScrollContainer';
|
||||
|
||||
import { AsyncError, NotFoundError } from './MessageRows';
|
||||
import { fuzzyFind, itemToString } from './filter';
|
||||
import { itemToString } from './filter';
|
||||
import { getComboboxStyles, MENU_OPTION_HEIGHT, MENU_OPTION_HEIGHT_DESCRIPTION } from './getComboboxStyles';
|
||||
import { ComboboxOption } from './types';
|
||||
import { useComboboxFloat } from './useComboboxFloat';
|
||||
import { StaleResultError, useLatestAsyncCall } from './useLatestAsyncCall';
|
||||
import { useOptions } from './useOptions';
|
||||
|
||||
// TODO: It would be great if ComboboxOption["label"] was more generic so that if consumers do pass it in (for async),
|
||||
// then the onChange handler emits ComboboxOption with the label as non-undefined.
|
||||
@@ -64,8 +62,6 @@ export interface ComboboxBaseProps<T extends string | number>
|
||||
onBlur?: () => void;
|
||||
}
|
||||
|
||||
const RECOMMENDED_ITEMS_AMOUNT = 100_000;
|
||||
|
||||
type ClearableConditionals<T extends number | string> =
|
||||
| {
|
||||
/**
|
||||
@@ -102,7 +98,6 @@ export type ComboboxProps<T extends string | number> = ComboboxBaseProps<T> &
|
||||
ClearableConditionals<T>;
|
||||
|
||||
const noop = () => {};
|
||||
const asyncNoop = () => Promise.resolve([]);
|
||||
|
||||
export const VIRTUAL_OVERSCAN_ITEMS = 4;
|
||||
|
||||
@@ -113,7 +108,7 @@ export const VIRTUAL_OVERSCAN_ITEMS = 4;
|
||||
*/
|
||||
export const Combobox = <T extends string | number>(props: ComboboxProps<T>) => {
|
||||
const {
|
||||
options,
|
||||
options: allOptions,
|
||||
onChange,
|
||||
value: valueProp,
|
||||
placeholder: placeholderProp,
|
||||
@@ -135,45 +130,13 @@ export const Combobox = <T extends string | number>(props: ComboboxProps<T>) =>
|
||||
// get a consistent Value from it
|
||||
const value = typeof valueProp === 'object' ? valueProp?.value : valueProp;
|
||||
|
||||
const isAsync = typeof options === 'function';
|
||||
const loadOptions = useLatestAsyncCall(isAsync ? options : asyncNoop); // loadOptions isn't called at all if not async
|
||||
const [asyncLoading, setAsyncLoading] = useState(false);
|
||||
const [asyncError, setAsyncError] = useState(false);
|
||||
|
||||
// A custom setter to always prepend the custom value at the beginning, if needed
|
||||
const [items, baseSetItems] = useState(isAsync ? [] : options);
|
||||
const setItems = useCallback(
|
||||
(items: Array<ComboboxOption<T>>, inputValue: string | undefined) => {
|
||||
let itemsToSet = items;
|
||||
logOptions(itemsToSet.length, RECOMMENDED_ITEMS_AMOUNT, id, ariaLabelledBy);
|
||||
if (inputValue && createCustomValue) {
|
||||
//Since the label of a normal option does not have to match its value and a custom option has the same value and label,
|
||||
//we just focus on the value to check if the option already exists
|
||||
const optionMatchingInput = items.find((opt) => opt.value === inputValue);
|
||||
|
||||
if (!optionMatchingInput) {
|
||||
const customValueOption = {
|
||||
label: inputValue,
|
||||
// Type casting needed to make this work when T is a number
|
||||
value: inputValue as T,
|
||||
description: t('combobox.custom-value.description', 'Use custom value'),
|
||||
};
|
||||
|
||||
itemsToSet = items.slice(0);
|
||||
itemsToSet.unshift(customValueOption);
|
||||
}
|
||||
}
|
||||
|
||||
baseSetItems(itemsToSet);
|
||||
},
|
||||
[createCustomValue, id, ariaLabelledBy]
|
||||
);
|
||||
|
||||
// Memoize for using in fuzzy search
|
||||
const stringifiedItems = useMemo(
|
||||
() => (isAsync ? [] : options.map((item) => itemToString(item))),
|
||||
[options, isAsync]
|
||||
);
|
||||
const {
|
||||
options: filteredOptions,
|
||||
updateOptions,
|
||||
asyncLoading,
|
||||
asyncError,
|
||||
} = useOptions(props.options, createCustomValue);
|
||||
const isAsync = typeof allOptions === 'function';
|
||||
|
||||
const selectedItemIndex = useMemo(() => {
|
||||
if (isAsync) {
|
||||
@@ -184,13 +147,13 @@ export const Combobox = <T extends string | number>(props: ComboboxProps<T>) =>
|
||||
return null;
|
||||
}
|
||||
|
||||
const index = options.findIndex((option) => option.value === value);
|
||||
const index = allOptions.findIndex((option) => option.value === value);
|
||||
if (index === -1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return index;
|
||||
}, [valueProp, options, value, isAsync]);
|
||||
}, [valueProp, allOptions, value, isAsync]);
|
||||
|
||||
const selectedItem = useMemo(() => {
|
||||
if (valueProp === undefined || valueProp === null) {
|
||||
@@ -198,11 +161,11 @@ export const Combobox = <T extends string | number>(props: ComboboxProps<T>) =>
|
||||
}
|
||||
|
||||
if (selectedItemIndex !== null && !isAsync) {
|
||||
return options[selectedItemIndex];
|
||||
return allOptions[selectedItemIndex];
|
||||
}
|
||||
|
||||
return typeof valueProp === 'object' ? valueProp : { value: valueProp, label: valueProp.toString() };
|
||||
}, [selectedItemIndex, isAsync, valueProp, options]);
|
||||
}, [selectedItemIndex, isAsync, valueProp, allOptions]);
|
||||
|
||||
const menuId = `downshift-${useId().replace(/:/g, '--')}-menu`;
|
||||
const labelId = `downshift-${useId().replace(/:/g, '--')}-label`;
|
||||
@@ -210,33 +173,15 @@ export const Combobox = <T extends string | number>(props: ComboboxProps<T>) =>
|
||||
const styles = useStyles2(getComboboxStyles);
|
||||
|
||||
const virtualizerOptions = {
|
||||
count: items.length,
|
||||
count: filteredOptions.length,
|
||||
getScrollElement: () => scrollRef.current,
|
||||
estimateSize: (index: number) => (items[index].description ? MENU_OPTION_HEIGHT_DESCRIPTION : MENU_OPTION_HEIGHT),
|
||||
estimateSize: (index: number) =>
|
||||
filteredOptions[index].description ? MENU_OPTION_HEIGHT_DESCRIPTION : MENU_OPTION_HEIGHT,
|
||||
overscan: VIRTUAL_OVERSCAN_ITEMS,
|
||||
};
|
||||
|
||||
const rowVirtualizer = useVirtualizer(virtualizerOptions);
|
||||
|
||||
const debounceAsync = useMemo(
|
||||
() =>
|
||||
debounce((inputValue: string) => {
|
||||
loadOptions(inputValue)
|
||||
.then((opts) => {
|
||||
setItems(opts, inputValue);
|
||||
setAsyncLoading(false);
|
||||
setAsyncError(false);
|
||||
})
|
||||
.catch((err) => {
|
||||
if (!(err instanceof StaleResultError)) {
|
||||
setAsyncError(true);
|
||||
setAsyncLoading(false);
|
||||
}
|
||||
});
|
||||
}, 200),
|
||||
[loadOptions, setItems]
|
||||
);
|
||||
|
||||
const {
|
||||
isOpen,
|
||||
highlightedIndex,
|
||||
@@ -250,7 +195,7 @@ export const Combobox = <T extends string | number>(props: ComboboxProps<T>) =>
|
||||
menuId,
|
||||
labelId,
|
||||
inputId: id,
|
||||
items,
|
||||
items: filteredOptions,
|
||||
itemToString,
|
||||
selectedItem,
|
||||
|
||||
@@ -267,48 +212,9 @@ export const Combobox = <T extends string | number>(props: ComboboxProps<T>) =>
|
||||
|
||||
scrollIntoView: () => {},
|
||||
|
||||
onInputValueChange: ({ inputValue, isOpen }) => {
|
||||
if (!isOpen) {
|
||||
// Prevent stale options from showing on reopen
|
||||
if (isAsync) {
|
||||
setItems([], '');
|
||||
}
|
||||
|
||||
// Otherwise there's nothing else to do when the menu isnt open
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isAsync) {
|
||||
const filteredItems = fuzzyFind(options, stringifiedItems, inputValue);
|
||||
setItems(filteredItems, inputValue);
|
||||
} else {
|
||||
if (inputValue && createCustomValue) {
|
||||
setItems([], inputValue);
|
||||
}
|
||||
|
||||
setAsyncLoading(true);
|
||||
debounceAsync(inputValue);
|
||||
}
|
||||
},
|
||||
|
||||
onIsOpenChange: ({ isOpen, inputValue }) => {
|
||||
// Loading async options mostly happens in onInputValueChange, but if the menu is opened with an empty input
|
||||
// then onInputValueChange isn't called (because the input value hasn't changed)
|
||||
if (isAsync && isOpen && inputValue === '') {
|
||||
setAsyncLoading(true);
|
||||
// TODO: dedupe this loading logic with debounceAsync
|
||||
loadOptions(inputValue)
|
||||
.then((opts) => {
|
||||
setItems(opts, inputValue);
|
||||
setAsyncLoading(false);
|
||||
setAsyncError(false);
|
||||
})
|
||||
.catch((err) => {
|
||||
if (!(err instanceof StaleResultError)) {
|
||||
setAsyncError(true);
|
||||
setAsyncLoading(false);
|
||||
}
|
||||
});
|
||||
if (isOpen && inputValue === '') {
|
||||
updateOptions(inputValue);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -317,7 +223,16 @@ export const Combobox = <T extends string | number>(props: ComboboxProps<T>) =>
|
||||
rowVirtualizer.scrollToIndex(highlightedIndex);
|
||||
}
|
||||
},
|
||||
onStateChange: ({ inputValue: newInputValue, type, selectedItem: newSelectedItem }) => {
|
||||
switch (type) {
|
||||
case useCombobox.stateChangeTypes.InputChange:
|
||||
updateOptions(newInputValue ?? '');
|
||||
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
},
|
||||
stateReducer(state, actionAndChanges) {
|
||||
let { changes } = actionAndChanges;
|
||||
const menuBeingOpened = state.isOpen === false && changes.isOpen === true;
|
||||
@@ -353,7 +268,7 @@ export const Combobox = <T extends string | number>(props: ComboboxProps<T>) =>
|
||||
},
|
||||
});
|
||||
|
||||
const { inputRef, floatingRef, floatStyles, scrollRef } = useComboboxFloat(items, isOpen);
|
||||
const { inputRef, floatingRef, floatStyles, scrollRef } = useComboboxFloat(filteredOptions, isOpen);
|
||||
|
||||
const isAutoSize = width === 'auto';
|
||||
|
||||
@@ -429,14 +344,16 @@ export const Combobox = <T extends string | number>(props: ComboboxProps<T>) =>
|
||||
{!asyncError && (
|
||||
<ul style={{ height: rowVirtualizer.getTotalSize() }} className={styles.menuUlContainer}>
|
||||
{rowVirtualizer.getVirtualItems().map((virtualRow) => {
|
||||
const item = filteredOptions[virtualRow.index];
|
||||
|
||||
return (
|
||||
<li
|
||||
key={`${items[virtualRow.index].value}-${virtualRow.index}`}
|
||||
key={`${item.value}-${virtualRow.index}`}
|
||||
data-index={virtualRow.index}
|
||||
className={cx(
|
||||
styles.optionBasic,
|
||||
styles.option,
|
||||
selectedItem && items[virtualRow.index].value === selectedItem.value && styles.optionSelected,
|
||||
selectedItem && item.value === selectedItem.value && styles.optionSelected,
|
||||
highlightedIndex === virtualRow.index && styles.optionFocused
|
||||
)}
|
||||
style={{
|
||||
@@ -444,17 +361,13 @@ export const Combobox = <T extends string | number>(props: ComboboxProps<T>) =>
|
||||
transform: `translateY(${virtualRow.start}px)`,
|
||||
}}
|
||||
{...getItemProps({
|
||||
item: items[virtualRow.index],
|
||||
item: item,
|
||||
index: virtualRow.index,
|
||||
})}
|
||||
>
|
||||
<div className={styles.optionBody}>
|
||||
<span className={styles.optionLabel}>
|
||||
{items[virtualRow.index].label ?? items[virtualRow.index].value}
|
||||
</span>
|
||||
{items[virtualRow.index].description && (
|
||||
<span className={styles.optionDescription}>{items[virtualRow.index].description}</span>
|
||||
)}
|
||||
<span className={styles.optionLabel}>{item.label ?? item.value}</span>
|
||||
{item.description && <span className={styles.optionDescription}>{item.description}</span>}
|
||||
</div>
|
||||
</li>
|
||||
);
|
||||
@@ -463,7 +376,7 @@ export const Combobox = <T extends string | number>(props: ComboboxProps<T>) =>
|
||||
)}
|
||||
<div aria-live="polite">
|
||||
{asyncError && <AsyncError />}
|
||||
{items.length === 0 && !asyncError && <NotFoundError />}
|
||||
{filteredOptions.length === 0 && !asyncError && <NotFoundError />}
|
||||
</div>
|
||||
</ScrollContainer>
|
||||
)}
|
||||
|
||||
@@ -3,7 +3,7 @@ import { useState, useCallback, useMemo } from 'react';
|
||||
|
||||
import { t } from '../../utils/i18n';
|
||||
|
||||
import { itemFilter } from './filter';
|
||||
import { fuzzyFind, itemToString } from './filter';
|
||||
import { ComboboxOption } from './types';
|
||||
import { StaleResultError, useLatestAsyncCall } from './useLatestAsyncCall';
|
||||
|
||||
@@ -83,14 +83,11 @@ export function useOptions<T extends string | number>(rawOptions: AsyncOptions<T
|
||||
|
||||
const updateOptions = useCallback(
|
||||
(inputValue: string) => {
|
||||
if (!isAsync) {
|
||||
setUserTypedSearch(inputValue);
|
||||
return;
|
||||
setUserTypedSearch(inputValue);
|
||||
if (isAsync) {
|
||||
setAsyncLoading(true);
|
||||
debouncedLoadOptions(inputValue);
|
||||
}
|
||||
|
||||
setAsyncLoading(true);
|
||||
|
||||
debouncedLoadOptions(inputValue);
|
||||
},
|
||||
[debouncedLoadOptions, isAsync]
|
||||
);
|
||||
@@ -122,12 +119,16 @@ export function useOptions<T extends string | number>(rawOptions: AsyncOptions<T
|
||||
return reorganizeOptions;
|
||||
}, []);
|
||||
|
||||
const stringifiedOptions = useMemo(() => {
|
||||
return isAsync ? [] : rawOptions.map(itemToString);
|
||||
}, [isAsync, rawOptions]);
|
||||
|
||||
const finalOptions = useMemo(() => {
|
||||
const currentOptions = isAsync ? asyncOptions : rawOptions.filter(itemFilter(userTypedSearch));
|
||||
const currentOptions = isAsync ? asyncOptions : fuzzyFind(rawOptions, stringifiedOptions, userTypedSearch);
|
||||
const currentOptionsOrganised = organizeOptionsByGroup(currentOptions);
|
||||
|
||||
return addCustomValue(currentOptionsOrganised);
|
||||
}, [isAsync, organizeOptionsByGroup, addCustomValue, asyncOptions, rawOptions, userTypedSearch]);
|
||||
}, [isAsync, organizeOptionsByGroup, addCustomValue, asyncOptions, rawOptions, userTypedSearch, stringifiedOptions]);
|
||||
|
||||
return { options: finalOptions, updateOptions, asyncLoading, asyncError };
|
||||
}
|
||||
|
||||
@@ -7,8 +7,8 @@ import { Combobox } from '../Combobox/Combobox';
|
||||
import { ComboboxOption } from '../Combobox/types';
|
||||
|
||||
export interface Props {
|
||||
onChange: (weekStart: WeekStart) => void;
|
||||
value: string;
|
||||
onChange: (weekStart?: WeekStart) => void;
|
||||
value?: WeekStart;
|
||||
width?: number;
|
||||
autoFocus?: boolean;
|
||||
onBlur?: () => void;
|
||||
@@ -24,9 +24,9 @@ const weekStarts: ComboboxOption[] = [
|
||||
{ value: 'monday', label: 'Monday' },
|
||||
];
|
||||
|
||||
const isWeekStart = (value: string): value is WeekStart => {
|
||||
export function isWeekStart(value: string): value is WeekStart {
|
||||
return ['saturday', 'sunday', 'monday'].includes(value);
|
||||
};
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
@@ -57,13 +57,13 @@ export const WeekStartPicker = (props: Props) => {
|
||||
const onChangeWeekStart = useCallback(
|
||||
(selectable: ComboboxOption | null) => {
|
||||
if (selectable && selectable.value !== undefined) {
|
||||
onChange(selectable.value as WeekStart);
|
||||
onChange(isWeekStart(selectable.value) ? selectable.value : undefined);
|
||||
}
|
||||
},
|
||||
[onChange]
|
||||
);
|
||||
|
||||
const selected = useMemo(() => weekStarts.find((item) => item.value === value)?.value ?? null, [value]);
|
||||
const selected = useMemo(() => weekStarts.find((item) => item.value === value)?.value ?? '', [value]);
|
||||
|
||||
return (
|
||||
<Combobox
|
||||
|
||||
@@ -40,7 +40,7 @@ export { TimePickerTooltip } from './DateTimePickers/TimeRangePicker';
|
||||
export { TimeRangeLabel } from './DateTimePickers/TimeRangePicker/TimeRangeLabel';
|
||||
export { TimeOfDayPicker } from './DateTimePickers/TimeOfDayPicker';
|
||||
export { TimeZonePicker } from './DateTimePickers/TimeZonePicker';
|
||||
export { WeekStartPicker, getWeekStart, type WeekStart } from './DateTimePickers/WeekStartPicker';
|
||||
export { WeekStartPicker, getWeekStart, type WeekStart, isWeekStart } from './DateTimePickers/WeekStartPicker';
|
||||
export { DatePicker, type DatePickerProps } from './DateTimePickers/DatePicker/DatePicker';
|
||||
export {
|
||||
DatePickerWithInput,
|
||||
|
||||
@@ -41,6 +41,7 @@ func (hs *HTTPServer) GetAnnotations(c *contextmodel.ReqContext) response.Respon
|
||||
OrgID: c.SignedInUser.GetOrgID(),
|
||||
UserID: c.QueryInt64("userId"),
|
||||
AlertID: c.QueryInt64("alertId"),
|
||||
AlertUID: c.Query("alertUID"),
|
||||
DashboardID: c.QueryInt64("dashboardId"),
|
||||
DashboardUID: c.Query("dashboardUID"),
|
||||
PanelID: c.QueryInt64("panelId"),
|
||||
@@ -744,10 +745,15 @@ type GetAnnotationsParams struct {
|
||||
// in:query
|
||||
// required:false
|
||||
UserID int64 `json:"userId"`
|
||||
// Find annotations for a specified alert.
|
||||
// Find annotations for a specified alert rule by its ID.
|
||||
// deprecated: AlertID is deprecated and will be removed in future versions. Please use AlertUID instead.
|
||||
// in:query
|
||||
// required:false
|
||||
AlertID int64 `json:"alertId"`
|
||||
// Find annotations for a specified alert rule by its UID.
|
||||
// in:query
|
||||
// required:false
|
||||
AlertUID string `json:"alertUID"`
|
||||
// Find annotations that are scoped to a specific dashboard
|
||||
// in:query
|
||||
// required:false
|
||||
|
||||
@@ -119,7 +119,7 @@ type JobStatus struct {
|
||||
Progress float64 `json:"progress,omitempty"`
|
||||
|
||||
// Summary of processed actions
|
||||
Summary []JobResourceSummary `json:"summary,omitempty"`
|
||||
Summary []*JobResourceSummary `json:"summary,omitempty"`
|
||||
}
|
||||
|
||||
type JobResourceSummary struct {
|
||||
|
||||
@@ -28,10 +28,10 @@ type LocalRepositoryConfig struct {
|
||||
type Workflow string
|
||||
|
||||
const (
|
||||
// WriteWorkflow allows a user to write directly to the repository
|
||||
WriteWorkflow Workflow = "write"
|
||||
// BranchWorkflow creates a branch for changes
|
||||
BranchWorkflow Workflow = "branch"
|
||||
// PushWorkflow pushes changes directly the configured branch
|
||||
PushWorkflow Workflow = "push"
|
||||
)
|
||||
|
||||
type GitHubRepositoryConfig struct {
|
||||
@@ -39,23 +39,13 @@ type GitHubRepositoryConfig struct {
|
||||
URL string `json:"url,omitempty"`
|
||||
|
||||
// The branch to use in the repository.
|
||||
// By default, this is the main branch.
|
||||
Branch string `json:"branch,omitempty"`
|
||||
Branch string `json:"branch"`
|
||||
// Token for accessing the repository. If set, it will be encrypted into encryptedToken, then set to an empty string again.
|
||||
Token string `json:"token,omitempty"`
|
||||
// Token for accessing the repository, but encrypted. This is not possible to read back to a user decrypted.
|
||||
// +listType=atomic
|
||||
EncryptedToken []byte `json:"encryptedToken,omitempty"`
|
||||
|
||||
// Workflow allowed for changes to the repository.
|
||||
// The order is relevant for defining the precedence of the workflows.
|
||||
// Possible values: pull-request, branch, push.
|
||||
Workflows []Workflow `json:"workflows,omitempty"`
|
||||
|
||||
// Whether we should commit to change branches and use a Pull Request flow to achieve this.
|
||||
// By default, this is false (i.e. we will commit straight to the main branch).
|
||||
BranchWorkflow bool `json:"branchWorkflow,omitempty"`
|
||||
|
||||
// Whether we should show dashboard previews for pull requests.
|
||||
// By default, this is false (i.e. we will not create previews).
|
||||
GenerateDashboardPreviews bool `json:"generateDashboardPreviews,omitempty"`
|
||||
@@ -78,8 +68,10 @@ type RepositorySpec struct {
|
||||
// Repository description
|
||||
Description string `json:"description,omitempty"`
|
||||
|
||||
// ReadOnly repository does not allow any write commands
|
||||
ReadOnly bool `json:"readOnly"`
|
||||
// UI driven Workflow that allow changes to the contends of the repository.
|
||||
// The order is relevant for defining the precedence of the workflows.
|
||||
// When empty, the repository does not support any edits (eg, readonly)
|
||||
Workflows []Workflow `json:"workflows"`
|
||||
|
||||
// Sync settings -- how values are pulled from the repository into grafana
|
||||
Sync SyncOptions `json:"sync"`
|
||||
@@ -183,8 +175,8 @@ type SyncStatus struct {
|
||||
// +listType=atomic
|
||||
Message []string `json:"message"`
|
||||
|
||||
// The repository hash when the last sync ran
|
||||
Hash string `json:"hash,omitempty"`
|
||||
// The repository ref when the last successful sync ran
|
||||
LastRef string `json:"lastRef,omitempty"`
|
||||
|
||||
// Incremental synchronization for versioned repositories
|
||||
Incremental bool `json:"incremental,omitempty"`
|
||||
|
||||
@@ -98,11 +98,6 @@ func (in *GitHubRepositoryConfig) DeepCopyInto(out *GitHubRepositoryConfig) {
|
||||
*out = make([]byte, len(*in))
|
||||
copy(*out, *in)
|
||||
}
|
||||
if in.Workflows != nil {
|
||||
in, out := &in.Workflows, &out.Workflows
|
||||
*out = make([]Workflow, len(*in))
|
||||
copy(*out, *in)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -314,9 +309,13 @@ func (in *JobStatus) DeepCopyInto(out *JobStatus) {
|
||||
}
|
||||
if in.Summary != nil {
|
||||
in, out := &in.Summary, &out.Summary
|
||||
*out = make([]JobResourceSummary, len(*in))
|
||||
*out = make([]*JobResourceSummary, len(*in))
|
||||
for i := range *in {
|
||||
(*in)[i].DeepCopyInto(&(*out)[i])
|
||||
if (*in)[i] != nil {
|
||||
in, out := &(*in)[i], &(*out)[i]
|
||||
*out = new(JobResourceSummary)
|
||||
(*in).DeepCopyInto(*out)
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
@@ -444,6 +443,11 @@ func (in *RepositoryList) DeepCopyObject() runtime.Object {
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *RepositorySpec) DeepCopyInto(out *RepositorySpec) {
|
||||
*out = *in
|
||||
if in.Workflows != nil {
|
||||
in, out := &in.Workflows, &out.Workflows
|
||||
*out = make([]Workflow, len(*in))
|
||||
copy(*out, *in)
|
||||
}
|
||||
out.Sync = in.Sync
|
||||
if in.Local != nil {
|
||||
in, out := &in.Local, &out.Local
|
||||
|
||||
@@ -246,7 +246,8 @@ func schema_pkg_apis_provisioning_v0alpha1_GitHubRepositoryConfig(ref common.Ref
|
||||
},
|
||||
"branch": {
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Description: "The branch to use in the repository. By default, this is the main branch.",
|
||||
Description: "The branch to use in the repository.",
|
||||
Default: "",
|
||||
Type: []string{"string"},
|
||||
Format: "",
|
||||
},
|
||||
@@ -270,29 +271,6 @@ func schema_pkg_apis_provisioning_v0alpha1_GitHubRepositoryConfig(ref common.Ref
|
||||
Format: "byte",
|
||||
},
|
||||
},
|
||||
"workflows": {
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Description: "Workflow allowed for changes to the repository. The order is relevant for defining the precedence of the workflows. Possible values: pull-request, branch, push.",
|
||||
Type: []string{"array"},
|
||||
Items: &spec.SchemaOrArray{
|
||||
Schema: &spec.Schema{
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Default: "",
|
||||
Type: []string{"string"},
|
||||
Format: "",
|
||||
Enum: []interface{}{"branch", "push"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"branchWorkflow": {
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Description: "Whether we should commit to change branches and use a Pull Request flow to achieve this. By default, this is false (i.e. we will commit straight to the main branch).",
|
||||
Type: []string{"boolean"},
|
||||
Format: "",
|
||||
},
|
||||
},
|
||||
"generateDashboardPreviews": {
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Description: "Whether we should show dashboard previews for pull requests. By default, this is false (i.e. we will not create previews).",
|
||||
@@ -301,6 +279,7 @@ func schema_pkg_apis_provisioning_v0alpha1_GitHubRepositoryConfig(ref common.Ref
|
||||
},
|
||||
},
|
||||
},
|
||||
Required: []string{"branch"},
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -749,8 +728,7 @@ func schema_pkg_apis_provisioning_v0alpha1_JobStatus(ref common.ReferenceCallbac
|
||||
Items: &spec.SchemaOrArray{
|
||||
Schema: &spec.Schema{
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Default: map[string]interface{}{},
|
||||
Ref: ref("github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1.JobResourceSummary"),
|
||||
Ref: ref("github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1.JobResourceSummary"),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -977,12 +955,20 @@ func schema_pkg_apis_provisioning_v0alpha1_RepositorySpec(ref common.ReferenceCa
|
||||
Format: "",
|
||||
},
|
||||
},
|
||||
"readOnly": {
|
||||
"workflows": {
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Description: "ReadOnly repository does not allow any write commands",
|
||||
Default: false,
|
||||
Type: []string{"boolean"},
|
||||
Format: "",
|
||||
Description: "UI driven Workflow that allow changes to the contends of the repository. The order is relevant for defining the precedence of the workflows. When empty, the repository does not support any edits (eg, readonly)",
|
||||
Type: []string{"array"},
|
||||
Items: &spec.SchemaOrArray{
|
||||
Schema: &spec.Schema{
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Default: "",
|
||||
Type: []string{"string"},
|
||||
Format: "",
|
||||
Enum: []interface{}{"branch", "write"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"sync": {
|
||||
@@ -1014,7 +1000,7 @@ func schema_pkg_apis_provisioning_v0alpha1_RepositorySpec(ref common.ReferenceCa
|
||||
},
|
||||
},
|
||||
},
|
||||
Required: []string{"title", "readOnly", "sync", "type"},
|
||||
Required: []string{"title", "workflows", "sync", "type"},
|
||||
},
|
||||
},
|
||||
Dependencies: []string{
|
||||
@@ -1717,9 +1703,9 @@ func schema_pkg_apis_provisioning_v0alpha1_SyncStatus(ref common.ReferenceCallba
|
||||
},
|
||||
},
|
||||
},
|
||||
"hash": {
|
||||
"lastRef": {
|
||||
SchemaProps: spec.SchemaProps{
|
||||
Description: "The repository hash when the last sync ran",
|
||||
Description: "The repository ref when the last successful sync ran",
|
||||
Type: []string{"string"},
|
||||
Format: "",
|
||||
},
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,FileList,Items
|
||||
API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,GitHubRepositoryConfig,Workflows
|
||||
API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,HistoryList,Items
|
||||
API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,JobResourceSummary,Errors
|
||||
API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,JobStatus,Errors
|
||||
API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,JobStatus,Summary
|
||||
API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,RepositoryList,Items
|
||||
API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,RepositorySpec,Workflows
|
||||
API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,RepositoryViewList,Items
|
||||
API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,ResourceList,Items
|
||||
API rule violation: list_type_missing,github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1,ResourceStats,Items
|
||||
|
||||
@@ -48,9 +48,9 @@ func (rangedEvaluator) Kind() EvaluatorKind {
|
||||
// an AlertEvaluator depending on evaluation operator.
|
||||
func newAlertEvaluator(model ConditionEvalJSON) (evaluator, error) {
|
||||
switch model.Type {
|
||||
case "gt", "lt":
|
||||
case "gt", "lt", "eq", "ne", "gte", "lte":
|
||||
return newThresholdEvaluator(model)
|
||||
case "within_range", "outside_range":
|
||||
case "within_range", "outside_range", "within_range_included", "outside_range_included":
|
||||
return newRangedEvaluator(model)
|
||||
case "no_value":
|
||||
return &noValueEvaluator{}, nil
|
||||
@@ -70,6 +70,14 @@ func (e *thresholdEvaluator) Eval(reducedValue mathexp.Number) bool {
|
||||
return *fv > e.Threshold
|
||||
case "lt":
|
||||
return *fv < e.Threshold
|
||||
case "eq":
|
||||
return *fv == e.Threshold
|
||||
case "ne":
|
||||
return *fv != e.Threshold
|
||||
case "gte":
|
||||
return *fv >= e.Threshold
|
||||
case "lte":
|
||||
return *fv <= e.Threshold
|
||||
}
|
||||
|
||||
return false
|
||||
@@ -113,6 +121,10 @@ func (e *rangedEvaluator) Eval(reducedValue mathexp.Number) bool {
|
||||
return (e.Lower < *fv && e.Upper > *fv) || (e.Upper < *fv && e.Lower > *fv)
|
||||
case "outside_range":
|
||||
return (e.Upper < *fv && e.Lower < *fv) || (e.Upper > *fv && e.Lower > *fv)
|
||||
case "within_range_included":
|
||||
return (e.Lower <= *fv && e.Upper >= *fv) || (e.Upper <= *fv && e.Lower >= *fv)
|
||||
case "outside_range_included":
|
||||
return (e.Upper <= *fv && e.Lower <= *fv) || (e.Upper >= *fv && e.Lower >= *fv)
|
||||
}
|
||||
|
||||
return false
|
||||
|
||||
@@ -40,6 +40,48 @@ func TestThresholdEvaluator(t *testing.T) {
|
||||
inputNumber: newNumber(util.Pointer(1.0)),
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "value 1 is eq 1: false",
|
||||
evaluator: &thresholdEvaluator{"eq", 1},
|
||||
inputNumber: newNumber(util.Pointer(1.0)),
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "value 0 is eq 0: false",
|
||||
evaluator: &thresholdEvaluator{"eq", 0},
|
||||
inputNumber: newNumber(util.Pointer(0.0)),
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "value 1 is eq 0: false",
|
||||
evaluator: &thresholdEvaluator{"eq", 0},
|
||||
inputNumber: newNumber(util.Pointer(1.0)),
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "value 0 is eq 1: false",
|
||||
evaluator: &thresholdEvaluator{"eq", 1},
|
||||
inputNumber: newNumber(util.Pointer(0.0)),
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "value 1 is ne 1: false",
|
||||
evaluator: &thresholdEvaluator{"ne", 1},
|
||||
inputNumber: newNumber(util.Pointer(1.0)),
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "value 3 is gte 3: false",
|
||||
evaluator: &thresholdEvaluator{"gte", 3},
|
||||
inputNumber: newNumber(util.Pointer(3.0)),
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "value 5 is lte 4: false",
|
||||
evaluator: &thresholdEvaluator{"lte", 4},
|
||||
inputNumber: newNumber(util.Pointer(5.0)),
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
||||
@@ -712,8 +712,14 @@
|
||||
"enum": [
|
||||
"gt",
|
||||
"lt",
|
||||
"eq",
|
||||
"ne",
|
||||
"gte",
|
||||
"lte",
|
||||
"within_range",
|
||||
"outside_range"
|
||||
"outside_range",
|
||||
"within_range_included",
|
||||
"outside_range_included"
|
||||
],
|
||||
"x-enum-description": {}
|
||||
}
|
||||
@@ -744,8 +750,14 @@
|
||||
"enum": [
|
||||
"gt",
|
||||
"lt",
|
||||
"eq",
|
||||
"ne",
|
||||
"gte",
|
||||
"lte",
|
||||
"within_range",
|
||||
"outside_range"
|
||||
"outside_range",
|
||||
"within_range_included",
|
||||
"outside_range_included"
|
||||
],
|
||||
"x-enum-description": {}
|
||||
}
|
||||
@@ -1013,4 +1025,4 @@
|
||||
},
|
||||
"additionalProperties": true,
|
||||
"$schema": "https://json-schema.org/draft-04/schema#"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -754,8 +754,14 @@
|
||||
"enum": [
|
||||
"gt",
|
||||
"lt",
|
||||
"eq",
|
||||
"ne",
|
||||
"gte",
|
||||
"lte",
|
||||
"within_range",
|
||||
"outside_range"
|
||||
"outside_range",
|
||||
"within_range_included",
|
||||
"outside_range_included"
|
||||
],
|
||||
"x-enum-description": {}
|
||||
}
|
||||
@@ -786,8 +792,14 @@
|
||||
"enum": [
|
||||
"gt",
|
||||
"lt",
|
||||
"eq",
|
||||
"ne",
|
||||
"gte",
|
||||
"lte",
|
||||
"within_range",
|
||||
"outside_range"
|
||||
"outside_range",
|
||||
"within_range_included",
|
||||
"outside_range_included"
|
||||
],
|
||||
"x-enum-description": {}
|
||||
}
|
||||
@@ -1071,4 +1083,4 @@
|
||||
},
|
||||
"additionalProperties": false,
|
||||
"$schema": "https://json-schema.org/draft-04/schema#"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -395,8 +395,14 @@
|
||||
"enum": [
|
||||
"gt",
|
||||
"lt",
|
||||
"eq",
|
||||
"ne",
|
||||
"gte",
|
||||
"lte",
|
||||
"within_range",
|
||||
"outside_range"
|
||||
"outside_range",
|
||||
"within_range_included",
|
||||
"outside_range_included"
|
||||
],
|
||||
"type": "string",
|
||||
"x-enum-description": {}
|
||||
@@ -427,8 +433,14 @@
|
||||
"enum": [
|
||||
"gt",
|
||||
"lt",
|
||||
"eq",
|
||||
"ne",
|
||||
"gte",
|
||||
"lte",
|
||||
"within_range",
|
||||
"outside_range"
|
||||
"outside_range",
|
||||
"within_range_included",
|
||||
"outside_range_included"
|
||||
],
|
||||
"type": "string",
|
||||
"x-enum-description": {}
|
||||
@@ -579,4 +591,4 @@
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
+96
-4
@@ -32,18 +32,30 @@ type ThresholdCommand struct {
|
||||
type ThresholdType string
|
||||
|
||||
const (
|
||||
ThresholdIsAbove ThresholdType = "gt"
|
||||
ThresholdIsBelow ThresholdType = "lt"
|
||||
ThresholdIsWithinRange ThresholdType = "within_range"
|
||||
ThresholdIsOutsideRange ThresholdType = "outside_range"
|
||||
ThresholdIsAbove ThresholdType = "gt"
|
||||
ThresholdIsBelow ThresholdType = "lt"
|
||||
ThresholdIsEqual ThresholdType = "eq"
|
||||
ThresholdIsNotEqual ThresholdType = "ne"
|
||||
ThresholdIsGreaterThanEqual ThresholdType = "gte"
|
||||
ThresholdIsLessThanEqual ThresholdType = "lte"
|
||||
ThresholdIsWithinRange ThresholdType = "within_range"
|
||||
ThresholdIsOutsideRange ThresholdType = "outside_range"
|
||||
ThresholdIsWithinRangeIncluded ThresholdType = "within_range_included"
|
||||
ThresholdIsOutsideRangeIncluded ThresholdType = "outside_range_included"
|
||||
)
|
||||
|
||||
var (
|
||||
supportedThresholdFuncs = []string{
|
||||
string(ThresholdIsAbove),
|
||||
string(ThresholdIsBelow),
|
||||
string(ThresholdIsEqual),
|
||||
string(ThresholdIsNotEqual),
|
||||
string(ThresholdIsGreaterThanEqual),
|
||||
string(ThresholdIsLessThanEqual),
|
||||
string(ThresholdIsWithinRange),
|
||||
string(ThresholdIsOutsideRange),
|
||||
string(ThresholdIsWithinRangeIncluded),
|
||||
string(ThresholdIsOutsideRangeIncluded),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -60,6 +72,16 @@ func NewThresholdCommand(refID, referenceVar string, thresholdFunc ThresholdType
|
||||
return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 2", thresholdFunc, len(conditions))
|
||||
}
|
||||
predicate = withinRangePredicate{left: conditions[0], right: conditions[1]}
|
||||
case ThresholdIsWithinRangeIncluded:
|
||||
if len(conditions) < 2 {
|
||||
return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 2", thresholdFunc, len(conditions))
|
||||
}
|
||||
predicate = withinRangeIncludedPredicate{left: conditions[0], right: conditions[1]}
|
||||
case ThresholdIsOutsideRangeIncluded:
|
||||
if len(conditions) < 2 {
|
||||
return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 2", thresholdFunc, len(conditions))
|
||||
}
|
||||
predicate = outsideRangeIncludedPredicate{left: conditions[0], right: conditions[1]}
|
||||
case ThresholdIsAbove:
|
||||
if len(conditions) < 1 {
|
||||
return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 1", thresholdFunc, len(conditions))
|
||||
@@ -70,6 +92,26 @@ func NewThresholdCommand(refID, referenceVar string, thresholdFunc ThresholdType
|
||||
return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 1", thresholdFunc, len(conditions))
|
||||
}
|
||||
predicate = lessThanPredicate{value: conditions[0]}
|
||||
case ThresholdIsEqual:
|
||||
if len(conditions) < 1 {
|
||||
return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 1", thresholdFunc, len(conditions))
|
||||
}
|
||||
predicate = equalPredicate{value: conditions[0]}
|
||||
case ThresholdIsNotEqual:
|
||||
if len(conditions) < 1 {
|
||||
return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 1", thresholdFunc, len(conditions))
|
||||
}
|
||||
predicate = notEqualPredicate{value: conditions[0]}
|
||||
case ThresholdIsGreaterThanEqual:
|
||||
if len(conditions) < 1 {
|
||||
return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 1", thresholdFunc, len(conditions))
|
||||
}
|
||||
predicate = greaterThanEqualPredicate{value: conditions[0]}
|
||||
case ThresholdIsLessThanEqual:
|
||||
if len(conditions) < 1 {
|
||||
return nil, fmt.Errorf("incorrect number of arguments for threshold function '%s': got %d but need 1", thresholdFunc, len(conditions))
|
||||
}
|
||||
predicate = lessThanEqualPredicate{value: conditions[0]}
|
||||
default:
|
||||
return nil, fmt.Errorf("expected threshold function to be one of [%s], got %s", strings.Join(supportedThresholdFuncs, ", "), thresholdFunc)
|
||||
}
|
||||
@@ -279,6 +321,24 @@ func (r outsideRangePredicate) Eval(f float64) bool {
|
||||
return f < r.left || f > r.right
|
||||
}
|
||||
|
||||
type withinRangeIncludedPredicate struct {
|
||||
left float64
|
||||
right float64
|
||||
}
|
||||
|
||||
func (r withinRangeIncludedPredicate) Eval(f float64) bool {
|
||||
return f >= r.left && f <= r.right
|
||||
}
|
||||
|
||||
type outsideRangeIncludedPredicate struct {
|
||||
left float64
|
||||
right float64
|
||||
}
|
||||
|
||||
func (r outsideRangeIncludedPredicate) Eval(f float64) bool {
|
||||
return f <= r.left || f >= r.right
|
||||
}
|
||||
|
||||
type lessThanPredicate struct {
|
||||
value float64
|
||||
}
|
||||
@@ -294,3 +354,35 @@ type greaterThanPredicate struct {
|
||||
func (r greaterThanPredicate) Eval(f float64) bool {
|
||||
return f > r.value
|
||||
}
|
||||
|
||||
type equalPredicate struct {
|
||||
value float64
|
||||
}
|
||||
|
||||
func (r equalPredicate) Eval(f float64) bool {
|
||||
return f == r.value
|
||||
}
|
||||
|
||||
type notEqualPredicate struct {
|
||||
value float64
|
||||
}
|
||||
|
||||
func (r notEqualPredicate) Eval(f float64) bool {
|
||||
return f != r.value
|
||||
}
|
||||
|
||||
type greaterThanEqualPredicate struct {
|
||||
value float64
|
||||
}
|
||||
|
||||
func (r greaterThanEqualPredicate) Eval(f float64) bool {
|
||||
return f >= r.value
|
||||
}
|
||||
|
||||
type lessThanEqualPredicate struct {
|
||||
value float64
|
||||
}
|
||||
|
||||
func (r lessThanEqualPredicate) Eval(f float64) bool {
|
||||
return f <= r.value
|
||||
}
|
||||
|
||||
@@ -38,6 +38,26 @@ func TestNewThresholdCommand(t *testing.T) {
|
||||
args: []float64{0},
|
||||
shouldError: false,
|
||||
},
|
||||
{
|
||||
fn: "eq",
|
||||
args: []float64{0},
|
||||
shouldError: false,
|
||||
},
|
||||
{
|
||||
fn: "ne",
|
||||
args: []float64{0},
|
||||
shouldError: false,
|
||||
},
|
||||
{
|
||||
fn: "gte",
|
||||
args: []float64{0},
|
||||
shouldError: false,
|
||||
},
|
||||
{
|
||||
fn: "lte",
|
||||
args: []float64{0},
|
||||
shouldError: false,
|
||||
},
|
||||
{
|
||||
fn: "within_range",
|
||||
args: []float64{0, 1},
|
||||
@@ -48,6 +68,16 @@ func TestNewThresholdCommand(t *testing.T) {
|
||||
args: []float64{0, 1},
|
||||
shouldError: false,
|
||||
},
|
||||
{
|
||||
fn: "within_range_included",
|
||||
args: []float64{0, 1},
|
||||
shouldError: false,
|
||||
},
|
||||
{
|
||||
fn: "outside_range_included",
|
||||
args: []float64{0, 1},
|
||||
shouldError: false,
|
||||
},
|
||||
{
|
||||
fn: "gt",
|
||||
args: []float64{},
|
||||
@@ -60,6 +90,30 @@ func TestNewThresholdCommand(t *testing.T) {
|
||||
shouldError: true,
|
||||
expectedError: "incorrect number of arguments",
|
||||
},
|
||||
{
|
||||
fn: "eq",
|
||||
args: []float64{},
|
||||
shouldError: true,
|
||||
expectedError: "incorrect number of arguments",
|
||||
},
|
||||
{
|
||||
fn: "ne",
|
||||
args: []float64{},
|
||||
shouldError: true,
|
||||
expectedError: "incorrect number of arguments",
|
||||
},
|
||||
{
|
||||
fn: "gte",
|
||||
args: []float64{},
|
||||
shouldError: true,
|
||||
expectedError: "incorrect number of arguments",
|
||||
},
|
||||
{
|
||||
fn: "lte",
|
||||
args: []float64{},
|
||||
shouldError: true,
|
||||
expectedError: "incorrect number of arguments",
|
||||
},
|
||||
{
|
||||
fn: "within_range",
|
||||
args: []float64{0},
|
||||
@@ -72,6 +126,18 @@ func TestNewThresholdCommand(t *testing.T) {
|
||||
shouldError: true,
|
||||
expectedError: "incorrect number of arguments",
|
||||
},
|
||||
{
|
||||
fn: "within_range_included",
|
||||
args: []float64{0},
|
||||
shouldError: true,
|
||||
expectedError: "incorrect number of arguments",
|
||||
},
|
||||
{
|
||||
fn: "outside_range_included",
|
||||
args: []float64{0},
|
||||
shouldError: true,
|
||||
expectedError: "incorrect number of arguments",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
@@ -249,6 +315,22 @@ func TestIsSupportedThresholdFunc(t *testing.T) {
|
||||
function: ThresholdIsBelow,
|
||||
supported: true,
|
||||
},
|
||||
{
|
||||
function: ThresholdIsEqual,
|
||||
supported: true,
|
||||
},
|
||||
{
|
||||
function: ThresholdIsNotEqual,
|
||||
supported: true,
|
||||
},
|
||||
{
|
||||
function: ThresholdIsGreaterThanEqual,
|
||||
supported: true,
|
||||
},
|
||||
{
|
||||
function: ThresholdIsLessThanEqual,
|
||||
supported: true,
|
||||
},
|
||||
{
|
||||
function: ThresholdIsWithinRange,
|
||||
supported: true,
|
||||
@@ -257,6 +339,14 @@ func TestIsSupportedThresholdFunc(t *testing.T) {
|
||||
function: ThresholdIsOutsideRange,
|
||||
supported: true,
|
||||
},
|
||||
{
|
||||
function: ThresholdIsWithinRangeIncluded,
|
||||
supported: true,
|
||||
},
|
||||
{
|
||||
function: ThresholdIsOutsideRangeIncluded,
|
||||
supported: true,
|
||||
},
|
||||
{
|
||||
function: "foo",
|
||||
supported: false,
|
||||
|
||||
@@ -19,7 +19,6 @@ import (
|
||||
_ "github.com/grpc-ecosystem/go-grpc-middleware/v2"
|
||||
_ "github.com/hashicorp/go-multierror"
|
||||
_ "github.com/hashicorp/golang-lru/v2"
|
||||
_ "github.com/linkedin/goavro/v2"
|
||||
_ "github.com/m3db/prometheus_remote_client_golang/promremote"
|
||||
_ "github.com/phpdave11/gofpdi"
|
||||
_ "github.com/robfig/cron/v3"
|
||||
|
||||
@@ -4,20 +4,14 @@
|
||||
|
||||
package v0alpha1
|
||||
|
||||
import (
|
||||
provisioningv0alpha1 "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1"
|
||||
)
|
||||
|
||||
// GitHubRepositoryConfigApplyConfiguration represents a declarative configuration of the GitHubRepositoryConfig type for use
|
||||
// with apply.
|
||||
type GitHubRepositoryConfigApplyConfiguration struct {
|
||||
URL *string `json:"url,omitempty"`
|
||||
Branch *string `json:"branch,omitempty"`
|
||||
Token *string `json:"token,omitempty"`
|
||||
EncryptedToken []byte `json:"encryptedToken,omitempty"`
|
||||
Workflows []provisioningv0alpha1.Workflow `json:"workflows,omitempty"`
|
||||
BranchWorkflow *bool `json:"branchWorkflow,omitempty"`
|
||||
GenerateDashboardPreviews *bool `json:"generateDashboardPreviews,omitempty"`
|
||||
URL *string `json:"url,omitempty"`
|
||||
Branch *string `json:"branch,omitempty"`
|
||||
Token *string `json:"token,omitempty"`
|
||||
EncryptedToken []byte `json:"encryptedToken,omitempty"`
|
||||
GenerateDashboardPreviews *bool `json:"generateDashboardPreviews,omitempty"`
|
||||
}
|
||||
|
||||
// GitHubRepositoryConfigApplyConfiguration constructs a declarative configuration of the GitHubRepositoryConfig type for use with
|
||||
@@ -60,24 +54,6 @@ func (b *GitHubRepositoryConfigApplyConfiguration) WithEncryptedToken(values ...
|
||||
return b
|
||||
}
|
||||
|
||||
// WithWorkflows adds the given value to the Workflows field in the declarative configuration
|
||||
// and returns the receiver, so that objects can be build by chaining "With" function invocations.
|
||||
// If called multiple times, values provided by each call will be appended to the Workflows field.
|
||||
func (b *GitHubRepositoryConfigApplyConfiguration) WithWorkflows(values ...provisioningv0alpha1.Workflow) *GitHubRepositoryConfigApplyConfiguration {
|
||||
for i := range values {
|
||||
b.Workflows = append(b.Workflows, values[i])
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// WithBranchWorkflow sets the BranchWorkflow field in the declarative configuration to the given value
|
||||
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
|
||||
// If called multiple times, the BranchWorkflow field is set to the value of the last call.
|
||||
func (b *GitHubRepositoryConfigApplyConfiguration) WithBranchWorkflow(value bool) *GitHubRepositoryConfigApplyConfiguration {
|
||||
b.BranchWorkflow = &value
|
||||
return b
|
||||
}
|
||||
|
||||
// WithGenerateDashboardPreviews sets the GenerateDashboardPreviews field in the declarative configuration to the given value
|
||||
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
|
||||
// If called multiple times, the GenerateDashboardPreviews field is set to the value of the last call.
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
type RepositorySpecApplyConfiguration struct {
|
||||
Title *string `json:"title,omitempty"`
|
||||
Description *string `json:"description,omitempty"`
|
||||
ReadOnly *bool `json:"readOnly,omitempty"`
|
||||
Workflows []provisioningv0alpha1.Workflow `json:"workflows,omitempty"`
|
||||
Sync *SyncOptionsApplyConfiguration `json:"sync,omitempty"`
|
||||
Type *provisioningv0alpha1.RepositoryType `json:"type,omitempty"`
|
||||
Local *LocalRepositoryConfigApplyConfiguration `json:"local,omitempty"`
|
||||
@@ -42,11 +42,13 @@ func (b *RepositorySpecApplyConfiguration) WithDescription(value string) *Reposi
|
||||
return b
|
||||
}
|
||||
|
||||
// WithReadOnly sets the ReadOnly field in the declarative configuration to the given value
|
||||
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
|
||||
// If called multiple times, the ReadOnly field is set to the value of the last call.
|
||||
func (b *RepositorySpecApplyConfiguration) WithReadOnly(value bool) *RepositorySpecApplyConfiguration {
|
||||
b.ReadOnly = &value
|
||||
// WithWorkflows adds the given value to the Workflows field in the declarative configuration
|
||||
// and returns the receiver, so that objects can be build by chaining "With" function invocations.
|
||||
// If called multiple times, values provided by each call will be appended to the Workflows field.
|
||||
func (b *RepositorySpecApplyConfiguration) WithWorkflows(values ...provisioningv0alpha1.Workflow) *RepositorySpecApplyConfiguration {
|
||||
for i := range values {
|
||||
b.Workflows = append(b.Workflows, values[i])
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ type SyncStatusApplyConfiguration struct {
|
||||
Finished *int64 `json:"finished,omitempty"`
|
||||
Scheduled *int64 `json:"scheduled,omitempty"`
|
||||
Message []string `json:"message,omitempty"`
|
||||
Hash *string `json:"hash,omitempty"`
|
||||
LastRef *string `json:"lastRef,omitempty"`
|
||||
Incremental *bool `json:"incremental,omitempty"`
|
||||
}
|
||||
|
||||
@@ -77,11 +77,11 @@ func (b *SyncStatusApplyConfiguration) WithMessage(values ...string) *SyncStatus
|
||||
return b
|
||||
}
|
||||
|
||||
// WithHash sets the Hash field in the declarative configuration to the given value
|
||||
// WithLastRef sets the LastRef field in the declarative configuration to the given value
|
||||
// and returns the receiver, so that objects can be built by chaining "With" function invocations.
|
||||
// If called multiple times, the Hash field is set to the value of the last call.
|
||||
func (b *SyncStatusApplyConfiguration) WithHash(value string) *SyncStatusApplyConfiguration {
|
||||
b.Hash = &value
|
||||
// If called multiple times, the LastRef field is set to the value of the last call.
|
||||
func (b *SyncStatusApplyConfiguration) WithLastRef(value string) *SyncStatusApplyConfiguration {
|
||||
b.LastRef = &value
|
||||
return b
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,203 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana-app-sdk/logging"
|
||||
provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/provisioning/repository"
|
||||
)
|
||||
|
||||
// maybeNotifyProgress will only notify if a certain amount of time has passed
|
||||
// or if the job completed
|
||||
func maybeNotifyProgress(threshold time.Duration, fn ProgressFn) ProgressFn {
|
||||
var last time.Time
|
||||
|
||||
return func(ctx context.Context, status provisioning.JobStatus) error {
|
||||
if status.Finished != 0 || last.IsZero() || time.Since(last) > threshold {
|
||||
last = time.Now()
|
||||
return fn(ctx, status)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: ProgressRecorder should be initialized in the queue
|
||||
type JobResourceResult struct {
|
||||
Name string
|
||||
Resource string
|
||||
Group string
|
||||
Path string
|
||||
Action repository.FileAction
|
||||
Error error
|
||||
}
|
||||
|
||||
type jobProgressRecorder struct {
|
||||
started time.Time
|
||||
total int
|
||||
ref string
|
||||
message string
|
||||
resultCount int
|
||||
errorCount int
|
||||
errors []string
|
||||
progressFn ProgressFn
|
||||
summaries map[string]*provisioning.JobResourceSummary
|
||||
}
|
||||
|
||||
func newJobProgressRecorder(ProgressFn ProgressFn) JobProgressRecorder {
|
||||
return &jobProgressRecorder{
|
||||
started: time.Now(),
|
||||
progressFn: maybeNotifyProgress(5*time.Second, ProgressFn),
|
||||
summaries: make(map[string]*provisioning.JobResourceSummary),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *jobProgressRecorder) Record(ctx context.Context, result JobResourceResult) {
|
||||
r.resultCount++
|
||||
|
||||
logger := logging.FromContext(ctx).With("path", result.Path, "resource", result.Resource, "group", result.Group, "action", result.Action, "name", result.Name)
|
||||
if result.Error != nil {
|
||||
logger.Error("job resource operation failed", "err", result.Error)
|
||||
if len(r.errors) < 20 {
|
||||
r.errors = append(r.errors, result.Error.Error())
|
||||
}
|
||||
r.errorCount++
|
||||
} else {
|
||||
logger.Info("job resource operation succeeded")
|
||||
}
|
||||
|
||||
r.updateSummary(result)
|
||||
r.notify(ctx)
|
||||
}
|
||||
|
||||
func (r *jobProgressRecorder) SetMessage(msg string) {
|
||||
r.message = msg
|
||||
}
|
||||
|
||||
func (r *jobProgressRecorder) GetMessage() string {
|
||||
return r.message
|
||||
}
|
||||
|
||||
func (r *jobProgressRecorder) SetRef(ref string) {
|
||||
r.ref = ref
|
||||
}
|
||||
|
||||
func (r *jobProgressRecorder) GetRef() string {
|
||||
return r.ref
|
||||
}
|
||||
|
||||
func (r *jobProgressRecorder) SetTotal(total int) {
|
||||
r.total = total
|
||||
}
|
||||
|
||||
func (r *jobProgressRecorder) TooManyErrors() error {
|
||||
if r.errorCount > 20 {
|
||||
return fmt.Errorf("too many errors: %d", r.errorCount)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *jobProgressRecorder) summary() []*provisioning.JobResourceSummary {
|
||||
if len(r.summaries) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
summaries := make([]*provisioning.JobResourceSummary, 0, len(r.summaries))
|
||||
for _, summary := range r.summaries {
|
||||
summaries = append(summaries, summary)
|
||||
}
|
||||
|
||||
return summaries
|
||||
}
|
||||
|
||||
func (r *jobProgressRecorder) updateSummary(result JobResourceResult) {
|
||||
key := result.Resource + ":" + result.Group
|
||||
summary, exists := r.summaries[key]
|
||||
if !exists {
|
||||
summary = &provisioning.JobResourceSummary{
|
||||
Resource: result.Resource,
|
||||
Group: result.Group,
|
||||
}
|
||||
r.summaries[key] = summary
|
||||
}
|
||||
|
||||
if result.Error != nil {
|
||||
summary.Errors = append(summary.Errors, result.Error.Error())
|
||||
summary.Error++
|
||||
} else {
|
||||
switch result.Action {
|
||||
case repository.FileActionDeleted:
|
||||
summary.Delete++
|
||||
case repository.FileActionUpdated:
|
||||
summary.Update++
|
||||
case repository.FileActionCreated:
|
||||
summary.Create++
|
||||
case repository.FileActionIgnored:
|
||||
summary.Noop++
|
||||
case repository.FileActionRenamed:
|
||||
summary.Delete++
|
||||
summary.Create++
|
||||
}
|
||||
summary.Write = summary.Create + summary.Update
|
||||
}
|
||||
}
|
||||
|
||||
func (r *jobProgressRecorder) progress() float64 {
|
||||
if r.total == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
return float64(r.resultCount) / float64(r.total) * 100
|
||||
}
|
||||
|
||||
func (r *jobProgressRecorder) notify(ctx context.Context) {
|
||||
jobStatus := provisioning.JobStatus{
|
||||
State: provisioning.JobStateWorking,
|
||||
Message: r.message,
|
||||
Errors: r.errors,
|
||||
Progress: r.progress(),
|
||||
Summary: r.summary(),
|
||||
}
|
||||
|
||||
logger := logging.FromContext(ctx)
|
||||
if err := r.progressFn(ctx, jobStatus); err != nil {
|
||||
logger.Warn("error notifying progress", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *jobProgressRecorder) Complete(ctx context.Context, err error) provisioning.JobStatus {
|
||||
// Initialize base job status
|
||||
jobStatus := provisioning.JobStatus{
|
||||
Started: r.started.UnixMilli(),
|
||||
// FIXME: if we call this method twice, the state will be different
|
||||
// This results in sync status to be different from job status
|
||||
Finished: time.Now().UnixMilli(),
|
||||
State: provisioning.JobStateSuccess,
|
||||
Message: "completed successfully",
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
jobStatus.State = provisioning.JobStateError
|
||||
jobStatus.Message = err.Error()
|
||||
}
|
||||
|
||||
jobStatus.Summary = r.summary()
|
||||
jobStatus.Errors = r.errors
|
||||
|
||||
// Check for errors during execution
|
||||
if len(jobStatus.Errors) > 0 && jobStatus.State != provisioning.JobStateError {
|
||||
jobStatus.State = provisioning.JobStateError
|
||||
jobStatus.Message = "completed with errors"
|
||||
}
|
||||
|
||||
// Override message if progress have a more explicit message
|
||||
if r.message != "" && jobStatus.State != provisioning.JobStateError {
|
||||
jobStatus.Message = r.message
|
||||
}
|
||||
|
||||
return jobStatus
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/provisioning/repository"
|
||||
)
|
||||
|
||||
type RepoGetter interface {
|
||||
GetRepository(ctx context.Context, name string) (repository.Repository, error)
|
||||
}
|
||||
|
||||
// Basic job queue infrastructure
|
||||
type JobQueue interface {
|
||||
// Add a new Job to the Queue. The status must be empty
|
||||
Add(ctx context.Context, job *provisioning.Job) (*provisioning.Job, error)
|
||||
|
||||
// Get the next job we should process
|
||||
Next(ctx context.Context) *provisioning.Job
|
||||
|
||||
// Update the status on a given job
|
||||
// This is only valid if current job is not finished
|
||||
Update(ctx context.Context, namespace string, name string, status provisioning.JobStatus) error
|
||||
|
||||
// Register a worker (inline for now)
|
||||
Register(worker Worker)
|
||||
}
|
||||
|
||||
type JobProgressRecorder interface {
|
||||
Record(ctx context.Context, result JobResourceResult)
|
||||
SetMessage(msg string)
|
||||
GetMessage() string
|
||||
SetRef(ref string)
|
||||
GetRef() string
|
||||
SetTotal(total int)
|
||||
TooManyErrors() error
|
||||
Complete(ctx context.Context, err error) provisioning.JobStatus
|
||||
}
|
||||
|
||||
type Worker interface {
|
||||
IsSupported(ctx context.Context, job provisioning.Job) bool
|
||||
Process(ctx context.Context, repo repository.Repository, job provisioning.Job, progress JobProgressRecorder) error
|
||||
}
|
||||
|
||||
// ProgressFn is a function that can be called to update the progress of a job
|
||||
type ProgressFn func(ctx context.Context, status provisioning.JobStatus) error
|
||||
@@ -0,0 +1,367 @@
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/internalversion"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/fields"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/apiserver/pkg/endpoints/request"
|
||||
"k8s.io/apiserver/pkg/registry/rest"
|
||||
"k8s.io/apiserver/pkg/storage"
|
||||
|
||||
"github.com/grafana/grafana-app-sdk/logging"
|
||||
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
||||
provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
var (
|
||||
_ JobQueue = (*jobStore)(nil)
|
||||
_ rest.Scoper = (*jobStore)(nil)
|
||||
_ rest.SingularNameProvider = (*jobStore)(nil)
|
||||
_ rest.Getter = (*jobStore)(nil)
|
||||
_ rest.Lister = (*jobStore)(nil)
|
||||
_ rest.Storage = (*jobStore)(nil)
|
||||
_ rest.Watcher = (*jobStore)(nil)
|
||||
)
|
||||
|
||||
func NewJobStore(capacity int, getter RepoGetter) *jobStore {
|
||||
return &jobStore{
|
||||
workers: make([]Worker, 0),
|
||||
getter: getter,
|
||||
rv: 1,
|
||||
capacity: capacity,
|
||||
jobs: []provisioning.Job{},
|
||||
watchSet: NewWatchSet(),
|
||||
versioner: &storage.APIObjectVersioner{},
|
||||
}
|
||||
}
|
||||
|
||||
type jobStore struct {
|
||||
getter RepoGetter
|
||||
capacity int
|
||||
workers []Worker
|
||||
|
||||
// All jobs
|
||||
jobs []provisioning.Job
|
||||
rv int64 // updates whenever changed
|
||||
watchSet *WatchSet
|
||||
versioner storage.Versioner
|
||||
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
// Implementing Kube interfaces
|
||||
|
||||
func (s *jobStore) New() runtime.Object {
|
||||
return provisioning.JobResourceInfo.NewFunc()
|
||||
}
|
||||
|
||||
func (s *jobStore) Destroy() {}
|
||||
|
||||
func (s *jobStore) NamespaceScoped() bool {
|
||||
return true // namespace == org
|
||||
}
|
||||
|
||||
func (s *jobStore) GetSingularName() string {
|
||||
return provisioning.JobResourceInfo.GetSingularName()
|
||||
}
|
||||
|
||||
func (s *jobStore) NewList() runtime.Object {
|
||||
return provisioning.JobResourceInfo.NewListFunc()
|
||||
}
|
||||
|
||||
func (s *jobStore) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
|
||||
return provisioning.JobResourceInfo.TableConverter().ConvertToTable(ctx, object, tableOptions)
|
||||
}
|
||||
|
||||
func (s *jobStore) List(ctx context.Context, options *internalversion.ListOptions) (runtime.Object, error) {
|
||||
ns, ok := request.NamespaceFrom(ctx)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("missing namespace")
|
||||
}
|
||||
|
||||
queue := &provisioning.JobList{
|
||||
ListMeta: metav1.ListMeta{
|
||||
ResourceVersion: strconv.FormatInt(s.rv, 10),
|
||||
},
|
||||
}
|
||||
|
||||
query := options.LabelSelector
|
||||
|
||||
s.mutex.RLock()
|
||||
defer s.mutex.RUnlock()
|
||||
|
||||
for _, job := range s.jobs {
|
||||
if job.Namespace != ns {
|
||||
continue
|
||||
}
|
||||
|
||||
// maybe filter
|
||||
if query != nil && !query.Matches(labels.Set(job.Labels)) {
|
||||
continue
|
||||
}
|
||||
|
||||
copy := job.DeepCopy()
|
||||
queue.Items = append(queue.Items, *copy)
|
||||
}
|
||||
|
||||
return queue, nil
|
||||
}
|
||||
|
||||
func (s *jobStore) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
|
||||
s.mutex.RLock()
|
||||
defer s.mutex.RUnlock()
|
||||
|
||||
ns, ok := request.NamespaceFrom(ctx)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("missing namespace")
|
||||
}
|
||||
|
||||
for _, job := range s.jobs {
|
||||
if job.Name == name && job.Namespace == ns {
|
||||
return job.DeepCopy(), nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, apierrors.NewNotFound(provisioning.JobResourceInfo.GroupResource(), name)
|
||||
}
|
||||
|
||||
func (s *jobStore) Watch(ctx context.Context, opts *internalversion.ListOptions) (watch.Interface, error) {
|
||||
ns, ok := request.NamespaceFrom(ctx)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("missing namespace")
|
||||
}
|
||||
|
||||
p := storage.SelectionPredicate{
|
||||
Label: labels.Everything(), // TODO... limit
|
||||
Field: fields.Everything(),
|
||||
}
|
||||
|
||||
// Can watch by label selection
|
||||
jw := s.watchSet.newWatch(ctx, 0, p, s.versioner, &ns)
|
||||
jw.Start()
|
||||
return jw, nil
|
||||
}
|
||||
|
||||
// Implementing JobQueue
|
||||
|
||||
// Register a worker (inline for now)
|
||||
func (s *jobStore) Register(worker Worker) {
|
||||
s.workers = append(s.workers, worker)
|
||||
}
|
||||
|
||||
func (s *jobStore) Add(ctx context.Context, job *provisioning.Job) (*provisioning.Job, error) {
|
||||
if job.Namespace == "" {
|
||||
return nil, apierrors.NewBadRequest("missing metadata.namespace")
|
||||
}
|
||||
if job.Name != "" {
|
||||
return nil, apierrors.NewBadRequest("name will always be generated")
|
||||
}
|
||||
if job.Spec.Repository == "" {
|
||||
return nil, apierrors.NewBadRequest("missing spec.repository")
|
||||
}
|
||||
if job.Spec.Action == "" {
|
||||
return nil, apierrors.NewBadRequest("missing spec.action")
|
||||
}
|
||||
if job.Spec.Action == provisioning.JobActionExport && job.Spec.Export == nil {
|
||||
return nil, apierrors.NewBadRequest("missing spec.export")
|
||||
}
|
||||
|
||||
if job.Spec.Action == provisioning.JobActionSync && job.Spec.Sync == nil {
|
||||
return nil, apierrors.NewBadRequest("missing spec.sync")
|
||||
}
|
||||
|
||||
// Only for add
|
||||
if job.Status.State != "" {
|
||||
return nil, apierrors.NewBadRequest("must add jobs with empty status")
|
||||
}
|
||||
|
||||
if job.Labels == nil {
|
||||
job.Labels = make(map[string]string)
|
||||
}
|
||||
job.Labels["repository"] = job.Spec.Repository // for now, make sure we can search Multi-tenant
|
||||
job.Name = fmt.Sprintf("%s:%s:%s", job.Spec.Repository, job.Spec.Action, util.GenerateShortUID())
|
||||
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
|
||||
s.rv++
|
||||
job.ResourceVersion = strconv.FormatInt(s.rv, 10)
|
||||
job.Status.State = provisioning.JobStatePending
|
||||
job.CreationTimestamp = metav1.NewTime(time.Now())
|
||||
|
||||
jobs := make([]provisioning.Job, 0, len(s.jobs)+2)
|
||||
jobs = append(jobs, *job)
|
||||
for i, j := range s.jobs {
|
||||
if i >= s.capacity {
|
||||
// Remove the old jobs
|
||||
s.watchSet.notifyWatchers(watch.Event{
|
||||
Object: j.DeepCopyObject(),
|
||||
Type: watch.Deleted,
|
||||
}, nil)
|
||||
continue
|
||||
}
|
||||
jobs = append(jobs, j)
|
||||
}
|
||||
|
||||
// Send add event
|
||||
s.watchSet.notifyWatchers(watch.Event{
|
||||
Object: job.DeepCopyObject(),
|
||||
Type: watch.Added,
|
||||
}, nil)
|
||||
|
||||
// For now, start a thread processing each job
|
||||
go s.drainPending()
|
||||
|
||||
s.jobs = jobs // replace existing list
|
||||
return job, nil
|
||||
}
|
||||
|
||||
// Reads the queue until no jobs remain
|
||||
func (s *jobStore) drainPending() {
|
||||
logger := logging.DefaultLogger.With("logger", "job-store")
|
||||
ctx := logging.Context(context.Background(), logger)
|
||||
|
||||
var err error
|
||||
for {
|
||||
time.Sleep(time.Microsecond * 200)
|
||||
|
||||
job := s.Next(ctx)
|
||||
if job == nil {
|
||||
return // done
|
||||
}
|
||||
logger := logger.With("job", job.GetName(), "namespace", job.GetNamespace())
|
||||
ctx := logging.Context(ctx, logger)
|
||||
|
||||
var foundWorker bool
|
||||
recorder := newJobProgressRecorder(func(ctx context.Context, j provisioning.JobStatus) error {
|
||||
return s.Update(ctx, job.Namespace, job.Name, j)
|
||||
})
|
||||
|
||||
for _, worker := range s.workers {
|
||||
if !worker.IsSupported(ctx, *job) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Already found a worker, no need to continue
|
||||
foundWorker = true
|
||||
err = s.processByWorker(ctx, worker, *job, recorder)
|
||||
break
|
||||
}
|
||||
|
||||
if !foundWorker {
|
||||
err = errors.New("no registered worker supports this job")
|
||||
}
|
||||
|
||||
status := recorder.Complete(ctx, err)
|
||||
err = s.Update(ctx, job.Namespace, job.Name, status)
|
||||
if err != nil {
|
||||
logger.Error("error running job", "error", err)
|
||||
}
|
||||
logger.Debug("job has been fully completed")
|
||||
}
|
||||
}
|
||||
|
||||
func (s *jobStore) processByWorker(ctx context.Context, worker Worker, job provisioning.Job, recorder JobProgressRecorder) error {
|
||||
ctx = request.WithNamespace(ctx, job.Namespace)
|
||||
ctx, _, err := identity.WithProvisioningIdentitiy(ctx, job.Namespace)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get worker identity: %w", err)
|
||||
}
|
||||
repoName := job.Spec.Repository
|
||||
|
||||
logger := logging.FromContext(ctx)
|
||||
logger = logger.With("repository", repoName)
|
||||
ctx = logging.Context(ctx, logger)
|
||||
|
||||
repo, err := s.getter.GetRepository(ctx, repoName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get repository: %w", err)
|
||||
}
|
||||
|
||||
// TODO: does this really happen?
|
||||
if repo == nil {
|
||||
return errors.New("unknown repository")
|
||||
}
|
||||
|
||||
return worker.Process(ctx, repo, job, recorder)
|
||||
}
|
||||
|
||||
// Checkout the next "pending" job
|
||||
func (s *jobStore) Next(ctx context.Context) *provisioning.Job {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
|
||||
// The oldest jobs should be checked out first
|
||||
for i := len(s.jobs) - 1; i >= 0; i-- {
|
||||
if s.jobs[i].Status.State == provisioning.JobStatePending {
|
||||
oldObj := s.jobs[i].DeepCopyObject()
|
||||
|
||||
s.rv++
|
||||
s.jobs[i].ResourceVersion = strconv.FormatInt(s.rv, 10)
|
||||
s.jobs[i].Status.State = provisioning.JobStateWorking
|
||||
s.jobs[i].Status.Started = time.Now().UnixMilli()
|
||||
job := s.jobs[i]
|
||||
|
||||
s.watchSet.notifyWatchers(watch.Event{
|
||||
Object: job.DeepCopyObject(),
|
||||
Type: watch.Modified,
|
||||
}, oldObj)
|
||||
return &job
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *jobStore) Update(ctx context.Context, namespace string, name string, status provisioning.JobStatus) error {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
|
||||
s.rv++
|
||||
|
||||
if status.State == "" {
|
||||
return apierrors.NewBadRequest("The state must be set")
|
||||
}
|
||||
if status.Progress > 100 || status.Progress < 0 {
|
||||
return apierrors.NewBadRequest("progress must be between 0 and 100")
|
||||
}
|
||||
|
||||
for idx, job := range s.jobs {
|
||||
if job.Name == name && job.Namespace == namespace {
|
||||
if job.Status.State.Finished() {
|
||||
return &apierrors.StatusError{ErrStatus: metav1.Status{
|
||||
Code: http.StatusPreconditionFailed,
|
||||
Message: "The job is already finished and can not be updated",
|
||||
}}
|
||||
}
|
||||
if status.State.Finished() {
|
||||
status.Finished = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
oldObj := job.DeepCopyObject()
|
||||
job.ResourceVersion = strconv.FormatInt(s.rv, 10)
|
||||
job.Status = status
|
||||
s.jobs[idx] = job
|
||||
|
||||
s.watchSet.notifyWatchers(watch.Event{
|
||||
Object: job.DeepCopyObject(),
|
||||
Type: watch.Modified,
|
||||
}, oldObj)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return apierrors.NewNotFound(provisioning.JobResourceInfo.GroupResource(), name)
|
||||
}
|
||||
@@ -0,0 +1,379 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Provenance-includes-location: https://github.com/tilt-dev/tilt-apiserver/blob/main/pkg/storage/filepath/watchset.go
|
||||
// Provenance-includes-license: Apache-2.0
|
||||
// Provenance-includes-copyright: The Kubernetes Authors.
|
||||
|
||||
// See also
|
||||
// https://github.com/grafana/grafana/blob/v11.1.9/pkg/apiserver/storage/file/watchset.go
|
||||
|
||||
package jobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/apiserver/pkg/storage"
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
UpdateChannelSize = 25
|
||||
InitialWatchNodesSize = 20
|
||||
InitialBufferedEventsSize = 25
|
||||
)
|
||||
|
||||
type eventWrapper struct {
|
||||
ev watch.Event
|
||||
// optional: oldObject is only set for modifications for determining their type as necessary (when using predicate filtering)
|
||||
oldObject runtime.Object
|
||||
}
|
||||
|
||||
type watchNode struct {
|
||||
ctx context.Context
|
||||
s *WatchSet
|
||||
id uint64
|
||||
updateCh chan eventWrapper
|
||||
outCh chan watch.Event
|
||||
requestedRV uint64
|
||||
// the watch may or may not be namespaced for a namespaced resource. This is always nil for cluster-scoped kinds
|
||||
watchNamespace *string
|
||||
predicate storage.SelectionPredicate
|
||||
versioner storage.Versioner
|
||||
}
|
||||
|
||||
// Keeps track of which watches need to be notified
|
||||
type WatchSet struct {
|
||||
mu sync.RWMutex
|
||||
// mu protects both nodes and counter
|
||||
nodes map[uint64]*watchNode
|
||||
counter atomic.Uint64
|
||||
buffered []eventWrapper
|
||||
bufferedMutex sync.RWMutex
|
||||
}
|
||||
|
||||
func NewWatchSet() *WatchSet {
|
||||
return &WatchSet{
|
||||
buffered: make([]eventWrapper, 0, InitialBufferedEventsSize),
|
||||
nodes: make(map[uint64]*watchNode, InitialWatchNodesSize),
|
||||
}
|
||||
}
|
||||
|
||||
// Creates a new watch with a unique id, but
|
||||
// does not start sending events to it until start() is called.
|
||||
func (s *WatchSet) newWatch(ctx context.Context, requestedRV uint64, p storage.SelectionPredicate, versioner storage.Versioner, namespace *string) *watchNode {
|
||||
s.counter.Add(1)
|
||||
|
||||
node := &watchNode{
|
||||
ctx: ctx,
|
||||
requestedRV: requestedRV,
|
||||
id: s.counter.Load(),
|
||||
s: s,
|
||||
// updateCh size needs to be > 1 to allow slower clients to not block passing new events
|
||||
updateCh: make(chan eventWrapper, UpdateChannelSize),
|
||||
// outCh size needs to be > 1 for single process use-cases such as tests where watch and event seeding from CUD
|
||||
// events is happening on the same thread
|
||||
outCh: make(chan watch.Event, UpdateChannelSize),
|
||||
predicate: p,
|
||||
watchNamespace: namespace,
|
||||
versioner: versioner,
|
||||
}
|
||||
|
||||
return node
|
||||
}
|
||||
|
||||
func (s *WatchSet) CleanupWatchers() {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
for _, w := range s.nodes {
|
||||
w.stop()
|
||||
}
|
||||
}
|
||||
|
||||
// oldObject is only passed in the event of a modification
|
||||
// in case a predicate filtered watch is impacted as a result of modification
|
||||
// NOTE: this function gives one the misperception that a newly added node will never
|
||||
// get a double event, one from buffered and one from the update channel
|
||||
// That perception is not true. Even though this function maintains the lock throughout the function body
|
||||
// it is not true of the Start function. So basically, the Start function running after this function
|
||||
// fully stands the chance of another future notifyWatchers double sending it the event through the two means mentioned
|
||||
func (s *WatchSet) notifyWatchers(ev watch.Event, oldObject runtime.Object) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
updateEv := eventWrapper{
|
||||
ev: ev,
|
||||
}
|
||||
if oldObject != nil {
|
||||
updateEv.oldObject = oldObject
|
||||
}
|
||||
|
||||
// Events are always buffered.
|
||||
// this is because of an inadvertent delay which is built into the watch process
|
||||
// Watch() from storage returns Watch.Interface with a async start func.
|
||||
// The only way to guarantee that we can interpret the passed RV correctly is to play it against missed events
|
||||
// (notice the loop below over s.nodes isn't exactly going to work on a new node
|
||||
// unless start is called on it)
|
||||
s.bufferedMutex.Lock()
|
||||
s.buffered = append(s.buffered, updateEv)
|
||||
s.bufferedMutex.Unlock()
|
||||
|
||||
for _, w := range s.nodes {
|
||||
w.updateCh <- updateEv
|
||||
}
|
||||
}
|
||||
|
||||
// isValid is not necessary to be called on oldObject in UpdateEvents - assuming the Watch pushes correctly setup eventWrapper our way
|
||||
// first bool is whether the event is valid for current watcher
|
||||
// second bool is whether checking the old value against the predicate may be valuable to the caller
|
||||
// second bool may be a helpful aid to establish context around MODIFIED events
|
||||
// (note that this second bool is only marked true if we pass other checks first, namely RV and namespace)
|
||||
func (w *watchNode) isValid(e eventWrapper) (bool, bool, error) {
|
||||
obj, err := meta.Accessor(e.ev.Object)
|
||||
if err != nil {
|
||||
klog.Error("Could not get accessor to object in event")
|
||||
return false, false, nil
|
||||
}
|
||||
|
||||
eventRV, err := w.getResourceVersionAsInt(e.ev.Object)
|
||||
if err != nil {
|
||||
return false, false, err
|
||||
}
|
||||
|
||||
if eventRV < w.requestedRV {
|
||||
return false, false, nil
|
||||
}
|
||||
|
||||
if w.watchNamespace != nil && *w.watchNamespace != obj.GetNamespace() {
|
||||
return false, false, err
|
||||
}
|
||||
|
||||
valid, err := w.predicate.Matches(e.ev.Object)
|
||||
if err != nil {
|
||||
return false, false, err
|
||||
}
|
||||
|
||||
return valid, e.ev.Type == watch.Modified, nil
|
||||
}
|
||||
|
||||
// Only call this method if current object matches the predicate
|
||||
func (w *watchNode) handleAddedForFilteredList(e eventWrapper) (*watch.Event, error) {
|
||||
if e.oldObject == nil {
|
||||
return nil, fmt.Errorf("oldObject should be set for modified events")
|
||||
}
|
||||
|
||||
ok, err := w.predicate.Matches(e.oldObject)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !ok {
|
||||
e.ev.Type = watch.Added
|
||||
return &e.ev, nil
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (w *watchNode) handleDeletedForFilteredList(e eventWrapper) (*watch.Event, error) {
|
||||
if e.oldObject == nil {
|
||||
return nil, fmt.Errorf("oldObject should be set for modified events")
|
||||
}
|
||||
|
||||
ok, err := w.predicate.Matches(e.oldObject)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// isn't a match but used to be
|
||||
e.ev.Type = watch.Deleted
|
||||
|
||||
oldObjectAccessor, err := meta.Accessor(e.oldObject)
|
||||
if err != nil {
|
||||
klog.Errorf("Could not get accessor to correct the old RV of filtered out object")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
currentRV, err := getResourceVersion(e.ev.Object)
|
||||
if err != nil {
|
||||
klog.Errorf("Could not get accessor to object in event")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
oldObjectAccessor.SetResourceVersion(currentRV)
|
||||
e.ev.Object = e.oldObject
|
||||
|
||||
return &e.ev, nil
|
||||
}
|
||||
|
||||
func (w *watchNode) processEvent(e eventWrapper, isInitEvent bool) error {
|
||||
if isInitEvent {
|
||||
// Init events have already been vetted against the predicate and other RV behavior
|
||||
// Let them pass through
|
||||
w.outCh <- e.ev
|
||||
return nil
|
||||
}
|
||||
|
||||
valid, runDeleteFromFilteredListHandler, err := w.isValid(e)
|
||||
if err != nil {
|
||||
klog.Errorf("Could not determine validity of the event: %v", err)
|
||||
return err
|
||||
}
|
||||
if valid {
|
||||
if e.ev.Type == watch.Modified {
|
||||
ev, err := w.handleAddedForFilteredList(e)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ev != nil {
|
||||
w.outCh <- *ev
|
||||
} else {
|
||||
// forward the original event if add handling didn't signal any impact
|
||||
w.outCh <- e.ev
|
||||
}
|
||||
} else {
|
||||
w.outCh <- e.ev
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if runDeleteFromFilteredListHandler {
|
||||
if e.ev.Type == watch.Modified {
|
||||
ev, err := w.handleDeletedForFilteredList(e)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ev != nil {
|
||||
w.outCh <- *ev
|
||||
}
|
||||
} // explicitly doesn't have an event forward for the else case here
|
||||
return nil
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Start sending events to this watch.
|
||||
func (w *watchNode) Start(initEvents ...watch.Event) {
|
||||
w.s.mu.Lock()
|
||||
w.s.nodes[w.id] = w
|
||||
w.s.mu.Unlock()
|
||||
|
||||
go func() {
|
||||
maxRV := uint64(0)
|
||||
for _, ev := range initEvents {
|
||||
currentRV, err := w.getResourceVersionAsInt(ev.Object)
|
||||
if err != nil {
|
||||
klog.Errorf("Could not determine init event RV for deduplication of buffered events: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if maxRV < currentRV {
|
||||
maxRV = currentRV
|
||||
}
|
||||
|
||||
if err := w.processEvent(eventWrapper{ev: ev}, true); err != nil {
|
||||
klog.Errorf("Could not process event: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// If we had no init events, simply rely on the passed RV
|
||||
if maxRV == 0 {
|
||||
maxRV = w.requestedRV
|
||||
}
|
||||
|
||||
w.s.bufferedMutex.RLock()
|
||||
for _, e := range w.s.buffered {
|
||||
eventRV, err := w.getResourceVersionAsInt(e.ev.Object)
|
||||
if err != nil {
|
||||
klog.Errorf("Could not determine RV for deduplication of buffered events: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if maxRV >= eventRV {
|
||||
continue
|
||||
} else {
|
||||
maxRV = eventRV
|
||||
}
|
||||
|
||||
if err := w.processEvent(e, false); err != nil {
|
||||
klog.Errorf("Could not process event: %v", err)
|
||||
}
|
||||
}
|
||||
w.s.bufferedMutex.RUnlock()
|
||||
|
||||
for {
|
||||
select {
|
||||
case e, ok := <-w.updateCh:
|
||||
if !ok {
|
||||
close(w.outCh)
|
||||
return
|
||||
}
|
||||
|
||||
eventRV, err := w.getResourceVersionAsInt(e.ev.Object)
|
||||
if err != nil {
|
||||
klog.Errorf("Could not determine RV for deduplication of channel events: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if maxRV >= eventRV {
|
||||
continue
|
||||
} else {
|
||||
maxRV = eventRV
|
||||
}
|
||||
|
||||
if err := w.processEvent(e, false); err != nil {
|
||||
klog.Errorf("Could not process event: %v", err)
|
||||
}
|
||||
case <-w.ctx.Done():
|
||||
close(w.outCh)
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (w *watchNode) Stop() {
|
||||
w.s.mu.Lock()
|
||||
defer w.s.mu.Unlock()
|
||||
w.stop()
|
||||
}
|
||||
|
||||
// Unprotected func: ensure mutex on the parent watch set is locked before calling
|
||||
func (w *watchNode) stop() {
|
||||
if _, ok := w.s.nodes[w.id]; ok {
|
||||
delete(w.s.nodes, w.id)
|
||||
close(w.updateCh)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *watchNode) ResultChan() <-chan watch.Event {
|
||||
return w.outCh
|
||||
}
|
||||
|
||||
func getResourceVersion(obj runtime.Object) (string, error) {
|
||||
accessor, err := meta.Accessor(obj)
|
||||
if err != nil {
|
||||
klog.Error("Could not get accessor to object in event")
|
||||
return "", err
|
||||
}
|
||||
return accessor.GetResourceVersion(), nil
|
||||
}
|
||||
|
||||
func (w *watchNode) getResourceVersionAsInt(obj runtime.Object) (uint64, error) {
|
||||
accessor, err := meta.Accessor(obj)
|
||||
if err != nil {
|
||||
klog.Error("Could not get accessor to object in event")
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return w.versioner.ParseResourceVersion(accessor.GetResourceVersion())
|
||||
}
|
||||
@@ -6,8 +6,12 @@ import (
|
||||
|
||||
provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1"
|
||||
grafanaregistry "github.com/grafana/grafana/pkg/apiserver/registry/generic"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/provisioning/jobs"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/provisioning/repository"
|
||||
"github.com/grafana/grafana/pkg/registry/apis/provisioning/secrets"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/builder"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
grafanasecrets "github.com/grafana/grafana/pkg/services/secrets"
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
@@ -22,16 +26,28 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
_ builder.APIGroupBuilder = (*APIBuilder)(nil)
|
||||
_ builder.APIGroupBuilder = (*APIBuilder)(nil)
|
||||
_ builder.APIGroupMutation = (*APIBuilder)(nil)
|
||||
_ builder.APIGroupValidation = (*APIBuilder)(nil)
|
||||
_ builder.APIGroupPostStartHookProvider = (*APIBuilder)(nil)
|
||||
_ builder.OpenAPIPostProcessor = (*APIBuilder)(nil)
|
||||
)
|
||||
|
||||
type APIBuilder struct{}
|
||||
type APIBuilder struct {
|
||||
secrets secrets.Service
|
||||
jobs jobs.JobQueue
|
||||
getter rest.Getter
|
||||
}
|
||||
|
||||
// NewAPIBuilder creates an API builder.
|
||||
// It avoids anything that is core to Grafana, such that it can be used in a multi-tenant service down the line.
|
||||
// This means there are no hidden dependencies, and no use of e.g. *settings.Cfg.
|
||||
func NewAPIBuilder() *APIBuilder {
|
||||
return &APIBuilder{}
|
||||
func NewAPIBuilder(
|
||||
secrets secrets.Service,
|
||||
) *APIBuilder {
|
||||
return &APIBuilder{
|
||||
secrets: secrets,
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterAPIService returns an API builder, from [NewAPIBuilder]. It is called by Wire.
|
||||
@@ -39,13 +55,14 @@ func NewAPIBuilder() *APIBuilder {
|
||||
func RegisterAPIService(
|
||||
features featuremgmt.FeatureToggles,
|
||||
apiregistration builder.APIRegistrar,
|
||||
secretsSvc grafanasecrets.Service,
|
||||
) (*APIBuilder, error) {
|
||||
if !features.IsEnabledGlobally(featuremgmt.FlagProvisioning) &&
|
||||
!features.IsEnabledGlobally(featuremgmt.FlagGrafanaAPIServerWithExperimentalAPIs) {
|
||||
return nil, nil // skip registration unless opting into experimental apis OR the feature specifically
|
||||
}
|
||||
|
||||
builder := NewAPIBuilder()
|
||||
builder := NewAPIBuilder(secrets.NewSingleTenant(secretsSvc))
|
||||
apiregistration.RegisterAPI(builder)
|
||||
return builder, nil
|
||||
}
|
||||
@@ -87,9 +104,14 @@ func (b *APIBuilder) UpdateAPIGroupInfo(apiGroupInfo *genericapiserver.APIGroupI
|
||||
return fmt.Errorf("failed to create repository storage: %w", err)
|
||||
}
|
||||
|
||||
// FIXME: Make job queue store the jobs somewhere persistent.
|
||||
jobStore := jobs.NewJobStore(50, b) // in memory, for now...
|
||||
b.jobs = jobStore
|
||||
|
||||
repositoryStatusStorage := grafanaregistry.NewRegistryStatusStore(opts.Scheme, repositoryStorage)
|
||||
|
||||
storage := map[string]rest.Storage{}
|
||||
storage[provisioning.JobResourceInfo.StoragePath()] = jobStore
|
||||
storage[provisioning.RepositoryResourceInfo.StoragePath()] = repositoryStorage
|
||||
storage[provisioning.RepositoryResourceInfo.StoragePath("status")] = repositoryStatusStorage
|
||||
apiGroupInfo.VersionedResourcesStorageMap[provisioning.VERSION] = storage
|
||||
@@ -158,3 +180,16 @@ func (b *APIBuilder) PostProcessOpenAPI(oas *spec3.OpenAPI) (*spec3.OpenAPI, err
|
||||
|
||||
return oas, nil
|
||||
}
|
||||
|
||||
// Helpers for fetching valid Repository objects
|
||||
|
||||
func (b *APIBuilder) GetRepository(ctx context.Context, name string) (repository.Repository, error) {
|
||||
obj, err := b.getter.Get(ctx, name, &metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_ = obj
|
||||
// FIXME: Return a valid Repository object with the correct underlying storage.
|
||||
panic("FIXME")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
|
||||
provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
// Config returns the saved Kubernetes object.
|
||||
Config() *provisioning.Repository
|
||||
|
||||
// Validate ensures the resource _looks_ correct.
|
||||
// It should be called before trying to upsert a resource into the Kubernetes API server.
|
||||
// This is not an indication that the connection information works, just that they are reasonably configured (see also Test).
|
||||
Validate() field.ErrorList
|
||||
|
||||
// Test checks if the connection information actually works.
|
||||
Test(ctx context.Context) (*provisioning.TestResults, error)
|
||||
}
|
||||
|
||||
// ErrFileNotFound indicates that a path could not be found in the repository.
|
||||
var ErrFileNotFound error = fs.ErrNotExist
|
||||
|
||||
type FileInfo struct {
|
||||
// Path to the file on disk.
|
||||
// No leading or trailing slashes will be contained within.
|
||||
// This uses '/' for separation. Use the 'path' package to interact with this.
|
||||
Path string
|
||||
// The raw bytes
|
||||
Data []byte
|
||||
// The git branch or reference commit
|
||||
Ref string
|
||||
// The git hash for a given file
|
||||
Hash string
|
||||
// When was the file changed (if known)
|
||||
Modified *metav1.Time
|
||||
}
|
||||
|
||||
// An entry in the file tree, as returned by 'ReadFileTree'. Like FileInfo, but contains less information.
|
||||
type FileTreeEntry struct {
|
||||
// The path to the file from the base path given (if any).
|
||||
// No leading or trailing slashes will be contained within.
|
||||
// This uses '/' for separation. Use the 'path' package to interact with this.
|
||||
Path string
|
||||
// The hash for the file. Lower-case hex.
|
||||
// Empty string if Blob is false.
|
||||
Hash string
|
||||
// The size of the file.
|
||||
// 0 if Blob is false.
|
||||
Size int64
|
||||
// Whether this entry is a blob or a subtree.
|
||||
Blob bool
|
||||
}
|
||||
|
||||
type Reader interface {
|
||||
// Read a file from the resource
|
||||
// This data will be parsed and validated before it is shown to end users
|
||||
Read(ctx context.Context, path, ref string) (*FileInfo, error)
|
||||
|
||||
// Read all file names from the tree.
|
||||
// This data will be parsed and validated before it is shown.
|
||||
//
|
||||
// TODO: Make some API contract that lets us ignore files that aren't relevant to us (e.g. CI/CD, CODEOWNERS, other configs or source code).
|
||||
// TODO: Test scale: do we want to stream entries instead somehow?
|
||||
ReadTree(ctx context.Context, ref string) ([]FileTreeEntry, error)
|
||||
}
|
||||
|
||||
type Writer interface {
|
||||
// Write a file to the repository.
|
||||
// The data has already been validated and is ready for save
|
||||
Create(ctx context.Context, path, ref string, data []byte, message string) error
|
||||
|
||||
// Update a file in the remote repository
|
||||
// The data has already been validated and is ready for save
|
||||
Update(ctx context.Context, path, ref string, data []byte, message string) error
|
||||
|
||||
// Write a file to the repository.
|
||||
// Functionally the same as Read then Create or Update, but more efficient depending on the backend
|
||||
Write(ctx context.Context, path, ref string, data []byte, message string) error
|
||||
|
||||
// Delete a file in the remote repository
|
||||
Delete(ctx context.Context, path, ref, message string) error
|
||||
}
|
||||
|
||||
// Hooks called after the repository has been created, updated or deleted
|
||||
type Hooks interface {
|
||||
// For repositories that support webhooks
|
||||
Webhook(ctx context.Context, req *http.Request) (*provisioning.WebhookResponse, error)
|
||||
OnCreate(ctx context.Context) (*provisioning.WebhookStatus, error)
|
||||
OnUpdate(ctx context.Context) (*provisioning.WebhookStatus, error)
|
||||
OnDelete(ctx context.Context) error
|
||||
}
|
||||
|
||||
type FileAction string
|
||||
|
||||
const (
|
||||
FileActionCreated FileAction = "created"
|
||||
FileActionUpdated FileAction = "updated"
|
||||
FileActionDeleted FileAction = "deleted"
|
||||
FileActionIgnored FileAction = "ignored"
|
||||
|
||||
// Renamed actions may be reconstructed as delete then create
|
||||
FileActionRenamed FileAction = "renamed"
|
||||
)
|
||||
|
||||
type VersionedFileChange struct {
|
||||
Action FileAction
|
||||
Path string
|
||||
|
||||
Ref string
|
||||
PreviousRef string // rename | update
|
||||
PreviousPath string // rename
|
||||
}
|
||||
|
||||
// Versioned is a repository that supports versioning.
|
||||
// This interface may be extended to make the the original Repository interface more agnostic to the underlying storage system.
|
||||
type Versioned interface {
|
||||
// History of changes for a path
|
||||
History(ctx context.Context, path, ref string) ([]provisioning.HistoryItem, error)
|
||||
LatestRef(ctx context.Context) (string, error)
|
||||
CompareFiles(ctx context.Context, base, ref string) ([]VersionedFileChange, error)
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package safepath
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
)
|
||||
|
||||
// ErrUnsafePathTraversal indicates that an input path had a path traversal which led to escaping the required prefix.
|
||||
// E.g. Join("/test", "..") would return this, because it doesn't stay within the '/test' directory.
|
||||
var ErrUnsafePathTraversal = apierrors.NewBadRequest("the input path had an unacceptable path traversal")
|
||||
|
||||
// Join joins any number of elements in a path under a common prefix path.
|
||||
// If the elems do path traversal, they are permitted to do so under their own directories.
|
||||
// The output result will _always_ have a prefix of the given prefix, and no path traversals in the output string.
|
||||
// The output result will not end with a trailing slash.
|
||||
// The output result will have a leading slash if one is given as a prefix.
|
||||
// If the prefix would ultimately be escaped, an error is returned.
|
||||
//
|
||||
// This function is safe for <https://securego.io/docs/rules/g304.html>.
|
||||
func Join(prefix string, elem ...string) (string, error) {
|
||||
// We clean early to make the HasPrefix check be sensible after path.Join does a Clean for us.
|
||||
prefix = replaceOSSeparators(path.Clean(prefix))
|
||||
if len(elem) == 0 {
|
||||
return prefix, nil
|
||||
}
|
||||
|
||||
for i, e := range elem {
|
||||
// We don't use Clean here because the output of path.Join will clean for us.
|
||||
elem[i] = replaceOSSeparators(e)
|
||||
}
|
||||
subPath := path.Join(elem...) // performs a Clean after joining
|
||||
completePath := path.Join(prefix, subPath)
|
||||
if !strings.HasPrefix(completePath, prefix) {
|
||||
return "", ErrUnsafePathTraversal
|
||||
}
|
||||
return completePath, nil
|
||||
}
|
||||
|
||||
// Performs a [path.Clean] on the path, as well as replacing its OS separators.
|
||||
// Note that this does no effort to ensure the paths are safe to use. It only cleans them.
|
||||
func Clean(p string) string {
|
||||
return path.Clean(replaceOSSeparators(p))
|
||||
}
|
||||
|
||||
// osSeparator is declared as a var here only to ensure we can change it in tests.
|
||||
var osSeparator = os.PathSeparator
|
||||
|
||||
// This replaces the OS separator with a slash.
|
||||
// All OSes we target (Linux, macOS, and Windows) support forward-slashes in path traversals, as such it's simpler to use the same character everywhere.
|
||||
// BSDs do as well (even though they're not a target as of writing).
|
||||
func replaceOSSeparators(p string) string {
|
||||
if osSeparator == '/' { // perf: nothing to do!
|
||||
return p
|
||||
}
|
||||
return strings.ReplaceAll(p, string(osSeparator), "/")
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package safepath
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPathJoin(t *testing.T) {
|
||||
orig := osSeparator
|
||||
osSeparator = '\\' // pretend we're on Windows
|
||||
defer func() { osSeparator = orig }()
|
||||
|
||||
testCases := []struct {
|
||||
Comment string
|
||||
In []string
|
||||
Out any // string or error
|
||||
}{
|
||||
{"Empty elements should not change input", []string{"/test/"}, "/test"},
|
||||
{"Empty elements without leading slash should not change input", []string{"test/"}, "test"},
|
||||
{"Single element should be added to path", []string{"/test/", "abc"}, "/test/abc"},
|
||||
{"Single element should be added to path with current dir prefix", []string{"./test/", "abc"}, "test/abc"},
|
||||
{"Single element with leading slash should be added to path", []string{"/test/", "/abc"}, "/test/abc"},
|
||||
{"Many elements are all appended to path", []string{"/test/", "a", "b", "c"}, "/test/a/b/c"},
|
||||
{"Path traversal within same directory should be expanded", []string{"/test/", "a", "..", "b", ".", "..", "c"}, "/test/c"},
|
||||
{"Path traversal escaping root dir prefix should return err", []string{"/test/", ".."}, ErrUnsafePathTraversal},
|
||||
{"Path traversal escaping no dir prefix should return err", []string{"test/", ".."}, ErrUnsafePathTraversal},
|
||||
{"Path traversal escaping current dir prefix should return err", []string{"./test/", ".."}, ErrUnsafePathTraversal},
|
||||
{"Complex path traversal escaping prefix should return err", []string{"/test/", "a/..///c/", "../../test/d/../a/../.."}, ErrUnsafePathTraversal},
|
||||
{"Complex path traversal remaining in prefix should be expanded", []string{"/test/", "a/..///c/", "../../test/d/"}, "/test/d"},
|
||||
{"Problematic code example from the g304 website", []string{"/safe/path", "../../private/path"}, ErrUnsafePathTraversal},
|
||||
{"Traversing beyond root should be expanded", []string{"/test/", "/../a"}, "/test/a"},
|
||||
{"OS separator should be replaced with a slash", []string{"/test\\test", "abc\\test"}, "/test/test/abc/test"},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
t.Run(tc.Comment, func(t *testing.T) {
|
||||
path, err := Join(tc.In[0], tc.In[1:]...)
|
||||
if ee, ok := tc.Out.(error); ok {
|
||||
assert.ErrorIs(t, err, ee, "expected unsuccessful outcome")
|
||||
assert.Empty(t, path, "expected empty string when unsuccessful")
|
||||
} else if str, ok := tc.Out.(string); ok {
|
||||
assert.NoError(t, err, "expected successful outcome")
|
||||
assert.Equal(t, str, path)
|
||||
} else {
|
||||
panic("expected out was neither string nor error")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPathClean(t *testing.T) {
|
||||
orig := osSeparator
|
||||
osSeparator = '\\' // pretend we're on Windows
|
||||
defer func() { osSeparator = orig }()
|
||||
|
||||
testCases := []struct {
|
||||
Comment string
|
||||
In string
|
||||
Out string
|
||||
}{
|
||||
{"Simple path", "/test/", "/test"},
|
||||
{"Simple path with OS separators", "\\test\\here", "/test/here"},
|
||||
{"Simple path with mixed separators", "\\test/here", "/test/here"},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
t.Run(tc.Comment, func(t *testing.T) {
|
||||
assert.Equal(t, tc.Out, Clean(tc.In))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package safepath
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type WalkFunc = func(ctx context.Context, path string) error
|
||||
|
||||
// Walk walks the given folder path and calls the given function for each folder.
|
||||
func Walk(ctx context.Context, p string, fn WalkFunc) error {
|
||||
if p == "." || p == "/" {
|
||||
return nil
|
||||
}
|
||||
|
||||
var currentPath string
|
||||
for _, folder := range strings.Split(p, "/") {
|
||||
if folder == "" {
|
||||
// Trailing / leading slash?
|
||||
continue
|
||||
}
|
||||
|
||||
currentPath = path.Join(currentPath, folder)
|
||||
if err := fn(ctx, currentPath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package secrets
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/secrets"
|
||||
)
|
||||
|
||||
// A secrets encryption service. It only operates on values, no names or similar.
|
||||
// It is likely we will need to change this when the multi-tenant service comes around.
|
||||
//
|
||||
// FIXME: this is a temporary service/package until we can make use of
|
||||
// the new secrets service in app platform.
|
||||
type Service interface {
|
||||
Encrypt(ctx context.Context, data []byte) ([]byte, error)
|
||||
Decrypt(ctx context.Context, data []byte) ([]byte, error)
|
||||
}
|
||||
|
||||
var _ Service = (*singleTenant)(nil)
|
||||
|
||||
type singleTenant struct {
|
||||
inner secrets.Service
|
||||
}
|
||||
|
||||
func NewSingleTenant(svc secrets.Service) *singleTenant {
|
||||
return &singleTenant{svc}
|
||||
}
|
||||
|
||||
func (s *singleTenant) Encrypt(ctx context.Context, data []byte) ([]byte, error) {
|
||||
return s.inner.Encrypt(ctx, data, secrets.WithoutScope())
|
||||
}
|
||||
|
||||
func (s *singleTenant) Decrypt(ctx context.Context, data []byte) ([]byte, error) {
|
||||
return s.inner.Decrypt(ctx, data)
|
||||
}
|
||||
@@ -91,20 +91,22 @@ func (r *LokiHistorianStore) Get(ctx context.Context, query annotations.ItemQuer
|
||||
return make([]*annotations.ItemDTO, 0), nil
|
||||
}
|
||||
|
||||
rule := &ngmodels.AlertRule{}
|
||||
if query.AlertID != 0 {
|
||||
var err error
|
||||
rule, err = r.ruleStore.GetRuleByID(ctx, ngmodels.GetAlertRuleByIDQuery{OrgID: query.OrgID, ID: query.AlertID})
|
||||
var ruleUID string
|
||||
if query.AlertUID != "" {
|
||||
ruleUID = query.AlertUID
|
||||
} else if query.AlertID != 0 {
|
||||
rule, err := r.ruleStore.GetRuleByID(ctx, ngmodels.GetAlertRuleByIDQuery{OrgID: query.OrgID, ID: query.AlertID})
|
||||
if err != nil {
|
||||
if errors.Is(err, ngmodels.ErrAlertRuleNotFound) {
|
||||
return make([]*annotations.ItemDTO, 0), ErrLokiStoreNotFound.Errorf("rule with ID %d does not exist", query.AlertID)
|
||||
}
|
||||
return make([]*annotations.ItemDTO, 0), ErrLokiStoreInternal.Errorf("failed to query rule: %w", err)
|
||||
}
|
||||
ruleUID = rule.UID
|
||||
}
|
||||
|
||||
// No folders in the filter because it filter by Dashboard UID, and the request is already authorized.
|
||||
logQL, err := historian.BuildLogQuery(buildHistoryQuery(&query, accessResources.Dashboards, rule.UID), nil, r.client.MaxQuerySize())
|
||||
logQL, err := historian.BuildLogQuery(buildHistoryQuery(&query, accessResources.Dashboards, ruleUID), nil, r.client.MaxQuerySize())
|
||||
if err != nil {
|
||||
grafanaErr := errutil.Error{}
|
||||
if errors.As(err, &grafanaErr) {
|
||||
|
||||
@@ -108,7 +108,34 @@ func TestIntegrationAlertStateHistoryStore(t *testing.T) {
|
||||
require.Len(t, res, numTransitions)
|
||||
})
|
||||
|
||||
t.Run("should return ErrLokiStoreNotFound if rule is not found", func(t *testing.T) {
|
||||
t.Run("can query history by alert uid", func(t *testing.T) {
|
||||
rule := dashboardRules[dashboard1.UID][0]
|
||||
|
||||
fakeLokiClient.rangeQueryRes = []historian.Stream{
|
||||
historian.StatesToStream(ruleMetaFromRule(t, rule), transitions, map[string]string{}, log.NewNopLogger()),
|
||||
}
|
||||
|
||||
query := annotations.ItemQuery{
|
||||
OrgID: 1,
|
||||
AlertUID: rule.UID,
|
||||
From: start.UnixMilli(),
|
||||
To: start.Add(time.Second * time.Duration(numTransitions+1)).UnixMilli(),
|
||||
}
|
||||
res, err := store.Get(
|
||||
context.Background(),
|
||||
query,
|
||||
&annotation_ac.AccessResources{
|
||||
Dashboards: map[string]int64{
|
||||
dashboard1.UID: dashboard1.ID,
|
||||
},
|
||||
CanAccessDashAnnotations: true,
|
||||
},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, res, numTransitions)
|
||||
})
|
||||
|
||||
t.Run("should return ErrLokiStoreNotFound if rule is not found by ID", func(t *testing.T) {
|
||||
var rules = slices.Concat(maps.Values(dashboardRules)...)
|
||||
id := rand.Int63n(1000) // in Postgres ID is integer, so limit range
|
||||
// make sure id is not known
|
||||
@@ -137,6 +164,27 @@ func TestIntegrationAlertStateHistoryStore(t *testing.T) {
|
||||
require.ErrorIs(t, err, ErrLokiStoreNotFound)
|
||||
})
|
||||
|
||||
t.Run("should return empty response if rule is not found by UID", func(t *testing.T) {
|
||||
query := annotations.ItemQuery{
|
||||
OrgID: 1,
|
||||
AlertUID: "not-found-uid",
|
||||
From: start.UnixMilli(),
|
||||
To: start.Add(time.Second * time.Duration(numTransitions+1)).UnixMilli(),
|
||||
}
|
||||
res, err := store.Get(
|
||||
context.Background(),
|
||||
query,
|
||||
&annotation_ac.AccessResources{
|
||||
Dashboards: map[string]int64{
|
||||
dashboard1.UID: dashboard1.ID,
|
||||
},
|
||||
CanAccessDashAnnotations: true,
|
||||
},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, res)
|
||||
})
|
||||
|
||||
t.Run("can query history by dashboard id", func(t *testing.T) {
|
||||
fakeLokiClient.rangeQueryRes = []historian.Stream{
|
||||
historian.StatesToStream(ruleMetaFromRule(t, dashboardRules[dashboard1.UID][0]), transitions, map[string]string{}, log.NewNopLogger()),
|
||||
|
||||
@@ -267,10 +267,10 @@ func (r *xormRepositoryImpl) Get(ctx context.Context, query annotations.ItemQuer
|
||||
annotation.updated,
|
||||
usr.email,
|
||||
usr.login,
|
||||
alert.name as alert_name
|
||||
r.title as alert_name
|
||||
FROM annotation
|
||||
LEFT OUTER JOIN ` + r.db.GetDialect().Quote("user") + ` as usr on usr.id = annotation.user_id
|
||||
LEFT OUTER JOIN alert on alert.id = annotation.alert_id
|
||||
LEFT OUTER JOIN alert_rule as r on r.id = annotation.alert_id
|
||||
INNER JOIN (
|
||||
SELECT a.id from annotation a
|
||||
`)
|
||||
@@ -287,6 +287,9 @@ func (r *xormRepositoryImpl) Get(ctx context.Context, query annotations.ItemQuer
|
||||
if query.AlertID != 0 {
|
||||
sql.WriteString(` AND a.alert_id = ?`)
|
||||
params = append(params, query.AlertID)
|
||||
} else if query.AlertUID != "" {
|
||||
sql.WriteString(` AND a.alert_id = (SELECT id FROM alert_rule WHERE uid = ? and org_id = ?)`)
|
||||
params = append(params, query.AlertUID, query.OrgID)
|
||||
}
|
||||
|
||||
if query.DashboardID != 0 {
|
||||
|
||||
@@ -11,6 +11,7 @@ type ItemQuery struct {
|
||||
To int64 `json:"to"`
|
||||
UserID int64 `json:"userId"`
|
||||
AlertID int64 `json:"alertId"`
|
||||
AlertUID string `json:"alertUID"`
|
||||
DashboardID int64 `json:"dashboardId"`
|
||||
DashboardUID string `json:"dashboardUID"`
|
||||
PanelID int64 `json:"panelId"`
|
||||
|
||||
@@ -151,20 +151,24 @@ func RegisterRBACAuthZService(
|
||||
reg prometheus.Registerer,
|
||||
cache cache.Cache,
|
||||
exchangeClient authnlib.TokenExchanger,
|
||||
folderAPIURL string,
|
||||
cfg RBACServerSettings,
|
||||
) {
|
||||
var folderStore store.FolderStore
|
||||
// FIXME: for now we default to using database read proxy for folders if the api url is not configured.
|
||||
// we should remove this and the sql implementation once we have verified that is works correctly
|
||||
if folderAPIURL == "" {
|
||||
if cfg.Folder.Host == "" {
|
||||
folderStore = store.NewSQLFolderStore(db, tracer)
|
||||
} else {
|
||||
folderStore = store.NewAPIFolderStore(tracer, func(ctx context.Context) (*rest.Config, error) {
|
||||
return &rest.Config{
|
||||
Host: folderAPIURL,
|
||||
Host: cfg.Folder.Host,
|
||||
WrapTransport: func(rt http.RoundTripper) http.RoundTripper {
|
||||
return &tokenExhangeRoundTripper{te: exchangeClient, rt: rt}
|
||||
},
|
||||
TLSClientConfig: rest.TLSClientConfig{
|
||||
Insecure: cfg.Folder.Insecure,
|
||||
CAFile: cfg.Folder.CAFile,
|
||||
},
|
||||
QPS: 50,
|
||||
Burst: 100,
|
||||
}, nil
|
||||
|
||||
@@ -57,3 +57,16 @@ func readAuthzClientSettings(cfg *setting.Cfg) (*authzClientSettings, error) {
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
type RBACServerSettings struct {
|
||||
Folder FolderAPISettings
|
||||
}
|
||||
|
||||
type FolderAPISettings struct {
|
||||
// Host is hostname for folder api
|
||||
Host string
|
||||
// Insecure will skip verification of ceritificates. Should only be used for testing
|
||||
Insecure bool
|
||||
// CAFile is a filepath to trusted root certificates for server
|
||||
CAFile string
|
||||
}
|
||||
|
||||
@@ -1789,6 +1789,15 @@ var (
|
||||
HideFromAdminPage: true,
|
||||
HideFromDocs: true,
|
||||
},
|
||||
{
|
||||
Name: "rendererDisableAppPluginsPreload",
|
||||
Description: "Disable pre-loading app plugins when the request is coming from the renderer",
|
||||
Stage: FeatureStageExperimental,
|
||||
Owner: grafanaSharingSquad,
|
||||
HideFromAdminPage: true,
|
||||
HideFromDocs: true,
|
||||
FrontendOnly: true,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -237,3 +237,4 @@ pluginsCDNSyncLoader,experimental,@grafana/plugins-platform-backend,false,false,
|
||||
alertingJiraIntegration,experimental,@grafana/alerting-squad,false,false,true
|
||||
alertingRuleVersionHistoryRestore,experimental,@grafana/alerting-squad,false,false,true
|
||||
newShareReportDrawer,experimental,@grafana/sharing-squad,false,false,false
|
||||
rendererDisableAppPluginsPreload,experimental,@grafana/sharing-squad,false,false,true
|
||||
|
||||
|
@@ -958,4 +958,8 @@ const (
|
||||
// FlagNewShareReportDrawer
|
||||
// Enables the report creation drawer in a dashboard
|
||||
FlagNewShareReportDrawer = "newShareReportDrawer"
|
||||
|
||||
// FlagRendererDisableAppPluginsPreload
|
||||
// Disable pre-loading app plugins when the request is coming from the renderer
|
||||
FlagRendererDisableAppPluginsPreload = "rendererDisableAppPluginsPreload"
|
||||
)
|
||||
|
||||
@@ -3593,6 +3593,21 @@
|
||||
"hideFromAdminPage": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"metadata": {
|
||||
"name": "rendererDisableAppPluginsPreload",
|
||||
"resourceVersion": "1740386710764",
|
||||
"creationTimestamp": "2025-02-24T08:45:10Z"
|
||||
},
|
||||
"spec": {
|
||||
"description": "Disable pre-loading app plugins when the request is coming from the renderer",
|
||||
"stage": "experimental",
|
||||
"codeowner": "@grafana/sharing-squad",
|
||||
"frontend": true,
|
||||
"hideFromAdminPage": true,
|
||||
"hideFromDocs": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"metadata": {
|
||||
"name": "reportingRetries",
|
||||
|
||||
@@ -91,6 +91,13 @@ func ContactPointToContactPointExport(cp definitions.ContactPoint) (notify.APIRe
|
||||
}
|
||||
integration = append(integration, el)
|
||||
}
|
||||
for _, i := range cp.Jira {
|
||||
el, err := marshallIntegration(j, "jira", i, i.DisableResolveMessage)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
integration = append(integration, el)
|
||||
}
|
||||
for _, i := range cp.Kafka {
|
||||
el, err := marshallIntegration(j, "kafka", i, i.DisableResolveMessage)
|
||||
if err != nil {
|
||||
@@ -271,6 +278,11 @@ func parseIntegration(json jsoniter.API, result *definitions.ContactPoint, recei
|
||||
if err = json.Unmarshal(data, &integration); err == nil {
|
||||
result.Googlechat = append(result.Googlechat, integration)
|
||||
}
|
||||
case "jira":
|
||||
integration := definitions.JiraIntegration{DisableResolveMessage: disable}
|
||||
if err = json.Unmarshal(data, &integration); err == nil {
|
||||
result.Jira = append(result.Jira, integration)
|
||||
}
|
||||
case "kafka":
|
||||
integration := definitions.KafkaIntegration{DisableResolveMessage: disable}
|
||||
if err = json.Unmarshal(data, &integration); err == nil {
|
||||
|
||||
@@ -62,6 +62,29 @@ type GooglechatIntegration struct {
|
||||
Message *string `json:"message,omitempty" yaml:"message,omitempty" hcl:"message"`
|
||||
}
|
||||
|
||||
type JiraIntegration struct {
|
||||
DisableResolveMessage *bool `json:"-" yaml:"-" hcl:"disable_resolve_message"`
|
||||
|
||||
URL string `yaml:"api_url,omitempty" json:"api_url,omitempty" hcl:"api_url"`
|
||||
Project string `yaml:"project,omitempty" json:"project,omitempty" hcl:"project"`
|
||||
IssueType string `yaml:"issue_type,omitempty" json:"issue_type,omitempty" hcl:"issue_type"`
|
||||
|
||||
Summary *string `yaml:"summary,omitempty" json:"summary,omitempty" hcl:"summary"`
|
||||
Description *string `yaml:"description,omitempty" json:"description,omitempty" hcl:"description"`
|
||||
Labels *[]string `yaml:"labels,omitempty" json:"labels,omitempty" hcl:"labels"`
|
||||
Priority *string `yaml:"priority,omitempty" json:"priority,omitempty" hcl:"priority"`
|
||||
ReopenTransition *string `yaml:"reopen_transition,omitempty" json:"reopen_transition,omitempty" hcl:"reopen_transition"`
|
||||
ResolveTransition *string `yaml:"resolve_transition,omitempty" json:"resolve_transition,omitempty" hcl:"resolve_transition"`
|
||||
WontFixResolution *string `yaml:"wont_fix_resolution,omitempty" json:"wont_fix_resolution,omitempty" hcl:"wont_fix_resolution"`
|
||||
ReopenDuration *string `yaml:"reopen_duration,omitempty" json:"reopen_duration,omitempty" hcl:"reopen_duration"`
|
||||
DedupKeyFieldName *string `yaml:"dedup_key_field,omitempty" json:"dedup_key_field,omitempty" hcl:"dedup_key_field"`
|
||||
Fields *map[string]any `yaml:"fields,omitempty" json:"fields,omitempty" hcl:"fields"`
|
||||
|
||||
User *Secret `yaml:"user,omitempty" json:"user,omitempty" hcl:"user"`
|
||||
Password *Secret `yaml:"password,omitempty" json:"password,omitempty" hcl:"password"`
|
||||
Token *Secret `yaml:"api_token,omitempty" json:"api_token,omitempty" hcl:"api_token"`
|
||||
}
|
||||
|
||||
type KafkaIntegration struct {
|
||||
DisableResolveMessage *bool `json:"-" yaml:"-" hcl:"disable_resolve_message"`
|
||||
|
||||
@@ -321,6 +344,7 @@ type ContactPoint struct {
|
||||
Discord []DiscordIntegration `json:"discord" yaml:"discord" hcl:"discord,block"`
|
||||
Email []EmailIntegration `json:"email" yaml:"email" hcl:"email,block"`
|
||||
Googlechat []GooglechatIntegration `json:"googlechat" yaml:"googlechat" hcl:"googlechat,block"`
|
||||
Jira []JiraIntegration `json:"jira" yaml:"jira" hcl:"jira,block"`
|
||||
Kafka []KafkaIntegration `json:"kafka" yaml:"kafka" hcl:"kafka,block"`
|
||||
Line []LineIntegration `json:"line" yaml:"line" hcl:"line,block"`
|
||||
Mqtt []MqttIntegration `json:"mqtt" yaml:"mqtt" hcl:"mqtt,block"`
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/alerting/receivers/jira"
|
||||
alertingMqtt "github.com/grafana/alerting/receivers/mqtt"
|
||||
alertingOpsgenie "github.com/grafana/alerting/receivers/opsgenie"
|
||||
alertingPagerduty "github.com/grafana/alerting/receivers/pagerduty"
|
||||
@@ -365,7 +366,7 @@ func GetAvailableNotifiers() []*NotifierPlugin {
|
||||
InputType: InputTypeText,
|
||||
PropertyName: "details",
|
||||
},
|
||||
{ //New in 11.1
|
||||
{ // New in 11.1
|
||||
Label: "URL",
|
||||
Description: "The URL to send API requests to",
|
||||
Element: ElementTypeInput,
|
||||
@@ -1708,6 +1709,154 @@ func GetAvailableNotifiers() []*NotifierPlugin {
|
||||
},
|
||||
},
|
||||
},
|
||||
{ // Since Grafana 11.6
|
||||
Type: "jira",
|
||||
Name: "Jira",
|
||||
Description: "Creates Jira issues from alerts",
|
||||
Heading: "Jira settings",
|
||||
Options: []NotifierOption{
|
||||
{
|
||||
Label: "API URL of Jira instance, including version of API",
|
||||
Element: ElementTypeInput,
|
||||
InputType: InputTypeText,
|
||||
Placeholder: "https://grafana.atlassian.net/rest/api/3",
|
||||
PropertyName: "api_url",
|
||||
Description: "Supported v2 or v3 APIs",
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Label: "HTTP Basic Authentication - Username",
|
||||
Element: ElementTypeInput,
|
||||
InputType: InputTypeText,
|
||||
PropertyName: "user",
|
||||
Description: "Username to use for Jira authentication.",
|
||||
Secure: true,
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Label: "HTTP Basic Authentication - Password",
|
||||
Element: ElementTypeInput,
|
||||
InputType: InputTypePassword,
|
||||
PropertyName: "password",
|
||||
// Go to https://id.atlassian.com/manage-profile/security/api-tokens to obtain a token.
|
||||
Description: "Password to use for Jira authentication.",
|
||||
Secure: true,
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Label: "Authorization Header - Personal Access Token",
|
||||
Element: ElementTypeInput,
|
||||
InputType: InputTypePassword,
|
||||
PropertyName: "api_token",
|
||||
// Go to https://confluence.atlassian.com/enterprise/using-personal-access-tokens-1026032365.html for how to obtain a token.
|
||||
Description: "Personal Access Token that is used as a bearer authorization header.",
|
||||
Secure: true,
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Label: "Project Key",
|
||||
Description: "The project key associated with the relevant Jira project",
|
||||
Element: ElementTypeInput,
|
||||
InputType: InputTypeText,
|
||||
Placeholder: "Grafana",
|
||||
PropertyName: "project",
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Label: "Issue Type",
|
||||
Description: "The type of the Jira issue (e.g., Bug, Task, Story). You can use templates to customize this field.",
|
||||
Element: ElementTypeInput,
|
||||
InputType: InputTypeText,
|
||||
Placeholder: "Task",
|
||||
Required: true,
|
||||
PropertyName: "issue_type",
|
||||
},
|
||||
{
|
||||
Label: "Summary",
|
||||
Description: fmt.Sprintf("The summary of the Jira issue. You can use templates to customize this field. Maximum length is %d characters.", jira.MaxSummaryLenRunes),
|
||||
Element: ElementTypeInput,
|
||||
InputType: InputTypeText,
|
||||
Placeholder: jira.DefaultSummary,
|
||||
PropertyName: "summary",
|
||||
},
|
||||
{
|
||||
Label: "Description",
|
||||
Description: fmt.Sprintf("The description of the Jira issue. You can use templates to customize this field. Maximum length is %d characters.", jira.MaxDescriptionLenRunes),
|
||||
Element: ElementTypeTextArea,
|
||||
InputType: InputTypeText,
|
||||
Placeholder: jira.DefaultDescription,
|
||||
PropertyName: "description",
|
||||
},
|
||||
{
|
||||
Label: "Labels",
|
||||
Description: "Labels to assign to the Jira issue. You can use templates to customize this field.",
|
||||
Element: ElementStringArray,
|
||||
Placeholder: "",
|
||||
PropertyName: "labels",
|
||||
},
|
||||
{
|
||||
Label: "Priority",
|
||||
Description: "The priority of the Jira issue (e.g., High, Medium, Low). You can use templates to customize this field.",
|
||||
Element: ElementTypeInput,
|
||||
InputType: InputTypeText,
|
||||
Placeholder: jira.DefaultPriority,
|
||||
PropertyName: "priority",
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Label: "Resolve Transition",
|
||||
Description: `Name of the workflow transition to resolve an issue. The target status must have the category "done". If not set, the issue will not be resolved.`,
|
||||
Element: ElementTypeInput,
|
||||
InputType: InputTypeText,
|
||||
Placeholder: "",
|
||||
PropertyName: "resolve_transition",
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Label: "Reopen Transition",
|
||||
Description: `Name of the workflow transition to resolve an issue. The target status must not have the category "done". If not set, the issue will not be reopened.`,
|
||||
Element: ElementTypeInput,
|
||||
InputType: InputTypeText,
|
||||
Placeholder: "",
|
||||
PropertyName: "reopen_transition",
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Label: "Reopen Duration",
|
||||
Description: "Reopen the issue when it is not older than this value in minutes. Otherwise, create a new issue.",
|
||||
Element: ElementTypeInput,
|
||||
InputType: InputTypeText,
|
||||
Placeholder: "10m",
|
||||
PropertyName: "reopen_duration",
|
||||
},
|
||||
{
|
||||
Label: "\"Won't fix\" Transition",
|
||||
Description: `If reopen transition is defined, ignore issues with that resolution.`,
|
||||
Element: ElementTypeInput,
|
||||
InputType: InputTypeText,
|
||||
Placeholder: "",
|
||||
PropertyName: "wont_fix_resolution",
|
||||
Required: false,
|
||||
},
|
||||
{
|
||||
Label: "Custom field ID for deduplication",
|
||||
Description: "Id of the custom field where the deduplication key should be stored. Otherwise, it is added to labels in format 'ALERT($KEY).'",
|
||||
Element: ElementTypeInput,
|
||||
InputType: InputTypeText,
|
||||
Placeholder: "10000",
|
||||
ValidationRule: "^[0-9]+$",
|
||||
PropertyName: "dedup_key_field",
|
||||
},
|
||||
{
|
||||
Label: "Custom Field Data",
|
||||
Description: "Custom field data to set on the Jira issue.",
|
||||
Element: ElementTypeKeyValueMap,
|
||||
InputType: InputTypeText,
|
||||
Placeholder: "",
|
||||
PropertyName: "fields",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,20 +27,38 @@ func TestGetSecretKeysForContactPointType(t *testing.T) {
|
||||
{receiverType: "prometheus-alertmanager", expectedSecretFields: []string{"basicAuthPassword"}},
|
||||
{receiverType: "discord", expectedSecretFields: []string{"url"}},
|
||||
{receiverType: "googlechat", expectedSecretFields: []string{"url"}},
|
||||
{receiverType: "line", expectedSecretFields: []string{"token"}},
|
||||
{receiverType: "LINE", expectedSecretFields: []string{"token"}},
|
||||
{receiverType: "threema", expectedSecretFields: []string{"api_secret"}},
|
||||
{receiverType: "opsgenie", expectedSecretFields: []string{"apiKey"}},
|
||||
{receiverType: "webex", expectedSecretFields: []string{"bot_token"}},
|
||||
{receiverType: "sns", expectedSecretFields: []string{"sigv4.access_key", "sigv4.secret_key"}},
|
||||
{receiverType: "mqtt", expectedSecretFields: []string{"password", "tlsConfig.caCertificate", "tlsConfig.clientCertificate", "tlsConfig.clientKey"}},
|
||||
{receiverType: "jira", expectedSecretFields: []string{"user", "password", "api_token"}},
|
||||
}
|
||||
n := GetAvailableNotifiers()
|
||||
allTypes := make(map[string]struct{}, len(n))
|
||||
for _, plugin := range n {
|
||||
allTypes[plugin.Type] = struct{}{}
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
delete(allTypes, testCase.receiverType)
|
||||
t.Run(testCase.receiverType, func(t *testing.T) {
|
||||
got, err := GetSecretKeysForContactPointType(testCase.receiverType)
|
||||
require.NoError(t, err)
|
||||
t.Logf("got secret fields: %#v", got)
|
||||
require.ElementsMatch(t, testCase.expectedSecretFields, got)
|
||||
})
|
||||
}
|
||||
|
||||
for integrationType := range allTypes {
|
||||
t.Run(integrationType, func(t *testing.T) {
|
||||
got, err := GetSecretKeysForContactPointType(integrationType)
|
||||
require.NoError(t, err)
|
||||
require.Emptyf(t, got, "secret keys for %s should be empty", integrationType)
|
||||
})
|
||||
}
|
||||
|
||||
require.Emptyf(t, allTypes, "not all types are covered: %s", allTypes)
|
||||
}
|
||||
|
||||
func Test_getSecretFields(t *testing.T) {
|
||||
|
||||
@@ -45,6 +45,8 @@ const (
|
||||
ElementTypeSubform = "subform"
|
||||
// ElementSubformArray will render a multiple sub-forms with schema defined in SubformOptions
|
||||
ElementSubformArray = "subform_array"
|
||||
// ElementStringArray will render a set of fields to manage an array of strings.
|
||||
ElementStringArray = "string_array"
|
||||
)
|
||||
|
||||
// InputType is the type of input that can be rendered in the frontend.
|
||||
|
||||
@@ -1,16 +1,31 @@
|
||||
package prom
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
const (
|
||||
// ruleUIDLabel is a special label that can be used to set a custom UID for a Prometheus
|
||||
// alert rule when converting it to a Grafana alert rule. If this label is not present,
|
||||
// a stable UID will be generated automatically based on the rule's data.
|
||||
ruleUIDLabel = "__grafana_alert_rule_uid__"
|
||||
)
|
||||
|
||||
const (
|
||||
queryRefID = "query"
|
||||
prometheusMathRefID = "prometheus_math"
|
||||
thresholdRefID = "threshold"
|
||||
)
|
||||
|
||||
// Config defines the configuration options for the Prometheus to Grafana rules converter.
|
||||
type Config struct {
|
||||
DatasourceUID string
|
||||
DatasourceType string
|
||||
@@ -22,6 +37,7 @@ type Config struct {
|
||||
AlertRules RulesConfig
|
||||
}
|
||||
|
||||
// RulesConfig contains configuration that applies to either recording or alerting rules.
|
||||
type RulesConfig struct {
|
||||
IsPaused bool
|
||||
}
|
||||
@@ -34,7 +50,7 @@ var (
|
||||
FromTimeRange: &defaultTimeRange,
|
||||
EvaluationOffset: &defaultEvaluationOffset,
|
||||
ExecErrState: models.ErrorErrState,
|
||||
NoDataState: models.NoData,
|
||||
NoDataState: models.OK,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -42,6 +58,9 @@ type Converter struct {
|
||||
cfg Config
|
||||
}
|
||||
|
||||
// NewConverter creates a new Converter instance with the provided configuration.
|
||||
// It validates the configuration and returns an error if any required fields are missing
|
||||
// or if the configuration is invalid.
|
||||
func NewConverter(cfg Config) (*Converter, error) {
|
||||
if cfg.DatasourceUID == "" {
|
||||
return nil, fmt.Errorf("datasource UID is required")
|
||||
@@ -114,6 +133,12 @@ func (p *Converter) convertRuleGroup(orgID int64, namespaceUID string, promGroup
|
||||
gr.Title = fmt.Sprintf("%s (%d)", gr.Title, val)
|
||||
}
|
||||
|
||||
uid, err := getUID(orgID, namespaceUID, promGroup.Name, i, rule)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to generate UID for rule '%s': %w", gr.Title, err)
|
||||
}
|
||||
gr.UID = uid
|
||||
|
||||
rules = append(rules, gr)
|
||||
}
|
||||
|
||||
@@ -127,21 +152,52 @@ func (p *Converter) convertRuleGroup(orgID int64, namespaceUID string, promGroup
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// getUID returns a UID for a Prometheus rule.
|
||||
// If the rule has a special label its value is used.
|
||||
// Otherwise, a stable UUID is generated by using a hash of the rule's data.
|
||||
func getUID(orgID int64, namespaceUID string, group string, position int, promRule PrometheusRule) (string, error) {
|
||||
if uid, ok := promRule.Labels[ruleUIDLabel]; ok {
|
||||
if err := util.ValidateUID(uid); err != nil {
|
||||
return "", fmt.Errorf("invalid UID label value: %s; %w", uid, err)
|
||||
}
|
||||
return uid, nil
|
||||
}
|
||||
|
||||
// Generate stable UUID based on the orgID, namespace, group and position.
|
||||
uidData := fmt.Sprintf("%d|%s|%s|%d", orgID, namespaceUID, group, position)
|
||||
u := uuid.NewSHA1(uuid.NameSpaceOID, []byte(uidData))
|
||||
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
func (p *Converter) convertRule(orgID int64, namespaceUID, group string, rule PrometheusRule) (models.AlertRule, error) {
|
||||
var forInterval time.Duration
|
||||
if rule.For != nil {
|
||||
forInterval = time.Duration(*rule.For)
|
||||
}
|
||||
|
||||
queryNode, err := createAlertQueryNode(p.cfg.DatasourceUID, p.cfg.DatasourceType, rule.Expr, *p.cfg.FromTimeRange, *p.cfg.EvaluationOffset)
|
||||
var query []models.AlertQuery
|
||||
var title string
|
||||
var isPaused bool
|
||||
var record *models.Record
|
||||
var err error
|
||||
|
||||
isRecordingRule := rule.Record != ""
|
||||
query, err = p.createQuery(rule.Expr, isRecordingRule)
|
||||
if err != nil {
|
||||
return models.AlertRule{}, err
|
||||
}
|
||||
|
||||
var title string
|
||||
if rule.Record != "" {
|
||||
if isRecordingRule {
|
||||
record = &models.Record{
|
||||
From: queryRefID,
|
||||
Metric: rule.Record,
|
||||
}
|
||||
|
||||
isPaused = p.cfg.RecordingRules.IsPaused
|
||||
title = rule.Record
|
||||
} else {
|
||||
isPaused = p.cfg.AlertRules.IsPaused
|
||||
title = rule.Alert
|
||||
}
|
||||
|
||||
@@ -159,14 +215,16 @@ func (p *Converter) convertRule(orgID int64, namespaceUID, group string, rule Pr
|
||||
OrgID: orgID,
|
||||
NamespaceUID: namespaceUID,
|
||||
Title: title,
|
||||
Data: []models.AlertQuery{queryNode},
|
||||
Condition: "A",
|
||||
Data: query,
|
||||
Condition: query[len(query)-1].RefID,
|
||||
NoDataState: p.cfg.NoDataState,
|
||||
ExecErrState: p.cfg.ExecErrState,
|
||||
Annotations: rule.Annotations,
|
||||
Labels: labels,
|
||||
For: forInterval,
|
||||
RuleGroup: group,
|
||||
IsPaused: isPaused,
|
||||
Record: record,
|
||||
Metadata: models.AlertRuleMetadata{
|
||||
PrometheusStyleRule: &models.PrometheusStyleRule{
|
||||
OriginalRuleDefinition: string(originalRuleDefinition),
|
||||
@@ -174,47 +232,41 @@ func (p *Converter) convertRule(orgID int64, namespaceUID, group string, rule Pr
|
||||
},
|
||||
}
|
||||
|
||||
if rule.Record != "" {
|
||||
result.Record = &models.Record{
|
||||
From: "A",
|
||||
Metric: rule.Record,
|
||||
}
|
||||
result.IsPaused = p.cfg.RecordingRules.IsPaused
|
||||
} else {
|
||||
result.IsPaused = p.cfg.AlertRules.IsPaused
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func createAlertQueryNode(datasourceUID, datasourceType, expr string, fromTimeRange, evaluationOffset time.Duration) (models.AlertQuery, error) {
|
||||
modelData := map[string]interface{}{
|
||||
"datasource": map[string]interface{}{
|
||||
"type": datasourceType,
|
||||
"uid": datasourceUID,
|
||||
},
|
||||
"expr": expr,
|
||||
"instant": true,
|
||||
"range": false,
|
||||
"refId": "A",
|
||||
}
|
||||
|
||||
if datasourceType == datasources.DS_LOKI {
|
||||
modelData["queryType"] = "instant"
|
||||
}
|
||||
|
||||
modelJSON, err := json.Marshal(modelData)
|
||||
// createQuery constructs the alert query nodes for a given Prometheus rule expression.
|
||||
// It returns a slice of AlertQuery that represent the evaluation steps for the rule.
|
||||
//
|
||||
// For recording rules it generates a single query node that
|
||||
// executes the PromQL query in the configured datasource.
|
||||
//
|
||||
// For alerting rules, it generates three query nodes:
|
||||
// 1. Query Node (query): Executes the PromQL query using the configured datasource.
|
||||
// 2. Math Node (prometheus_math): Applies a math expression "is_number($query) || is_nan($query) || is_inf($query)".
|
||||
// 3. Threshold Node (threshold): Gets the result from the math node and checks that it's greater than 0.
|
||||
//
|
||||
// This is needed to ensure that we keep the Prometheus behaviour, where any returned result
|
||||
// is considered alerting, and only when the query returns no data is the alert treated as normal.
|
||||
func (p *Converter) createQuery(expr string, isRecordingRule bool) ([]models.AlertQuery, error) {
|
||||
queryNode, err := createQueryNode(p.cfg.DatasourceUID, p.cfg.DatasourceType, expr, *p.cfg.FromTimeRange, *p.cfg.EvaluationOffset)
|
||||
if err != nil {
|
||||
return models.AlertQuery{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return models.AlertQuery{
|
||||
DatasourceUID: datasourceUID,
|
||||
Model: modelJSON,
|
||||
RefID: "A",
|
||||
RelativeTimeRange: models.RelativeTimeRange{
|
||||
From: models.Duration(fromTimeRange + evaluationOffset),
|
||||
To: models.Duration(0 + evaluationOffset),
|
||||
},
|
||||
}, nil
|
||||
if isRecordingRule {
|
||||
return []models.AlertQuery{queryNode}, nil
|
||||
}
|
||||
|
||||
mathNode, err := createMathNode()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
thresholdNode, err := createThresholdNode()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return []models.AlertQuery{queryNode, mathNode, thresholdNode}, nil
|
||||
}
|
||||
|
||||
@@ -1,15 +1,21 @@
|
||||
package prom
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
prommodel "github.com/prometheus/common/model"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/grafana/grafana/pkg/expr"
|
||||
"github.com/grafana/grafana/pkg/expr/mathexp"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
)
|
||||
|
||||
func TestPrometheusRulesToGrafana(t *testing.T) {
|
||||
@@ -121,6 +127,9 @@ func TestPrometheusRulesToGrafana(t *testing.T) {
|
||||
|
||||
if promRule.Record != "" {
|
||||
require.Equal(t, promRule.Record, grafanaRule.Title)
|
||||
require.NotNil(t, grafanaRule.Record)
|
||||
require.Equal(t, grafanaRule.Record.From, queryRefID)
|
||||
require.Equal(t, promRule.Record, grafanaRule.Record.Metric)
|
||||
} else {
|
||||
require.Equal(t, promRule.Alert, grafanaRule.Title)
|
||||
}
|
||||
@@ -136,6 +145,10 @@ func TestPrometheusRulesToGrafana(t *testing.T) {
|
||||
expectedLabels[k] = v
|
||||
}
|
||||
|
||||
uidData := fmt.Sprintf("%d|%s|%s|%d", tc.orgID, tc.namespace, tc.promGroup.Name, j)
|
||||
u := uuid.NewSHA1(uuid.NameSpaceOID, []byte(uidData))
|
||||
require.Equal(t, u.String(), grafanaRule.UID, tc.name)
|
||||
|
||||
require.Equal(t, expectedLabels, grafanaRule.Labels, tc.name)
|
||||
require.Equal(t, promRule.Annotations, grafanaRule.Annotations, tc.name)
|
||||
require.Equal(t, models.Duration(0*time.Minute), grafanaRule.Data[0].RelativeTimeRange.To)
|
||||
@@ -190,3 +203,221 @@ func TestPrometheusRulesToGrafanaWithDuplicateRuleNames(t *testing.T) {
|
||||
require.Equal(t, "another alert", group.Rules[2].Title)
|
||||
require.Equal(t, "alert (3)", group.Rules[3].Title)
|
||||
}
|
||||
|
||||
func TestCreateMathNode(t *testing.T) {
|
||||
node, err := createMathNode()
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, expr.DatasourceUID, node.DatasourceUID)
|
||||
require.Equal(t, string(expr.QueryTypeMath), node.QueryType)
|
||||
require.Equal(t, "prometheus_math", node.RefID)
|
||||
|
||||
var model map[string]interface{}
|
||||
err = json.Unmarshal(node.Model, &model)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, "prometheus_math", model["refId"])
|
||||
require.Equal(t, string(expr.QueryTypeMath), model["type"])
|
||||
require.Equal(t, "is_number($query) || is_nan($query) || is_inf($query)", model["expression"])
|
||||
|
||||
ds := model["datasource"].(map[string]interface{})
|
||||
require.Equal(t, expr.DatasourceUID, ds["name"])
|
||||
require.Equal(t, expr.DatasourceType, ds["type"])
|
||||
require.Equal(t, expr.DatasourceUID, ds["uid"])
|
||||
}
|
||||
|
||||
func TestCreateThresholdNode(t *testing.T) {
|
||||
node, err := createThresholdNode()
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, expr.DatasourceUID, node.DatasourceUID)
|
||||
require.Equal(t, string(expr.QueryTypeThreshold), node.QueryType)
|
||||
require.Equal(t, "threshold", node.RefID)
|
||||
|
||||
var model map[string]interface{}
|
||||
err = json.Unmarshal(node.Model, &model)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, "threshold", model["refId"])
|
||||
require.Equal(t, string(expr.QueryTypeThreshold), model["type"])
|
||||
|
||||
ds := model["datasource"].(map[string]interface{})
|
||||
require.Equal(t, expr.DatasourceUID, ds["name"])
|
||||
require.Equal(t, expr.DatasourceType, ds["type"])
|
||||
require.Equal(t, expr.DatasourceUID, ds["uid"])
|
||||
|
||||
conditions := model["conditions"].([]interface{})
|
||||
require.Len(t, conditions, 1)
|
||||
|
||||
condition := conditions[0].(map[string]interface{})
|
||||
evaluator := condition["evaluator"].(map[string]interface{})
|
||||
require.Equal(t, string(expr.ThresholdIsAbove), evaluator["type"])
|
||||
require.Equal(t, []interface{}{float64(0)}, evaluator["params"])
|
||||
}
|
||||
|
||||
func TestPrometheusRulesToGrafana_NodesInRules(t *testing.T) {
|
||||
cfg := Config{
|
||||
DatasourceUID: "datasource-uid",
|
||||
DatasourceType: datasources.DS_PROMETHEUS,
|
||||
}
|
||||
converter, err := NewConverter(cfg)
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("alert rule should have math and threshold nodes", func(t *testing.T) {
|
||||
group := PrometheusRuleGroup{
|
||||
Name: "test",
|
||||
Rules: []PrometheusRule{
|
||||
{
|
||||
Alert: "alert1",
|
||||
Expr: "up == 0",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
result, err := converter.PrometheusRulesToGrafana(1, "namespace", group)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, result.Rules, 1)
|
||||
require.Len(t, result.Rules[0].Data, 3)
|
||||
|
||||
// First node should be query
|
||||
require.Equal(t, "query", result.Rules[0].Data[0].RefID)
|
||||
|
||||
// Second node should be math
|
||||
require.Equal(t, "prometheus_math", result.Rules[0].Data[1].RefID)
|
||||
require.Equal(t, string(expr.QueryTypeMath), result.Rules[0].Data[1].QueryType)
|
||||
// Check that the math expression is valid
|
||||
var model map[string]interface{}
|
||||
err = json.Unmarshal(result.Rules[0].Data[1].Model, &model)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "is_number($query) || is_nan($query) || is_inf($query)", model["expression"])
|
||||
// The math expression should be parsed successfully
|
||||
_, err = mathexp.New(model["expression"].(string))
|
||||
require.NoError(t, err)
|
||||
|
||||
// Third node should be threshold
|
||||
require.Equal(t, "threshold", result.Rules[0].Data[2].RefID)
|
||||
require.Equal(t, string(expr.QueryTypeThreshold), result.Rules[0].Data[2].QueryType)
|
||||
})
|
||||
|
||||
t.Run("recording rule should only have query node", func(t *testing.T) {
|
||||
group := PrometheusRuleGroup{
|
||||
Name: "test",
|
||||
Rules: []PrometheusRule{
|
||||
{
|
||||
Record: "metric",
|
||||
Expr: "sum(rate(http_requests_total[5m]))",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
result, err := converter.PrometheusRulesToGrafana(1, "namespace", group)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, result.Rules, 1)
|
||||
require.Len(t, result.Rules[0].Data, 1)
|
||||
|
||||
// Should only have query node
|
||||
require.Equal(t, "query", result.Rules[0].Data[0].RefID)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPrometheusRulesToGrafana_UID(t *testing.T) {
|
||||
orgID := int64(1)
|
||||
namespace := "some-namespace"
|
||||
|
||||
promGroup := PrometheusRuleGroup{
|
||||
Name: "test-group-1",
|
||||
Interval: prommodel.Duration(10 * time.Second),
|
||||
Rules: []PrometheusRule{
|
||||
{
|
||||
Alert: "alert-1",
|
||||
Expr: "cpu_usage > 80",
|
||||
For: util.Pointer(prommodel.Duration(5 * time.Minute)),
|
||||
Labels: map[string]string{
|
||||
"severity": "critical",
|
||||
ruleUIDLabel: "rule-uid-1",
|
||||
},
|
||||
Annotations: map[string]string{
|
||||
"summary": "CPU usage is critical",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
converter, err := NewConverter(Config{
|
||||
DatasourceUID: "datasource-uid",
|
||||
DatasourceType: datasources.DS_PROMETHEUS,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("if not specified, UID is generated based on the rule index", func(t *testing.T) {
|
||||
grafanaGroup, err := converter.PrometheusRulesToGrafana(orgID, namespace, promGroup)
|
||||
require.NoError(t, err)
|
||||
|
||||
firstUID := grafanaGroup.Rules[0].UID
|
||||
|
||||
// Convert again
|
||||
grafanaGroup, err = converter.PrometheusRulesToGrafana(orgID, namespace, promGroup)
|
||||
require.NoError(t, err)
|
||||
|
||||
secondUID := grafanaGroup.Rules[0].UID
|
||||
|
||||
// They must be equal
|
||||
require.NotEmpty(t, firstUID)
|
||||
require.Equal(t, firstUID, secondUID)
|
||||
})
|
||||
|
||||
t.Run("if the special label is specified", func(t *testing.T) {
|
||||
t.Run("and the label is valid it should be used", func(t *testing.T) {
|
||||
orgID := int64(1)
|
||||
namespace := "some-namespace"
|
||||
|
||||
converter, err := NewConverter(Config{
|
||||
DatasourceUID: "datasource-uid",
|
||||
DatasourceType: datasources.DS_PROMETHEUS,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
promGroup.Rules[0].Labels[ruleUIDLabel] = "rule-uid-1"
|
||||
|
||||
grafanaGroup, err := converter.PrometheusRulesToGrafana(orgID, namespace, promGroup)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, "rule-uid-1", grafanaGroup.Rules[0].UID)
|
||||
})
|
||||
|
||||
t.Run("and the label is invalid", func(t *testing.T) {
|
||||
orgID := int64(1)
|
||||
namespace := "some-namespace"
|
||||
|
||||
converter, err := NewConverter(Config{
|
||||
DatasourceUID: "datasource-uid",
|
||||
DatasourceType: datasources.DS_PROMETHEUS,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// create a string of 50 characters
|
||||
promGroup.Rules[0].Labels[ruleUIDLabel] = "aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjkkkkllllmm" // too long
|
||||
|
||||
grafanaGroup, err := converter.PrometheusRulesToGrafana(orgID, namespace, promGroup)
|
||||
require.Errorf(t, err, "invalid UID label value")
|
||||
require.Nil(t, grafanaGroup)
|
||||
})
|
||||
|
||||
t.Run("and the label is empty", func(t *testing.T) {
|
||||
orgID := int64(1)
|
||||
namespace := "some-namespace"
|
||||
|
||||
converter, err := NewConverter(Config{
|
||||
DatasourceUID: "datasource-uid",
|
||||
DatasourceType: datasources.DS_PROMETHEUS,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
promGroup.Rules[0].Labels[ruleUIDLabel] = ""
|
||||
|
||||
grafanaGroup, err := converter.PrometheusRulesToGrafana(orgID, namespace, promGroup)
|
||||
require.Errorf(t, err, "invalid UID label value")
|
||||
require.Nil(t, grafanaGroup)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
package prom
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/expr"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||
)
|
||||
|
||||
type CommonQueryModel struct {
|
||||
Datasource datasources.DataSource `json:"datasource"`
|
||||
RefID string `json:"refId"`
|
||||
Type expr.QueryType `json:"type"`
|
||||
}
|
||||
|
||||
func createQueryNode(datasourceUID, datasourceType, expr string, fromTimeRange, evaluationOffset time.Duration) (models.AlertQuery, error) {
|
||||
modelData := map[string]interface{}{
|
||||
"datasource": map[string]interface{}{
|
||||
"type": datasourceType,
|
||||
"uid": datasourceUID,
|
||||
},
|
||||
"expr": expr,
|
||||
"instant": true,
|
||||
"range": false,
|
||||
"refId": queryRefID,
|
||||
}
|
||||
|
||||
if datasourceType == datasources.DS_LOKI {
|
||||
modelData["queryType"] = "instant"
|
||||
}
|
||||
|
||||
modelJSON, err := json.Marshal(modelData)
|
||||
if err != nil {
|
||||
return models.AlertQuery{}, err
|
||||
}
|
||||
|
||||
return models.AlertQuery{
|
||||
DatasourceUID: datasourceUID,
|
||||
Model: modelJSON,
|
||||
RefID: queryRefID,
|
||||
RelativeTimeRange: models.RelativeTimeRange{
|
||||
From: models.Duration(fromTimeRange + evaluationOffset),
|
||||
To: models.Duration(0 + evaluationOffset),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
type MathQueryModel struct {
|
||||
expr.MathQuery
|
||||
CommonQueryModel
|
||||
}
|
||||
|
||||
func createMathNode() (models.AlertQuery, error) {
|
||||
ds, err := expr.DataSourceModelFromNodeType(expr.TypeCMDNode)
|
||||
if err != nil {
|
||||
return models.AlertQuery{}, err
|
||||
}
|
||||
|
||||
model := MathQueryModel{
|
||||
CommonQueryModel: CommonQueryModel{
|
||||
Datasource: *ds,
|
||||
RefID: prometheusMathRefID,
|
||||
Type: expr.QueryTypeMath,
|
||||
},
|
||||
MathQuery: expr.MathQuery{
|
||||
Expression: fmt.Sprintf("is_number($%[1]s) || is_nan($%[1]s) || is_inf($%[1]s)", queryRefID),
|
||||
},
|
||||
}
|
||||
|
||||
modelJSON, err := json.Marshal(model)
|
||||
if err != nil {
|
||||
return models.AlertQuery{}, err
|
||||
}
|
||||
|
||||
return models.AlertQuery{
|
||||
DatasourceUID: expr.DatasourceUID,
|
||||
Model: modelJSON,
|
||||
RefID: prometheusMathRefID,
|
||||
QueryType: string(model.Type),
|
||||
}, nil
|
||||
}
|
||||
|
||||
type ThresholdQueryModel struct {
|
||||
expr.ThresholdQuery
|
||||
CommonQueryModel
|
||||
}
|
||||
|
||||
func createThresholdNode() (models.AlertQuery, error) {
|
||||
ds, err := expr.DataSourceModelFromNodeType(expr.TypeCMDNode)
|
||||
if err != nil {
|
||||
return models.AlertQuery{}, err
|
||||
}
|
||||
|
||||
model := ThresholdQueryModel{
|
||||
CommonQueryModel: CommonQueryModel{
|
||||
Datasource: *ds,
|
||||
RefID: thresholdRefID,
|
||||
Type: expr.QueryTypeThreshold,
|
||||
},
|
||||
ThresholdQuery: expr.ThresholdQuery{
|
||||
Expression: prometheusMathRefID,
|
||||
Conditions: []expr.ThresholdConditionJSON{
|
||||
{
|
||||
Evaluator: expr.ConditionEvalJSON{
|
||||
Type: expr.ThresholdIsAbove,
|
||||
Params: []float64{0},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
modelJSON, err := json.Marshal(model)
|
||||
if err != nil {
|
||||
return models.AlertQuery{}, err
|
||||
}
|
||||
|
||||
return models.AlertQuery{
|
||||
DatasourceUID: expr.DatasourceUID,
|
||||
Model: modelJSON,
|
||||
RefID: thresholdRefID,
|
||||
QueryType: string(model.Type),
|
||||
}, nil
|
||||
}
|
||||
@@ -423,6 +423,9 @@ func TestRemoveSecretsForContactPoint(t *testing.T) {
|
||||
"webhook": func(settings map[string]any) { // add additional field to the settings because valid config does not allow it to be specified along with password
|
||||
settings["authorization_credentials"] = "test-authz-creds"
|
||||
},
|
||||
"jira": func(settings map[string]any) { // add additional field to the settings because valid config does not allow it to be specified along with password
|
||||
settings["api_token"] = "test-token"
|
||||
},
|
||||
}
|
||||
|
||||
configs := notify.AllKnownConfigsForTesting
|
||||
|
||||
@@ -25,10 +25,11 @@ const backendType = "prometheus"
|
||||
|
||||
const (
|
||||
// Fixed error messages
|
||||
MimirDuplicateTimestampError = "err-mimir-sample-duplicate-timestamp"
|
||||
MimirInvalidLabelError = "err-mimir-label-invalid"
|
||||
MimirMaxSeriesPerUserError = "err-mimir-max-series-per-user"
|
||||
MimirLabelValueTooLongError = "err-mimir-label-value-too-long"
|
||||
MimirDuplicateTimestampError = "err-mimir-sample-duplicate-timestamp"
|
||||
MimirInvalidLabelError = "err-mimir-label-invalid"
|
||||
MimirLabelValueTooLongError = "err-mimir-label-value-too-long"
|
||||
MimirMaxLabelNamesPerSeriesError = "err-mimir-max-label-names-per-series"
|
||||
MimirMaxSeriesPerUserError = "err-mimir-max-series-per-user"
|
||||
|
||||
// Best effort error messages
|
||||
PrometheusDuplicateTimestampError = "duplicate sample for timestamp"
|
||||
@@ -267,16 +268,12 @@ func checkWriteError(writeErr promremote.WriteError) (err error, ignored bool) {
|
||||
}
|
||||
}
|
||||
|
||||
if strings.Contains(msg, MimirInvalidLabelError) {
|
||||
return errors.Join(ErrRejectedWrite, writeErr), false
|
||||
}
|
||||
|
||||
// this can happen when user exceeded defined maximum of
|
||||
if strings.Contains(msg, MimirMaxSeriesPerUserError) {
|
||||
return errors.Join(ErrRejectedWrite, writeErr), false
|
||||
}
|
||||
|
||||
if strings.Contains(msg, MimirLabelValueTooLongError) {
|
||||
// Check for expected user errors.
|
||||
switch {
|
||||
case strings.Contains(msg, MimirInvalidLabelError),
|
||||
strings.Contains(msg, MimirMaxSeriesPerUserError),
|
||||
strings.Contains(msg, MimirMaxLabelNamesPerSeriesError),
|
||||
strings.Contains(msg, MimirLabelValueTooLongError):
|
||||
return errors.Join(ErrRejectedWrite, writeErr), false
|
||||
}
|
||||
|
||||
|
||||
@@ -240,7 +240,23 @@ func TestPrometheusWriter_Write(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("too long labels fit under the client error category", func(t *testing.T) {
|
||||
msg := "received a series whose label value length exceeds the limit, label: 'label-1', value: 'value-1' (truncated) series: 'some_series (err-mimir-label-value-too-long). To adjust the related per-tenant limit, configure -validation.max-length-label-value, or contact your service administrator."
|
||||
msg := "received a series whose label value length exceeds the limit, label: 'label-1', value: 'value-1' (truncated) series: 'some_series' (err-mimir-label-value-too-long). To adjust the related per-tenant limit, configure -validation.max-length-label-value, or contact your service administrator."
|
||||
clientErr := testClientWriteError{
|
||||
statusCode: http.StatusBadRequest,
|
||||
msg: &msg,
|
||||
}
|
||||
client.writeSeriesFunc = func(ctx context.Context, ts promremote.TSList, opts promremote.WriteOptions) (promremote.WriteResult, promremote.WriteError) {
|
||||
return promremote.WriteResult{}, clientErr
|
||||
}
|
||||
|
||||
err := writer.Write(ctx, "test", now, frames, 1, map[string]string{"extra": "label"})
|
||||
|
||||
require.Error(t, err)
|
||||
require.ErrorIs(t, err, ErrRejectedWrite)
|
||||
})
|
||||
|
||||
t.Run("too many labels fit under the client error category", func(t *testing.T) {
|
||||
msg := "received a series whose number of labels exceeds the limit (actual: 50, limit: 40) series: 'some_series' (err-mimir-max-label-names-per-series). To adjust the related per-tenant limit, configure -validation.max-label-names-per-series, or contact your service administrator."
|
||||
clientErr := testClientWriteError{
|
||||
statusCode: http.StatusBadRequest,
|
||||
msg: &msg,
|
||||
|
||||
@@ -192,7 +192,7 @@ require (
|
||||
github.com/googleapis/gax-go/v2 v2.14.1 // indirect
|
||||
github.com/gorilla/mux v1.8.1 // indirect
|
||||
github.com/gorilla/websocket v1.5.3 // indirect
|
||||
github.com/grafana/alerting v0.0.0-20250219153626-c475b1a572b0 // indirect
|
||||
github.com/grafana/alerting v0.0.0-20250221202230-9d7e00921e44 // indirect
|
||||
github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7 // indirect
|
||||
github.com/grafana/dataplane/sdata v0.0.9 // indirect
|
||||
github.com/grafana/dskit v0.0.0-20241105154643-a6b453a88040 // indirect
|
||||
|
||||
@@ -566,8 +566,8 @@ github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
|
||||
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/grafana/alerting v0.0.0-20250219153626-c475b1a572b0 h1:LGH+tVzHCDrR9hsltmkP4jmNRg5IreQw5CNFbJKlnts=
|
||||
github.com/grafana/alerting v0.0.0-20250219153626-c475b1a572b0/go.mod h1:hdGB3dSl8Ma9Rjo2YiAEAjMkZ5HiNJbNDqRKDefRZrM=
|
||||
github.com/grafana/alerting v0.0.0-20250221202230-9d7e00921e44 h1:vboqvbAO0s0CTALHnqfmNvhCP1ziBcZNpYDbORqvOgg=
|
||||
github.com/grafana/alerting v0.0.0-20250221202230-9d7e00921e44/go.mod h1:hdGB3dSl8Ma9Rjo2YiAEAjMkZ5HiNJbNDqRKDefRZrM=
|
||||
github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7 h1:NTMmow+74I3Jb033xhbRgWQS7A//5TDhiM4tl7bsVP4=
|
||||
github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7/go.mod h1:T3X4z0ejGfJOiOmZLFeKCRT/yxWJq/RtclAc/PHj/w4=
|
||||
github.com/grafana/authlib/types v0.0.0-20250219092154-21ce22b49f31 h1:EokLC5grHwLPs4tXW8T6E8187H1e5G9AP0QQ5B60HbA=
|
||||
|
||||
@@ -117,7 +117,7 @@ require (
|
||||
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
|
||||
github.com/googleapis/gax-go/v2 v2.14.1 // indirect
|
||||
github.com/gorilla/mux v1.8.1 // indirect
|
||||
github.com/grafana/alerting v0.0.0-20250219153626-c475b1a572b0 // indirect
|
||||
github.com/grafana/alerting v0.0.0-20250221202230-9d7e00921e44 // indirect
|
||||
github.com/grafana/dataplane/sdata v0.0.9 // indirect
|
||||
github.com/grafana/grafana-app-sdk/logging v0.30.0 // indirect
|
||||
github.com/grafana/grafana-aws-sdk v0.31.5 // indirect
|
||||
|
||||
@@ -397,8 +397,8 @@ github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2z
|
||||
github.com/gorilla/mux v1.7.1/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
|
||||
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
|
||||
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
|
||||
github.com/grafana/alerting v0.0.0-20250219153626-c475b1a572b0 h1:LGH+tVzHCDrR9hsltmkP4jmNRg5IreQw5CNFbJKlnts=
|
||||
github.com/grafana/alerting v0.0.0-20250219153626-c475b1a572b0/go.mod h1:hdGB3dSl8Ma9Rjo2YiAEAjMkZ5HiNJbNDqRKDefRZrM=
|
||||
github.com/grafana/alerting v0.0.0-20250221202230-9d7e00921e44 h1:vboqvbAO0s0CTALHnqfmNvhCP1ziBcZNpYDbORqvOgg=
|
||||
github.com/grafana/alerting v0.0.0-20250221202230-9d7e00921e44/go.mod h1:hdGB3dSl8Ma9Rjo2YiAEAjMkZ5HiNJbNDqRKDefRZrM=
|
||||
github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7 h1:NTMmow+74I3Jb033xhbRgWQS7A//5TDhiM4tl7bsVP4=
|
||||
github.com/grafana/authlib v0.0.0-20250219100139-6a3b1bbb50e7/go.mod h1:T3X4z0ejGfJOiOmZLFeKCRT/yxWJq/RtclAc/PHj/w4=
|
||||
github.com/grafana/authlib/types v0.0.0-20250219092154-21ce22b49f31 h1:EokLC5grHwLPs4tXW8T6E8187H1e5G9AP0QQ5B60HbA=
|
||||
|
||||
@@ -1392,9 +1392,15 @@ func TestIntegrationCRUD(t *testing.T) {
|
||||
t.Run("should return secrets in secureFields but not settings", func(t *testing.T) {
|
||||
for _, integration := range get.Spec.Integrations {
|
||||
t.Run(integration.Type, func(t *testing.T) {
|
||||
expected := notify.AllKnownConfigsForTesting[strings.ToLower(integration.Type)]
|
||||
var fields map[string]any
|
||||
require.NoError(t, json.Unmarshal([]byte(expected.Config), &fields))
|
||||
secretFields, err := channels_config.GetSecretKeysForContactPointType(integration.Type)
|
||||
require.NoError(t, err)
|
||||
for _, field := range secretFields {
|
||||
if _, ok := fields[field]; !ok { // skip field that is not in the original setting
|
||||
continue
|
||||
}
|
||||
assert.Contains(t, integration.SecureFields, field)
|
||||
assert.Truef(t, integration.SecureFields[field], "secure field should be always true")
|
||||
|
||||
|
||||
@@ -36,6 +36,380 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"/apis/provisioning.grafana.app/v0alpha1/jobs": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"Job"
|
||||
],
|
||||
"description": "list or watch objects of kind Job",
|
||||
"operationId": "listJobForAllNamespaces",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList"
|
||||
}
|
||||
},
|
||||
"application/json;stream=watch": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList"
|
||||
}
|
||||
},
|
||||
"application/vnd.kubernetes.protobuf": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList"
|
||||
}
|
||||
},
|
||||
"application/vnd.kubernetes.protobuf;stream=watch": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList"
|
||||
}
|
||||
},
|
||||
"application/yaml": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"x-kubernetes-action": "list",
|
||||
"x-kubernetes-group-version-kind": {
|
||||
"group": "provisioning.grafana.app",
|
||||
"version": "v0alpha1",
|
||||
"kind": "Job"
|
||||
}
|
||||
},
|
||||
"parameters": [
|
||||
{
|
||||
"name": "allowWatchBookmarks",
|
||||
"in": "query",
|
||||
"description": "allowWatchBookmarks requests watch events with type \"BOOKMARK\". Servers that do not implement bookmarks may ignore this flag and bookmarks are sent at the server's discretion. Clients should not assume bookmarks are returned at any specific interval, nor may they assume the server will send any BOOKMARK event during a session. If this is not a watch, this field is ignored.",
|
||||
"schema": {
|
||||
"type": "boolean",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "continue",
|
||||
"in": "query",
|
||||
"description": "The continue option should be set when retrieving more results from the server. Since this value is server defined, clients may only use the continue value from a previous query result with identical query parameters (except for the value of continue) and the server may reject a continue value it does not recognize. If the specified continue value is no longer valid whether due to expiration (generally five to fifteen minutes) or a configuration change on the server, the server will respond with a 410 ResourceExpired error together with a continue token. If the client needs a consistent list, it must restart their list without the continue field. Otherwise, the client may send another list request with the token received with the 410 error, the server will respond with a list starting from the next key, but from the latest snapshot, which is inconsistent from the previous list results - objects that are created, modified, or deleted after the first list request will be included in the response, as long as their keys are after the \"next key\".\n\nThis field is not supported when watch is true. Clients may start a watch from the last resourceVersion value returned by the server and not miss any modifications.",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "fieldSelector",
|
||||
"in": "query",
|
||||
"description": "A selector to restrict the list of returned objects by their fields. Defaults to everything.",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "labelSelector",
|
||||
"in": "query",
|
||||
"description": "A selector to restrict the list of returned objects by their labels. Defaults to everything.",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
"description": "limit is a maximum number of responses to return for a list call. If more items exist, the server will set the `continue` field on the list metadata to a value that can be used with the same initial query to retrieve the next set of results. Setting a limit may return fewer than the requested amount of items (up to zero items) in the event all requested objects are filtered out and clients should only use the presence of the continue field to determine whether more results are available. Servers may choose not to support the limit argument and will return all of the available results. If limit is specified and the continue field is empty, clients may assume that no more results are available. This field is not supported if watch is true.\n\nThe server guarantees that the objects returned when using continue will be identical to issuing a single list call without a limit - that is, no objects created, modified, or deleted after the first request is issued will be included in any subsequent continued requests. This is sometimes referred to as a consistent snapshot, and ensures that a client that is using limit to receive smaller chunks of a very large result can ensure they see all possible objects. If objects are updated during a chunked list the version of the object that was present at the time the first list result was calculated is returned.",
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "pretty",
|
||||
"in": "query",
|
||||
"description": "If 'true', then the output is pretty printed. Defaults to 'false' unless the user-agent indicates a browser or command-line HTTP tool (curl and wget).",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "resourceVersion",
|
||||
"in": "query",
|
||||
"description": "resourceVersion sets a constraint on what resource versions a request may be served from. See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details.\n\nDefaults to unset",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "resourceVersionMatch",
|
||||
"in": "query",
|
||||
"description": "resourceVersionMatch determines how resourceVersion is applied to list calls. It is highly recommended that resourceVersionMatch be set for list calls where resourceVersion is set See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details.\n\nDefaults to unset",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "sendInitialEvents",
|
||||
"in": "query",
|
||||
"description": "`sendInitialEvents=true` may be set together with `watch=true`. In that case, the watch stream will begin with synthetic events to produce the current state of objects in the collection. Once all such events have been sent, a synthetic \"Bookmark\" event will be sent. The bookmark will report the ResourceVersion (RV) corresponding to the set of objects, and be marked with `\"k8s.io/initial-events-end\": \"true\"` annotation. Afterwards, the watch stream will proceed as usual, sending watch events corresponding to changes (subsequent to the RV) to objects watched.\n\nWhen `sendInitialEvents` option is set, we require `resourceVersionMatch` option to also be set. The semantic of the watch request is as following: - `resourceVersionMatch` = NotOlderThan\n is interpreted as \"data at least as new as the provided `resourceVersion`\"\n and the bookmark event is send when the state is synced\n to a `resourceVersion` at least as fresh as the one provided by the ListOptions.\n If `resourceVersion` is unset, this is interpreted as \"consistent read\" and the\n bookmark event is send when the state is synced at least to the moment\n when request started being processed.\n- `resourceVersionMatch` set to any other value or unset\n Invalid error is returned.\n\nDefaults to true if `resourceVersion=\"\"` or `resourceVersion=\"0\"` (for backward compatibility reasons) and to false otherwise.",
|
||||
"schema": {
|
||||
"type": "boolean",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "timeoutSeconds",
|
||||
"in": "query",
|
||||
"description": "Timeout for the list/watch call. This limits the duration of the call, regardless of any activity or inactivity.",
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "watch",
|
||||
"in": "query",
|
||||
"description": "Watch for changes to the described resources and return them as a stream of add, update, and remove notifications. Specify resourceVersion.",
|
||||
"schema": {
|
||||
"type": "boolean",
|
||||
"uniqueItems": true
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"/apis/provisioning.grafana.app/v0alpha1/namespaces/{namespace}/jobs": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"Job"
|
||||
],
|
||||
"description": "list or watch objects of kind Job",
|
||||
"operationId": "listJob",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList"
|
||||
}
|
||||
},
|
||||
"application/json;stream=watch": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList"
|
||||
}
|
||||
},
|
||||
"application/vnd.kubernetes.protobuf": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList"
|
||||
}
|
||||
},
|
||||
"application/vnd.kubernetes.protobuf;stream=watch": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList"
|
||||
}
|
||||
},
|
||||
"application/yaml": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"x-kubernetes-action": "list",
|
||||
"x-kubernetes-group-version-kind": {
|
||||
"group": "provisioning.grafana.app",
|
||||
"version": "v0alpha1",
|
||||
"kind": "Job"
|
||||
}
|
||||
},
|
||||
"parameters": [
|
||||
{
|
||||
"name": "allowWatchBookmarks",
|
||||
"in": "query",
|
||||
"description": "allowWatchBookmarks requests watch events with type \"BOOKMARK\". Servers that do not implement bookmarks may ignore this flag and bookmarks are sent at the server's discretion. Clients should not assume bookmarks are returned at any specific interval, nor may they assume the server will send any BOOKMARK event during a session. If this is not a watch, this field is ignored.",
|
||||
"schema": {
|
||||
"type": "boolean",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "continue",
|
||||
"in": "query",
|
||||
"description": "The continue option should be set when retrieving more results from the server. Since this value is server defined, clients may only use the continue value from a previous query result with identical query parameters (except for the value of continue) and the server may reject a continue value it does not recognize. If the specified continue value is no longer valid whether due to expiration (generally five to fifteen minutes) or a configuration change on the server, the server will respond with a 410 ResourceExpired error together with a continue token. If the client needs a consistent list, it must restart their list without the continue field. Otherwise, the client may send another list request with the token received with the 410 error, the server will respond with a list starting from the next key, but from the latest snapshot, which is inconsistent from the previous list results - objects that are created, modified, or deleted after the first list request will be included in the response, as long as their keys are after the \"next key\".\n\nThis field is not supported when watch is true. Clients may start a watch from the last resourceVersion value returned by the server and not miss any modifications.",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "fieldSelector",
|
||||
"in": "query",
|
||||
"description": "A selector to restrict the list of returned objects by their fields. Defaults to everything.",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "labelSelector",
|
||||
"in": "query",
|
||||
"description": "A selector to restrict the list of returned objects by their labels. Defaults to everything.",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "limit",
|
||||
"in": "query",
|
||||
"description": "limit is a maximum number of responses to return for a list call. If more items exist, the server will set the `continue` field on the list metadata to a value that can be used with the same initial query to retrieve the next set of results. Setting a limit may return fewer than the requested amount of items (up to zero items) in the event all requested objects are filtered out and clients should only use the presence of the continue field to determine whether more results are available. Servers may choose not to support the limit argument and will return all of the available results. If limit is specified and the continue field is empty, clients may assume that no more results are available. This field is not supported if watch is true.\n\nThe server guarantees that the objects returned when using continue will be identical to issuing a single list call without a limit - that is, no objects created, modified, or deleted after the first request is issued will be included in any subsequent continued requests. This is sometimes referred to as a consistent snapshot, and ensures that a client that is using limit to receive smaller chunks of a very large result can ensure they see all possible objects. If objects are updated during a chunked list the version of the object that was present at the time the first list result was calculated is returned.",
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "namespace",
|
||||
"in": "path",
|
||||
"description": "object name and auth scope, such as for teams and projects",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "pretty",
|
||||
"in": "query",
|
||||
"description": "If 'true', then the output is pretty printed. Defaults to 'false' unless the user-agent indicates a browser or command-line HTTP tool (curl and wget).",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "resourceVersion",
|
||||
"in": "query",
|
||||
"description": "resourceVersion sets a constraint on what resource versions a request may be served from. See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details.\n\nDefaults to unset",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "resourceVersionMatch",
|
||||
"in": "query",
|
||||
"description": "resourceVersionMatch determines how resourceVersion is applied to list calls. It is highly recommended that resourceVersionMatch be set for list calls where resourceVersion is set See https://kubernetes.io/docs/reference/using-api/api-concepts/#resource-versions for details.\n\nDefaults to unset",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "sendInitialEvents",
|
||||
"in": "query",
|
||||
"description": "`sendInitialEvents=true` may be set together with `watch=true`. In that case, the watch stream will begin with synthetic events to produce the current state of objects in the collection. Once all such events have been sent, a synthetic \"Bookmark\" event will be sent. The bookmark will report the ResourceVersion (RV) corresponding to the set of objects, and be marked with `\"k8s.io/initial-events-end\": \"true\"` annotation. Afterwards, the watch stream will proceed as usual, sending watch events corresponding to changes (subsequent to the RV) to objects watched.\n\nWhen `sendInitialEvents` option is set, we require `resourceVersionMatch` option to also be set. The semantic of the watch request is as following: - `resourceVersionMatch` = NotOlderThan\n is interpreted as \"data at least as new as the provided `resourceVersion`\"\n and the bookmark event is send when the state is synced\n to a `resourceVersion` at least as fresh as the one provided by the ListOptions.\n If `resourceVersion` is unset, this is interpreted as \"consistent read\" and the\n bookmark event is send when the state is synced at least to the moment\n when request started being processed.\n- `resourceVersionMatch` set to any other value or unset\n Invalid error is returned.\n\nDefaults to true if `resourceVersion=\"\"` or `resourceVersion=\"0\"` (for backward compatibility reasons) and to false otherwise.",
|
||||
"schema": {
|
||||
"type": "boolean",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "timeoutSeconds",
|
||||
"in": "query",
|
||||
"description": "Timeout for the list/watch call. This limits the duration of the call, regardless of any activity or inactivity.",
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "watch",
|
||||
"in": "query",
|
||||
"description": "Watch for changes to the described resources and return them as a stream of add, update, and remove notifications. Specify resourceVersion.",
|
||||
"schema": {
|
||||
"type": "boolean",
|
||||
"uniqueItems": true
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"/apis/provisioning.grafana.app/v0alpha1/namespaces/{namespace}/jobs/{name}": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"Job"
|
||||
],
|
||||
"description": "read the specified Job",
|
||||
"operationId": "getJob",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.Job"
|
||||
}
|
||||
},
|
||||
"application/vnd.kubernetes.protobuf": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.Job"
|
||||
}
|
||||
},
|
||||
"application/yaml": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.Job"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"x-kubernetes-action": "get",
|
||||
"x-kubernetes-group-version-kind": {
|
||||
"group": "provisioning.grafana.app",
|
||||
"version": "v0alpha1",
|
||||
"kind": "Job"
|
||||
}
|
||||
},
|
||||
"parameters": [
|
||||
{
|
||||
"name": "name",
|
||||
"in": "path",
|
||||
"description": "name of the Job",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "namespace",
|
||||
"in": "path",
|
||||
"description": "object name and auth scope, such as for teams and projects",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"uniqueItems": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "pretty",
|
||||
"in": "query",
|
||||
"description": "If 'true', then the output is pretty printed. Defaults to 'false' unless the user-agent indicates a browser or command-line HTTP tool (curl and wget).",
|
||||
"schema": {
|
||||
"type": "string",
|
||||
"uniqueItems": true
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"/apis/provisioning.grafana.app/v0alpha1/namespaces/{namespace}/repositories": {
|
||||
"get": {
|
||||
"tags": [
|
||||
@@ -1295,17 +1669,46 @@
|
||||
},
|
||||
"components": {
|
||||
"schemas": {
|
||||
"com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.GitHubRepositoryConfig": {
|
||||
"com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.ExportJobOptions": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"identifier"
|
||||
],
|
||||
"properties": {
|
||||
"branch": {
|
||||
"description": "The branch to use in the repository. By default, this is the main branch.",
|
||||
"description": "Target branch for export (only git)",
|
||||
"type": "string"
|
||||
},
|
||||
"branchWorkflow": {
|
||||
"description": "Whether we should commit to change branches and use a Pull Request flow to achieve this. By default, this is false (i.e. we will commit straight to the main branch).",
|
||||
"folder": {
|
||||
"description": "The source folder (or empty) to export",
|
||||
"type": "string"
|
||||
},
|
||||
"history": {
|
||||
"description": "Preserve history (if possible)",
|
||||
"type": "boolean"
|
||||
},
|
||||
"identifier": {
|
||||
"description": "Include the identifier in the exported metadata",
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
},
|
||||
"prefix": {
|
||||
"description": "Target file prefix",
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.GitHubRepositoryConfig": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"branch"
|
||||
],
|
||||
"properties": {
|
||||
"branch": {
|
||||
"description": "The branch to use in the repository.",
|
||||
"type": "string",
|
||||
"default": ""
|
||||
},
|
||||
"encryptedToken": {
|
||||
"description": "Token for accessing the repository, but encrypted. This is not possible to read back to a user decrypted.",
|
||||
"type": "string",
|
||||
@@ -1323,18 +1726,6 @@
|
||||
"url": {
|
||||
"description": "The repository URL (e.g. `https://github.com/example/test`).",
|
||||
"type": "string"
|
||||
},
|
||||
"workflows": {
|
||||
"description": "Workflow allowed for changes to the repository. The order is relevant for defining the precedence of the workflows. Possible values: pull-request, branch, push.",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string",
|
||||
"default": "",
|
||||
"enum": [
|
||||
"branch",
|
||||
"push"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -1365,6 +1756,243 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.Job": {
|
||||
"description": "The repository name and type are stored as labels",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"apiVersion": {
|
||||
"description": "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources",
|
||||
"type": "string"
|
||||
},
|
||||
"kind": {
|
||||
"description": "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds",
|
||||
"type": "string"
|
||||
},
|
||||
"metadata": {
|
||||
"default": {},
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta"
|
||||
}
|
||||
]
|
||||
},
|
||||
"spec": {
|
||||
"default": {},
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobSpec"
|
||||
}
|
||||
]
|
||||
},
|
||||
"status": {
|
||||
"default": {},
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobStatus"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"x-kubernetes-group-version-kind": [
|
||||
{
|
||||
"group": "provisioning.grafana.app",
|
||||
"kind": "Job",
|
||||
"version": "__internal"
|
||||
},
|
||||
{
|
||||
"group": "provisioning.grafana.app",
|
||||
"kind": "Job",
|
||||
"version": "v0alpha1"
|
||||
}
|
||||
]
|
||||
},
|
||||
"com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobList": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"apiVersion": {
|
||||
"description": "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources",
|
||||
"type": "string"
|
||||
},
|
||||
"items": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"default": {},
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.Job"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"kind": {
|
||||
"description": "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds",
|
||||
"type": "string"
|
||||
},
|
||||
"metadata": {
|
||||
"default": {},
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ListMeta"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"x-kubernetes-group-version-kind": [
|
||||
{
|
||||
"group": "provisioning.grafana.app",
|
||||
"kind": "JobList",
|
||||
"version": "__internal"
|
||||
},
|
||||
{
|
||||
"group": "provisioning.grafana.app",
|
||||
"kind": "JobList",
|
||||
"version": "v0alpha1"
|
||||
}
|
||||
]
|
||||
},
|
||||
"com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobResourceSummary": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"create": {
|
||||
"type": "integer",
|
||||
"format": "int64"
|
||||
},
|
||||
"delete": {
|
||||
"type": "integer",
|
||||
"format": "int64"
|
||||
},
|
||||
"error": {
|
||||
"description": "Create or update (export)",
|
||||
"type": "integer",
|
||||
"format": "int64"
|
||||
},
|
||||
"errors": {
|
||||
"description": "Report errors for this resource type This may not be an exhaustive list and recommend looking at the logs for more info",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string",
|
||||
"default": ""
|
||||
}
|
||||
},
|
||||
"group": {
|
||||
"type": "string"
|
||||
},
|
||||
"noop": {
|
||||
"description": "No action required (useful for sync)",
|
||||
"type": "integer",
|
||||
"format": "int64"
|
||||
},
|
||||
"resource": {
|
||||
"type": "string"
|
||||
},
|
||||
"total": {
|
||||
"type": "integer",
|
||||
"format": "int64"
|
||||
},
|
||||
"update": {
|
||||
"type": "integer",
|
||||
"format": "int64"
|
||||
},
|
||||
"write": {
|
||||
"type": "integer",
|
||||
"format": "int64"
|
||||
}
|
||||
}
|
||||
},
|
||||
"com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobSpec": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"action",
|
||||
"repository"
|
||||
],
|
||||
"properties": {
|
||||
"action": {
|
||||
"description": "Possible enum values:\n - `\"export\"` Export from grafana into the remote repository\n - `\"pr\"` Update a pull request -- send preview images, links etc\n - `\"sync\"` Sync the remote branch with the grafana instance",
|
||||
"type": "string",
|
||||
"default": "",
|
||||
"enum": [
|
||||
"export",
|
||||
"pr",
|
||||
"sync"
|
||||
]
|
||||
},
|
||||
"export": {
|
||||
"description": "Required when the action is `export`",
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.ExportJobOptions"
|
||||
}
|
||||
]
|
||||
},
|
||||
"pr": {
|
||||
"description": "Pull request options",
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.PullRequestJobOptions"
|
||||
}
|
||||
]
|
||||
},
|
||||
"repository": {
|
||||
"description": "The the repository reference (for now also in labels)",
|
||||
"type": "string",
|
||||
"default": ""
|
||||
},
|
||||
"sync": {
|
||||
"description": "Required when the action is `sync`",
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.SyncJobOptions"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobStatus": {
|
||||
"description": "The job status",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"errors": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string",
|
||||
"default": ""
|
||||
}
|
||||
},
|
||||
"finished": {
|
||||
"type": "integer",
|
||||
"format": "int64"
|
||||
},
|
||||
"message": {
|
||||
"type": "string"
|
||||
},
|
||||
"progress": {
|
||||
"description": "Optional value 0-100 that can be set while running",
|
||||
"type": "number",
|
||||
"format": "double"
|
||||
},
|
||||
"started": {
|
||||
"type": "integer",
|
||||
"format": "int64"
|
||||
},
|
||||
"state": {
|
||||
"description": "Possible enum values:\n - `\"error\"` Finished with errors\n - `\"pending\"` Job has been submitted, but not processed yet\n - `\"success\"` Finished with success\n - `\"working\"` The job is running",
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"error",
|
||||
"pending",
|
||||
"success",
|
||||
"working"
|
||||
]
|
||||
},
|
||||
"summary": {
|
||||
"description": "Summary of processed actions",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.JobResourceSummary"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.LocalRepositoryConfig": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
@@ -1373,6 +2001,27 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.PullRequestJobOptions": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"hash": {
|
||||
"type": "string"
|
||||
},
|
||||
"pr": {
|
||||
"description": "Pull request number (when appropriate)",
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
},
|
||||
"ref": {
|
||||
"description": "The branch of commit hash",
|
||||
"type": "string"
|
||||
},
|
||||
"url": {
|
||||
"description": "URL to the originator (eg, PR URL)",
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.Repository": {
|
||||
"description": "When this code is changed, make sure to update the code generation. As of writing, this can be done via the hack dir in the root of the repo: ./hack/update-codegen.sh provisioning If you've opened the generated files in this dir at some point in VSCode, you may also have to re-open them to clear errors.",
|
||||
"type": "object",
|
||||
@@ -1472,7 +2121,7 @@
|
||||
"type": "object",
|
||||
"required": [
|
||||
"title",
|
||||
"readOnly",
|
||||
"workflows",
|
||||
"sync",
|
||||
"type"
|
||||
],
|
||||
@@ -1497,11 +2146,6 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"readOnly": {
|
||||
"description": "ReadOnly repository does not allow any write commands",
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
},
|
||||
"sync": {
|
||||
"description": "Sync settings -- how values are pulled from the repository into grafana",
|
||||
"default": {},
|
||||
@@ -1524,6 +2168,18 @@
|
||||
"github",
|
||||
"local"
|
||||
]
|
||||
},
|
||||
"workflows": {
|
||||
"description": "UI driven Workflow that allow changes to the contends of the repository. The order is relevant for defining the precedence of the workflows. When empty, the repository does not support any edits (eg, readonly)",
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string",
|
||||
"default": "",
|
||||
"enum": [
|
||||
"branch",
|
||||
"write"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -1610,6 +2266,19 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.SyncJobOptions": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"incremental"
|
||||
],
|
||||
"properties": {
|
||||
"incremental": {
|
||||
"description": "Incremental synchronization for versioned repositories",
|
||||
"type": "boolean",
|
||||
"default": false
|
||||
}
|
||||
}
|
||||
},
|
||||
"com.github.grafana.grafana.pkg.apis.provisioning.v0alpha1.SyncOptions": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
@@ -1650,10 +2319,6 @@
|
||||
"type": "integer",
|
||||
"format": "int64"
|
||||
},
|
||||
"hash": {
|
||||
"description": "The repository hash when the last sync ran",
|
||||
"type": "string"
|
||||
},
|
||||
"incremental": {
|
||||
"description": "Incremental synchronization for versioned repositories",
|
||||
"type": "boolean"
|
||||
@@ -1662,6 +2327,10 @@
|
||||
"description": "The ID for the job that ran this sync",
|
||||
"type": "string"
|
||||
},
|
||||
"lastRef": {
|
||||
"description": "The repository ref when the last successful sync ran",
|
||||
"type": "string"
|
||||
},
|
||||
"message": {
|
||||
"description": "Summary messages (will be shown to users)",
|
||||
"type": "array",
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
"github": {
|
||||
"url": "https://github.com/grafana/git-ui-sync-demo",
|
||||
"branch": "dummy-branch",
|
||||
"branchWorkflow": true,
|
||||
"generateDashboardPreviews": true,
|
||||
"token": "github_pat_dummy"
|
||||
},
|
||||
@@ -20,6 +19,6 @@
|
||||
"target": "",
|
||||
"intervalSeconds": 60
|
||||
},
|
||||
"readOnly": false
|
||||
"workflows": ["push"]
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -7,7 +7,7 @@
|
||||
"spec": {
|
||||
"title": "Load devenv dashboards",
|
||||
"description": "Load /devenv/dev-dashboards (from root of repository)",
|
||||
"readOnly": false,
|
||||
"workflows": ["write"],
|
||||
"sync": {
|
||||
"enabled": true,
|
||||
"target": "mirror",
|
||||
|
||||
@@ -1833,10 +1833,16 @@
|
||||
{
|
||||
"type": "integer",
|
||||
"format": "int64",
|
||||
"description": "Find annotations for a specified alert.",
|
||||
"description": "Find annotations for a specified alert rule by its ID.\ndeprecated: AlertID is deprecated and will be removed in future versions. Please use AlertUID instead.",
|
||||
"name": "alertId",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "Find annotations for a specified alert rule by its UID.",
|
||||
"name": "alertUID",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"format": "int64",
|
||||
|
||||
+4
-1
@@ -205,7 +205,10 @@ export class GrafanaApp {
|
||||
setDataSourceSrv(dataSourceSrv);
|
||||
initWindowRuntime();
|
||||
|
||||
if (contextSrv.user.orgRole !== '') {
|
||||
// Do not pre-load apps if rendererDisableAppPluginsPreload is true and the request comes from the image renderer
|
||||
const skipAppPluginsPreload =
|
||||
config.featureToggles.rendererDisableAppPluginsPreload && contextSrv.user.authenticatedBy === 'render';
|
||||
if (contextSrv.user.orgRole !== '' && !skipAppPluginsPreload) {
|
||||
const appPluginsToAwait = getAppPluginsToAwait();
|
||||
const appPluginsToPreload = getAppPluginsToPreload();
|
||||
|
||||
|
||||
@@ -18,13 +18,14 @@ import {
|
||||
Combobox,
|
||||
ComboboxOption,
|
||||
TextLink,
|
||||
WeekStart,
|
||||
isWeekStart,
|
||||
} from '@grafana/ui';
|
||||
import { DashboardPicker } from 'app/core/components/Select/DashboardPicker';
|
||||
import { t, Trans } from 'app/core/internationalization';
|
||||
import { LANGUAGES, PSEUDO_LOCALE } from 'app/core/internationalization/constants';
|
||||
import { PreferencesService } from 'app/core/services/PreferencesService';
|
||||
import { changeTheme } from 'app/core/services/theme';
|
||||
|
||||
export interface Props {
|
||||
resourceUri: string;
|
||||
disabled?: boolean;
|
||||
@@ -152,8 +153,8 @@ export class SharedPreferences extends PureComponent<Props, State> {
|
||||
this.setState({ timezone: timezone });
|
||||
};
|
||||
|
||||
onWeekStartChanged = (weekStart: string) => {
|
||||
this.setState({ weekStart: weekStart });
|
||||
onWeekStartChanged = (weekStart?: WeekStart) => {
|
||||
this.setState({ weekStart: weekStart ?? '' });
|
||||
};
|
||||
|
||||
onHomeDashboardChanged = (dashboardUID: string) => {
|
||||
@@ -249,7 +250,7 @@ export class SharedPreferences extends PureComponent<Props, State> {
|
||||
data-testid={selectors.components.WeekStartPicker.containerV2}
|
||||
>
|
||||
<WeekStartPicker
|
||||
value={weekStart || ''}
|
||||
value={weekStart && isWeekStart(weekStart) ? weekStart : undefined}
|
||||
onChange={this.onWeekStartChanged}
|
||||
inputId="shared-preferences-week-start-picker"
|
||||
/>
|
||||
|
||||
@@ -29,6 +29,26 @@ export class ThresholdMapper {
|
||||
thresholds.push({ value: value, op: 'lt', visible });
|
||||
break;
|
||||
}
|
||||
case 'eq': {
|
||||
const value = evaluator.params[0];
|
||||
thresholds.push({ value: value, op: 'eq', visible });
|
||||
break;
|
||||
}
|
||||
case 'ne': {
|
||||
const value = evaluator.params[0];
|
||||
thresholds.push({ value: value, op: 'ne', visible });
|
||||
break;
|
||||
}
|
||||
case 'gte': {
|
||||
const value = evaluator.params[0];
|
||||
thresholds.push({ value: value, op: 'ge', visible });
|
||||
break;
|
||||
}
|
||||
case 'lte': {
|
||||
const value = evaluator.params[0];
|
||||
thresholds.push({ value: value, op: 'le', visible });
|
||||
break;
|
||||
}
|
||||
case 'outside_range': {
|
||||
const value1 = evaluator.params[0];
|
||||
const value2 = evaluator.params[1];
|
||||
@@ -56,6 +76,33 @@ export class ThresholdMapper {
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'outside_range_included': {
|
||||
const value1 = evaluator.params[0];
|
||||
const value2 = evaluator.params[1];
|
||||
|
||||
if (value1 >= value2) {
|
||||
thresholds.push({ value: value1, op: 'ge', visible });
|
||||
thresholds.push({ value: value2, op: 'le', visible });
|
||||
} else {
|
||||
thresholds.push({ value: value1, op: 'le', visible });
|
||||
thresholds.push({ value: value2, op: 'ge', visible });
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case 'within_range_included': {
|
||||
const value1 = evaluator.params[0];
|
||||
const value2 = evaluator.params[1];
|
||||
|
||||
if (value1 >= value2) {
|
||||
thresholds.push({ value: value1, op: 'le', visible });
|
||||
thresholds.push({ value: value2, op: 'ge', visible });
|
||||
} else {
|
||||
thresholds.push({ value: value1, op: 'ge', visible });
|
||||
thresholds.push({ value: value2, op: 'le', visible });
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -32,16 +32,28 @@ const alertStateSortScore = {
|
||||
export enum EvalFunction {
|
||||
'IsAbove' = 'gt',
|
||||
'IsBelow' = 'lt',
|
||||
'IsEqual' = 'eq',
|
||||
'IsNotEqual' = 'ne',
|
||||
'IsGreaterThanEqual' = 'gte',
|
||||
'IsLessThanEqual' = 'lte',
|
||||
'IsOutsideRange' = 'outside_range',
|
||||
'IsWithinRange' = 'within_range',
|
||||
'IsWithinRangeIncluded' = 'within_range_included',
|
||||
'IsOutsideRangeIncluded' = 'outside_range_included',
|
||||
'HasNoValue' = 'no_value',
|
||||
}
|
||||
|
||||
const evalFunctions = [
|
||||
{ value: EvalFunction.IsAbove, text: 'IS ABOVE' },
|
||||
{ value: EvalFunction.IsBelow, text: 'IS BELOW' },
|
||||
{ value: EvalFunction.IsEqual, text: 'IS EQUAL TO' },
|
||||
{ value: EvalFunction.IsNotEqual, text: 'IS NOT EQUAL TO' },
|
||||
{ value: EvalFunction.IsGreaterThanEqual, text: 'IS ABOVE OR EQUAL TO' },
|
||||
{ value: EvalFunction.IsLessThanEqual, text: 'IS BELOW OR EQUAL TO' },
|
||||
{ value: EvalFunction.IsOutsideRange, text: 'IS OUTSIDE RANGE' },
|
||||
{ value: EvalFunction.IsWithinRange, text: 'IS WITHIN RANGE' },
|
||||
{ value: EvalFunction.IsOutsideRangeIncluded, text: 'IS OUTSIDE RANGE INCLUDED' },
|
||||
{ value: EvalFunction.IsWithinRangeIncluded, text: 'IS WITHIN RANGE INCLUDED' },
|
||||
{ value: EvalFunction.HasNoValue, text: 'HAS NO VALUE' },
|
||||
];
|
||||
|
||||
|
||||
@@ -538,5 +538,10 @@ const getCommonQueryStyles = (theme: GrafanaTheme2) => ({
|
||||
});
|
||||
|
||||
function isRangeEvaluator(evaluator: { params: number[]; type: EvalFunction }) {
|
||||
return evaluator.type === EvalFunction.IsWithinRange || evaluator.type === EvalFunction.IsOutsideRange;
|
||||
return (
|
||||
evaluator.type === EvalFunction.IsWithinRange ||
|
||||
evaluator.type === EvalFunction.IsOutsideRange ||
|
||||
evaluator.type === EvalFunction.IsOutsideRangeIncluded ||
|
||||
evaluator.type === EvalFunction.IsWithinRangeIncluded
|
||||
);
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ describe('annotations', () => {
|
||||
it('should fetch annotation for an alertId', () => {
|
||||
const ALERT_ID = 'abc123';
|
||||
fetchAnnotations(ALERT_ID);
|
||||
expect(get).toBeCalledWith('/api/annotations', { alertId: ALERT_ID });
|
||||
expect(get).toBeCalledWith('/api/annotations', { alertUID: ALERT_ID });
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { getBackendSrv } from '@grafana/runtime';
|
||||
import { StateHistoryItem } from 'app/types/unified-alerting';
|
||||
|
||||
export function fetchAnnotations(alertId: string): Promise<StateHistoryItem[]> {
|
||||
export function fetchAnnotations(alertUID: string): Promise<StateHistoryItem[]> {
|
||||
return getBackendSrv()
|
||||
.get('/api/annotations', {
|
||||
alertId,
|
||||
alertUID,
|
||||
})
|
||||
.then((result) => {
|
||||
return result?.sort(sortStateHistory);
|
||||
|
||||
@@ -279,6 +279,53 @@ export function getThresholdsForQueries(queries: AlertQuery[], condition: string
|
||||
);
|
||||
}
|
||||
|
||||
if (type === EvalFunction.IsWithinRangeIncluded) {
|
||||
thresholds[refId].config.steps.push(
|
||||
...[
|
||||
{
|
||||
value: -Infinity,
|
||||
color: 'transparent',
|
||||
},
|
||||
{
|
||||
value: values[0],
|
||||
color: config.theme2.colors.error.main,
|
||||
},
|
||||
{
|
||||
value: values[1],
|
||||
color: config.theme2.colors.error.main,
|
||||
},
|
||||
{
|
||||
value: values[1],
|
||||
color: 'transparent',
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
if (type === EvalFunction.IsOutsideRangeIncluded) {
|
||||
thresholds[refId].config.steps.push(
|
||||
...[
|
||||
{
|
||||
value: -Infinity,
|
||||
color: config.theme2.colors.error.main,
|
||||
},
|
||||
// we have to duplicate this value, or the graph will not display the handle in the right color
|
||||
{
|
||||
value: values[0],
|
||||
color: config.theme2.colors.error.main,
|
||||
},
|
||||
{
|
||||
value: values[0],
|
||||
color: 'transparent',
|
||||
},
|
||||
{
|
||||
value: values[1],
|
||||
color: config.theme2.colors.error.main,
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
// now also sort the threshold values, if we don't then they will look weird in the time series panel
|
||||
// TODO this doesn't work for negative values for now, those need to be sorted inverse
|
||||
thresholds[refId].config.steps.sort((a, b) => a.value - b.value);
|
||||
@@ -292,7 +339,10 @@ export function getThresholdsForQueries(queries: AlertQuery[], condition: string
|
||||
|
||||
function isRangeCondition(condition: ClassicCondition) {
|
||||
return (
|
||||
condition.evaluator.type === EvalFunction.IsWithinRange || condition.evaluator.type === EvalFunction.IsOutsideRange
|
||||
condition.evaluator.type === EvalFunction.IsWithinRange ||
|
||||
condition.evaluator.type === EvalFunction.IsOutsideRange ||
|
||||
condition.evaluator.type === EvalFunction.IsOutsideRangeIncluded ||
|
||||
condition.evaluator.type === EvalFunction.IsWithinRangeIncluded
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -26,13 +26,12 @@ const History = ({ rule }: HistoryProps) => {
|
||||
? StateHistoryImplementation.Loki
|
||||
: StateHistoryImplementation.Annotations;
|
||||
|
||||
const ruleID = rule.grafana_alert.id ?? '';
|
||||
const ruleUID = rule.grafana_alert.uid;
|
||||
|
||||
return (
|
||||
<Suspense fallback={'Loading...'}>
|
||||
{implementation === StateHistoryImplementation.Loki && <LokiStateHistory ruleUID={ruleUID} />}
|
||||
{implementation === StateHistoryImplementation.Annotations && <AnnotationsStateHistory alertId={ruleID} />}
|
||||
{implementation === StateHistoryImplementation.Annotations && <AnnotationsStateHistory ruleUID={ruleUID} />}
|
||||
</Suspense>
|
||||
);
|
||||
};
|
||||
|
||||
+3
-3
@@ -27,16 +27,16 @@ type StateHistoryMap = Record<string, StateHistoryRowItem[]>;
|
||||
type StateHistoryRow = DynamicTableItemProps<StateHistoryRowItem>;
|
||||
|
||||
interface Props {
|
||||
alertId: string;
|
||||
ruleUID: string;
|
||||
}
|
||||
|
||||
const StateHistory = ({ alertId }: Props) => {
|
||||
const StateHistory = ({ ruleUID }: Props) => {
|
||||
const [textFilter, setTextFilter] = useState<string>('');
|
||||
const handleTextFilter = useCallback((event: FormEvent<HTMLInputElement>) => {
|
||||
setTextFilter(event.currentTarget.value);
|
||||
}, []);
|
||||
|
||||
const { loading, error, result = [] } = useManagedAlertStateHistory(alertId);
|
||||
const { loading, error, result = [] } = useManagedAlertStateHistory(ruleUID);
|
||||
|
||||
const styles = useStyles2(getStyles);
|
||||
|
||||
|
||||
@@ -1,35 +1,23 @@
|
||||
import { useEffect } from 'react';
|
||||
import { skipToken } from '@reduxjs/toolkit/query/react';
|
||||
|
||||
import { FolderDTO, useDispatch } from 'app/types';
|
||||
|
||||
import { fetchFolderIfNotFetchedAction } from '../state/actions';
|
||||
import { initialAsyncRequestState } from '../utils/redux';
|
||||
|
||||
import { useUnifiedAlertingSelector } from './useUnifiedAlertingSelector';
|
||||
import { useGetFolderQuery } from 'app/features/browse-dashboards/api/browseDashboardsAPI';
|
||||
import { FolderDTO } from 'app/types';
|
||||
|
||||
interface ReturnBag {
|
||||
folder?: FolderDTO;
|
||||
loading: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a folderDTO for the given uid – uses cached values
|
||||
* @TODO propagate error state
|
||||
*/
|
||||
export function useFolder(uid?: string): ReturnBag {
|
||||
const dispatch = useDispatch();
|
||||
const folderRequests = useUnifiedAlertingSelector((state) => state.folders);
|
||||
useEffect(() => {
|
||||
if (uid) {
|
||||
dispatch(fetchFolderIfNotFetchedAction(uid));
|
||||
}
|
||||
}, [dispatch, uid]);
|
||||
const fetchFolderState = useGetFolderQuery(uid || skipToken);
|
||||
|
||||
if (uid) {
|
||||
const request = folderRequests[uid] || initialAsyncRequestState;
|
||||
return {
|
||||
folder: request.result,
|
||||
loading: request.loading,
|
||||
};
|
||||
}
|
||||
return {
|
||||
loading: false,
|
||||
loading: fetchFolderState.isLoading,
|
||||
folder: fetchFolderState.data,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -39,6 +27,6 @@ export function stringifyFolder({ title, parents }: FolderDTO) {
|
||||
: encodeTitle(title);
|
||||
}
|
||||
|
||||
export function encodeTitle(title: string): string {
|
||||
function encodeTitle(title: string): string {
|
||||
return title.replaceAll('/', '\\/');
|
||||
}
|
||||
|
||||
@@ -8,15 +8,15 @@ import { AsyncRequestState } from '../utils/redux';
|
||||
|
||||
import { useUnifiedAlertingSelector } from './useUnifiedAlertingSelector';
|
||||
|
||||
export function useManagedAlertStateHistory(alertId: string) {
|
||||
export function useManagedAlertStateHistory(ruleUID: string) {
|
||||
const dispatch = useDispatch();
|
||||
const history = useUnifiedAlertingSelector<AsyncRequestState<StateHistoryItem[]>>(
|
||||
(state) => state.managedAlertStateHistory
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
dispatch(fetchGrafanaAnnotationsAction(alertId));
|
||||
}, [dispatch, alertId]);
|
||||
dispatch(fetchGrafanaAnnotationsAction(ruleUID));
|
||||
}, [dispatch, ruleUID]);
|
||||
|
||||
return history;
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ function useStateHistoryModal() {
|
||||
<Suspense fallback={'Loading...'}>
|
||||
{implementation === StateHistoryImplementation.Loki && <LokiStateHistory ruleUID={rule.grafana_alert.uid} />}
|
||||
{implementation === StateHistoryImplementation.Annotations && (
|
||||
<AnnotationsStateHistory alertId={rule.grafana_alert.id ?? ''} />
|
||||
<AnnotationsStateHistory ruleUID={rule.grafana_alert.uid ?? ''} />
|
||||
)}
|
||||
</Suspense>
|
||||
</Modal>
|
||||
|
||||
@@ -10,11 +10,10 @@ import {
|
||||
Receiver,
|
||||
TestReceiversAlert,
|
||||
} from 'app/plugins/datasource/alertmanager/types';
|
||||
import { FolderDTO, ThunkResult } from 'app/types';
|
||||
import { ThunkResult } from 'app/types';
|
||||
import { RuleIdentifier, RuleNamespace, StateHistoryItem } from 'app/types/unified-alerting';
|
||||
import { RulerRuleDTO, RulerRulesConfigDTO } from 'app/types/unified-alerting-dto';
|
||||
|
||||
import { backendSrv } from '../../../../core/services/backend_srv';
|
||||
import { withPromRulesMetadataLogging, withRulerRulesMetadataLogging } from '../Analytics';
|
||||
import {
|
||||
deleteAlertManagerConfig,
|
||||
@@ -183,7 +182,7 @@ export function fetchAllPromRulesAction(
|
||||
|
||||
export const fetchGrafanaAnnotationsAction = createAsyncThunk(
|
||||
'unifiedalerting/fetchGrafanaAnnotations',
|
||||
(alertId: string): Promise<StateHistoryItem[]> => withSerializedError(fetchAnnotations(alertId))
|
||||
(ruleUID: string): Promise<StateHistoryItem[]> => withSerializedError(fetchAnnotations(ruleUID))
|
||||
);
|
||||
|
||||
interface UpdateAlertManagerConfigActionOptions {
|
||||
@@ -241,19 +240,6 @@ export const updateAlertManagerConfigAction = createAsyncThunk<void, UpdateAlert
|
||||
)
|
||||
);
|
||||
|
||||
export const fetchFolderAction = createAsyncThunk(
|
||||
'unifiedalerting/fetchFolder',
|
||||
(uid: string): Promise<FolderDTO> => withSerializedError(backendSrv.getFolderByUid(uid, { withAccessControl: true }))
|
||||
);
|
||||
|
||||
export const fetchFolderIfNotFetchedAction = (uid: string): ThunkResult<void> => {
|
||||
return (dispatch, getState) => {
|
||||
if (!getState().unifiedAlerting.folders[uid]?.dispatched) {
|
||||
dispatch(fetchFolderAction(uid));
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export const fetchAlertGroupsAction = createAsyncThunk(
|
||||
'unifiedalerting/fetchAlertGroups',
|
||||
(alertManagerSourceName: string): Promise<AlertmanagerGroup[]> => {
|
||||
|
||||
@@ -5,7 +5,6 @@ import { createAsyncMapSlice, createAsyncSlice } from '../utils/redux';
|
||||
import {
|
||||
deleteAlertManagerConfigAction,
|
||||
fetchAlertGroupsAction,
|
||||
fetchFolderAction,
|
||||
fetchGrafanaAnnotationsAction,
|
||||
fetchPromRulesAction,
|
||||
fetchRulerRulesAction,
|
||||
@@ -19,7 +18,6 @@ export const reducer = combineReducers({
|
||||
.reducer,
|
||||
saveAMConfig: createAsyncSlice('saveAMConfig', updateAlertManagerConfigAction).reducer,
|
||||
deleteAMConfig: createAsyncSlice('deleteAMConfig', deleteAlertManagerConfigAction).reducer,
|
||||
folders: createAsyncMapSlice('folders', fetchFolderAction, (uid) => uid).reducer,
|
||||
amAlertGroups: createAsyncMapSlice(
|
||||
'amAlertGroups',
|
||||
fetchAlertGroupsAction,
|
||||
|
||||
@@ -400,6 +400,14 @@ export const cloudNotifierTypes: Array<NotifierDTO<CloudNotifierType>> = [
|
||||
},
|
||||
}
|
||||
),
|
||||
option(
|
||||
'timeout',
|
||||
'Timeout',
|
||||
'The maximum time to wait for a webhook request to complete, before failing the request and allowing it to be retried. The default value of 0s indicates that no timeout should be applied. NOTE: This will have no effect if set higher than the group_interval.',
|
||||
{
|
||||
placeholder: 'Use duration format, for example: 1.2s, 100ms',
|
||||
}
|
||||
),
|
||||
httpConfigOption,
|
||||
],
|
||||
},
|
||||
|
||||
@@ -446,6 +446,7 @@ export const {
|
||||
useDeleteItemsMutation,
|
||||
useGetAffectedItemsQuery,
|
||||
useGetFolderQuery,
|
||||
useLazyGetFolderQuery,
|
||||
useMoveFolderMutation,
|
||||
useMoveItemsMutation,
|
||||
useNewFolderMutation,
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
import { selectors } from '@grafana/e2e-selectors';
|
||||
import { Box, Card, Icon } from '@grafana/ui';
|
||||
import { t, Trans } from 'app/core/internationalization';
|
||||
|
||||
import { DashboardInteractions } from '../utils/interactions';
|
||||
import { getDashboardSceneFor } from '../utils/utils';
|
||||
|
||||
import { DashboardEditPane } from './DashboardEditPane';
|
||||
|
||||
export interface Props {
|
||||
editPane: DashboardEditPane;
|
||||
}
|
||||
|
||||
export function DashboardAddPane({ editPane }: Props) {
|
||||
const dashboard = getDashboardSceneFor(editPane);
|
||||
|
||||
return (
|
||||
<Box display={'flex'} direction={'column'} gap={1} padding={2}>
|
||||
<Card
|
||||
onClick={() => dashboard.onCreateNewPanel()}
|
||||
data-testid={selectors.components.PageToolbar.itemButton('add_visualization')}
|
||||
title={t('dashboard.toolbar.add-panel-description', 'A container for visualizations and other content')}
|
||||
>
|
||||
<Card.Heading>
|
||||
<Trans i18nKey="dashboard.toolbar.add-panel">Panel</Trans>
|
||||
</Card.Heading>
|
||||
<Card.Figure>
|
||||
<Icon name="graph-bar" size="xl" />
|
||||
</Card.Figure>
|
||||
</Card>
|
||||
<Card
|
||||
onClick={() => {
|
||||
dashboard.onShowAddLibraryPanelDrawer();
|
||||
DashboardInteractions.toolbarAddButtonClicked({ item: 'add_library_panel' });
|
||||
}}
|
||||
data-testid={selectors.pages.AddDashboard.itemButton('Add new panel from panel library menu item')}
|
||||
title={t(
|
||||
'dashboard.toolbar.libray-panel-description',
|
||||
'Libray panels allow you share and reuse panels between dashboards'
|
||||
)}
|
||||
>
|
||||
<Card.Heading>
|
||||
<Trans i18nKey="dashboard.toolbar.add-panel-lib">Import library panel</Trans>
|
||||
</Card.Heading>
|
||||
<Card.Figure>
|
||||
<Icon name="import" size="xl" />
|
||||
</Card.Figure>
|
||||
</Card>
|
||||
<Card
|
||||
onClick={() => dashboard.onCreateNewRow()}
|
||||
data-testid={selectors.components.PageToolbar.itemButton('add_row')}
|
||||
title={t('dashboard.toolbar.row-description', 'A group of panels with an optional header')}
|
||||
>
|
||||
<Card.Heading>
|
||||
<Trans i18nKey="dashboard.toolbar.add-row">Row</Trans>
|
||||
</Card.Heading>
|
||||
<Card.Figure>
|
||||
<Icon name="list-ul" size="xl" />
|
||||
</Card.Figure>
|
||||
</Card>
|
||||
<Card
|
||||
onClick={() => dashboard.onCreateNewTab()}
|
||||
data-testid={selectors.components.PageToolbar.itemButton('add_tab')}
|
||||
title={t('dashboard.toolbar.tabs-description', 'Break up your dashboard into different horizontal tabs')}
|
||||
>
|
||||
<Card.Heading>
|
||||
<Trans i18nKey="dashboard.toolbar.add-tab">Tab</Trans>
|
||||
</Card.Heading>
|
||||
<Card.Figure>
|
||||
<Icon name="layer-group" size="xl" />
|
||||
</Card.Figure>
|
||||
</Card>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
@@ -4,12 +4,20 @@ import { useEffect, useRef } from 'react';
|
||||
|
||||
import { GrafanaTheme2 } from '@grafana/data';
|
||||
import { SceneObjectState, SceneObjectBase, SceneObject, sceneGraph, useSceneObjectState } from '@grafana/scenes';
|
||||
import { ElementSelectionContextItem, ElementSelectionContextState, ToolbarButton, useStyles2 } from '@grafana/ui';
|
||||
import {
|
||||
ElementSelectionContextItem,
|
||||
ElementSelectionContextState,
|
||||
Tab,
|
||||
TabsBar,
|
||||
ToolbarButton,
|
||||
useStyles2,
|
||||
} from '@grafana/ui';
|
||||
import { t } from 'app/core/internationalization';
|
||||
|
||||
import { isInCloneChain } from '../utils/clone';
|
||||
import { getDashboardSceneFor } from '../utils/utils';
|
||||
|
||||
import { DashboardAddPane } from './DashboardAddPane';
|
||||
import { ElementEditPane } from './ElementEditPane';
|
||||
import { ElementSelection } from './ElementSelection';
|
||||
import { useEditableElement } from './useEditableElement';
|
||||
@@ -17,8 +25,11 @@ import { useEditableElement } from './useEditableElement';
|
||||
export interface DashboardEditPaneState extends SceneObjectState {
|
||||
selection?: ElementSelection;
|
||||
selectionContext: ElementSelectionContextState;
|
||||
tab?: EditPaneTab;
|
||||
}
|
||||
|
||||
export type EditPaneTab = 'add' | 'configure' | 'outline';
|
||||
|
||||
export class DashboardEditPane extends SceneObjectBase<DashboardEditPaneState> {
|
||||
public constructor() {
|
||||
super({
|
||||
@@ -122,6 +133,10 @@ export class DashboardEditPane extends SceneObjectBase<DashboardEditPaneState> {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
public onChangeTab = (tab: EditPaneTab) => {
|
||||
this.setState({ tab });
|
||||
};
|
||||
}
|
||||
|
||||
export interface Props {
|
||||
@@ -157,7 +172,7 @@ export function DashboardEditPaneRenderer({ editPane, isCollapsed, onToggleColla
|
||||
}
|
||||
}, [editPane, isCollapsed]);
|
||||
|
||||
const { selection } = useSceneObjectState(editPane, { shouldActivateOrKeepAlive: true });
|
||||
const { selection, tab = 'configure' } = useSceneObjectState(editPane, { shouldActivateOrKeepAlive: true });
|
||||
const styles = useStyles2(getStyles);
|
||||
const paneRef = useRef<HTMLDivElement>(null);
|
||||
const editableElement = useEditableElement(selection);
|
||||
@@ -191,7 +206,28 @@ export function DashboardEditPaneRenderer({ editPane, isCollapsed, onToggleColla
|
||||
|
||||
return (
|
||||
<div className={styles.wrapper} ref={paneRef}>
|
||||
<ElementEditPane element={editableElement} key={editableElement.typeName} />
|
||||
<TabsBar className={styles.tabsbar}>
|
||||
<Tab
|
||||
active={tab === 'add'}
|
||||
label={t('dashboard.editpane.add', 'Add')}
|
||||
onChangeTab={() => editPane.onChangeTab('add')}
|
||||
/>
|
||||
<Tab
|
||||
active={tab === 'configure'}
|
||||
label={t('dashboard.editpane.configure', 'Configure')}
|
||||
onChangeTab={() => editPane.onChangeTab('configure')}
|
||||
/>
|
||||
<Tab
|
||||
active={tab === 'outline'}
|
||||
label={t('dashboard.editpane.outline', 'Outline')}
|
||||
onChangeTab={() => editPane.onChangeTab('outline')}
|
||||
/>
|
||||
</TabsBar>
|
||||
<div className={styles.tabContent}>
|
||||
{tab === 'add' && <DashboardAddPane editPane={editPane} />}
|
||||
{tab === 'configure' && <ElementEditPane element={editableElement} key={editableElement.typeName} />}
|
||||
{tab === 'outline' && <div />}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -202,11 +238,21 @@ function getStyles(theme: GrafanaTheme2) {
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
flex: '1 1 0',
|
||||
}),
|
||||
tabContent: css({
|
||||
display: 'flex',
|
||||
flex: '1 1 0',
|
||||
flexDirection: 'column',
|
||||
minHeight: 0,
|
||||
overflow: 'auto',
|
||||
}),
|
||||
rotate180: css({
|
||||
rotate: '180deg',
|
||||
}),
|
||||
tabsbar: css({
|
||||
padding: theme.spacing(0, 1),
|
||||
margin: theme.spacing(0.5, 1),
|
||||
}),
|
||||
expandOptionsWrapper: css({
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
|
||||
@@ -156,83 +156,6 @@ export function ToolbarActions({ dashboard }: Props) {
|
||||
}
|
||||
|
||||
if (dashboardNewLayouts) {
|
||||
leftActions.push({
|
||||
group: 'add-panel',
|
||||
condition: isEditingAndShowingDashboard,
|
||||
render: () => (
|
||||
<Button
|
||||
key="add-panel-button"
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
icon="plus"
|
||||
fill="text"
|
||||
onClick={() => {
|
||||
dashboard.onCreateNewPanel();
|
||||
}}
|
||||
data-testid={selectors.components.PageToolbar.itemButton('add_visualization')}
|
||||
>
|
||||
<Trans i18nKey="dashboard.toolbar.add-panel">Panel</Trans>
|
||||
</Button>
|
||||
),
|
||||
});
|
||||
leftActions.push({
|
||||
group: 'add-panel',
|
||||
condition: isEditingAndShowingDashboard,
|
||||
render: () => (
|
||||
<Button
|
||||
key="add-panel-button"
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
icon="plus"
|
||||
fill="text"
|
||||
onClick={() => {
|
||||
dashboard.onCreateNewRow();
|
||||
}}
|
||||
data-testid={selectors.components.PageToolbar.itemButton('add_row')}
|
||||
>
|
||||
<Trans i18nKey="dashboard.toolbar.add-row">Row</Trans>
|
||||
</Button>
|
||||
),
|
||||
});
|
||||
leftActions.push({
|
||||
group: 'add-panel',
|
||||
condition: isEditingAndShowingDashboard,
|
||||
render: () => (
|
||||
<Button
|
||||
key="add-tab-button"
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
icon="plus"
|
||||
fill="text"
|
||||
onClick={() => {
|
||||
dashboard.onCreateNewTab();
|
||||
}}
|
||||
data-testid={selectors.components.PageToolbar.itemButton('add_tab')}
|
||||
>
|
||||
<Trans i18nKey="dashboard.toolbar.add-tab">Tab</Trans>
|
||||
</Button>
|
||||
),
|
||||
});
|
||||
leftActions.push({
|
||||
group: 'add-panel',
|
||||
condition: isEditingAndShowingDashboard,
|
||||
render: () => (
|
||||
<Button
|
||||
key="add-panel-lib"
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
icon="plus"
|
||||
fill="text"
|
||||
data-testid={selectors.pages.AddDashboard.itemButton('Add new panel from panel library menu item')}
|
||||
onClick={() => {
|
||||
dashboard.onShowAddLibraryPanelDrawer();
|
||||
DashboardInteractions.toolbarAddButtonClicked({ item: 'add_library_panel' });
|
||||
}}
|
||||
>
|
||||
<Trans i18nKey="dashboard.toolbar.add-panel-lib">Import</Trans>
|
||||
</Button>
|
||||
),
|
||||
});
|
||||
leftActions.push({
|
||||
group: 'hidden-elements',
|
||||
condition: isEditingAndShowingDashboard,
|
||||
|
||||
@@ -70,10 +70,6 @@ export class TabItem
|
||||
return new TabItems(items.filter((item) => item instanceof TabItem));
|
||||
}
|
||||
|
||||
public onChangeTab() {
|
||||
this.getParentLayout().changeTab(this);
|
||||
}
|
||||
|
||||
public onChangeTitle(title: string) {
|
||||
this.setState({ title });
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user