diff --git a/.editorconfig b/.editorconfig index 386c27fceb8..146224e7330 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,13 +1,6 @@ # http://editorconfig.org root = true -[*.go] -indent_style = tab -indent_size = 4 -charset = utf-8 -trim_trailing_whitespace = true -insert_final_newline = true - [*] indent_style = space indent_size = 2 @@ -15,5 +8,12 @@ charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true +[*.go] +indent_style = tab +indent_size = 4 +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + [*.md] trim_trailing_whitespace = false diff --git a/.gitignore b/.gitignore index 03178388a7c..fb43f933731 100644 --- a/.gitignore +++ b/.gitignore @@ -41,6 +41,14 @@ profile.cov .notouch /pkg/cmd/grafana-cli/grafana-cli /pkg/cmd/grafana-server/grafana-server +/pkg/cmd/grafana-server/debug /examples/*/dist /packaging/**/*.rpm /packaging/**/*.deb + +/vendor/**/*.py +/vendor/**/*.xml +/vendor/**/*.yml +/vendor/**/*_test.go +/vendor/**/.editorconfig +/vendor/**/appengine* diff --git a/CHANGELOG.md b/CHANGELOG.md index b40f20fde0f..ef6c3ffc273 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,12 +14,17 @@ * **Prometheus**: Adds /metrics endpoint for exposing Grafana metrics. [#9187](https://github.com/grafana/grafana/pull/9187) * **Graph**: Add support for local formating in axis. [#1395](https://github.com/grafana/grafana/issues/1395), thx [@m0nhawk](https://github.com/m0nhawk) * **Jaeger**: Add support for open tracing using jaeger in Grafana. [#9213](https://github.com/grafana/grafana/pull/9213) -* **Unit types**: New date & time unit types added, useful in singlestat to show dates & times. [#3678](https://github.com/grafana/grafana/issues/3678), [#6710](https://github.com/grafana/grafana/issues/6710), [#2764](https://github.com/grafana/grafana/issues/6710) +* **Unit types**: New date & time unit types added, useful in singlestat to show dates & times. [#3678](https://github.com/grafana/grafana/issues/3678), [#6710](https://github.com/grafana/grafana/issues/6710), [#2764](https://github.com/grafana/grafana/issues/2764) * **CLI**: Make it possible to install plugins from any url [#5873](https://github.com/grafana/grafana/issues/5873) * **Prometheus**: Add support for instant queries [#5765](https://github.com/grafana/grafana/issues/5765), thx [@mtanda](https://github.com/mtanda) * **Cloudwatch**: Add support for alerting using the cloudwatch datasource [#8050](https://github.com/grafana/grafana/pull/8050), thx [@mtanda](https://github.com/mtanda) * **Pagerduty**: Include triggering series in pagerduty notification [#8479](https://github.com/grafana/grafana/issues/8479), thx [@rickymoorhouse](https://github.com/rickymoorhouse) * **Timezone**: Time ranges like Today & Yesterday now work correctly when timezone setting is set to UTC [#8916](https://github.com/grafana/grafana/issues/8916), thx [@ctide](https://github.com/ctide) +* **Prometheus**: Align $__interval with the step parameters. [#9226](https://github.com/grafana/grafana/pull/9226), thx [@alin-amana](https://github.com/alin-amana) +* **Prometheus**: Autocomplete for label name and label value [#9208](https://github.com/grafana/grafana/pull/9208), thx [@mtanda](https://github.com/mtanda) +* **Postgres**: New Postgres data source [#9209](https://github.com/grafana/grafana/pull/9209), thx [@svenklemm](https://github.com/svenklemm) +* **Datasources**: Make datasource HTTP requests verify TLS by default. closes [#9371](https://github.com/grafana/grafana/issues/9371), [#5334](https://github.com/grafana/grafana/issues/5334), [#8812](https://github.com/grafana/grafana/issues/8812), thx [@mattbostock](https://github.com/mattbostock) +* **OAuth**: Verify TLS during OAuth callback [#9373](https://github.com/grafana/grafana/issues/9373), thx [@mattbostock](https://github.com/mattbostock) ## Minor * **SMTP**: Make it possible to set specific EHLO for smtp client. [#9319](https://github.com/grafana/grafana/issues/9319) @@ -27,9 +32,14 @@ * **HTTP**: set net.Dialer.DualStack to true for all http clients [#9367](https://github.com/grafana/grafana/pull/9367) * **Alerting**: Add diff and percent diff as series reducers [#9386](https://github.com/grafana/grafana/pull/9386), thx [@shanhuhai5739](https://github.com/shanhuhai5739) * **Slack**: Allow images to be uploaded to slack when Token is precent [#7175](https://github.com/grafana/grafana/issues/7175), thx [@xginn8](https://github.com/xginn8) +* **Opsgenie**: Use their latest API instead of old version [#9399](https://github.com/grafana/grafana/pull/9399), thx [@cglrkn](https://github.com/cglrkn) +* **Table**: Add support for displaying the timestamp with milliseconds [#9429](https://github.com/grafana/grafana/pull/9429), thx [@s1061123](https://github.com/s1061123) +* **Hipchat**: Add metrics, message and image to hipchat notifications [#9110](https://github.com/grafana/grafana/issues/9110), thx [@eloo](https://github.com/eloo) +* **Kafka**: Add support for sending alert notifications to kafka [#7104](https://github.com/grafana/grafana/issues/7104), thx [@utkarshcmu](https://github.com/utkarshcmu) ## Tech * **Go**: Grafana is now built using golang 1.9 +* **Webpack**: Changed from systemjs to webpack (see readme or building from source guide for new build instructions). Systemjs is still used to load plugins but now plugins can only import a limited set of dependencies. See [PLUGIN_DEV.md](https://github.com/grafana/grafana/blob/master/PLUGIN_DEV.md) for more details on how this can effect some plugins. # 4.5.2 (2017-09-22) diff --git a/PLUGIN_DEV.md b/PLUGIN_DEV.md new file mode 100644 index 00000000000..1f672aa5a3d --- /dev/null +++ b/PLUGIN_DEV.md @@ -0,0 +1,28 @@ +# Plugin Development + +This document is not meant as complete guide for developing plugins but more as a changelog for changes in +Grafana that can impact plugin development. When ever you as plugin author encounter an issue with your plugin after +upgrading Grafana please check here before creating an issue. + +## Links + +- [Datasource plugin written in typescript](https://github.com/grafana/typescript-template-datasource) +- [Simple json dataource plugin](https://github.com/grafana/simple-json-datasource) +- [Plugin development guide](http://docs.grafana.org/plugins/developing/development/) + +## Changes in v4.6 + +This version of Grafana has big changes that will impact a limited set of plugins. We moved from systemjs to webpack +for built-in plugins & everything internal. External plugins still use systemjs but now with a limited +set of Grafana components they can import. Plugins can depend on libs like lodash & moment and internal components +like before using the same import paths. However since everything in Grafana is no longer accessible, a few plugins could encounter issues when importing a Grafana dependency. + +[List of exposed components plugins can import/require](https://github.com/grafana/grafana/blob/master/public/app/features/plugins/plugin_loader.ts#L48) + +If you think we missed exposing a crucial lib or Grafana component let us know by opening an issue. + +### Deprecated components + +The angular directive `` is no deprecated (will still work for a version more) but we recommend plugin authors +to upgrade to new `` + diff --git a/README.md b/README.md index 44bf4398fc2..28f8bfc1ba2 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ yarn install --pure-lockfile npm run build ``` -To rebuild frontend assets (typesript, sass etc) as you change them start the watcher via. +To rebuild frontend assets (typescript, sass etc) as you change them start the watcher via. ```bash npm run watch @@ -82,10 +82,17 @@ You only need to add the options you want to override. Config files are applied In your custom.ini uncomment (remove the leading `;`) sign. And set `app_mode = development`. ## Contribute + If you have any idea for an improvement or found a bug do not hesitate to open an issue. And if you have time clone this repo and submit a pull request and help me make Grafana the kickass metrics & devops dashboard we all dream about! +## Plugin development + +Checkout the [Plugin Development Guide](http://docs.grafana.org/plugins/developing/development/) and checkout the [PLUGIN_DEV.md](https://github.com/grafana/grafana/blob/master/PLUGIN_DEV.md) file for changes in Grafana that relate to +plugin development. + ## License + Grafana is distributed under Apache 2.0 License. -Work in progress Grafana 2.0 (with included Grafana backend) + diff --git a/ROADMAP.md b/ROADMAP.md index 62e6719c4f0..3ce0c33f088 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -17,7 +17,7 @@ But it will give you an idea of our current vision and plan. ### Long term - Backend plugins to support more Auth options, Alerting data sources & notifications -- Universial time series transformations for any data source (meta queries) +- Universal time series transformations for any data source (meta queries) - Reporting - Web socket & live data streams - Migrate to Angular2 or react diff --git a/appveyor.yml b/appveyor.yml index d626f6bd93f..19de1d3a793 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -7,7 +7,7 @@ clone_folder: c:\gopath\src\github.com\grafana\grafana environment: nodejs_version: "6" GOPATH: c:\gopath - GOVERSION: 1.9 + GOVERSION: 1.9.1 install: - rmdir c:\go /s /q diff --git a/circle.yml b/circle.yml index 0d535a9c1c2..ba44172ddf3 100644 --- a/circle.yml +++ b/circle.yml @@ -9,7 +9,7 @@ machine: GOPATH: "/home/ubuntu/.go_workspace" ORG_PATH: "github.com/grafana" REPO_PATH: "${ORG_PATH}/grafana" - GODIST: "go1.9.linux-amd64.tar.gz" + GODIST: "go1.9.1.linux-amd64.tar.gz" post: - mkdir -p ~/download - mkdir -p ~/docker diff --git a/conf/defaults.ini b/conf/defaults.ini index dfa6bf99017..14e77449241 100644 --- a/conf/defaults.ini +++ b/conf/defaults.ini @@ -479,6 +479,7 @@ provider = bucket_url = bucket = region = +path = access_key = secret_key = diff --git a/conf/sample.ini b/conf/sample.ini index 89170000df6..1aedfbf6532 100644 --- a/conf/sample.ini +++ b/conf/sample.ini @@ -424,6 +424,7 @@ [external_image_storage.s3] ;bucket = ;region = +;path = ;access_key = ;secret_key = diff --git a/docs/sources/administration/cli.md b/docs/sources/administration/cli.md index 645f75ab412..2be1881cfab 100644 --- a/docs/sources/administration/cli.md +++ b/docs/sources/administration/cli.md @@ -41,7 +41,7 @@ then there are two flags that can be used to set homepath and the config file pa If you have not lost the admin password then it is better to set in the Grafana UI. If you need to set the password in a script then the [Grafana API](http://docs.grafana.org/http_api/user/#change-password) can be used. Here is an example with curl using basic auth: -``` +```bash curl -X PUT -H "Content-Type: application/json" -d '{ "oldPassword": "admin", "newPassword": "newpass", diff --git a/docs/sources/alerting/notifications.md b/docs/sources/alerting/notifications.md index de9e5abd472..102134f34dd 100644 --- a/docs/sources/alerting/notifications.md +++ b/docs/sources/alerting/notifications.md @@ -115,6 +115,17 @@ In DingTalk PC Client: Dingtalk supports the following "message type": `text`, `link` and `markdown`. Only the `text` message type is supported. +### Kafka + +Notifications can be sent to a Kafka topic from Grafana using [Kafka REST Proxy](https://docs.confluent.io/1.0/kafka-rest/docs/index.html). +There are couple of configurations options which need to be set in Grafana UI under Kafka Settings: + +1. Kafka REST Proxy endpoint. + +2. Kafka Topic. + +Once these two properties are set, you can send the alerts to Kafka for further processing or throttling them. + ### Other Supported Notification Channels Grafana also supports the following Notification Channels: diff --git a/docs/sources/features/datasources/cloudwatch.md b/docs/sources/features/datasources/cloudwatch.md index 61dd90d2881..29da06bfb1e 100644 --- a/docs/sources/features/datasources/cloudwatch.md +++ b/docs/sources/features/datasources/cloudwatch.md @@ -50,11 +50,12 @@ Create a file at `~/.aws/credentials`. That is the `HOME` path for user running Example content: - [default] - aws_access_key_id = asdsadasdasdasd - aws_secret_access_key = dasdasdsadasdasdasdsa - region = us-west-2 - +```bash +[default] +aws_access_key_id = asdsadasdasdasd +aws_secret_access_key = dasdasdsadasdasdasdsa +region = us-west-2 +``` ## Metric Query Editor @@ -117,7 +118,9 @@ Filters syntax: Example `ec2_instance_attribute()` query - ec2_instance_attribute(us-east-1, InstanceId, { "tag:Environment": [ "production" ] }) +```javascript +ec2_instance_attribute(us-east-1, InstanceId, { "tag:Environment": [ "production" ] }) +``` ### Selecting Attributes @@ -156,7 +159,9 @@ Tags can be selected by prepending the tag name with `Tags.` Example `ec2_instance_attribute()` query - ec2_instance_attribute(us-east-1, Tags.Name, { "tag:Team": [ "sysops" ] }) +```javascript +ec2_instance_attribute(us-east-1, Tags.Name, { "tag:Team": [ "sysops" ] }) +``` ## Cost diff --git a/docs/sources/features/datasources/elasticsearch.md b/docs/sources/features/datasources/elasticsearch.md index 25cdb98c8c5..6ce17113a9b 100644 --- a/docs/sources/features/datasources/elasticsearch.md +++ b/docs/sources/features/datasources/elasticsearch.md @@ -38,8 +38,10 @@ Proxy access means that the Grafana backend will proxy all requests from the bro If you select direct access you must update your Elasticsearch configuration to allow other domains to access Elasticsearch from the browser. You do this by specifying these to options in your **elasticsearch.yml** config file. - http.cors.enabled: true - http.cors.allow-origin: "*" +```bash +http.cors.enabled: true +http.cors.allow-origin: "*" +``` ### Index settings @@ -133,6 +135,5 @@ Name | Description ------------ | ------------- Query | You can leave the search query blank or specify a lucene query Time | The name of the time field, needs to be date field. -Title | The name of the field to use for the event title. +Text | Event description field. Tags | Optional field name to use for event tags (can be an array or a CSV string). -Text | Optional field name to use event text body. diff --git a/docs/sources/features/datasources/postgres.md b/docs/sources/features/datasources/postgres.md new file mode 100644 index 00000000000..2f170ba40be --- /dev/null +++ b/docs/sources/features/datasources/postgres.md @@ -0,0 +1,186 @@ ++++ +title = "Using PostgreSQL in Grafana" +description = "Guide for using PostgreSQL in Grafana" +keywords = ["grafana", "postgresql", "guide"] +type = "docs" +[menu.docs] +name = "PostgreSQL" +parent = "datasources" +weight = 7 ++++ + +# Using PostgreSQL in Grafana + +Grafana ships with a built-in PostgreSQL data source plugin that allows you to query and visualize data from a PostgreSQL compatible database. + +## Adding the data source + +1. Open the side menu by clicking the Grafana icon in the top header. +2. In the side menu under the `Dashboards` link you should find a link named `Data Sources`. +3. Click the `+ Add data source` button in the top header. +4. Select *PostgreSQL* from the *Type* dropdown. + +### 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 & 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** recommmend 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. + +## Macros + +To simplify syntax and to allow for dynamic parts, like date range filters, the query can contain macros. + +Macro example | Description +------------ | ------------- +*$__time(dateColumn)* | Will be replaced by an expression to rename the column to `time`. For example, *dateColumn as time* +*$__timeSec(dateColumn)* | Will be replaced by an expression to rename the column to `time` and converting the value to unix timestamp. For example, *extract(epoch from dateColumn) as time* +*$__timeFilter(dateColumn)* | Will be replaced by a time range filter using the specified column name. For example, *dateColumn > to_timestamp(1494410783) AND dateColumn < to_timestamp(1494497183)* +*$__timeFrom()* | Will be replaced by the start of the currently active time selection. For example, *to_timestamp(1494410783)* +*$__timeTo()* | Will be replaced by the end of the currently active time selection. For example, *to_timestamp(1494497183)* +*$__timeGroup(dateColumn,'5m')* | Will be replaced by an expression usable in GROUP BY clause. For example, *(extract(epoch from "dateColumn")/extract(epoch from '5m'::interval))::int* +*$__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* + +We plan to add many more macros. If you have suggestions for what macros you would like to see, please [open an issue](https://github.com/grafana/grafana) in our GitHub repo. + +The query editor has a link named `Generated SQL` that shows up after a query as been executed, while in panel edit mode. Click on it and it will expand and show the raw interpolated SQL string that was executed. + +## 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 & rows your query returns. + +Query editor with example query: + +![](/img/docs/v46/postgres_table_query.png) + + +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: + +![](/img/docs/v46/postgres_table.png) + +### Time series queries + +If you set `Format as` to `Time series`, for use in Graph panel for example, then the query must return a column named `time` that returns either a sql datetime or any numeric datatype representing unix epoch in seconds. +Any column except `time` and `metric` is treated as a value column. +You may return a column named `metric` that is used as metric name for the value column. + +Example with `metric` column + +```sql +SELECT + min(time_date_time) as time, + min(value_double), + 'min' as metric +FROM test_data +WHERE $__timeFilter(time_date_time) +GROUP BY metric1, (extract(epoch from time_date_time)/extract(epoch from $__interval::interval))::int +ORDER BY time asc +``` + +Example with multiple columns: + +```sql +SELECT + min(time_date_time) as time, + min(value_double) as min_value, + max(value_double) as max_value +FROM test_data +WHERE $__timeFilter(time_date_time) +GROUP BY metric1, (extract(epoch from time_date_time)/extract(epoch from $__interval::interval))::int +ORDER BY time asc +``` + +## Templating + +Instead of hard-coding things like server, application and sensor name in you metric queries you can use variables in their place. Variables are shown as dropdown select boxes at the top of the dashboard. These dropdowns makes it easy to change the data being displayed in your dashboard. + +Checkout the [Templating]({{< relref "reference/templating.md" >}}) documentation 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 +``` + +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. For example if you had another variable named `region`. Then 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 Variables in Queries + +Template variables are quoted automatically so if it is a string value do not wrap them in quotes in where clauses. 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: + +`$` 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 +``` + +## Alerting + +Time series queries should work in alerting conditions. Table formatted queries is not yet supported in alert rule +conditions. diff --git a/docs/sources/features/panels/alertlist.md b/docs/sources/features/panels/alertlist.md index 8d235c20669..9307bb71391 100644 --- a/docs/sources/features/panels/alertlist.md +++ b/docs/sources/features/panels/alertlist.md @@ -18,7 +18,7 @@ The alert list panel allows you to display your dashbords alerts. The list can b ## Alert List Options -{{< docs-imagebox img="/img/docs/v45/alert-list-options.png" max-width="600px" class="docs-image--no-shadow docs-image--right">}} +{{< docs-imagebox img="/img/docs/v45/alert-list-options.png" max-width="600px" class="docs-image--no-shadow docs-image--right" >}} 1. **Show**: Lets you choose between current state or recent state changes. 2. **Max Items**: Max items set the maximum of items in a list. diff --git a/docs/sources/guides/whats-new-in-v4-6.md b/docs/sources/guides/whats-new-in-v4-6.md new file mode 100644 index 00000000000..3bc80799afc --- /dev/null +++ b/docs/sources/guides/whats-new-in-v4-6.md @@ -0,0 +1,74 @@ ++++ +title = "What's New in Grafana v4.6" +description = "Feature & improvement highlights for Grafana v4.6" +keywords = ["grafana", "new", "documentation", "4.6"] +type = "docs" +[menu.docs] +name = "Version 4.6" +identifier = "v4.6" +parent = "whatsnew" +weight = -5 ++++ + +# What's New in Grafana v4.6 + +Grafana v4.6 brings many enhancements to Annotations, Cloudwatch & Prometheus. It also adds support for Postgres as metric & table data source! + +### Annotations + +{{< docs-imagebox img="/img/docs/v46/add_annotation_region.png" max-width= "800px" >}} + +You can now add annotation events and regions right from the graph panel! Just hold CTRL/CMD + click or drag region to open the **Add Annotation** view. The +[Annotations]({{< relref "reference/annotations.md" >}}) documentation is updated to include details on this new exciting feature. + +### Cloudwatch + +Cloudwatch now supports alerting. Setup alert rules for any Cloudwatch metric! + +{{< docs-imagebox img="/img/docs/v46/cloudwatch_alerting.png" max-width= "800px" >}} + +### Postgres + +Grafana v4.6 now ships with a built-in datasource plugin for Postgres. Have logs or metric data in Postgres? You can now visualize that data and +define alert rules on it like any of our other data sources. + +{{< docs-imagebox img="/img/docs/v46/postgres_table_query.png" max-width= "800px" >}} + +### Prometheus + +New enhancements include support for **instant queries** and improvements to query editor in the form of autocomplete for label names and label values. +This makes exploring and filtering Prometheus data much easier. + +## Changelog + +### New Features + +* **GCS**: Adds support for Google Cloud Storage [#8370](https://github.com/grafana/grafana/issues/8370) thx [@chuhlomin](https://github.com/chuhlomin) +* **Prometheus**: Adds /metrics endpoint for exposing Grafana metrics. [#9187](https://github.com/grafana/grafana/pull/9187) +* **Graph**: Add support for local formating in axis. [#1395](https://github.com/grafana/grafana/issues/1395), thx [@m0nhawk](https://github.com/m0nhawk) +* **Jaeger**: Add support for open tracing using jaeger in Grafana. [#9213](https://github.com/grafana/grafana/pull/9213) +* **Unit types**: New date & time unit types added, useful in singlestat to show dates & times. [#3678](https://github.com/grafana/grafana/issues/3678), [#6710](https://github.com/grafana/grafana/issues/6710), [#2764](https://github.com/grafana/grafana/issues/2764) +* **CLI**: Make it possible to install plugins from any url [#5873](https://github.com/grafana/grafana/issues/5873) +* **Prometheus**: Add support for instant queries [#5765](https://github.com/grafana/grafana/issues/5765), thx [@mtanda](https://github.com/mtanda) +* **Cloudwatch**: Add support for alerting using the cloudwatch datasource [#8050](https://github.com/grafana/grafana/pull/8050), thx [@mtanda](https://github.com/mtanda) +* **Pagerduty**: Include triggering series in pagerduty notification [#8479](https://github.com/grafana/grafana/issues/8479), thx [@rickymoorhouse](https://github.com/rickymoorhouse) +* **Timezone**: Time ranges like Today & Yesterday now work correctly when timezone setting is set to UTC [#8916](https://github.com/grafana/grafana/issues/8916), thx [@ctide](https://github.com/ctide) +* **Prometheus**: Align $__interval with the step parameters. [#9226](https://github.com/grafana/grafana/pull/9226), thx [@alin-amana](https://github.com/alin-amana) +* **Prometheus**: Autocomplete for label name and label value [#9208](https://github.com/grafana/grafana/pull/9208), thx [@mtanda](https://github.com/mtanda) +* **Postgres**: New Postgres data source [#9209](https://github.com/grafana/grafana/pull/9209), thx [@svenklemm](https://github.com/svenklemm) +* **Datasources**: closes [#9371](https://github.com/grafana/grafana/issues/9371), [#5334](https://github.com/grafana/grafana/issues/5334), [#8812](https://github.com/grafana/grafana/issues/8812), thx [@mattbostock](https://github.com/mattbostock) + +### Minor Changes + +* **SMTP**: Make it possible to set specific EHLO for smtp client. [#9319](https://github.com/grafana/grafana/issues/9319) +* **Dataproxy**: Allow grafan to renegotiate tls connection [#9250](https://github.com/grafana/grafana/issues/9250) +* **HTTP**: set net.Dialer.DualStack to true for all http clients [#9367](https://github.com/grafana/grafana/pull/9367) +* **Alerting**: Add diff and percent diff as series reducers [#9386](https://github.com/grafana/grafana/pull/9386), thx [@shanhuhai5739](https://github.com/shanhuhai5739) +* **Slack**: Allow images to be uploaded to slack when Token is precent [#7175](https://github.com/grafana/grafana/issues/7175), thx [@xginn8](https://github.com/xginn8) +* **Opsgenie**: Use their latest API instead of old version [#9399](https://github.com/grafana/grafana/pull/9399), thx [@cglrkn](https://github.com/cglrkn) +* **Table**: Add support for displaying the timestamp with milliseconds [#9429](https://github.com/grafana/grafana/pull/9429), thx [@s1061123](https://github.com/s1061123) +* **Hipchat**: Add metrics, message and image to hipchat notifications [#9110](https://github.com/grafana/grafana/issues/9110), thx [@eloo](https://github.com/eloo) + +### Tech +* **Go**: Grafana is now built using golang 1.9 + diff --git a/docs/sources/http_api/admin.md b/docs/sources/http_api/admin.md index 55bec79c7f8..3ef5fb1136a 100644 --- a/docs/sources/http_api/admin.md +++ b/docs/sources/http_api/admin.md @@ -23,158 +23,162 @@ Only works with Basic Authentication (username and password). See [introduction] **Example Request**: - GET /api/admin/settings - Accept: application/json - Content-Type: application/json +```bash +GET /api/admin/settings +Accept: application/json +Content-Type: application/json +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json - - { - "DEFAULT": - { - "app_mode":"production"}, - "analytics": - { - "google_analytics_ua_id":"", - "reporting_enabled":"false" - }, - "auth.anonymous":{ - "enabled":"true", - "org_name":"Main Org.", - "org_role":"Viewer" - }, - "auth.basic":{ - "enabled":"false" - }, - "auth.github":{ - "allow_sign_up":"false", - "allowed_domains":"", - "allowed_organizations":"", - "api_url":"https://api.github.com/user", - "auth_url":"https://github.com/login/oauth/authorize", - "client_id":"some_id", - "client_secret":"************", - "enabled":"false", - "scopes":"user:email", - "team_ids":"", - "token_url":"https://github.com/login/oauth/access_token" - }, - "auth.google":{ - "allow_sign_up":"false","allowed_domains":"", - "api_url":"https://www.googleapis.com/oauth2/v1/userinfo", - "auth_url":"https://accounts.google.com/o/oauth2/auth", - "client_id":"some_client_id", - "client_secret":"************", - "enabled":"false", - "scopes":"https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email", - "token_url":"https://accounts.google.com/o/oauth2/token" - }, - "auth.ldap":{ - "config_file":"/etc/grafana/ldap.toml", - "enabled":"false" - }, - "auth.proxy":{ - "auto_sign_up":"true", - "enabled":"false", - "header_name":"X-WEBAUTH-USER", - "header_property":"username" - }, - "dashboards.json":{ - "enabled":"false", - "path":"/var/lib/grafana/dashboards" - }, - "database":{ - "host":"127.0.0.1:0000", - "name":"grafana", - "password":"************", - "path":"grafana.db", - "ssl_mode":"disable", - "type":"sqlite3", - "user":"root" - }, - "emails":{ - "templates_pattern":"emails/*.html", - "welcome_email_on_sign_up":"false" - }, - "event_publisher":{ - "enabled":"false", - "exchange":"grafana_events", - "rabbitmq_url":"amqp://localhost/" - }, - "log":{ - "buffer_len":"10000", - "level":"Info", - "mode":"file" - }, - "log.console":{ - "level":"" - }, - "log.file":{ - "daily_rotate":"true", - "file_name":"", - "level":"", - "log_rotate":"true", - "max_days":"7", - "max_lines":"1000000", - "max_lines_shift":"28", - "max_size_shift":"" - }, - "paths":{ - "data":"/tsdb/grafana", - "logs":"/logs/apps/grafana"}, - "security":{ - "admin_password":"************", - "admin_user":"admin", - "cookie_remember_name":"grafana_remember", - "cookie_username":"grafana_user", - "disable_gravatar":"false", - "login_remember_days":"7", - "secret_key":"************" - }, - "server":{ - "cert_file":"", - "cert_key":"", - "domain":"mygraf.com", - "enable_gzip":"false", - "enforce_domain":"false", - "http_addr":"127.0.0.1", - "http_port":"0000", - "protocol":"http", - "root_url":"%(protocol)s://%(domain)s:%(http_port)s/", - "router_logging":"true", - "data_proxy_logging":"true", - "static_root_path":"public" - }, - "session":{ - "cookie_name":"grafana_sess", - "cookie_secure":"false", - "gc_interval_time":"", - "provider":"file", - "provider_config":"sessions", - "session_life_time":"86400" - }, - "smtp":{ - "cert_file":"", - "enabled":"false", - "from_address":"admin@grafana.localhost", - "from_name":"Grafana", - "ehlo_identity":"dashboard.example.com", - "host":"localhost:25", - "key_file":"", - "password":"************", - "skip_verify":"false", - "user":""}, - "users":{ - "allow_org_create":"true", - "allow_sign_up":"false", - "auto_assign_org":"true", - "auto_assign_org_role":"Viewer" - } - } +```bash +HTTP/1.1 200 +Content-Type: application/json +{ +"DEFAULT": +{ + "app_mode":"production"}, + "analytics": + { + "google_analytics_ua_id":"", + "reporting_enabled":"false" + }, + "auth.anonymous":{ + "enabled":"true", + "org_name":"Main Org.", + "org_role":"Viewer" + }, + "auth.basic":{ + "enabled":"false" + }, + "auth.github":{ + "allow_sign_up":"false", + "allowed_domains":"", + "allowed_organizations":"", + "api_url":"https://api.github.com/user", + "auth_url":"https://github.com/login/oauth/authorize", + "client_id":"some_id", + "client_secret":"************", + "enabled":"false", + "scopes":"user:email", + "team_ids":"", + "token_url":"https://github.com/login/oauth/access_token" + }, + "auth.google":{ + "allow_sign_up":"false","allowed_domains":"", + "api_url":"https://www.googleapis.com/oauth2/v1/userinfo", + "auth_url":"https://accounts.google.com/o/oauth2/auth", + "client_id":"some_client_id", + "client_secret":"************", + "enabled":"false", + "scopes":"https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email", + "token_url":"https://accounts.google.com/o/oauth2/token" + }, + "auth.ldap":{ + "config_file":"/etc/grafana/ldap.toml", + "enabled":"false" + }, + "auth.proxy":{ + "auto_sign_up":"true", + "enabled":"false", + "header_name":"X-WEBAUTH-USER", + "header_property":"username" + }, + "dashboards.json":{ + "enabled":"false", + "path":"/var/lib/grafana/dashboards" + }, + "database":{ + "host":"127.0.0.1:0000", + "name":"grafana", + "password":"************", + "path":"grafana.db", + "ssl_mode":"disable", + "type":"sqlite3", + "user":"root" + }, + "emails":{ + "templates_pattern":"emails/*.html", + "welcome_email_on_sign_up":"false" + }, + "event_publisher":{ + "enabled":"false", + "exchange":"grafana_events", + "rabbitmq_url":"amqp://localhost/" + }, + "log":{ + "buffer_len":"10000", + "level":"Info", + "mode":"file" + }, + "log.console":{ + "level":"" + }, + "log.file":{ + "daily_rotate":"true", + "file_name":"", + "level":"", + "log_rotate":"true", + "max_days":"7", + "max_lines":"1000000", + "max_lines_shift":"28", + "max_size_shift":"" + }, + "paths":{ + "data":"/tsdb/grafana", + "logs":"/logs/apps/grafana"}, + "security":{ + "admin_password":"************", + "admin_user":"admin", + "cookie_remember_name":"grafana_remember", + "cookie_username":"grafana_user", + "disable_gravatar":"false", + "login_remember_days":"7", + "secret_key":"************" + }, + "server":{ + "cert_file":"", + "cert_key":"", + "domain":"mygraf.com", + "enable_gzip":"false", + "enforce_domain":"false", + "http_addr":"127.0.0.1", + "http_port":"0000", + "protocol":"http", + "root_url":"%(protocol)s://%(domain)s:%(http_port)s/", + "router_logging":"true", + "data_proxy_logging":"true", + "static_root_path":"public" + }, + "session":{ + "cookie_name":"grafana_sess", + "cookie_secure":"false", + "gc_interval_time":"", + "provider":"file", + "provider_config":"sessions", + "session_life_time":"86400" + }, + "smtp":{ + "cert_file":"", + "enabled":"false", + "from_address":"admin@grafana.localhost", + "from_name":"Grafana", + "ehlo_identity":"dashboard.example.com", + "host":"localhost:25", + "key_file":"", + "password":"************", + "skip_verify":"false", + "user":"" + }, + "users":{ + "allow_org_create":"true", + "allow_sign_up":"false", + "auto_assign_org":"true", + "auto_assign_org_role":"Viewer" + } +} +``` ## Grafana Stats `GET /api/admin/stats` @@ -183,26 +187,30 @@ Only works with Basic Authentication (username and password). See [introduction] **Example Request**: - GET /api/admin/stats - Accept: application/json - Content-Type: application/json +```bash +GET /api/admin/stats +Accept: application/json +Content-Type: application/json +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```json +HTTP/1.1 200 +Content-Type: application/json - { - "user_count":2, - "org_count":1, - "dashboard_count":4, - "db_snapshot_count":2, - "db_tag_count":6, - "data_source_count":1, - "playlist_count":1, - "starred_db_count":2, - "grafana_admin_count":2 - } +{ + "user_count":2, + "org_count":1, + "dashboard_count":4, + "db_snapshot_count":2, + "db_tag_count":6, + "data_source_count":1, + "playlist_count":1, + "starred_db_count":2, + "grafana_admin_count":2 +} +``` ## Global Users @@ -211,24 +219,28 @@ Only works with Basic Authentication (username and password). See [introduction] Create new user. Only works with Basic Authentication (username and password). See [introduction](http://docs.grafana.org/http_api/admin/#admin-api) for an explanation. **Example Request**: +```json - POST /api/admin/users HTTP/1.1 - Accept: application/json - Content-Type: application/json +POST /api/admin/users HTTP/1.1 +Accept: application/json +Content-Type: application/json - { - "name":"User", - "email":"user@graf.com", - "login":"user", - "password":"userpassword" - } +{ + "name":"User", + "email":"user@graf.com", + "login":"user", + "password":"userpassword" +} +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```json +HTTP/1.1 200 +Content-Type: application/json - {"id":5,"message":"User created"} +{"id":5,"message":"User created"} +``` ## Password for User @@ -239,18 +251,22 @@ Change password for a specific user. **Example Request**: - PUT /api/admin/users/2/password HTTP/1.1 - Accept: application/json - Content-Type: application/json +```json +PUT /api/admin/users/2/password HTTP/1.1 +Accept: application/json +Content-Type: application/json - {"password":"userpassword"} +{"password":"userpassword"} +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```json +HTTP/1.1 200 +Content-Type: application/json - {"message": "User password updated"} +{"message": "User password updated"} +``` ## Permissions @@ -260,18 +276,22 @@ Only works with Basic Authentication (username and password). See [introduction] **Example Request**: - PUT /api/admin/users/2/permissions HTTP/1.1 - Accept: application/json - Content-Type: application/json +```json +PUT /api/admin/users/2/permissions HTTP/1.1 +Accept: application/json +Content-Type: application/json - {"isGrafanaAdmin": true} +{"isGrafanaAdmin": true} +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```json +HTTP/1.1 200 +Content-Type: application/json - {message: "User permissions updated"} +{message: "User permissions updated"} +``` ## Delete global User @@ -281,16 +301,20 @@ Only works with Basic Authentication (username and password). See [introduction] **Example Request**: - DELETE /api/admin/users/2 HTTP/1.1 - Accept: application/json - Content-Type: application/json +```json +DELETE /api/admin/users/2 HTTP/1.1 +Accept: application/json +Content-Type: application/json +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```json +HTTP/1.1 200 +Content-Type: application/json - {message: "User deleted"} +{message: "User deleted"} +``` ## Pause all alerts @@ -300,13 +324,15 @@ Only works with Basic Authentication (username and password). See [introduction] **Example Request**: - POST /api/admin/pause-all-alerts HTTP/1.1 - Accept: application/json - Content-Type: application/json +```json +POST /api/admin/pause-all-alerts HTTP/1.1 +Accept: application/json +Content-Type: application/json - { - "paused": true - } +{ + "paused": true +} +``` JSON Body schema: @@ -314,7 +340,9 @@ JSON Body schema: **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```json +HTTP/1.1 200 +Content-Type: application/json - {state: "new state", message: "alerts pause/un paused", "alertsAffected": 100} +{state: "new state", message: "alerts pause/un paused", "alertsAffected": 100} +``` \ No newline at end of file diff --git a/docs/sources/http_api/alerting.md b/docs/sources/http_api/alerting.md index 1aab7253373..c5172c64203 100644 --- a/docs/sources/http_api/alerting.md +++ b/docs/sources/http_api/alerting.md @@ -23,11 +23,12 @@ This API can also be used to create, update and delete alert notifications. **Example Request**: - GET /api/alerts HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk - +```http +GET /api/alerts HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` Querystring Parameters: These parameters are used as querystring parameters. For example: @@ -41,28 +42,30 @@ This API can also be used to create, update and delete alert notifications. **Example Response**: - HTTP/1.1 200 - Content-Type: application/json - [ +```http +HTTP/1.1 200 +Content-Type: application/json +[ + { + "id": 1, + "dashboardId": 1, + "panelId": 1, + "name": "fire place sensor", + "message": "Someone is trying to break in through the fire place", + "state": "alerting", + "evalDate": "0001-01-01T00:00:00Z", + "evalData": [ { - "id": 1, - "dashboardId": 1, - "panelId": 1, - "name": "fire place sensor", - "message": "Someone is trying to break in through the fire place", - "state": "alerting", - "evalDate": "0001-01-01T00:00:00Z", - "evalData": [ - { - "metric": "fire", - "tags": null, - "value": 5.349999999999999 - } - "newStateDate": "2016-12-25", - "executionError": "", - "dashboardUri": "http://grafana.com/dashboard/db/sensors" + "metric": "fire", + "tags": null, + "value": 5.349999999999999 } - ] + "newStateDate": "2016-12-25", + "executionError": "", + "dashboardUri": "http://grafana.com/dashboard/db/sensors" + } +] +``` ## Get one alert @@ -70,26 +73,30 @@ This API can also be used to create, update and delete alert notifications. **Example Request**: - GET /api/alerts/1 HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/alerts/1 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json - { - "id": 1, - "dashboardId": 1, - "panelId": 1, - "name": "fire place sensor", - "message": "Someone is trying to break in through the fire place", - "state": "alerting", - "newStateDate": "2016-12-25", - "executionError": "", - "dashboardUri": "http://grafana.com/dashboard/db/sensors" - } +```http +HTTP/1.1 200 +Content-Type: application/json +{ + "id": 1, + "dashboardId": 1, + "panelId": 1, + "name": "fire place sensor", + "message": "Someone is trying to break in through the fire place", + "state": "alerting", + "newStateDate": "2016-12-25", + "executionError": "", + "dashboardUri": "http://grafana.com/dashboard/db/sensors" +} +``` ## Pause alert @@ -97,14 +104,16 @@ This API can also be used to create, update and delete alert notifications. **Example Request**: - POST /api/alerts/1/pause HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +POST /api/alerts/1/pause HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk - { - "paused": true - } +{ + "paused": true +} +``` The :id query parameter is the id of the alert to be paused or unpaused. @@ -114,13 +123,15 @@ JSON Body Schema: **Example Response**: - HTTP/1.1 200 - Content-Type: application/json - { - "alertId": 1, - "state": "Paused", - "message": "alert paused" - } +```http +HTTP/1.1 200 +Content-Type: application/json +{ + "alertId": 1, + "state": "Paused", + "message": "alert paused" +} +``` ## Get alert notifications @@ -128,26 +139,29 @@ JSON Body Schema: **Example Request**: - GET /api/alert-notifications HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk - +```http +GET /api/alert-notifications HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - { - "id": 1, - "name": "Team A", - "type": "email", - "isDefault": true, - "created": "2017-01-01 12:45", - "updated": "2017-01-01 12:45" - } +{ + "id": 1, + "name": "Team A", + "type": "email", + "isDefault": true, + "created": "2017-01-01 12:45", + "updated": "2017-01-01 12:45" +} +``` ## Create alert notification @@ -155,34 +169,37 @@ JSON Body Schema: **Example Request**: - POST /api/alert-notifications HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk - - { - "name": "new alert notification", //Required - "type": "email", //Required - "isDefault": false, - "settings": { - "addresses": "carl@grafana.com;dev@grafana.com" - } - } +```http +POST /api/alert-notifications HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +{ + "name": "new alert notification", //Required + "type": "email", //Required + "isDefault": false, + "settings": { + "addresses": "carl@grafana.com;dev@grafana.com" + } +} +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json - { - "id": 1, - "name": "new alert notification", - "type": "email", - "isDefault": false, - "settings": { addresses: "carl@grafana.com;dev@grafana.com"} } - "created": "2017-01-01 12:34", - "updated": "2017-01-01 12:34" - } +```http +HTTP/1.1 200 +Content-Type: application/json +{ + "id": 1, + "name": "new alert notification", + "type": "email", + "isDefault": false, + "settings": { addresses: "carl@grafana.com;dev@grafana.com"} } + "created": "2017-01-01 12:34", + "updated": "2017-01-01 12:34" +} +``` ## Update alert notification @@ -190,35 +207,38 @@ JSON Body Schema: **Example Request**: - PUT /api/alert-notifications/1 HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk - - { - "id": 1, - "name": "new alert notification", //Required - "type": "email", //Required - "isDefault": false, - "settings": { - "addresses: "carl@grafana.com;dev@grafana.com" - } - } +```http +PUT /api/alert-notifications/1 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +{ + "id": 1, + "name": "new alert notification", //Required + "type": "email", //Required + "isDefault": false, + "settings": { + "addresses: "carl@grafana.com;dev@grafana.com" + } +} +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json - { - "id": 1, - "name": "new alert notification", - "type": "email", - "isDefault": false, - "settings": { addresses: "carl@grafana.com;dev@grafana.com"} } - "created": "2017-01-01 12:34", - "updated": "2017-01-01 12:34" - } +```http +HTTP/1.1 200 +Content-Type: application/json +{ + "id": 1, + "name": "new alert notification", + "type": "email", + "isDefault": false, + "settings": { addresses: "carl@grafana.com;dev@grafana.com"} } + "created": "2017-01-01 12:34", + "updated": "2017-01-01 12:34" +} +``` ## Delete alert notification @@ -226,15 +246,19 @@ JSON Body Schema: **Example Request**: - DELETE /api/alert-notifications/1 HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +DELETE /api/alert-notifications/1 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json - { - "message": "Notification deleted" - } +```http +HTTP/1.1 200 +Content-Type: application/json +{ + "message": "Notification deleted" +} +``` \ No newline at end of file diff --git a/docs/sources/http_api/annotations.md b/docs/sources/http_api/annotations.md new file mode 100644 index 00000000000..2f148e9aded --- /dev/null +++ b/docs/sources/http_api/annotations.md @@ -0,0 +1,189 @@ ++++ +title = "Annotations HTTP API " +description = "Grafana Annotations HTTP API" +keywords = ["grafana", "http", "documentation", "api", "annotation", "annotations", "comment"] +aliases = ["/http_api/annotations/"] +type = "docs" +[menu.docs] +name = "Annotations" +identifier = "annotationshttp" +parent = "http_api" ++++ + +# Annotations resources / actions + +This is the API documentation for the new Grafana Annotations feature released in Grafana 4.6. Annotations are saved in the Grafana database (sqlite, mysql or postgres). Annotations can be global annotations that can be shown on any dashboard by configuring an annotation data source - they are filtered by tags. Or they can be tied to a panel on a dashboard and are then only shown on that panel. + +## Find Annotations + +`GET /api/annotations?from=1506676478816&to=1507281278816&tags=tag1&tags=tag2&limit=100` + +**Example Request**: + +```http +GET /api/annotations?from=1506676478816&to=1507281278816&tags=tag1&tags=tag2&limit=100 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Basic YWRtaW46YWRtaW4= +``` + + +Query Parameters: + +- `from`: epoch datetime in milliseconds. Optional. +- `to`: epoch datetime in milliseconds. Optional. +- `limit`: number. Optional - default is 10. Max limit for results returned. +- `alertId`: number. Optional. Find annotations for a specified alert. +- `dashboardId`: number. Optional. Find annotations that are scoped to a specific dashboard +- `panelId`: number. Optional. Find annotations that are scoped to a specific panel +- `tags`: string. Optional. Use this to filter global annotations. Global annotations are annotations from an annotation data source that are not connected specifically to a dashboard or panel. To do an "AND" filtering with multiple tags, specify the tags parameter multiple times e.g. `tags=tag1&tags=tag2`. + +**Example Response**: + +```http +HTTP/1.1 200 +Content-Type: application/json +[ + { + "id": 1124, + "alertId": 0, + "dashboardId": 468, + "panelId": 2, + "userId": 1, + "userName": "", + "newState": "", + "prevState": "", + "time": 1507266395000, + "text": "test", + "metric": "", + "regionId": 1123, + "type": "event", + "tags": [ + "tag1", + "tag2" + ], + "data": {} + }, + { + "id": 1123, + "alertId": 0, + "dashboardId": 468, + "panelId": 2, + "userId": 1, + "userName": "", + "newState": "", + "prevState": "", + "time": 1507265111000, + "text": "test", + "metric": "", + "regionId": 1123, + "type": "event", + "tags": [ + "tag1", + "tag2" + ], + "data": {} + } +] +``` + +## Create Annotation + +Creates an annotation in the Grafana database. The `dashboardId` and `panelId` fields are optional. If they are not specified then a global annotation is created and can be queried in any dashboard that adds the Grafana annotations data source. + +`POST /api/annotations` + +**Example Request**: + +```json +POST /api/annotations HTTP/1.1 +Accept: application/json +Content-Type: application/json + +{ + "dashboardId":468, + "panelId":1, + "time":1507037197339, + "isRegion":true, + "timeEnd":1507180805056, + "tags":["tag1","tag2"], + "text":"Annotation Description" +} +``` + +**Example Response**: + +```json +HTTP/1.1 200 +Content-Type: application/json + +{"message":"Annotation added"} +``` + +## Update Annotation + +`PUT /api/annotations/:id` + +**Example Request**: + +```json +PUT /api/annotations/1141 HTTP/1.1 +Accept: application/json +Content-Type: application/json + +{ + "time":1507037197339, + "isRegion":true, + "timeEnd":1507180805056, + "text":"Annotation Description", + "tags":["tag3","tag4","tag5"] +} +``` + +## Delete Annotation By Id + +`DELETE /api/annotation/:id` + +Deletes the annotation that matches the specified id. + +**Example Request**: + +```http +DELETE /api/annotation/1 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` + +**Example Response**: + +```http +HTTP/1.1 200 +Content-Type: application/json + +{"message":"Annotation deleted"} +``` + +## Delete Annotation By RegionId + +`DELETE /api/annotation/region/:id` + +Deletes the annotation that matches the specified region id. A region is an annotation that covers a timerange and has a start and end time. In the Grafana database, this is a stored as two annotations connected by a region id. + +**Example Request**: + +```http +DELETE /api/annotation/region/1 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` + +**Example Response**: + +```http +HTTP/1.1 200 +Content-Type: application/json + +{"message":"Annotation region deleted"} +``` diff --git a/docs/sources/http_api/auth.md b/docs/sources/http_api/auth.md index d8ded124ac5..b526031fdeb 100644 --- a/docs/sources/http_api/auth.md +++ b/docs/sources/http_api/auth.md @@ -21,7 +21,7 @@ If basic auth is enabled (it is enabled by default) you can authenticate your HT standard basic auth. Basic auth will also authenticate LDAP users. curl example: -``` +```bash ?curl http://admin:admin@localhost:3000/api/org {"id":1,"name":"Main Org."} ``` @@ -36,9 +36,11 @@ You use the token in all requests in the `Authorization` header, like this: **Example**: - GET http://your.grafana.com/api/dashboards/db/mydash HTTP/1.1 - Accept: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET http://your.grafana.com/api/dashboards/db/mydash HTTP/1.1 +Accept: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` The `Authorization` header value should be `Bearer `. @@ -50,28 +52,32 @@ The `Authorization` header value should be `Bearer `. **Example Request**: - GET /api/auth/keys HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/auth/keys HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - [ - { - "id": 3, - "name": "API", - "role": "Admin" - }, - { - "id": 1, - "name": "TestAdmin", - "role": "Admin" - } - ] +[ + { + "id": 3, + "name": "API", + "role": "Admin" + }, + { + "id": 1, + "name": "TestAdmin", + "role": "Admin" + } +] +``` ## Create API Key @@ -79,15 +85,17 @@ The `Authorization` header value should be `Bearer `. **Example Request**: - POST /api/auth/keys HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +POST /api/auth/keys HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk - { - "name": "mykey", - "role": "Admin" - } +{ + "name": "mykey", + "role": "Admin" +} +``` JSON Body schema: @@ -96,10 +104,12 @@ JSON Body schema: **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - {"name":"mykey","key":"eyJrIjoiWHZiSWd3NzdCYUZnNUtibE9obUpESmE3bzJYNDRIc0UiLCJuIjoibXlrZXkiLCJpZCI6MX1="} +{"name":"mykey","key":"eyJrIjoiWHZiSWd3NzdCYUZnNUtibE9obUpESmE3bzJYNDRIc0UiLCJuIjoibXlrZXkiLCJpZCI6MX1="} +``` ## Delete API Key @@ -107,14 +117,17 @@ JSON Body schema: **Example Request**: - DELETE /api/auth/keys/3 HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk - +```http +DELETE /api/auth/keys/3 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - {"message":"API key deleted"} +{"message":"API key deleted"} +``` \ No newline at end of file diff --git a/docs/sources/http_api/dashboard.md b/docs/sources/http_api/dashboard.md index 899c76ce6e0..300e5613db4 100644 --- a/docs/sources/http_api/dashboard.md +++ b/docs/sources/http_api/dashboard.md @@ -158,53 +158,57 @@ Will return the home dashboard. **Example Request**: - GET /api/dashboards/home HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/dashboards/home HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json +{ + "meta": { + "isHome":true, + "canSave":false, + "canEdit":false, + "canStar":false, + "slug":"", + "expires":"0001-01-01T00:00:00Z", + "created":"0001-01-01T00:00:00Z" + }, + "dashboard": { + "editable":false, + "hideControls":true, + "nav":[ { - "meta": { - "isHome":true, - "canSave":false, - "canEdit":false, - "canStar":false, - "slug":"", - "expires":"0001-01-01T00:00:00Z", - "created":"0001-01-01T00:00:00Z" - }, - "dashboard": { - "editable":false, - "hideControls":true, - "nav":[ - { - "enable":false, - "type":"timepicker" - } - ], - "rows": [ - { - - } - ], - "style":"dark", - "tags":[], - "templating":{ - "list":[ - ] - }, - "time":{ - }, - "timezone":"browser", - "title":"Home", - "version":5 - } + "enable":false, + "type":"timepicker" } + ], + "rows": [ + { + + } + ], + "style":"dark", + "tags":[], + "templating":{ + "list":[ + ] + }, + "time":{ + }, + "timezone":"browser", + "title":"Home", + "version":5 + } +} +``` ## Tags for Dashboard @@ -215,26 +219,30 @@ Get all tags of dashboards **Example Request**: - GET /api/dashboards/tags HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/dashboards/tags HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - [ - { - "term":"tag1", - "count":1 - }, - { - "term":"tag2", - "count":4 - } - ] +[ + { + "term":"tag1", + "count":1 + }, + { + "term":"tag2", + "count":4 + } +] +``` ## Search Dashboards @@ -249,23 +257,27 @@ Query parameters: **Example Request**: - GET /api/search?query=MyDashboard&starred=true&tag=prod HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/search?query=MyDashboard&starred=true&tag=prod HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - [ - { - "id":1, - "title":"Production Overview", - "uri":"db/production-overview", - "type":"dash-db", - "tags":[], - "isStarred":false - } - ] +[ + { + "id":1, + "title":"Production Overview", + "uri":"db/production-overview", + "type":"dash-db", + "tags":[], + "isStarred":false + } +] +``` \ No newline at end of file diff --git a/docs/sources/http_api/data_source.md b/docs/sources/http_api/data_source.md index 62b09fb5d2c..364b55b0cfc 100644 --- a/docs/sources/http_api/data_source.md +++ b/docs/sources/http_api/data_source.md @@ -18,34 +18,38 @@ parent = "http_api" **Example Request**: - GET /api/datasources HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/datasources HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - [ - { - "id":1, - "orgId":1, - "name":"datasource_elastic", - "type":"elasticsearch", - "access":"proxy", - "url":"http://mydatasource.com", - "password":"", - "user":"", - "database":"grafana-dash", - "basicAuth":false, - "basicAuthUser":"", - "basicAuthPassword":"", - "isDefault":false, - "jsonData":null - } - ] +[ + { + "id":1, + "orgId":1, + "name":"datasource_elastic", + "type":"elasticsearch", + "access":"proxy", + "url":"http://mydatasource.com", + "password":"", + "user":"", + "database":"grafana-dash", + "basicAuth":false, + "basicAuthUser":"", + "basicAuthPassword":"", + "isDefault":false, + "jsonData":null + } +] +``` ## Get a single data sources by Id @@ -53,32 +57,36 @@ parent = "http_api" **Example Request**: - GET /api/datasources/1 HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/datasources/1 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - { - "id":1, - "orgId":1, - "name":"test_datasource", - "type":"graphite", - "access":"proxy", - "url":"http://mydatasource.com", - "password":"", - "user":"", - "database":"", - "basicAuth":false, - "basicAuthUser":"", - "basicAuthPassword":"", - "isDefault":false, - "jsonData":null - } +{ + "id":1, + "orgId":1, + "name":"test_datasource", + "type":"graphite", + "access":"proxy", + "url":"http://mydatasource.com", + "password":"", + "user":"", + "database":"", + "basicAuth":false, + "basicAuthUser":"", + "basicAuthPassword":"", + "isDefault":false, + "jsonData":null +} +``` ## Get a single data source by Name @@ -86,32 +94,36 @@ parent = "http_api" **Example Request**: - GET /api/datasources/name/test_datasource HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/datasources/name/test_datasource HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - { - "id":1, - "orgId":1, - "name":"test_datasource", - "type":"graphite", - "access":"proxy", - "url":"http://mydatasource.com", - "password":"", - "user":"", - "database":"", - "basicAuth":false, - "basicAuthUser":"", - "basicAuthPassword":"", - "isDefault":false, - "jsonData":null - } +{ + "id":1, + "orgId":1, + "name":"test_datasource", + "type":"graphite", + "access":"proxy", + "url":"http://mydatasource.com", + "password":"", + "user":"", + "database":"", + "basicAuth":false, + "basicAuthUser":"", + "basicAuthPassword":"", + "isDefault":false, + "jsonData":null +} +``` ## Get data source Id by Name @@ -119,19 +131,23 @@ parent = "http_api" **Example Request**: - GET /api/datasources/id/test_datasource HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/datasources/id/test_datasource HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - { - "id":1 - } +{ + "id":1 +} +``` ## Create data source @@ -139,48 +155,53 @@ parent = "http_api" **Example Graphite Request**: - POST /api/datasources HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +POST /api/datasources HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk - { - "name":"test_datasource", - "type":"graphite", - "url":"http://mydatasource.com", - "access":"proxy", - "basicAuth":false - } +{ + "name":"test_datasource", + "type":"graphite", + "url":"http://mydatasource.com", + "access":"proxy", + "basicAuth":false +} +``` **Example CloudWatch Request**: - ``` - POST /api/datasources HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk - - { - "name": "test_datasource", - "type": "cloudwatch", - "url": "http://monitoring.us-west-1.amazonaws.com", - "access": "proxy", - "jsonData": { - "authType": "keys", - "defaultRegion": "us-west-1" - }, - "secureJsonData": { - "accessKey": "Ol4pIDpeKSA6XikgOl4p", - "secretKey": "dGVzdCBrZXkgYmxlYXNlIGRvbid0IHN0ZWFs" - } + +```http +POST /api/datasources HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk + +{ + "name": "test_datasource", + "type": "cloudwatch", + "url": "http://monitoring.us-west-1.amazonaws.com", + "access": "proxy", + "jsonData": { + "authType": "keys", + "defaultRegion": "us-west-1" + }, + "secureJsonData": { + "accessKey": "Ol4pIDpeKSA6XikgOl4p", + "secretKey": "dGVzdCBrZXkgYmxlYXNlIGRvbid0IHN0ZWFs" } - ``` +} +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - {"id":1,"message":"Datasource added", "name": "test_datasource"} +{"id":1,"message":"Datasource added", "name": "test_datasource"} +``` ## Update an existing data source @@ -188,34 +209,38 @@ parent = "http_api" **Example Request**: - PUT /api/datasources/1 HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +PUT /api/datasources/1 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk - { - "id":1, - "orgId":1, - "name":"test_datasource", - "type":"graphite", - "access":"proxy", - "url":"http://mydatasource.com", - "password":"", - "user":"", - "database":"", - "basicAuth":true, - "basicAuthUser":"basicuser", - "basicAuthPassword":"basicuser", - "isDefault":false, - "jsonData":null - } +{ + "id":1, + "orgId":1, + "name":"test_datasource", + "type":"graphite", + "access":"proxy", + "url":"http://mydatasource.com", + "password":"", + "user":"", + "database":"", + "basicAuth":true, + "basicAuthUser":"basicuser", + "basicAuthPassword":"basicuser", + "isDefault":false, + "jsonData":null +} +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - {"message":"Datasource updated", "id": 1, "name": "test_datasource"} +{"message":"Datasource updated", "id": 1, "name": "test_datasource"} +``` ## Delete an existing data source by id @@ -223,17 +248,21 @@ parent = "http_api" **Example Request**: - DELETE /api/datasources/1 HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +DELETE /api/datasources/1 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - {"message":"Data source deleted"} +{"message":"Data source deleted"} +``` ## Delete an existing data source by name @@ -241,17 +270,21 @@ parent = "http_api" **Example Request**: - DELETE /api/datasources/name/test_datasource HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +DELETE /api/datasources/name/test_datasource HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - {"message":"Data source deleted"} +{"message":"Data source deleted"} +``` ## Data source proxy calls diff --git a/docs/sources/http_api/org.md b/docs/sources/http_api/org.md index 72c995adedf..6542f00fd81 100644 --- a/docs/sources/http_api/org.md +++ b/docs/sources/http_api/org.md @@ -18,20 +18,24 @@ parent = "http_api" **Example Request**: - GET /api/org HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/org HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - { - "id":1, - "name":"Main Org." - } +{ + "id":1, + "name":"Main Org." +} +``` ## Get Organisation by Id @@ -39,57 +43,64 @@ parent = "http_api" **Example Request**: - GET /api/orgs/1 HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/orgs/1 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json - - { - "id":1, - "name":"Main Org.", - "address":{ - "address1":"", - "address2":"", - "city":"", - "zipCode":"", - "state":"", - "country":"" - } - } +```http +HTTP/1.1 200 +Content-Type: application/json +{ + "id":1, + "name":"Main Org.", + "address":{ + "address1":"", + "address2":"", + "city":"", + "zipCode":"", + "state":"", + "country":"" + } +} +``` ## Get Organisation by Name `GET /api/orgs/name/:orgName` **Example Request**: - GET /api/orgs/name/Main%20Org%2E HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/orgs/name/Main%20Org%2E HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - { - "id":1, - "name":"Main Org.", - "address":{ - "address1":"", - "address2":"", - "city":"", - "zipCode":"", - "state":"", - "country":"" - } - } +{ + "id":1, + "name":"Main Org.", + "address":{ + "address1":"", + "address2":"", + "city":"", + "zipCode":"", + "state":"", + "country":"" + } +} +``` ## Create Organisation @@ -97,26 +108,28 @@ parent = "http_api" **Example Request**: - POST /api/orgs HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk - - { - "name":"New Org." - } +```http +POST /api/orgs HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +{ + "name":"New Org." +} +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json - - { - "orgId":"1", - "message":"Organization created" - } +```http +HTTP/1.1 200 +Content-Type: application/json +{ + "orgId":"1", + "message":"Organization created" +} +``` ## Update current Organisation @@ -125,23 +138,25 @@ parent = "http_api" **Example Request**: - PUT /api/org HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk - - { - "name":"Main Org." - } +```http +PUT /api/org HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +{ + "name":"Main Org." +} +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json - - {"message":"Organization updated"} +```http +HTTP/1.1 200 +Content-Type: application/json +{"message":"Organization updated"} +``` ## Get all users within the actual organisation @@ -149,25 +164,29 @@ parent = "http_api" **Example Request**: - GET /api/org/users HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/org/users HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - [ - { - "orgId":1, - "userId":1, - "email":"admin@mygraf.com", - "login":"admin", - "role":"Admin" - } - ] +[ + { + "orgId":1, + "userId":1, + "email":"admin@mygraf.com", + "login":"admin", + "role":"Admin" + } +] +``` ## Add a new user to the actual organisation @@ -177,23 +196,26 @@ Adds a global user to the actual organisation. **Example Request**: - POST /api/org/users HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk - - { - "role": "Admin", - "loginOrEmail": "admin" - } +```http +POST /api/org/users HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +{ + "role": "Admin", + "loginOrEmail": "admin" +} +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - {"message":"User added to organization"} +{"message":"User added to organization"} +``` ## Updates the given user @@ -201,23 +223,25 @@ Adds a global user to the actual organisation. **Example Request**: - PATCH /api/org/users/1 HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk - - { - "role": "Viewer", - } +```http +PATCH /api/org/users/1 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +{ + "role": "Viewer", +} +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json - - {"message":"Organization user updated"} +```http +HTTP/1.1 200 +Content-Type: application/json +{"message":"Organization user updated"} +``` ## Delete user in actual organisation @@ -225,18 +249,21 @@ Adds a global user to the actual organisation. **Example Request**: - DELETE /api/org/users/1 HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +DELETE /api/org/users/1 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json - - {"message":"User removed from organization"} +```http +HTTP/1.1 200 +Content-Type: application/json +{"message":"User removed from organization"} +``` # Organisations @@ -246,22 +273,26 @@ Adds a global user to the actual organisation. **Example Request**: - GET /api/orgs HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/orgs HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - [ - { - "id":1, - "name":"Main Org." - } - ] +[ + { + "id":1, + "name":"Main Org." + } +] +``` ## Update Organisation @@ -271,22 +302,25 @@ Update Organisation, fields *Adress 1*, *Adress 2*, *City* are not implemented y **Example Request**: - PUT /api/orgs/1 HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk - - { - "name":"Main Org 2." - } +```http +PUT /api/orgs/1 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +{ + "name":"Main Org 2." +} +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - {"message":"Organization updated"} +{"message":"Organization updated"} +``` ## Get Users in Organisation @@ -294,24 +328,28 @@ Update Organisation, fields *Adress 1*, *Adress 2*, *City* are not implemented y **Example Request**: - GET /api/orgs/1/users HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/orgs/1/users HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json - [ - { - "orgId":1, - "userId":1, - "email":"admin@mygraf.com", - "login":"admin", - "role":"Admin" - } - ] +```http +HTTP/1.1 200 +Content-Type: application/json +[ + { + "orgId":1, + "userId":1, + "email":"admin@mygraf.com", + "login":"admin", + "role":"Admin" + } +] +``` ## Add User in Organisation @@ -319,22 +357,26 @@ Update Organisation, fields *Adress 1*, *Adress 2*, *City* are not implemented y **Example Request**: - POST /api/orgs/1/users HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +POST /api/orgs/1/users HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk - { - "loginOrEmail":"user", - "role":"Viewer" - } +{ + "loginOrEmail":"user", + "role":"Viewer" +} +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - {"message":"User added to organization"} +{"message":"User added to organization"} +``` ## Update Users in Organisation @@ -342,21 +384,25 @@ Update Organisation, fields *Adress 1*, *Adress 2*, *City* are not implemented y **Example Request**: - PATCH /api/orgs/1/users/2 HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +PATCH /api/orgs/1/users/2 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk - { - "role":"Admin" - } +{ + "role":"Admin" +} +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - {"message":"Organization user updated"} +{"message":"Organization user updated"} +``` ## Delete User in Organisation @@ -364,14 +410,18 @@ Update Organisation, fields *Adress 1*, *Adress 2*, *City* are not implemented y **Example Request**: - DELETE /api/orgs/1/users/2 HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +DELETE /api/orgs/1/users/2 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - {"message":"User removed from organization"} +{"message":"User removed from organization"} +``` \ No newline at end of file diff --git a/docs/sources/http_api/other.md b/docs/sources/http_api/other.md index 65d18f94ea4..5bf0cde05fe 100644 --- a/docs/sources/http_api/other.md +++ b/docs/sources/http_api/other.md @@ -18,43 +18,47 @@ parent = "http_api" **Example Request**: - GET /api/frontend/settings HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/frontend/settings HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - { - "allowOrgCreate":true, - "appSubUrl":"", - "buildInfo":{ - "buildstamp":xxxxxx, - "commit":"vyyyy", - "version":"zzzzz" - }, - "datasources":{ - "datasourcename":{ - "index":"grafana-dash", - "meta":{ - "annotations":true, - "module":"plugins/datasource/grafana/datasource", - "name":"Grafana", - "partials":{ - "annotations":"app/plugins/datasource/grafana/partials/annotations.editor.html", - "config":"app/plugins/datasource/grafana/partials/config.html" - }, - "pluginType":"datasource", - "serviceName":"Grafana", - "type":"grafanasearch" - } - } - }, - "defaultDatasource": "Grafana" +{ + "allowOrgCreate":true, + "appSubUrl":"", + "buildInfo":{ + "buildstamp":xxxxxx, + "commit":"vyyyy", + "version":"zzzzz" + }, + "datasources":{ + "datasourcename":{ + "index":"grafana-dash", + "meta":{ + "annotations":true, + "module":"plugins/datasource/grafana/datasource", + "name":"Grafana", + "partials":{ + "annotations":"app/plugins/datasource/grafana/partials/annotations.editor.html", + "config":"app/plugins/datasource/grafana/partials/config.html" + }, + "pluginType":"datasource", + "serviceName":"Grafana", + "type":"grafanasearch" + } } + }, + "defaultDatasource": "Grafana" +} +``` # Login API @@ -64,14 +68,18 @@ parent = "http_api" **Example Request**: - GET /api/login/ping HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/login/ping HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - {"message": "Logged in"} +{"message": "Logged in"} +``` \ No newline at end of file diff --git a/docs/sources/http_api/preferences.md b/docs/sources/http_api/preferences.md index a4b953cdaed..ac1d1ee7a0d 100644 --- a/docs/sources/http_api/preferences.md +++ b/docs/sources/http_api/preferences.md @@ -26,17 +26,21 @@ system default value. **Example Request**: - GET /api/user/preferences HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/user/preferences HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - {"theme":"","homeDashboardId":0,"timezone":""} +{"theme":"","homeDashboardId":0,"timezone":""} +``` ## Update Current User Prefs @@ -44,23 +48,27 @@ system default value. **Example Request**: - PUT /api/user/preferences HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +PUT /api/user/preferences HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk - { - "theme": "", - "homeDashboardId":0, - "timezone":"utc" - } +{ + "theme": "", + "homeDashboardId":0, + "timezone":"utc" +} +``` **Example Response**: - HTTP/1.1 200 - Content-Type: text/plain; charset=utf-8 +```http +HTTP/1.1 200 +Content-Type: text/plain; charset=utf-8 - {"message":"Preferences updated"} +{"message":"Preferences updated"} +``` ## Get Current Org Prefs @@ -68,17 +76,21 @@ system default value. **Example Request**: - GET /api/org/preferences HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/org/preferences HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - {"theme":"","homeDashboardId":0,"timezone":""} +{"theme":"","homeDashboardId":0,"timezone":""} +``` ## Update Current Org Prefs @@ -86,20 +98,24 @@ system default value. **Example Request**: - PUT /api/org/preferences HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +PUT /api/org/preferences HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk - { - "theme": "", - "homeDashboardId":0, - "timezone":"utc" - } +{ + "theme": "", + "homeDashboardId":0, + "timezone":"utc" +} +``` **Example Response**: - HTTP/1.1 200 - Content-Type: text/plain; charset=utf-8 +```http +HTTP/1.1 200 +Content-Type: text/plain; charset=utf-8 - {"message":"Preferences updated"} +{"message":"Preferences updated"} +``` \ No newline at end of file diff --git a/docs/sources/http_api/snapshot.md b/docs/sources/http_api/snapshot.md index d466d01e051..5cecdb85fc3 100644 --- a/docs/sources/http_api/snapshot.md +++ b/docs/sources/http_api/snapshot.md @@ -17,6 +17,7 @@ parent = "http_api" **Example Request**: +```http POST /api/snapshots HTTP/1.1 Accept: application/json Content-Type: application/json @@ -51,18 +52,20 @@ parent = "http_api" }, "expires": 3600 } +``` JSON Body schema: - **dashboard** – Required. The complete dashboard model. - **name** – Optional. snapshot name -- **expires** - Optional. When the snapshot should expire in seconds. 3600 is 1 hour, 86400 is 1 day. Default is never to expire. +- **expires** - Optional. When the snapshot should expire in seconds. 3600 is 1 hour, 86400 is 1 day. Default is never to expire. - **external** - Optional. Save the snapshot on an external server rather than locally. Default is `false`. - **key** - Optional. Define the unique key. Required if **external** is `true`. - **deleteKey** - Optional. Unique key used to delete the snapshot. It is different from the **key** so that only the creator can delete the snapshot. Required if **external** is `true`. **Example Response**: +```http HTTP/1.1 200 Content-Type: application/json { @@ -71,6 +74,7 @@ JSON Body schema: "key":"YYYYYYY", "url":"myurl/dashboard/snapshot/YYYYYYY" } +``` Keys: @@ -83,54 +87,58 @@ Keys: **Example Request**: - GET /api/snapshots/YYYYYYY HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/snapshots/YYYYYYY HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - { - "meta":{ - "isSnapshot":true, - "type":"snapshot", - "canSave":false, - "canEdit":false, - "canStar":false, - "slug":"", - "expires":"2200-13-32T25:23:23+02:00", - "created":"2200-13-32T28:24:23+02:00" - }, - "dashboard": { - "editable":false, - "hideControls":true, - "nav":[ - { - "enable":false, - "type":"timepicker" - } - ], - "rows": [ - { +{ + "meta":{ + "isSnapshot":true, + "type":"snapshot", + "canSave":false, + "canEdit":false, + "canStar":false, + "slug":"", + "expires":"2200-13-32T25:23:23+02:00", + "created":"2200-13-32T28:24:23+02:00" + }, + "dashboard": { + "editable":false, + "hideControls":true, + "nav": [ + { + "enable":false, + "type":"timepicker" + } + ], + "rows": [ + { - } - ], - "style":"dark", - "tags":[], - "templating":{ - "list":[ - ] - }, - "time":{ - }, - "timezone":"browser", - "title":"Home", - "version":5 - } - } + } + ], + "style":"dark", + "tags":[], + "templating":{ + "list":[ + ] + }, + "time":{ + }, + "timezone":"browser", + "title":"Home", + "version":5 + } +} +``` ## Delete Snapshot by Id @@ -138,14 +146,18 @@ Keys: **Example Request**: - GET /api/snapshots/YYYYYYY HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/snapshots/YYYYYYY HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - {"message":"Snapshot deleted. It might take an hour before it's cleared from a CDN cache."} +{"message":"Snapshot deleted. It might take an hour before it's cleared from a CDN cache."} +``` \ No newline at end of file diff --git a/docs/sources/http_api/user.md b/docs/sources/http_api/user.md index 761ac938cd8..ba8afd4db22 100644 --- a/docs/sources/http_api/user.md +++ b/docs/sources/http_api/user.md @@ -17,34 +17,38 @@ parent = "http_api" **Example Request**: - GET /api/users HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Basic YWRtaW46YWRtaW4= +```http +GET /api/users HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Basic YWRtaW46YWRtaW4= +``` Default value for the `perpage` parameter is `1000` and for the `page` parameter is `1`. Requires basic authentication and that the authenticated user is a Grafana Admin. **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - [ - { - "id": 1, - "name": "Admin", - "login": "admin", - "email": "admin@mygraf.com", - "isAdmin": true - }, - { - "id": 2, - "name": "User", - "login": "user", - "email": "user@mygraf.com", - "isAdmin": false - } - ] +[ + { + "id": 1, + "name": "Admin", + "login": "admin", + "email": "admin@mygraf.com", + "isAdmin": true + }, + { + "id": 2, + "name": "User", + "login": "user", + "email": "user@mygraf.com", + "isAdmin": false + } +] +``` ## Search Users with Paging @@ -52,10 +56,12 @@ Default value for the `perpage` parameter is `1000` and for the `page` parameter **Example Request**: - GET /api/users/search?perpage=10&page=1&query=mygraf HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Basic YWRtaW46YWRtaW4= +```http +GET /api/users/search?perpage=10&page=1&query=mygraf HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Basic YWRtaW46YWRtaW4= +``` Default value for the `perpage` parameter is `1000` and for the `page` parameter is `1`. The `totalCount` field in the response can be used for pagination of the user list E.g. if `totalCount` is equal to 100 users and the `perpage` parameter is set to 10 then there are 10 pages of users. The `query` parameter is optional and it will return results where the query value is contained in one of the `name`, `login` or `email` fields. Query values with spaces need to be url encoded e.g. `query=Jane%20Doe`. @@ -63,29 +69,31 @@ Requires basic authentication and that the authenticated user is a Grafana Admin **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json +{ + "totalCount": 2, + "users": [ { - "totalCount": 2, - "users": [ - { - "id": 1, - "name": "Admin", - "login": "admin", - "email": "admin@mygraf.com", - "isAdmin": true - }, - { - "id": 2, - "name": "User", - "login": "user", - "email": "user@mygraf.com", - "isAdmin": false - } - ], - "page": 1, - "perPage": 10 + "id": 1, + "name": "Admin", + "login": "admin", + "email": "admin@mygraf.com", + "isAdmin": true + }, + { + "id": 2, + "name": "User", + "login": "user", + "email": "user@mygraf.com", + "isAdmin": false } + ], + "page": 1, + "perPage": 10 +} +``` ## Get single user by Id @@ -93,26 +101,29 @@ Requires basic authentication and that the authenticated user is a Grafana Admin **Example Request**: - GET /api/users/1 HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Basic YWRtaW46YWRtaW4= - +```http +GET /api/users/1 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Basic YWRtaW46YWRtaW4= +``` Requires basic authentication and that the authenticated user is a Grafana Admin. **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - { - "email": "user@mygraf.com" - "name": "admin", - "login": "admin", - "theme": "light", - "orgId": 1, - "isGrafanaAdmin": true - } +{ + "email": "user@mygraf.com" + "name": "admin", + "login": "admin", + "theme": "light", + "orgId": 1, + "isGrafanaAdmin": true +} +``` ## Get single user by Username(login) or Email @@ -120,34 +131,39 @@ Requires basic authentication and that the authenticated user is a Grafana Admin **Example Request using the email as option**: - GET /api/users/lookup?loginOrEmail=user@mygraf.com HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/users/lookup?loginOrEmail=user@mygraf.com HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Request using the username as option**: - - GET /api/users/lookup?loginOrEmail=admin HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Basic YWRtaW46YWRtaW4= + +```http +GET /api/users/lookup?loginOrEmail=admin HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Basic YWRtaW46YWRtaW4= +``` Requires basic authentication and that the authenticated user is a Grafana Admin. **Example Response**: - HTTP/1.1 200 - Content-Type: application/json - - { - "email": "user@mygraf.com" - "name": "admin", - "login": "admin", - "theme": "light", - "orgId": 1, - "isGrafanaAdmin": true - } +```http +HTTP/1.1 200 +Content-Type: application/json +{ + "email": "user@mygraf.com" + "name": "admin", + "login": "admin", + "theme": "light", + "orgId": 1, + "isGrafanaAdmin": true +} +``` ## User Update @@ -155,27 +171,30 @@ Requires basic authentication and that the authenticated user is a Grafana Admin **Example Request**: - PUT /api/users/2 HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Basic YWRtaW46YWRtaW4= +```http +PUT /api/users/2 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Basic YWRtaW46YWRtaW4= - { - "email":"user@mygraf.com", - "name":"User2", - "login":"user", - "theme":"light" - } +{ + "email":"user@mygraf.com", + "name":"User2", + "login":"user", + "theme":"light" +} +``` Requires basic authentication and that the authenticated user is a Grafana Admin. **Example Response**: - HTTP/1.1 200 - Content-Type: application/json - - {"message":"User updated"} +```http +HTTP/1.1 200 +Content-Type: application/json +{"message":"User updated"} +``` ## Get Organisations for user @@ -183,25 +202,29 @@ Requires basic authentication and that the authenticated user is a Grafana Admin **Example Request**: - GET /api/users/1/orgs HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Basic YWRtaW46YWRtaW4= +```http +GET /api/users/1/orgs HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Basic YWRtaW46YWRtaW4= +``` Requires basic authentication and that the authenticated user is a Grafana Admin. **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - [ - { - "orgId":1, - "name":"Main Org.", - "role":"Admin" - } - ] +[ + { + "orgId":1, + "name":"Main Org.", + "role":"Admin" + } +] +``` ## User @@ -211,24 +234,28 @@ Requires basic authentication and that the authenticated user is a Grafana Admin **Example Request**: - GET /api/user HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/user HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - { - "email":"admin@mygraf.com", - "name":"Admin", - "login":"admin", - "theme":"light", - "orgId":1, - "isGrafanaAdmin":true - } +{ + "email":"admin@mygraf.com", + "name":"Admin", + "login":"admin", + "theme":"light", + "orgId":1, + "isGrafanaAdmin":true +} +``` ## Change Password @@ -238,23 +265,27 @@ Changes the password for the user **Example Request**: - PUT /api/user/password HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +PUT /api/user/password HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk - { - "oldPassword": "old_password", - "newPassword": "new_password", - "confirmNew": "confirm_new_password" - } +{ + "oldPassword": "old_password", + "newPassword": "new_password", + "confirmNew": "confirm_new_password" +} +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - {"message":"User password changed"} +{"message":"User password changed"} +``` ## Switch user context for a specified user @@ -264,15 +295,19 @@ Switch user context to the given organization. Requires basic authentication and **Example Request**: - POST /api/users/7/using/2 HTTP/1.1 - Authorization: Basic YWRtaW46YWRtaW4= +```http +POST /api/users/7/using/2 HTTP/1.1 +Authorization: Basic YWRtaW46YWRtaW4= +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - {"message":"Active organization changed"} +{"message":"Active organization changed"} +``` ## Switch user context for signed in user @@ -282,17 +317,21 @@ Switch user context to the given organization. **Example Request**: - POST /api/user/using/2 HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +POST /api/user/using/2 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - {"message":"Active organization changed"} +{"message":"Active organization changed"} +``` ## Organisations of the actual User @@ -302,23 +341,27 @@ Return a list of all organisations of the current user. **Example Request**: - GET /api/user/orgs HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +GET /api/user/orgs HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - [ - { - "orgId":1, - "name":"Main Org.", - "role":"Admin" - } - ] +[ + { + "orgId":1, + "name":"Main Org.", + "role":"Admin" + } +] +``` ## Star a dashboard @@ -328,17 +371,21 @@ Stars the given Dashboard for the actual user. **Example Request**: - POST /api/user/stars/dashboard/1 HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +POST /api/user/stars/dashboard/1 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - {"message":"Dashboard starred!"} +{"message":"Dashboard starred!"} +``` ## Unstar a dashboard @@ -348,14 +395,18 @@ Deletes the starring of the given Dashboard for the actual user. **Example Request**: - DELETE /api/user/stars/dashboard/1 HTTP/1.1 - Accept: application/json - Content-Type: application/json - Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +```http +DELETE /api/user/stars/dashboard/1 HTTP/1.1 +Accept: application/json +Content-Type: application/json +Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk +``` **Example Response**: - HTTP/1.1 200 - Content-Type: application/json +```http +HTTP/1.1 200 +Content-Type: application/json - {"message":"Dashboard unstarred"} +{"message":"Dashboard unstarred"} +``` \ No newline at end of file diff --git a/docs/sources/installation/behind_proxy.md b/docs/sources/installation/behind_proxy.md index 0caa31bbfb5..3d89d8b3c2c 100644 --- a/docs/sources/installation/behind_proxy.md +++ b/docs/sources/installation/behind_proxy.md @@ -15,7 +15,7 @@ weight = 1 It should be straight forward to get Grafana up and running behind a reverse proxy. But here are some things that you might run into. Links and redirects will not be rendered correctly unless you set the server.domain setting. -``` +```bash [server] domain = foo.bar ``` @@ -28,14 +28,14 @@ Here are some example configurations for running Grafana behind a reverse proxy. ### Grafana configuration (ex http://foo.bar.com) -``` +```bash [server] domain = foo.bar ``` ### Nginx configuration -``` +```bash server { listen 80; root /usr/share/nginx/www; @@ -50,14 +50,14 @@ server { ### Examples with **sub path** (ex http://foo.bar.com/grafana) #### Grafana configuration with sub path -``` +```bash [server] domain = foo.bar root_url = %(protocol)s://%(domain)s:/grafana ``` #### Nginx configuration with sub path -``` +```bash server { listen 80; root /usr/share/nginx/www; diff --git a/docs/sources/installation/configuration.md b/docs/sources/installation/configuration.md index 85ff1026c10..627a76a963e 100644 --- a/docs/sources/installation/configuration.md +++ b/docs/sources/installation/configuration.md @@ -37,26 +37,31 @@ A common problem is forgetting to uncomment a line in the `custom.ini` (or `graf All options in the configuration file (listed below) can be overridden using environment variables using the syntax: - GF__ +```bash +GF__ +``` Where the section name is the text within the brackets. Everything should be upper case, `.` should be replaced by `_`. For example, given these configuration settings: - # default section - instance_name = ${HOSTNAME} +```bash +# default section +instance_name = ${HOSTNAME} - [security] - admin_user = admin - - [auth.google] - client_secret = 0ldS3cretKey +[security] +admin_user = admin +[auth.google] +client_secret = 0ldS3cretKey +``` Then you can override them using: - export GF_DEFAULT_INSTANCE_NAME=my-instance - export GF_SECURITY_ADMIN_USER=true - export GF_AUTH_GOOGLE_CLIENT_SECRET=newS3cretKey +```bash +export GF_DEFAULT_INSTANCE_NAME=my-instance +export GF_SECURITY_ADMIN_USER=true +export GF_AUTH_GOOGLE_CLIENT_SECRET=newS3cretKey +```
@@ -93,11 +98,15 @@ The IP address to bind to. If empty will bind to all interfaces The port to bind to, defaults to `3000`. To use port 80 you need to either give the Grafana binary permission for example: - $ sudo setcap 'cap_net_bind_service=+ep' /usr/sbin/grafana-server +```bash +$ sudo setcap 'cap_net_bind_service=+ep' /usr/sbin/grafana-server +``` Or redirect port 80 to the Grafana port using: - $ sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3000 +```bash +$ sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3000 +``` Another way is put a webserver like Nginx or Apache in front of Grafana and have them proxy requests to Grafana. @@ -312,7 +321,9 @@ You need to create a GitHub OAuth application (you find this under the GitHub settings page). When you create the application you will need to specify a callback URL. Specify this as callback: - http://:/login/github +```bash +http://:/login/github +``` This callback URL must match the full HTTP address that you use in your browser to access Grafana, but with the prefix path of `/login/github`. @@ -320,17 +331,19 @@ When the GitHub OAuth application is created you will get a Client ID and a Client Secret. Specify these in the Grafana configuration file. For example: - [auth.github] - enabled = true - allow_sign_up = true - client_id = YOUR_GITHUB_APP_CLIENT_ID - client_secret = YOUR_GITHUB_APP_CLIENT_SECRET - scopes = user:email - auth_url = https://github.com/login/oauth/authorize - token_url = https://github.com/login/oauth/access_token - api_url = https://api.github.com/user - team_ids = - allowed_organizations = +```bash +[auth.github] +enabled = true +allow_sign_up = true +client_id = YOUR_GITHUB_APP_CLIENT_ID +client_secret = YOUR_GITHUB_APP_CLIENT_SECRET +scopes = user:email +auth_url = https://github.com/login/oauth/authorize +token_url = https://github.com/login/oauth/access_token +api_url = https://api.github.com/user +team_ids = +allowed_organizations = +``` Restart the Grafana back-end. You should now see a GitHub login button on the login page. You can now login or sign up with your GitHub @@ -348,15 +361,17 @@ GitHub. If the authenticated user isn't a member of at least one of the teams they will not be able to register or authenticate with your Grafana instance. For example: - [auth.github] - enabled = true - client_id = YOUR_GITHUB_APP_CLIENT_ID - client_secret = YOUR_GITHUB_APP_CLIENT_SECRET - scopes = user:email,read:org - team_ids = 150,300 - auth_url = https://github.com/login/oauth/authorize - token_url = https://github.com/login/oauth/access_token - allow_sign_up = true +```bash +[auth.github] +enabled = true +client_id = YOUR_GITHUB_APP_CLIENT_ID +client_secret = YOUR_GITHUB_APP_CLIENT_SECRET +scopes = user:email,read:org +team_ids = 150,300 +auth_url = https://github.com/login/oauth/authorize +token_url = https://github.com/login/oauth/access_token +allow_sign_up = true +``` ### allowed_organizations @@ -365,16 +380,18 @@ organizations on GitHub. If the authenticated user isn't a member of at least one of the organizations they will not be able to register or authenticate with your Grafana instance. For example - [auth.github] - enabled = true - client_id = YOUR_GITHUB_APP_CLIENT_ID - client_secret = YOUR_GITHUB_APP_CLIENT_SECRET - scopes = user:email,read:org - auth_url = https://github.com/login/oauth/authorize - token_url = https://github.com/login/oauth/access_token - allow_sign_up = true - # space-delimited organization names - allowed_organizations = github google +```bash +[auth.github] +enabled = true +client_id = YOUR_GITHUB_APP_CLIENT_ID +client_secret = YOUR_GITHUB_APP_CLIENT_SECRET +scopes = user:email,read:org +auth_url = https://github.com/login/oauth/authorize +token_url = https://github.com/login/oauth/access_token +allow_sign_up = true +# space-delimited organization names +allowed_organizations = github google +```
@@ -385,22 +402,26 @@ Developer Console](https://console.developers.google.com/project). When you create the project you will need to specify a callback URL. Specify this as callback: - http://:/login/google +```bash +http://:/login/google +``` This callback URL must match the full HTTP address that you use in your browser to access Grafana, but with the prefix path of `/login/google`. When the Google project is created you will get a Client ID and a Client Secret. Specify these in the Grafana configuration file. For example: - [auth.google] - enabled = true - client_id = YOUR_GOOGLE_APP_CLIENT_ID - client_secret = YOUR_GOOGLE_APP_CLIENT_SECRET - scopes = https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email - auth_url = https://accounts.google.com/o/oauth2/auth - token_url = https://accounts.google.com/o/oauth2/token - allowed_domains = mycompany.com mycompany.org - allow_sign_up = true +```bash +[auth.google] +enabled = true +client_id = YOUR_GOOGLE_APP_CLIENT_ID +client_secret = YOUR_GOOGLE_APP_CLIENT_SECRET +scopes = https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email +auth_url = https://accounts.google.com/o/oauth2/auth +token_url = https://accounts.google.com/o/oauth2/token +allowed_domains = mycompany.com mycompany.org +allow_sign_up = true +``` Restart the Grafana back-end. You should now see a Google login button on the login page. You can now login or sign up with your Google @@ -418,19 +439,55 @@ This option could be used if have your own oauth service. This callback URL must match the full HTTP address that you use in your browser to access Grafana, but with the prefix path of `/login/generic_oauth`. - [auth.generic_oauth] - enabled = true - client_id = YOUR_APP_CLIENT_ID - client_secret = YOUR_APP_CLIENT_SECRET - scopes = - auth_url = - token_url = - api_url = - allowed_domains = mycompany.com mycompany.org - allow_sign_up = true +```bash +[auth.generic_oauth] +enabled = true +client_id = YOUR_APP_CLIENT_ID +client_secret = YOUR_APP_CLIENT_SECRET +scopes = +auth_url = +token_url = +api_url = +allowed_domains = mycompany.com mycompany.org +allow_sign_up = true +``` Set api_url to the resource that returns [OpenID UserInfo](https://connect2id.com/products/server/docs/api/userinfo) compatible information. +### Set up oauth2 with Okta + +First set up Grafana as an OpenId client "webapplication" in Okta. Then set the Base URIs to `https:///` and set the Login redirect URIs to `https:///login/generic_oauth`. + +Finaly set up the generic oauth module like this: +```bash +[auth.generic_oauth] +name = Okta +enabled = true +scopes = openid profile email +client_id = +client_secret = +auth_url = https:///oauth2/v1/authorize +token_url = https:///oauth2/v1/token +api_url = https:///oauth2/v1/userinfo +``` + +### Set up oauth2 with Bitbucket + +```bash +[auth.generic_oauth] +name = BitBucket +enabled = true +allow_sign_up = true +client_id = +client_secret = +scopes = account email +auth_url = https://bitbucket.org/site/oauth2/authorize +token_url = https://bitbucket.org/site/oauth2/access_token +api_url = https://api.bitbucket.org/2.0/user +team_ids = +allowed_organizations = +``` +
## [auth.basic] @@ -503,21 +560,25 @@ session table manually. Mysql Example: - CREATE TABLE `session` ( - `key` CHAR(16) NOT NULL, - `data` BLOB, - `expiry` INT(11) UNSIGNED NOT NULL, - PRIMARY KEY (`key`) - ) ENGINE=MyISAM DEFAULT CHARSET=utf8; +```bash +CREATE TABLE `session` ( + `key` CHAR(16) NOT NULL, + `data` BLOB, + `expiry` INT(11) UNSIGNED NOT NULL, + PRIMARY KEY (`key`) +) ENGINE=MyISAM DEFAULT CHARSET=utf8; +``` Postgres Example: - CREATE TABLE session ( - key CHAR(16) NOT NULL, - data BYTEA, - expiry INTEGER NOT NULL, - PRIMARY KEY (key) - ); +```bash +CREATE TABLE session ( + key CHAR(16) NOT NULL, + data BYTEA, + expiry INTEGER NOT NULL, + PRIMARY KEY (key) +); +``` Postgres valid `sslmode` are `disable`, `require` (default), `verify-ca`, and `verify-full`. @@ -651,11 +712,16 @@ These options control how images should be made public so they can be shared on You can choose between (s3, webdav, gcs). If left empty Grafana will ignore the upload action. ## [external_image_storage.s3] + ### bucket Bucket name for S3. e.g. grafana.snapshot + ### region Region name for S3. e.g. 'us-east-1', 'cn-north-1', etc +### path +Optional extra path inside bucket, useful to apply expiration policies + ### bucket_url (for backward compatibility, only works when no bucket or region are configured) Bucket URL for S3. AWS region can be specified within URL or defaults to 'us-east-1', e.g. @@ -693,7 +759,7 @@ Service Account keys can be created and downloaded from https://console.develope Service Account should have "Storage Object Writer" role. ### bucket name -Bucket Name on Google Cloud Storage. +Bucket Name on Google Cloud Storage. ## [alerting] diff --git a/docs/sources/installation/debian.md b/docs/sources/installation/debian.md index 330e3171e86..222a337855a 100644 --- a/docs/sources/installation/debian.md +++ b/docs/sources/installation/debian.md @@ -45,13 +45,17 @@ sudo dpkg -i grafana_4.5.2-beta1_amd64.deb Add the following line to your `/etc/apt/sources.list` file. - deb https://packagecloud.io/grafana/stable/debian/ jessie main +```bash +deb https://packagecloud.io/grafana/stable/debian/ jessie main +``` Use the above line even if you are on Ubuntu or another Debian version. There is also a testing repository if you want beta or release candidates. - deb https://packagecloud.io/grafana/testing/debian/ jessie main +```bash +deb https://packagecloud.io/grafana/testing/debian/ jessie main +``` Then add the [Package Cloud](https://packagecloud.io/grafana) key. This allows you to install signed packages. diff --git a/docs/sources/installation/docker.md b/docs/sources/installation/docker.md index 03e6979d72b..bfb754900fa 100644 --- a/docs/sources/installation/docker.md +++ b/docs/sources/installation/docker.md @@ -14,7 +14,9 @@ weight = 4 Grafana is very easy to install and run using the offical docker container. - $ docker run -d -p 3000:3000 grafana/grafana +```bash +$ docker run -d -p 3000:3000 grafana/grafana +``` All Grafana configuration settings can be defined using environment variables, this is especially useful when using the above container. @@ -26,10 +28,12 @@ folder `/var/lib/grafana` and configuration files is in `/etc/grafana/` folder. You can map these volumes to host folders when you start the container: - $ docker run -d -p 3000:3000 \ - -v /var/lib/grafana:/var/lib/grafana \ - -e "GF_SECURITY_ADMIN_PASSWORD=secret" \ - grafana/grafana +```bash +$ docker run -d -p 3000:3000 \ + -v /var/lib/grafana:/var/lib/grafana \ + -e "GF_SECURITY_ADMIN_PASSWORD=secret" \ + grafana/grafana +``` In the above example I map the data folder and sets a configuration option via an `ENV` instruction. diff --git a/docs/sources/installation/ldap.md b/docs/sources/installation/ldap.md index 769ca3fd1ba..8f6be6e1d8c 100644 --- a/docs/sources/installation/ldap.md +++ b/docs/sources/installation/ldap.md @@ -92,7 +92,7 @@ org_role = "Viewer" By default the configuration expects you to specify a bind DN and bind password. This should be a read only user that can perform LDAP searches. When the user DN is found a second bind is performed with the user provided username & password (in the normal Grafana login form). -``` +```bash bind_dn = "cn=admin,dc=grafana,dc=org" bind_password = "grafana" ``` @@ -102,7 +102,7 @@ bind_password = "grafana" If you can provide a single bind expression that matches all possible users, you can skip the second bind and bind against the user DN directly. This allows you to not specify a bind_password in the configuration file. -``` +```bash bind_dn = "cn=%s,o=users,dc=grafana,dc=org" ``` diff --git a/docs/sources/installation/mac.md b/docs/sources/installation/mac.md index a65c663f398..b1d4f18f699 100644 --- a/docs/sources/installation/mac.md +++ b/docs/sources/installation/mac.md @@ -15,7 +15,7 @@ Installation can be done using [homebrew](http://brew.sh/) Install latest stable: -``` +```bash brew update brew install grafana ``` @@ -24,7 +24,7 @@ To start grafana look at the command printed after the homebrew install complete To upgrade use the reinstall command -``` +```bash brew update brew reinstall grafana ``` @@ -34,13 +34,13 @@ brew reinstall grafana You can also install the latest unstable grafana from git: -``` +```bash brew install --HEAD grafana/grafana/grafana ``` To upgrade grafana if you've installed from HEAD: -``` +```bash brew reinstall --HEAD grafana/grafana/grafana ``` @@ -48,13 +48,13 @@ brew reinstall --HEAD grafana/grafana/grafana To start Grafana using homebrew services first make sure homebrew/services is installed. -``` +```bash brew tap homebrew/services ``` Then start Grafana using: -``` +```bash brew services start grafana ``` diff --git a/docs/sources/installation/rpm.md b/docs/sources/installation/rpm.md index 30b017314c6..cf08173f3e9 100644 --- a/docs/sources/installation/rpm.md +++ b/docs/sources/installation/rpm.md @@ -26,41 +26,53 @@ installation. You can install Grafana using Yum directly. - $ sudo yum install https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-4.5.2-1.x86_64.rpm +```bash +$ sudo yum install https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-4.5.2-1.x86_64.rpm +``` Or install manually using `rpm`. #### On CentOS / Fedora / Redhat: - $ wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-4.5.2-1.x86_64.rpm - $ sudo yum install initscripts fontconfig - $ sudo rpm -Uvh grafana-4.5.2-1.x86_64.rpm +```bash +$ wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-4.5.2-1.x86_64.rpm +$ sudo yum install initscripts fontconfig +$ sudo rpm -Uvh grafana-4.5.2-1.x86_64.rpm +``` #### On OpenSuse: - $ sudo rpm -i --nodeps grafana-4.5.2-1.x86_64.rpm +```bash +$ sudo rpm -i --nodeps grafana-4.5.2-1.x86_64.rpm +``` ## Install via YUM Repository Add the following to a new file at `/etc/yum.repos.d/grafana.repo` - [grafana] - name=grafana - baseurl=https://packagecloud.io/grafana/stable/el/6/$basearch - repo_gpgcheck=1 - enabled=1 - gpgcheck=1 - gpgkey=https://packagecloud.io/gpg.key https://grafanarel.s3.amazonaws.com/RPM-GPG-KEY-grafana - sslverify=1 - sslcacert=/etc/pki/tls/certs/ca-bundle.crt +```bash +[grafana] +name=grafana +baseurl=https://packagecloud.io/grafana/stable/el/6/$basearch +repo_gpgcheck=1 +enabled=1 +gpgcheck=1 +gpgkey=https://packagecloud.io/gpg.key https://grafanarel.s3.amazonaws.com/RPM-GPG-KEY-grafana +sslverify=1 +sslcacert=/etc/pki/tls/certs/ca-bundle.crt +``` There is also a testing repository if you want beta or release candidates. - baseurl=https://packagecloud.io/grafana/testing/el/6/$basearch +```bash +baseurl=https://packagecloud.io/grafana/testing/el/6/$basearch +``` Then install Grafana via the `yum` command. - $ sudo yum install grafana +```bash +$ sudo yum install grafana +``` ### RPM GPG Key @@ -81,7 +93,9 @@ key](https://grafanarel.s3.amazonaws.com/RPM-GPG-KEY-grafana). You can start Grafana by running: - $ sudo service grafana-server start +```bash +$ sudo service grafana-server start +``` This will start the `grafana-server` process as the `grafana` user, which is created during package installation. The default HTTP port is @@ -89,17 +103,23 @@ which is created during package installation. The default HTTP port is To configure the Grafana server to start at boot time: - $ sudo /sbin/chkconfig --add grafana-server +```bash +$ sudo /sbin/chkconfig --add grafana-server +``` ## Start the server (via systemd) - $ systemctl daemon-reload - $ systemctl start grafana-server - $ systemctl status grafana-server +```bash +$ systemctl daemon-reload +$ systemctl start grafana-server +$ systemctl status grafana-server +``` ### Enable the systemd service to start at boot - sudo systemctl enable grafana-server.service +```bash +sudo systemctl enable grafana-server.service +``` ## Environment file @@ -138,7 +158,7 @@ for example in alert notifications. If the image is missing text make sure you have font packages installed. -``` +```bash yum install fontconfig yum install freetype* yum install urw-fonts diff --git a/docs/sources/installation/upgrading.md b/docs/sources/installation/upgrading.md index 4cd8471e441..6a4b4e8f047 100644 --- a/docs/sources/installation/upgrading.md +++ b/docs/sources/installation/upgrading.md @@ -29,7 +29,7 @@ installed grafana to custom location using a binary tar/zip it is usally in ` mysqldump -u root -p[root_password] [grafana] > grafana_backup.sql @@ -39,7 +39,7 @@ restore: #### postgres -``` +```bash backup: > pg_dump grafana > grafana_backup @@ -54,7 +54,7 @@ and execute the same `dpkg -i` command but with the new package. It will upgrade If you used our APT repository: -``` +```bash sudo apt-get update sudo apt-get install grafana ``` @@ -73,14 +73,14 @@ and execute the same `yum install` or `rpm -i` command but with the new package. If you used our YUM repository: -``` +```bash sudo yum update grafana ``` ### Docker This just an example, details depend on how you configured your grafana container. -``` +```bash docker pull grafana docker stop my-grafana-container docker rm my-grafana-container diff --git a/docs/sources/plugins/developing/code-styleguide.md b/docs/sources/plugins/developing/code-styleguide.md index 44379c4d4e5..9ee91412e24 100644 --- a/docs/sources/plugins/developing/code-styleguide.md +++ b/docs/sources/plugins/developing/code-styleguide.md @@ -23,7 +23,7 @@ The most important fields are the first three, especially the id. The convention Examples: -``` +```bash raintank-worldping-app grafana-simple-json-datasource grafana-piechart-panel @@ -66,7 +66,7 @@ The README.md file is rendered both on Grafana.net and in the plugins section in Here is a typical directory structure for a plugin. -``` +```bash johnnyb-awesome-datasource |-- dist |-- spec diff --git a/docs/sources/plugins/developing/datasources.md b/docs/sources/plugins/developing/datasources.md index 612a0786976..0149f06e1aa 100644 --- a/docs/sources/plugins/developing/datasources.md +++ b/docs/sources/plugins/developing/datasources.md @@ -45,7 +45,7 @@ The javascript object that communicates with the database and transforms data to The Datasource should contain the following functions: -``` +```javascript query(options) //used by panels to get data testDatasource() //used by datasource configuration page to make sure the connection is working annotationQuery(options) // used by dashboards to get annotations diff --git a/docs/sources/plugins/installation.md b/docs/sources/plugins/installation.md index 27f6f583d9a..526f1d5d4eb 100644 --- a/docs/sources/plugins/installation.md +++ b/docs/sources/plugins/installation.md @@ -30,37 +30,37 @@ On Linux systems the grafana-cli will assume that the grafana plugin directory i ### Grafana-cli Commands List available plugins -``` +```bash grafana-cli plugins list-remote ``` Install the latest version of a plugin -``` +```bash grafana-cli plugins install ``` Install a specific version of a plugin -``` +```bash grafana-cli plugins install ``` List installed plugins -``` +```bash grafana-cli plugins ls ``` Update all installed plugins -``` +```bash grafana-cli plugins update-all ``` Update one plugin -``` +```bash grafana-cli plugins update ``` Remove one plugin -``` +```bash grafana-cli plugins remove ``` @@ -73,7 +73,7 @@ The Download URL from Grafana.com API is in this form: `https://grafana.com/api/plugins//versions//download` You can specify a local URL by using the `--pluginUrl` option. -``` +```bash grafana-cli --pluginUrl https://nexus.company.com/grafana/plugins/-.zip plugins install ``` @@ -84,7 +84,7 @@ To manually install a Plugin via the Grafana.com API: {{< imgbox img="/img/docs/installation-tab.png" caption="Installation Tab" >}} 2. Use the Grafana API to find the plugin using this url `https://grafana.com/api/plugins/`. For example: https://grafana.com/api/plugins/jdbranham-diagram-panel should return: - ``` + ```bash { "id": 145, "typeId": 3, @@ -97,7 +97,7 @@ To manually install a Plugin via the Grafana.com API: ``` 3. Find the download link: - ``` + ```bash { "rel": "download", "href": "/plugins/jdbranham-diagram-panel/versions/1.4.0/download" diff --git a/docs/sources/project/building_from_source.md b/docs/sources/project/building_from_source.md index c3f65db618d..1e4f0421a0c 100644 --- a/docs/sources/project/building_from_source.md +++ b/docs/sources/project/building_from_source.md @@ -13,27 +13,27 @@ dev environment. Grafana ships with its own required backend server; also comple ## Dependencies -- [Go 1.9](https://golang.org/dl/) +- [Go 1.9.1](https://golang.org/dl/) - [NodeJS LTS](https://nodejs.org/download/) - [Git](https://git-scm.com/downloads) ## Get Code Create a directory for the project and set your path accordingly (or use the [default Go workspace directory](https://golang.org/doc/code.html#GOPATH)). Then download and install Grafana into your $GOPATH directory: -``` +```bash export GOPATH=`pwd` go get github.com/grafana/grafana ``` On Windows use setx instead of export and then restart your command prompt: -``` +```bash setx GOPATH %cd% ``` You may see an error such as: `package github.com/grafana/grafana: no buildable Go source files`. This is just a warning, and you can proceed with the directions. ## Building the backend -``` +```bash cd $GOPATH/src/github.com/grafana/grafana go run build.go setup go run build.go build # (or 'go build ./pkg/cmd/grafana-server') @@ -45,7 +45,7 @@ to install GCC. We recommend [TDM-GCC](http://tdm-gcc.tdragon.net/download). [node-gyp](https://github.com/nodejs/node-gyp#installation) is the Node.js native addon build tool and it requires extra dependencies to be installed on Windows. In a command prompt which is run as administrator, run: -``` +```bash npm --add-python-to-path='true' --debug install --global windows-build-tools ``` @@ -53,7 +53,7 @@ npm --add-python-to-path='true' --debug install --global windows-build-tools For this you need nodejs (v.6+). -``` +```bash npm install -g yarn yarn install --pure-lockfile npm run build @@ -62,7 +62,7 @@ npm run build ## Running Grafana Locally You can run a local instance of Grafana by running: -``` +```bash ./bin/grafana-server ``` If you built the binary with `go run build.go build`, run `./bin/grafana-server` @@ -76,7 +76,7 @@ Open grafana in your browser (default [http://localhost:3000](http://localhost:3 To add features, customize your config, etc, you'll need to rebuild the backend when you change the source code. We use a tool named `bra` that does this. -``` +```bash go get github.com/Unknwon/bra bra run @@ -88,7 +88,7 @@ You'll also need to run `npm run watch` to watch for changes to the front-end (t This step builds linux packages and requires that fpm is installed. Install fpm via `gem install fpm`. -``` +```bash go run build.go build package ``` diff --git a/docs/sources/reference/annotations.md b/docs/sources/reference/annotations.md index e8a15707bec..1b904bc7c4a 100644 --- a/docs/sources/reference/annotations.md +++ b/docs/sources/reference/annotations.md @@ -10,12 +10,45 @@ weight = 2 # Annotations +{{< docs-imagebox img="/img/docs/v46/annotations.png" max-width="800px" >}} + Annotations provide a way to mark points on the graph with rich events. When you hover over an annotation -you can get title, tags, and text information for the event. +you can get event description and event tags. The text field can include links to other systems with more detail. -![](/img/docs/annotations/toggles.png) +## Native annotations -## Queries +Grafana v4.6+ comes with a native annotation store and the ability to add annotation events directly from the graph panel or via the [HTTP API]({{< relref "http_api/annotations.md" >}}). + +## Adding annotations + +By holding down CTRL/CMD + mouse click. Add tags to the annotation will make it searchable from other dashboards. + +{{< docs-imagebox img="/img/docs/annotations/annotation-still.png" +max-width="600px" animated-gif="/img/docs/annotations/annotation.gif" >}} + +### Adding regions events + +You can also hold down CTRL/CMD and select region to create a region annotation. + +{{< docs-imagebox img="/img/docs/annotations/region-annotation-still.png" +max-width="600px" animated-gif="/img/docs/annotations/region-annotation.gif" >}} + +### Built in query + +After you added an annotation they will still be visible. This is due to the built in annotation query that exists on all dashboards. This annotation query will +fetch all annotation events that originate from the current dashboard and show them on the panel where they where created. This includes alert state history annotations. You can +stop annotations from being fetched & drawn by opening the **Annotations** settings (via Dashboard cogs menu) and modifying the query named `Annotations & Alerts (Built-in)`. + +When you copy a dashboard using the **Save As** feature it will get a new dashboard id so annotations created on source dashboard will no longer be visible on the copy. You +can still show them if you add a new **Annotation Query** and filter by tags. But this only works if the annotations on the source dashboard had tags to filter by. + +### Query by tag + +You can create new annotation queries that fetch annotations from the native annotation store via the `-- Grafana --` data source and by setting *Filter by* to `Tags`. Specify at least +one tag. For example create an annotation query name `outages` and specify a tag named `outage`. This query will show all annotations you create (from any dashboard or via API) that +have the `outage` tag. + +## Querying other data sources Annotation events are fetched via annotation queries. To add a new annotation query to a dashboard open the dashboard settings menu, then select `Annotations`. This will open the dashboard annotations diff --git a/docs/sources/reference/dashboard.md b/docs/sources/reference/dashboard.md index 3bbecfcef4d..d2da621a5a3 100644 --- a/docs/sources/reference/dashboard.md +++ b/docs/sources/reference/dashboard.md @@ -24,7 +24,7 @@ When a user creates a new dashboard, a new dashboard JSON object is initialized > Note: In the following JSON, id is shown as null which is the default value assigned to it until a dashboard is saved. Once a dashboard is saved, an integer value is assigned to the `id` field. -``` +```json { "id": null, "title": "New dashboard", diff --git a/docs/sources/reference/sharing.md b/docs/sources/reference/sharing.md index 61ae1f761eb..badd3b5712a 100644 --- a/docs/sources/reference/sharing.md +++ b/docs/sources/reference/sharing.md @@ -43,7 +43,7 @@ You also get a link to service side rendered PNG of the panel. Useful if you wan Example of a link to a server-side rendered PNG: -``` +```bash http://play.grafana.org/render/dashboard-solo/db/grafana-play-home?orgId=1&panelId=4&from=1499272191563&to=1499279391563&width=1000&height=500&tz=UTC%2B02%3A00&timeout=5000 ``` diff --git a/docs/sources/tutorials/api_org_token_howto.md b/docs/sources/tutorials/api_org_token_howto.md index e985b499dbe..984cfd40bd0 100644 --- a/docs/sources/tutorials/api_org_token_howto.md +++ b/docs/sources/tutorials/api_org_token_howto.md @@ -22,24 +22,24 @@ Some parts of the API are only available through basic authentication and these The task is to create a new organization and then add a Token that can be used by other users. In the examples below which use basic auth, the user is `admin` and the password is `admin`. 1. [Create the org](http://docs.grafana.org/http_api/org/#create-organisation). Here is an example using curl: - ``` + ```bash curl -X POST -H "Content-Type: application/json" -d '{"name":"apiorg"}' http://admin:admin@localhost:3000/api/orgs ``` This should return a response: `{"message":"Organization created","orgId":6}`. Use the orgId for the next steps. 2. Optional step. If the org was created previously and/or step 3 fails then first [add your Admin user to the org](http://docs.grafana.org/http_api/org/#add-user-in-organisation): - ``` + ```bash curl -X POST -H "Content-Type: application/json" -d '{"loginOrEmail":"admin", "role": "Admin"}' http://admin:admin@localhost:3000/api/orgs//users ``` 3. [Switch the org context for the Admin user to the new org](http://docs.grafana.org/http_api/user/#switch-user-context): - ``` + ```bash curl -X POST http://admin:admin@localhost:3000/api/user/using/ ``` 4. [Create the API token](http://docs.grafana.org/http_api/auth/#create-api-key): - ``` + ```bash curl -X POST -H "Content-Type: application/json" -d '{"name":"apikeycurl", "role": "Admin"}' http://admin:admin@localhost:3000/api/auth/keys ``` @@ -49,11 +49,11 @@ The task is to create a new organization and then add a Token that can be used b ## How To Add A Dashboard -Using the Token that was created in the previous step, you can create a dashboard or carry out other actions without having to switch organizations. +Using the Token that was created in the previous step, you can create a dashboard or carry out other actions without having to switch organizations. 1. [Add a dashboard](http://docs.grafana.org/http_api/dashboard/#create-update-dashboard) using the key (or bearer token as it is also called): - ``` + ```bash curl -X POST --insecure -H "Authorization: Bearer eyJrIjoiR0ZXZmt1UFc0OEpIOGN5RWdUalBJTllUTk83VlhtVGwiLCJuIjoiYXBpa2V5Y3VybCIsImlkIjo2fQ==" -H "Content-Type: application/json" -d '{ "dashboard": { "id": null, diff --git a/docs/sources/tutorials/authproxy.md b/docs/sources/tutorials/authproxy.md new file mode 100644 index 00000000000..d4d2b9926fc --- /dev/null +++ b/docs/sources/tutorials/authproxy.md @@ -0,0 +1,243 @@ ++++ +title = "Grafana Authproxy" +type = "docs" +keywords = ["grafana", "tutorials", "authproxy"] +[menu.docs] +parent = "tutorials" +weight = 10 ++++ + +# Grafana Authproxy + +AuthProxy allows you to offload the authentication of users to a web server (there are many reasons why you’d want to run a web server in front of a production version of Grafana, especially if it’s exposed to the Internet). + +Popular web servers have a very extensive list of pluggable authentication modules, and any of them can be used with the AuthProxy feature. + +The Grafana AuthProxy feature is very simple in design, but it is this simplicity that makes it so powerful. + +## Interacting with Grafana’s AuthProxy via curl + +The AuthProxy feature can be configured through the Grafana configuration file with the following options: + +```js +[auth.proxy] +enabled = true +header_name = X-WEBAUTH-USER +header_property = username +auto_sign_up = true +``` + +* **enabled**: this is to toggle the feature on or off +* **header_name**: this is the HTTP header name that passes the username or email address of the authenticated user to Grafana. Grafana will trust what ever username is contained in this header and automatically log the user in. +* **header_property**: this tells Grafana whether the value in the header_name is a username or an email address. (In Grafana you can log in using your account username or account email) +* **auto_sign_up**: If set to true, Grafana will automatically create user accounts in the Grafana DB if one does not exist. If set to false, users who do not exist in the GrafanaDB won’t be able to log in, even though their username and password are valid. + +With a fresh install of Grafana, using the above configuration for the authProxy feature, we can send a simple API call to list all users. The only user that will be present is the default “Admin” user that is added the first time Grafana starts up. As you can see all we need to do to authenticate the request is to provide the “X-WEBAUTH-USER” header. + +```bash +curl -H "X-WEBAUTH-USER: admin" http://localhost:3000/api/users +[ + { + "id":1, + "name":"", + "login":"admin", + "email":"admin@localhost", + "isAdmin":true + } +] +``` + +We can then send a second request to the `/api/user` method which will return the details of the logged in user. We will use this request to show how Grafana automatically adds the new user we specify to the system. Here we create a new user called “anthony”. + +```bash +curl -H "X-WEBAUTH-USER: anthony" http://localhost:3000/api/user +{ + "email":"anthony", + "name":"", + "login":"anthony", + "theme":"", + "orgId":1, + "isGrafanaAdmin":false +} +``` + +## Making Apache’s auth work together with Grafana’s AuthProxy + +I’ll demonstrate how to use Apache for authenticating users. In this example we use BasicAuth with Apache’s text file based authentication handler, i.e. htpasswd files. However, any available Apache authentication capabilities could be used. + +### Apache BasicAuth + +In this example we use Apache as a reverseProxy in front of Grafana. Apache handles the Authentication of users before forwarding requests to the Grafana backend service. + +#### Apache configuration + +```bash + + ServerAdmin webmaster@authproxy + ServerName authproxy + ErrorLog "logs/authproxy-error_log" + CustomLog "logs/authproxy-access_log" common + + + AuthType Basic + AuthName GrafanaAuthProxy + AuthBasicProvider file + AuthUserFile /etc/apache2/grafana_htpasswd + Require valid-user + + RewriteEngine On + RewriteRule .* - [E=PROXY_USER:%{LA-U:REMOTE_USER},NS] + RequestHeader set X-WEBAUTH-USER "%{PROXY_USER}e" + + + RequestHeader unset Authorization + + ProxyRequests Off + ProxyPass / http://localhost:3000/ + ProxyPassReverse / http://localhost:3000/ + +``` + +* The first 4 lines of the virtualhost configuration are standard, so we won’t go into detail on what they do. + +* We use a **\** configuration block for applying our authentication rules to every proxied request. These rules include requiring basic authentication where user:password credentials are stored in the **/etc/apache2/grafana_htpasswd** file. This file can be created with the `htpasswd` command. + + * The next part of the configuration is the tricky part. We use Apache’s rewrite engine to create our **X-WEBAUTH-USER header**, populated with the authenticated user. + + * **RewriteRule .* - [E=PROXY_USER:%{LA-U:REMOTE_USER}, NS]**: This line is a little bit of magic. What it does, is for every request use the rewriteEngines look-ahead (LA-U) feature to determine what the REMOTE_USER variable would be set to after processing the request. Then assign the result to the variable PROXY_USER. This is neccessary as the REMOTE_USER variable is not available to the RequestHeader function. + + * **RequestHeader set X-WEBAUTH-USER “%{PROXY_USER}e”**: With the authenticated username now stored in the PROXY_USER variable, we create a new HTTP request header that will be sent to our backend Grafana containing the username. + +* The **RequestHeader unset Authorization** removes the Authorization header from the HTTP request before it is forwarded to Grafana. This ensures that Grafana does not try to authenticate the user using these credentials (BasicAuth is a supported authentication handler in Grafana). + +* The last 3 lines are then just standard reverse proxy configuration to direct all authenticated requests to our Grafana server running on port 3000. + +#### Grafana configuration + +```bash +############# Users ################ +[users] + # disable user signup / registration +allow_sign_up = false + +# Set to true to automatically assign new users to the default organization (id 1) +auto_assign_org = true + +# Default role new users will be automatically assigned (if auto_assign_org above is set to true) + auto_assign_org_role = Editor + + +############ Auth Proxy ######## +[auth.proxy] +enabled = true + +# the Header name that contains the authenticated user. +header_name = X-WEBAUTH-USER + +# does the user authenticate against the proxy using a 'username' or an 'email' +header_property = username + +# automatically add the user to the system if they don't already exist. +auto_sign_up = true +``` + +#### Full walk through using Docker. + +##### Grafana Container + +For this example, we use the offical Grafana docker image available at [Docker Hub](https://hub.docker.com/r/grafana/grafana/) + +* Create a file `grafana.ini` with the following contents + +```bash +[users] +allow_sign_up = false +auto_assign_org = true +auto_assign_org_role = Editor + +[auth.proxy] +enabled = true +header_name = X-WEBAUTH-USER +header_property = username +auto_sign_up = true +``` + +* Launch the Grafana container, using our custom grafana.ini to replace `/etc/grafana/grafana.ini`. We dont expose any ports for this container as it will only be connected to by our Apache container. + +```bash +docker run -i -v $(pwd)/grafana.ini:/etc/grafana/grafana.ini --name grafana grafana/grafana +``` + +### Apache Container + +For this example we use the offical Apache docker image available at [Docker Hub](https://hub.docker.com/_/httpd/) + +* Create a file `httpd.conf` with the following contents + +```bash +ServerRoot "/usr/local/apache2" +Listen 80 +LoadModule authn_file_module modules/mod_authn_file.so +LoadModule authn_core_module modules/mod_authn_core.so +LoadModule authz_host_module modules/mod_authz_host.so +LoadModule authz_user_module modules/mod_authz_user.so +LoadModule authz_core_module modules/mod_authz_core.so +LoadModule auth_basic_module modules/mod_auth_basic.so +LoadModule log_config_module modules/mod_log_config.so +LoadModule env_module modules/mod_env.so +LoadModule headers_module modules/mod_headers.so +LoadModule unixd_module modules/mod_unixd.so +LoadModule rewrite_module modules/mod_rewrite.so +LoadModule proxy_module modules/mod_proxy.so +LoadModule proxy_http_module modules/mod_proxy_http.so + +User daemon +Group daemon + +ServerAdmin you@example.com + + AllowOverride none + Require all denied + +DocumentRoot "/usr/local/apache2/htdocs" +ErrorLog /proc/self/fd/2 +LogLevel error + + LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined + LogFormat "%h %l %u %t \"%r\" %>s %b" common + + LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio + + CustomLog /proc/self/fd/1 common + + + AuthType Basic + AuthName GrafanaAuthProxy + AuthBasicProvider file + AuthUserFile /tmp/htpasswd + Require valid-user + RewriteEngine On + RewriteRule .* - [E=PROXY_USER:%{LA-U:REMOTE_USER},NS] + RequestHeader set X-WEBAUTH-USER "%{PROXY_USER}e" + +RequestHeader unset Authorization +ProxyRequests Off +ProxyPass / http://grafana:3000/ +ProxyPassReverse / http://grafana:3000/ +``` + +* Create a htpasswd file. We create a new user **anthony** with the password **password** + + ```bash + htpasswd -bc htpasswd anthony password + ``` + +* Launch the httpd container using our custom httpd.conf and our htpasswd file. The container will listen on port 80, and we create a link to the **grafana** container so that this container can resolve the hostname **grafana** to the grafana container’s ip address. + + ```bash + docker run -i -p 80:80 --link grafana:grafana -v $(pwd)/httpd.conf:/usr/local/apache2/conf/httpd.conf -v $(pwd)/htpasswd:/tmp/htpasswd httpd:2.4 + ``` + +### Use grafana. + +With our Grafana and Apache containers running, you can now connect to http://localhost/ and log in using the username/password we created in the htpasswd file. \ No newline at end of file diff --git a/docs/sources/tutorials/hubot_howto.md b/docs/sources/tutorials/hubot_howto.md index 58c902951ee..2f122e5b4e2 100644 --- a/docs/sources/tutorials/hubot_howto.md +++ b/docs/sources/tutorials/hubot_howto.md @@ -39,9 +39,9 @@ read the official [Getting Started With Hubot](https://hubot.github.com/docs/) g ## Install Hubot-Grafana script In your Hubot project repo install the Grafana plugin using `npm`: - - npm install hubot-grafana --save - +```bash +npm install hubot-grafana --save +``` Edit the file external-scripts.json, and add hubot-grafana to the list of plugins. ```json @@ -56,13 +56,15 @@ Edit the file external-scripts.json, and add hubot-grafana to the list of plugin The `hubot-grafana` plugin requires a number of environment variables to be set in order to work properly. - export HUBOT_GRAFANA_HOST=http://play.grafana.org - export HUBOT_GRAFANA_API_KEY=abcd01234deadbeef01234 - export HUBOT_GRAFANA_S3_BUCKET=mybucket - export HUBOT_GRAFANA_S3_ACCESS_KEY_ID=ABCDEF123456XYZ - export HUBOT_GRAFANA_S3_SECRET_ACCESS_KEY=aBcD01234dEaDbEef01234 - export HUBOT_GRAFANA_S3_PREFIX=graphs - export HUBOT_GRAFANA_S3_REGION=us-standard +```bash +export HUBOT_GRAFANA_HOST=http://play.grafana.org +export HUBOT_GRAFANA_API_KEY=abcd01234deadbeef01234 +export HUBOT_GRAFANA_S3_BUCKET=mybucket +export HUBOT_GRAFANA_S3_ACCESS_KEY_ID=ABCDEF123456XYZ +export HUBOT_GRAFANA_S3_SECRET_ACCESS_KEY=aBcD01234dEaDbEef01234 +export HUBOT_GRAFANA_S3_PREFIX=graphs +export HUBOT_GRAFANA_S3_REGION=us-standard +``` ### Grafana server side rendering @@ -112,7 +114,9 @@ can create hubot command aliases with the hubot script `hubot-alias`. Install it: - npm i --save hubot-alias +```bash +npm i --save hubot-alias +``` Now add `hubot-alias` to the list of plugins in `external-scripts.json` and restart hubot. diff --git a/package.json b/package.json index 83025a3e8fc..e037b0211cf 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "company": "Grafana Labs" }, "name": "grafana", - "version": "4.6.0-pre1", + "version": "4.6.0-beta1", "repository": { "type": "git", "url": "http://github.com/grafana/grafana.git" @@ -66,7 +66,7 @@ "karma-webpack": "^2.0.4", "lint-staged": "^4.2.3", "load-grunt-tasks": "3.5.2", - "mocha": "3.5.0", + "mocha": "^4.0.1", "ng-annotate-loader": "^0.6.1", "ng-annotate-webpack-plugin": "^0.2.1-pre", "ngtemplate-loader": "^2.0.1", @@ -97,13 +97,14 @@ "watch": "./node_modules/.bin/webpack --progress --colors --watch --config scripts/webpack/webpack.dev.js", "build": "./node_modules/.bin/grunt build", "test": "./node_modules/.bin/grunt test", - "lint" : "./node_modules/.bin/tslint -c tslint.json --project ./tsconfig.json --type-check", + "lint": "./node_modules/.bin/tslint -c tslint.json --project tsconfig.json --type-check", "watch-test": "./node_modules/grunt-cli/bin/grunt karma:dev" }, "license": "Apache-2.0", "dependencies": { "angular": "^1.6.6", "angular-bindonce": "^0.3.1", + "angular-mocks": "^1.6.6", "angular-native-dragdrop": "^1.2.2", "angular-route": "^1.6.6", "angular-sanitize": "^1.6.6", @@ -118,10 +119,11 @@ "mousetrap": "^1.6.0", "ngreact": "^0.4.1", "react": "^16.0.0", - "rxjs": "^5.4.3", "react-dom": "^16.0.0", "remarkable": "^1.7.1", + "rxjs": "^5.4.3", "tether": "^1.4.0", - "tether-drop": "https://github.com/torkelo/drop" + "tether-drop": "https://github.com/torkelo/drop", + "tinycolor2": "^1.4.1" } } diff --git a/pkg/api/annotations.go b/pkg/api/annotations.go index e07c77f1c1d..be069f2b07e 100644 --- a/pkg/api/annotations.go +++ b/pkg/api/annotations.go @@ -1,7 +1,12 @@ package api import ( + "fmt" + "strings" + "time" + "github.com/grafana/grafana/pkg/api/dtos" + "github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/middleware" "github.com/grafana/grafana/pkg/services/annotations" ) @@ -11,13 +16,12 @@ func GetAnnotations(c *middleware.Context) Response { query := &annotations.ItemQuery{ From: c.QueryInt64("from") / 1000, To: c.QueryInt64("to") / 1000, - Type: annotations.ItemType(c.Query("type")), OrgId: c.OrgId, AlertId: c.QueryInt64("alertId"), DashboardId: c.QueryInt64("dashboardId"), PanelId: c.QueryInt64("panelId"), Limit: c.QueryInt64("limit"), - NewState: c.QueryStrings("newState"), + Tags: c.QueryStrings("tags"), } repo := annotations.GetRepository() @@ -27,25 +31,14 @@ func GetAnnotations(c *middleware.Context) Response { return ApiError(500, "Failed to get annotations", err) } - result := make([]dtos.Annotation, 0) - for _, item := range items { - result = append(result, dtos.Annotation{ - AlertId: item.AlertId, - Time: item.Epoch * 1000, - Data: item.Data, - NewState: item.NewState, - PrevState: item.PrevState, - Text: item.Text, - Metric: item.Metric, - Title: item.Title, - PanelId: item.PanelId, - RegionId: item.RegionId, - Type: string(item.Type), - }) + if item.Email != "" { + item.AvatarUrl = dtos.GetGravatarUrl(item.Email) + } + item.Time = item.Time * 1000 } - return Json(200, result) + return Json(200, items) } func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response { @@ -53,14 +46,13 @@ func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response item := annotations.Item{ OrgId: c.OrgId, + UserId: c.UserId, DashboardId: cmd.DashboardId, PanelId: cmd.PanelId, Epoch: cmd.Time / 1000, - Title: cmd.Title, Text: cmd.Text, - CategoryId: cmd.CategoryId, - NewState: cmd.FillColor, - Type: annotations.EventType, + Data: cmd.Data, + Tags: cmd.Tags, } if err := repo.Save(&item); err != nil { @@ -71,12 +63,16 @@ func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response if cmd.IsRegion { item.RegionId = item.Id + if item.Data == nil { + item.Data = simplejson.New() + } + if err := repo.Update(&item); err != nil { return ApiError(500, "Failed set regionId on annotation", err) } item.Id = 0 - item.Epoch = cmd.TimeEnd + item.Epoch = cmd.TimeEnd / 1000 if err := repo.Save(&item); err != nil { return ApiError(500, "Failed save annotation for region end time", err) @@ -86,6 +82,95 @@ func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response return ApiSuccess("Annotation added") } +type GraphiteAnnotationError struct { + message string +} + +func (e *GraphiteAnnotationError) Error() string { + return e.message +} + +func formatGraphiteAnnotation(what string, data string) string { + return fmt.Sprintf("%s\n%s", what, data) +} + +func PostGraphiteAnnotation(c *middleware.Context, cmd dtos.PostGraphiteAnnotationsCmd) Response { + repo := annotations.GetRepository() + + if cmd.When == 0 { + cmd.When = time.Now().Unix() + } + text := formatGraphiteAnnotation(cmd.What, cmd.Data) + + // Support tags in prior to Graphite 0.10.0 format (string of tags separated by space) + var tagsArray []string + switch tags := cmd.Tags.(type) { + case string: + tagsArray = strings.Split(tags, " ") + case []interface{}: + for _, t := range tags { + if tagStr, ok := t.(string); ok { + tagsArray = append(tagsArray, tagStr) + } else { + err := &GraphiteAnnotationError{"tag should be a string"} + return ApiError(500, "Failed to save Graphite annotation", err) + } + } + default: + err := &GraphiteAnnotationError{"unsupported tags format"} + return ApiError(500, "Failed to save Graphite annotation", err) + } + + item := annotations.Item{ + OrgId: c.OrgId, + UserId: c.UserId, + Epoch: cmd.When, + Text: text, + Tags: tagsArray, + } + + if err := repo.Save(&item); err != nil { + return ApiError(500, "Failed to save Graphite annotation", err) + } + + return ApiSuccess("Graphite Annotation added") +} + +func UpdateAnnotation(c *middleware.Context, cmd dtos.UpdateAnnotationsCmd) Response { + annotationId := c.ParamsInt64(":annotationId") + + repo := annotations.GetRepository() + + item := annotations.Item{ + OrgId: c.OrgId, + UserId: c.UserId, + Id: annotationId, + Epoch: cmd.Time / 1000, + Text: cmd.Text, + Tags: cmd.Tags, + } + + if err := repo.Update(&item); err != nil { + return ApiError(500, "Failed to update annotation", err) + } + + if cmd.IsRegion { + itemRight := item + itemRight.RegionId = item.Id + itemRight.Epoch = cmd.TimeEnd / 1000 + + // We don't know id of region right event, so set it to 0 and find then using query like + // ... WHERE region_id = AND id != ... + itemRight.Id = 0 + + if err := repo.Update(&itemRight); err != nil { + return ApiError(500, "Failed to update annotation for region end time", err) + } + } + + return ApiSuccess("Annotation updated") +} + func DeleteAnnotations(c *middleware.Context, cmd dtos.DeleteAnnotationsCmd) Response { repo := annotations.GetRepository() @@ -101,3 +186,33 @@ func DeleteAnnotations(c *middleware.Context, cmd dtos.DeleteAnnotationsCmd) Res return ApiSuccess("Annotations deleted") } + +func DeleteAnnotationById(c *middleware.Context) Response { + repo := annotations.GetRepository() + annotationId := c.ParamsInt64(":annotationId") + + err := repo.Delete(&annotations.DeleteParams{ + Id: annotationId, + }) + + if err != nil { + return ApiError(500, "Failed to delete annotation", err) + } + + return ApiSuccess("Annotation deleted") +} + +func DeleteAnnotationRegion(c *middleware.Context) Response { + repo := annotations.GetRepository() + regionId := c.ParamsInt64(":regionId") + + err := repo.Delete(&annotations.DeleteParams{ + RegionId: regionId, + }) + + if err != nil { + return ApiError(500, "Failed to delete annotation region", err) + } + + return ApiSuccess("Annotation region deleted") +} diff --git a/pkg/api/api.go b/pkg/api/api.go index a979363d528..8c91120facc 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -289,6 +289,10 @@ func (hs *HttpServer) registerRoutes() { apiRoute.Group("/annotations", func(annotationsRoute RouteRegister) { annotationsRoute.Post("/", bind(dtos.PostAnnotationsCmd{}), wrap(PostAnnotation)) + annotationsRoute.Delete("/:annotationId", wrap(DeleteAnnotationById)) + annotationsRoute.Put("/:annotationId", bind(dtos.UpdateAnnotationsCmd{}), wrap(UpdateAnnotation)) + annotationsRoute.Delete("/region/:regionId", wrap(DeleteAnnotationRegion)) + annotationsRoute.Post("/graphite", bind(dtos.PostGraphiteAnnotationsCmd{}), wrap(PostGraphiteAnnotation)) }, reqEditorRole) // error test diff --git a/pkg/api/app_routes.go b/pkg/api/app_routes.go index 8992f8f66d6..0440c880979 100644 --- a/pkg/api/app_routes.go +++ b/pkg/api/app_routes.go @@ -6,31 +6,33 @@ import ( "net/http" "time" - "gopkg.in/macaron.v1" - "github.com/grafana/grafana/pkg/api/pluginproxy" "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/middleware" m "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/plugins" + "github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/util" + macaron "gopkg.in/macaron.v1" ) -var pluginProxyTransport = &http.Transport{ - TLSClientConfig: &tls.Config{ - InsecureSkipVerify: true, - Renegotiation: tls.RenegotiateFreelyAsClient, - }, - Proxy: http.ProxyFromEnvironment, - Dial: (&net.Dialer{ - Timeout: 30 * time.Second, - KeepAlive: 30 * time.Second, - DualStack: true, - }).Dial, - TLSHandshakeTimeout: 10 * time.Second, -} +var pluginProxyTransport *http.Transport func InitAppPluginRoutes(r *macaron.Macaron) { + pluginProxyTransport = &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: setting.PluginAppsSkipVerifyTLS, + Renegotiation: tls.RenegotiateFreelyAsClient, + }, + Proxy: http.ProxyFromEnvironment, + Dial: (&net.Dialer{ + Timeout: 30 * time.Second, + KeepAlive: 30 * time.Second, + DualStack: true, + }).Dial, + TLSHandshakeTimeout: 10 * time.Second, + } + for _, plugin := range plugins.Apps { for _, route := range plugin.Routes { url := util.JoinUrlFragments("/api/plugin-proxy/"+plugin.Id, route.Path) diff --git a/pkg/api/avatar/avatar.go b/pkg/api/avatar/avatar.go index 7abb5da1cec..80280fd3cc9 100644 --- a/pkg/api/avatar/avatar.go +++ b/pkg/api/avatar/avatar.go @@ -65,7 +65,7 @@ func New(hash string) *Avatar { return &Avatar{ hash: hash, reqParams: url.Values{ - "d": {"404"}, + "d": {"retro"}, "size": {"200"}, "r": {"pg"}}.Encode(), } @@ -146,7 +146,7 @@ func CacheServer() http.Handler { } func newNotFound() *Avatar { - avatar := &Avatar{} + avatar := &Avatar{notFound: true} // load transparent png into buffer path := filepath.Join(setting.StaticRootPath, "img", "transparent.png") diff --git a/pkg/api/dtos/annotations.go b/pkg/api/dtos/annotations.go index 958fdff89ca..c917b0d9feb 100644 --- a/pkg/api/dtos/annotations.go +++ b/pkg/api/dtos/annotations.go @@ -2,37 +2,37 @@ package dtos import "github.com/grafana/grafana/pkg/components/simplejson" -type Annotation struct { - AlertId int64 `json:"alertId"` - DashboardId int64 `json:"dashboardId"` - PanelId int64 `json:"panelId"` - NewState string `json:"newState"` - PrevState string `json:"prevState"` - Time int64 `json:"time"` - Title string `json:"title"` - Text string `json:"text"` - Metric string `json:"metric"` - RegionId int64 `json:"regionId"` - Type string `json:"type"` - - Data *simplejson.Json `json:"data"` +type PostAnnotationsCmd struct { + DashboardId int64 `json:"dashboardId"` + PanelId int64 `json:"panelId"` + Time int64 `json:"time"` + Text string `json:"text"` + Tags []string `json:"tags"` + Data *simplejson.Json `json:"data"` + IsRegion bool `json:"isRegion"` + TimeEnd int64 `json:"timeEnd"` } -type PostAnnotationsCmd struct { - DashboardId int64 `json:"dashboardId"` - PanelId int64 `json:"panelId"` - CategoryId int64 `json:"categoryId"` - Time int64 `json:"time"` - Title string `json:"title"` - Text string `json:"text"` - - FillColor string `json:"fillColor"` - IsRegion bool `json:"isRegion"` - TimeEnd int64 `json:"timeEnd"` +type UpdateAnnotationsCmd struct { + Id int64 `json:"id"` + Time int64 `json:"time"` + Text string `json:"text"` + Tags []string `json:"tags"` + IsRegion bool `json:"isRegion"` + TimeEnd int64 `json:"timeEnd"` } type DeleteAnnotationsCmd struct { - AlertId int64 `json:"alertId"` - DashboardId int64 `json:"dashboardId"` - PanelId int64 `json:"panelId"` + AlertId int64 `json:"alertId"` + DashboardId int64 `json:"dashboardId"` + PanelId int64 `json:"panelId"` + AnnotationId int64 `json:"annotationId"` + RegionId int64 `json:"regionId"` +} + +type PostGraphiteAnnotationsCmd struct { + When int64 `json:"when"` + What string `json:"what"` + Data string `json:"data"` + Tags interface{} `json:"tags"` } diff --git a/pkg/api/grafana_com_proxy.go b/pkg/api/grafana_com_proxy.go index 015f690adda..a2a446b48eb 100644 --- a/pkg/api/grafana_com_proxy.go +++ b/pkg/api/grafana_com_proxy.go @@ -1,7 +1,6 @@ package api import ( - "crypto/tls" "net" "net/http" "net/http/httputil" @@ -14,8 +13,7 @@ import ( ) var grafanaComProxyTransport = &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: false}, - Proxy: http.ProxyFromEnvironment, + Proxy: http.ProxyFromEnvironment, Dial: (&net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, diff --git a/pkg/api/http_server.go b/pkg/api/http_server.go index b43d55b2a8f..3107dcf7e4b 100644 --- a/pkg/api/http_server.go +++ b/pkg/api/http_server.go @@ -11,6 +11,8 @@ import ( "path" "time" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" gocache "github.com/patrickmn/go-cache" @@ -19,7 +21,6 @@ import ( "github.com/grafana/grafana/pkg/api/live" httpstatic "github.com/grafana/grafana/pkg/api/static" "github.com/grafana/grafana/pkg/bus" - "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger" "github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/middleware" @@ -153,7 +154,7 @@ func (hs *HttpServer) newMacaron() *macaron.Macaron { for _, route := range plugins.StaticRoutes { pluginRoute := path.Join("/public/plugins/", route.PluginId) - logger.Debug("Plugins: Adding route", "route", pluginRoute, "dir", route.Directory) + hs.log.Debug("Plugins: Adding route", "route", pluginRoute, "dir", route.Directory) hs.mapStatic(m, route.Directory, "", pluginRoute) } @@ -187,7 +188,9 @@ func (hs *HttpServer) metricsEndpoint(ctx *macaron.Context) { return } - promhttp.Handler().ServeHTTP(ctx.Resp, ctx.Req.Request) + promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{ + DisableCompression: true, + }).ServeHTTP(ctx.Resp, ctx.Req.Request) } func (hs *HttpServer) healthHandler(ctx *macaron.Context) { diff --git a/pkg/api/login_oauth.go b/pkg/api/login_oauth.go index 4be49915fd9..847f09f0eb8 100644 --- a/pkg/api/login_oauth.go +++ b/pkg/api/login_oauth.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" "io/ioutil" - "log" "net/http" "net/url" @@ -16,6 +15,7 @@ import ( "golang.org/x/oauth2" "github.com/grafana/grafana/pkg/bus" + "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/metrics" "github.com/grafana/grafana/pkg/middleware" m "github.com/grafana/grafana/pkg/models" @@ -29,6 +29,7 @@ var ( ErrSignUpNotAllowed = errors.New("Signup is not allowed for this adapter") ErrUsersQuotaReached = errors.New("Users quota reached") ErrNoEmail = errors.New("Login provider didn't return an email address") + oauthLogger = log.New("oauth.login") ) func GenStateString() string { @@ -50,10 +51,11 @@ func OAuthLogin(ctx *middleware.Context) { return } - error := ctx.Query("error") - if error != "" { + errorParam := ctx.Query("error") + if errorParam != "" { errorDesc := ctx.Query("error_description") - redirectWithError(ctx, ErrProviderDeniedRequest, "error", error, "errorDesc", errorDesc) + oauthLogger.Error("failed to login ", "error", errorParam, "errorDesc", errorDesc) + redirectWithError(ctx, ErrProviderDeniedRequest, "error", errorParam, "errorDesc", errorDesc) return } @@ -69,8 +71,12 @@ func OAuthLogin(ctx *middleware.Context) { return } - // verify state string - savedState := ctx.Session.Get(middleware.SESS_KEY_OAUTH_STATE).(string) + savedState, ok := ctx.Session.Get(middleware.SESS_KEY_OAUTH_STATE).(string) + if !ok { + ctx.Handle(500, "login.OAuthLogin(missing saved state)", nil) + return + } + queryState := ctx.Query("state") if savedState != queryState { ctx.Handle(500, "login.OAuthLogin(state mismatch)", nil) @@ -78,36 +84,37 @@ func OAuthLogin(ctx *middleware.Context) { } // handle call back + tr := &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: setting.OAuthService.OAuthInfos[name].TlsSkipVerify, + }, + } + oauthClient := &http.Client{ + Transport: tr, + } - // initialize oauth2 context - oauthCtx := oauth2.NoContext - if setting.OAuthService.OAuthInfos[name].TlsClientCert != "" { + if setting.OAuthService.OAuthInfos[name].TlsClientCert != "" || setting.OAuthService.OAuthInfos[name].TlsClientKey != "" { cert, err := tls.LoadX509KeyPair(setting.OAuthService.OAuthInfos[name].TlsClientCert, setting.OAuthService.OAuthInfos[name].TlsClientKey) if err != nil { - log.Fatal(err) + log.Fatal(1, "Failed to setup TlsClientCert", "oauth provider", name, "error", err) } - // Load CA cert + tr.TLSClientConfig.Certificates = append(tr.TLSClientConfig.Certificates, cert) + } + + if setting.OAuthService.OAuthInfos[name].TlsClientCa != "" { caCert, err := ioutil.ReadFile(setting.OAuthService.OAuthInfos[name].TlsClientCa) if err != nil { - log.Fatal(err) + log.Fatal(1, "Failed to setup TlsClientCa", "oauth provider", name, "error", err) } caCertPool := x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) - tr := &http.Transport{ - TLSClientConfig: &tls.Config{ - InsecureSkipVerify: true, - Certificates: []tls.Certificate{cert}, - RootCAs: caCertPool, - }, - } - sslcli := &http.Client{Transport: tr} - - oauthCtx = context.Background() - oauthCtx = context.WithValue(oauthCtx, oauth2.HTTPClient, sslcli) + tr.TLSClientConfig.RootCAs = caCertPool } + oauthCtx := context.WithValue(context.Background(), oauth2.HTTPClient, oauthClient) + // get token from provider token, err := connect.Exchange(oauthCtx, code) if err != nil { diff --git a/pkg/api/plugins.go b/pkg/api/plugins.go index 042c03f9832..0483b624a30 100644 --- a/pkg/api/plugins.go +++ b/pkg/api/plugins.go @@ -158,7 +158,9 @@ func GetPluginMarkdown(c *middleware.Context) Response { return ApiError(500, "Could not get markdown file", err) } else { - return Respond(200, content) + resp := Respond(200, content) + resp.Header("Content-Type", "text/plain; charset=utf-8") + return resp } } diff --git a/pkg/cmd/grafana-cli/main.go b/pkg/cmd/grafana-cli/main.go index 73548c3b159..86eb6bc271a 100644 --- a/pkg/cmd/grafana-cli/main.go +++ b/pkg/cmd/grafana-cli/main.go @@ -17,8 +17,6 @@ var version = "master" func main() { setupLogging() - services.Init(version) - app := cli.NewApp() app.Name = "Grafana cli" app.Usage = "" @@ -44,12 +42,20 @@ func main() { Value: "", EnvVar: "GF_PLUGIN_URL", }, + cli.BoolFlag{ + Name: "insecure", + Usage: "Skip TLS verification (insecure)", + }, cli.BoolFlag{ Name: "debug, d", Usage: "enable debug logging", }, } + app.Before = func(c *cli.Context) error { + services.Init(version, c.GlobalBool("insecure")) + return nil + } app.Commands = commands.Commands app.CommandNotFound = cmdNotFound diff --git a/pkg/cmd/grafana-cli/services/services.go b/pkg/cmd/grafana-cli/services/services.go index d3a05430944..d13e90d6a2f 100644 --- a/pkg/cmd/grafana-cli/services/services.go +++ b/pkg/cmd/grafana-cli/services/services.go @@ -22,7 +22,7 @@ var ( grafanaVersion string ) -func Init(version string) { +func Init(version string, skipTLSVerify bool) { grafanaVersion = version tr := &http.Transport{ @@ -36,8 +36,9 @@ func Init(version string) { IdleConnTimeout: 90 * time.Second, TLSHandshakeTimeout: 10 * time.Second, ExpectContinueTimeout: 1 * time.Second, - - TLSClientConfig: &tls.Config{InsecureSkipVerify: false}, + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: skipTLSVerify, + }, } HttpClient = http.Client{ diff --git a/pkg/cmd/grafana-server/main.go b/pkg/cmd/grafana-server/main.go index 6545987152d..fa63f05efba 100644 --- a/pkg/cmd/grafana-server/main.go +++ b/pkg/cmd/grafana-server/main.go @@ -26,7 +26,7 @@ import ( _ "github.com/grafana/grafana/pkg/tsdb/influxdb" _ "github.com/grafana/grafana/pkg/tsdb/mysql" _ "github.com/grafana/grafana/pkg/tsdb/opentsdb" - + _ "github.com/grafana/grafana/pkg/tsdb/postgres" _ "github.com/grafana/grafana/pkg/tsdb/prometheus" _ "github.com/grafana/grafana/pkg/tsdb/testdata" ) diff --git a/pkg/components/imguploader/imguploader.go b/pkg/components/imguploader/imguploader.go index 1feeb6f07c3..728614735d0 100644 --- a/pkg/components/imguploader/imguploader.go +++ b/pkg/components/imguploader/imguploader.go @@ -30,9 +30,15 @@ func NewImageUploader() (ImageUploader, error) { bucket := s3sec.Key("bucket").MustString("") region := s3sec.Key("region").MustString("") + path := s3sec.Key("path").MustString("") bucketUrl := s3sec.Key("bucket_url").MustString("") accessKey := s3sec.Key("access_key").MustString("") secretKey := s3sec.Key("secret_key").MustString("") + + if path != "" && path[len(path)-1:] != "/" { + path += "/" + } + if bucket == "" || region == "" { info, err := getRegionAndBucketFromUrl(bucketUrl) if err != nil { @@ -42,7 +48,7 @@ func NewImageUploader() (ImageUploader, error) { region = info.region } - return NewS3Uploader(region, bucket, "public-read", accessKey, secretKey), nil + return NewS3Uploader(region, bucket, path, "public-read", accessKey, secretKey), nil case "webdav": webdavSec, err := setting.Cfg.GetSection("external_image_storage.webdav") if err != nil { diff --git a/pkg/components/imguploader/s3uploader.go b/pkg/components/imguploader/s3uploader.go index 860bb1a1abd..62196357c61 100644 --- a/pkg/components/imguploader/s3uploader.go +++ b/pkg/components/imguploader/s3uploader.go @@ -19,16 +19,18 @@ import ( type S3Uploader struct { region string bucket string + path string acl string secretKey string accessKey string log log.Logger } -func NewS3Uploader(region, bucket, acl, accessKey, secretKey string) *S3Uploader { +func NewS3Uploader(region, bucket, path, acl, accessKey, secretKey string) *S3Uploader { return &S3Uploader{ region: region, bucket: bucket, + path: path, acl: acl, accessKey: accessKey, secretKey: secretKey, @@ -56,7 +58,7 @@ func (u *S3Uploader) Upload(ctx context.Context, imageDiskPath string) (string, } s3_endpoint, _ := endpoints.DefaultResolver().EndpointFor("s3", u.region) - key := util.GetRandomString(20) + ".png" + key := u.path + util.GetRandomString(20) + ".png" image_url := s3_endpoint.URL + "/" + u.bucket + "/" + key log.Debug("Uploading image to s3", "url = ", image_url) diff --git a/pkg/models/dashboard_snapshot.go b/pkg/models/dashboard_snapshot.go index 57c5524ace8..9273b88f291 100644 --- a/pkg/models/dashboard_snapshot.go +++ b/pkg/models/dashboard_snapshot.go @@ -73,11 +73,12 @@ type GetDashboardSnapshotQuery struct { } type DashboardSnapshots []*DashboardSnapshot +type DashboardSnapshotsList []*DashboardSnapshotDTO type GetDashboardSnapshotsQuery struct { Name string Limit int OrgId int64 - Result DashboardSnapshots + Result DashboardSnapshotsList } diff --git a/pkg/models/datasource.go b/pkg/models/datasource.go index 4f10033425f..069900cc091 100644 --- a/pkg/models/datasource.go +++ b/pkg/models/datasource.go @@ -17,6 +17,7 @@ const ( DS_CLOUDWATCH = "cloudwatch" DS_KAIROSDB = "kairosdb" DS_PROMETHEUS = "prometheus" + DS_POSTGRES = "postgres" DS_ACCESS_DIRECT = "direct" DS_ACCESS_PROXY = "proxy" ) @@ -62,6 +63,7 @@ var knownDatasourcePlugins map[string]bool = map[string]bool{ DS_CLOUDWATCH: true, DS_PROMETHEUS: true, DS_OPENTSDB: true, + DS_POSTGRES: true, "opennms": true, "druid": true, "dalmatinerdb": true, diff --git a/pkg/models/datasource_cache.go b/pkg/models/datasource_cache.go index 158018b0f0a..b4a4e7f8a4d 100644 --- a/pkg/models/datasource_cache.go +++ b/pkg/models/datasource_cache.go @@ -3,6 +3,7 @@ package models import ( "crypto/tls" "crypto/x509" + "errors" "net" "net/http" "sync" @@ -45,9 +46,16 @@ func (ds *DataSource) GetHttpTransport() (*http.Transport, error) { return t.Transport, nil } + var tlsSkipVerify, tlsClientAuth, tlsAuthWithCACert bool + if ds.JsonData != nil { + tlsClientAuth = ds.JsonData.Get("tlsAuth").MustBool(false) + tlsAuthWithCACert = ds.JsonData.Get("tlsAuthWithCACert").MustBool(false) + tlsSkipVerify = ds.JsonData.Get("tlsSkipVerify").MustBool(false) + } + transport := &http.Transport{ TLSClientConfig: &tls.Config{ - InsecureSkipVerify: true, + InsecureSkipVerify: tlsSkipVerify, Renegotiation: tls.RenegotiateFreelyAsClient, }, Proxy: http.ProxyFromEnvironment, @@ -62,30 +70,24 @@ func (ds *DataSource) GetHttpTransport() (*http.Transport, error) { IdleConnTimeout: 90 * time.Second, } - var tlsAuth, tlsAuthWithCACert bool - if ds.JsonData != nil { - tlsAuth = ds.JsonData.Get("tlsAuth").MustBool(false) - tlsAuthWithCACert = ds.JsonData.Get("tlsAuthWithCACert").MustBool(false) - } - - if tlsAuth { - transport.TLSClientConfig.InsecureSkipVerify = false - + if tlsClientAuth || tlsAuthWithCACert { decrypted := ds.SecureJsonData.Decrypt() - if tlsAuthWithCACert && len(decrypted["tlsCACert"]) > 0 { caPool := x509.NewCertPool() ok := caPool.AppendCertsFromPEM([]byte(decrypted["tlsCACert"])) - if ok { - transport.TLSClientConfig.RootCAs = caPool + if !ok { + return nil, errors.New("Failed to parse TLS CA PEM certificate") } + transport.TLSClientConfig.RootCAs = caPool } - cert, err := tls.X509KeyPair([]byte(decrypted["tlsClientCert"]), []byte(decrypted["tlsClientKey"])) - if err != nil { - return nil, err + if tlsClientAuth { + cert, err := tls.X509KeyPair([]byte(decrypted["tlsClientCert"]), []byte(decrypted["tlsClientKey"])) + if err != nil { + return nil, err + } + transport.TLSClientConfig.Certificates = []tls.Certificate{cert} } - transport.TLSClientConfig.Certificates = []tls.Certificate{cert} } ptc.cache[ds.Id] = cachedTransport{ diff --git a/pkg/models/datasource_cache_test.go b/pkg/models/datasource_cache_test.go index 5e821ea28c4..85ece0bbdcc 100644 --- a/pkg/models/datasource_cache_test.go +++ b/pkg/models/datasource_cache_test.go @@ -29,61 +29,140 @@ func TestDataSourceCache(t *testing.T) { Convey("Should be using the cached proxy", func() { So(t2, ShouldEqual, t1) }) + Convey("Should verify TLS by default", func() { + So(t1.TLSClientConfig.InsecureSkipVerify, ShouldEqual, false) + }) + Convey("Should have no TLS client certificate configured", func() { + So(len(t1.TLSClientConfig.Certificates), ShouldEqual, 0) + }) + Convey("Should have no user-supplied TLS CA onfigured", func() { + So(t1.TLSClientConfig.RootCAs, ShouldBeNil) + }) }) - Convey("When getting kubernetes datasource proxy", t, func() { + Convey("When caching a datasource proxy then updating it", t, func() { + clearCache() + setting.SecretKey = "password" + + json := simplejson.New() + json.Set("tlsAuthWithCACert", true) + + tlsCaCert, err := util.Encrypt([]byte(caCert), "password") + So(err, ShouldBeNil) + ds := DataSource{ + Id: 1, + Url: "http://k8s:8001", + Type: "Kubernetes", + SecureJsonData: map[string][]byte{"tlsCACert": tlsCaCert}, + Updated: time.Now().Add(-2 * time.Minute), + } + + t1, err := ds.GetHttpTransport() + So(err, ShouldBeNil) + + Convey("Should verify TLS by default", func() { + So(t1.TLSClientConfig.InsecureSkipVerify, ShouldEqual, false) + }) + Convey("Should have no TLS client certificate configured", func() { + So(len(t1.TLSClientConfig.Certificates), ShouldEqual, 0) + }) + Convey("Should have no user-supplied TLS CA configured", func() { + So(t1.TLSClientConfig.RootCAs, ShouldBeNil) + }) + + ds.JsonData = nil + ds.SecureJsonData = map[string][]byte{} + ds.Updated = time.Now() + + t2, err := ds.GetHttpTransport() + So(err, ShouldBeNil) + + Convey("Should have no user-supplied TLS CA configured after the update", func() { + So(t2.TLSClientConfig.RootCAs, ShouldBeNil) + }) + }) + + Convey("When caching a datasource proxy with TLS client authentication enabled", t, func() { clearCache() setting.SecretKey = "password" json := simplejson.New() json.Set("tlsAuth", true) + + tlsClientCert, err := util.Encrypt([]byte(clientCert), "password") + So(err, ShouldBeNil) + tlsClientKey, err := util.Encrypt([]byte(clientKey), "password") + So(err, ShouldBeNil) + + ds := DataSource{ + Id: 1, + Url: "http://k8s:8001", + Type: "Kubernetes", + JsonData: json, + SecureJsonData: map[string][]byte{ + "tlsClientCert": tlsClientCert, + "tlsClientKey": tlsClientKey, + }, + } + + tr, err := ds.GetHttpTransport() + So(err, ShouldBeNil) + + Convey("Should verify TLS by default", func() { + So(tr.TLSClientConfig.InsecureSkipVerify, ShouldEqual, false) + }) + Convey("Should have a TLS client certificate configured", func() { + So(len(tr.TLSClientConfig.Certificates), ShouldEqual, 1) + }) + }) + + Convey("When caching a datasource proxy with a user-supplied TLS CA", t, func() { + clearCache() + setting.SecretKey = "password" + + json := simplejson.New() json.Set("tlsAuthWithCACert", true) - t := time.Now() + tlsCaCert, err := util.Encrypt([]byte(caCert), "password") + So(err, ShouldBeNil) + ds := DataSource{ - Url: "http://k8s:8001", - Type: "Kubernetes", - Updated: t.Add(-2 * time.Minute), + Id: 1, + Url: "http://k8s:8001", + Type: "Kubernetes", + JsonData: json, + SecureJsonData: map[string][]byte{"tlsCACert": tlsCaCert}, } - transport, err := ds.GetHttpTransport() + tr, err := ds.GetHttpTransport() So(err, ShouldBeNil) - Convey("Should have no cert", func() { - So(transport.TLSClientConfig.InsecureSkipVerify, ShouldEqual, true) + Convey("Should verify TLS by default", func() { + So(tr.TLSClientConfig.InsecureSkipVerify, ShouldEqual, false) }) + Convey("Should have a TLS CA configured", func() { + So(len(tr.TLSClientConfig.RootCAs.Subjects()), ShouldEqual, 1) + }) + }) - ds.JsonData = json + Convey("When caching a datasource proxy when user skips TLS verification", t, func() { + clearCache() - tlsCaCert, _ := util.Encrypt([]byte(caCert), "password") - tlsClientCert, _ := util.Encrypt([]byte(clientCert), "password") - tlsClientKey, _ := util.Encrypt([]byte(clientKey), "password") + json := simplejson.New() + json.Set("tlsSkipVerify", true) - ds.SecureJsonData = map[string][]byte{ - "tlsCACert": tlsCaCert, - "tlsClientCert": tlsClientCert, - "tlsClientKey": tlsClientKey, + ds := DataSource{ + Id: 1, + Url: "http://k8s:8001", + Type: "Kubernetes", + JsonData: json, } - ds.Updated = t.Add(-1 * time.Minute) - transport, err = ds.GetHttpTransport() + tr, err := ds.GetHttpTransport() So(err, ShouldBeNil) - Convey("Should add cert", func() { - So(transport.TLSClientConfig.InsecureSkipVerify, ShouldEqual, false) - So(len(transport.TLSClientConfig.Certificates), ShouldEqual, 1) - }) - - ds.JsonData = nil - ds.SecureJsonData = map[string][]byte{} - ds.Updated = t - - transport, err = ds.GetHttpTransport() - So(err, ShouldBeNil) - - Convey("Should remove cert", func() { - So(transport.TLSClientConfig.InsecureSkipVerify, ShouldEqual, true) - So(len(transport.TLSClientConfig.Certificates), ShouldEqual, 0) + Convey("Should skip TLS verification", func() { + So(tr.TLSClientConfig.InsecureSkipVerify, ShouldEqual, true) }) }) } @@ -115,7 +194,8 @@ FHoXIyGOdq1chmRVocdGBCF8fUoGIbuF14r53rpvcbEKtKnnP8+96luKAZLq0a4n 3lb92xM= -----END CERTIFICATE-----` -const clientCert string = `-----BEGIN CERTIFICATE----- +const clientCert string = ` +-----BEGIN CERTIFICATE----- MIICsjCCAZoCCQCcd8sOfstQLzANBgkqhkiG9w0BAQsFADAXMRUwEwYDVQQDDAxj YS1rOHMtc3RobG0wHhcNMTYxMTAyMDkyNTE1WhcNMTcxMTAyMDkyNTE1WjAfMR0w GwYDVQQDDBRhZG0tZGFuaWVsLWs4cy1zdGhsbTCCASIwDQYJKoZIhvcNAQEBBQAD diff --git a/pkg/models/tags.go b/pkg/models/tags.go new file mode 100644 index 00000000000..1b90b7d55ba --- /dev/null +++ b/pkg/models/tags.go @@ -0,0 +1,60 @@ +package models + +import ( + "strings" +) + +type Tag struct { + Id int64 + Key string + Value string +} + +func ParseTagPairs(tagPairs []string) (tags []*Tag) { + if tagPairs == nil { + return []*Tag{} + } + + for _, tagPair := range tagPairs { + var tag Tag + + if strings.Contains(tagPair, ":") { + keyValue := strings.Split(tagPair, ":") + tag.Key = strings.Trim(keyValue[0], " ") + tag.Value = strings.Trim(keyValue[1], " ") + } else { + tag.Key = strings.Trim(tagPair, " ") + } + + if tag.Key == "" || ContainsTag(tags, &tag) { + continue + } + + tags = append(tags, &tag) + } + + return tags +} + +func ContainsTag(existingTags []*Tag, tag *Tag) bool { + for _, t := range existingTags { + if t.Key == tag.Key && t.Value == tag.Value { + return true + } + } + return false +} + +func JoinTagPairs(tags []*Tag) []string { + tagPairs := []string{} + + for _, tag := range tags { + if tag.Value != "" { + tagPairs = append(tagPairs, tag.Key+":"+tag.Value) + } else { + tagPairs = append(tagPairs, tag.Key) + } + } + + return tagPairs +} diff --git a/pkg/models/tags_test.go b/pkg/models/tags_test.go new file mode 100644 index 00000000000..7d95187d668 --- /dev/null +++ b/pkg/models/tags_test.go @@ -0,0 +1,95 @@ +package models + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestParsingTags(t *testing.T) { + Convey("Testing parsing a tag pairs into tags", t, func() { + Convey("Can parse one empty tag", func() { + tags := ParseTagPairs([]string{""}) + So(len(tags), ShouldEqual, 0) + }) + + Convey("Can parse valid tags", func() { + tags := ParseTagPairs([]string{"outage", "type:outage", "error"}) + So(len(tags), ShouldEqual, 3) + So(tags[0].Key, ShouldEqual, "outage") + So(tags[0].Value, ShouldEqual, "") + So(tags[1].Key, ShouldEqual, "type") + So(tags[1].Value, ShouldEqual, "outage") + So(tags[2].Key, ShouldEqual, "error") + So(tags[2].Value, ShouldEqual, "") + }) + + Convey("Can parse tags with spaces", func() { + tags := ParseTagPairs([]string{" outage ", " type : outage ", "error "}) + So(len(tags), ShouldEqual, 3) + So(tags[0].Key, ShouldEqual, "outage") + So(tags[0].Value, ShouldEqual, "") + So(tags[1].Key, ShouldEqual, "type") + So(tags[1].Value, ShouldEqual, "outage") + So(tags[2].Key, ShouldEqual, "error") + So(tags[2].Value, ShouldEqual, "") + }) + + Convey("Can parse empty tags", func() { + tags := ParseTagPairs([]string{" outage ", "", "", ":", "type : outage ", "error ", "", ""}) + So(len(tags), ShouldEqual, 3) + So(tags[0].Key, ShouldEqual, "outage") + So(tags[0].Value, ShouldEqual, "") + So(tags[1].Key, ShouldEqual, "type") + So(tags[1].Value, ShouldEqual, "outage") + So(tags[2].Key, ShouldEqual, "error") + So(tags[2].Value, ShouldEqual, "") + }) + + Convey("Can parse tags with extra colons", func() { + tags := ParseTagPairs([]string{" outage", "type : outage:outage2 :outage3 ", "error :"}) + So(len(tags), ShouldEqual, 3) + So(tags[0].Key, ShouldEqual, "outage") + So(tags[0].Value, ShouldEqual, "") + So(tags[1].Key, ShouldEqual, "type") + So(tags[1].Value, ShouldEqual, "outage") + So(tags[2].Key, ShouldEqual, "error") + So(tags[2].Value, ShouldEqual, "") + }) + + Convey("Can parse tags that contains key and values with spaces", func() { + tags := ParseTagPairs([]string{" outage 1", "type 1: outage 1 ", "has error "}) + So(len(tags), ShouldEqual, 3) + So(tags[0].Key, ShouldEqual, "outage 1") + So(tags[0].Value, ShouldEqual, "") + So(tags[1].Key, ShouldEqual, "type 1") + So(tags[1].Value, ShouldEqual, "outage 1") + So(tags[2].Key, ShouldEqual, "has error") + So(tags[2].Value, ShouldEqual, "") + }) + + Convey("Can filter out duplicate tags", func() { + tags := ParseTagPairs([]string{"test", "test", "key:val1", "key:val2"}) + So(len(tags), ShouldEqual, 3) + So(tags[0].Key, ShouldEqual, "test") + So(tags[0].Value, ShouldEqual, "") + So(tags[1].Key, ShouldEqual, "key") + So(tags[1].Value, ShouldEqual, "val1") + So(tags[2].Key, ShouldEqual, "key") + So(tags[2].Value, ShouldEqual, "val2") + }) + + Convey("Can join tag pairs", func() { + tagPairs := []*Tag{ + {Key: "key1", Value: "val1"}, + {Key: "key2", Value: ""}, + {Key: "key3"}, + } + tags := JoinTagPairs(tagPairs) + So(len(tags), ShouldEqual, 3) + So(tags[0], ShouldEqual, "key1:val1") + So(tags[1], ShouldEqual, "key2") + So(tags[2], ShouldEqual, "key3") + }) + }) +} diff --git a/pkg/services/alerting/notifiers/hipchat.go b/pkg/services/alerting/notifiers/hipchat.go index aefa7de6ede..f1f63d42a04 100644 --- a/pkg/services/alerting/notifiers/hipchat.go +++ b/pkg/services/alerting/notifiers/hipchat.go @@ -84,15 +84,17 @@ func (this *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error { return err } - message := evalContext.GetNotificationTitle() + " in state " + evalContext.GetStateModel().Text + "
Check Dashboard" - fields := make([]map[string]interface{}, 0) - message += "
" + attributes := make([]map[string]interface{}, 0) for index, evt := range evalContext.EvalMatches { - message += evt.Metric + " :: " + strconv.FormatFloat(evt.Value.Float64, 'f', -1, 64) + "
" - fields = append(fields, map[string]interface{}{ - "title": evt.Metric, - "value": evt.Value, - "short": true, + metricName := evt.Metric + if len(metricName) > 50 { + metricName = metricName[:50] + } + attributes = append(attributes, map[string]interface{}{ + "label": metricName, + "value": map[string]interface{}{ + "label": strconv.FormatFloat(evt.Value.Float64, 'f', -1, 64), + }, }) if index > maxFieldCount { break @@ -100,16 +102,23 @@ func (this *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error { } if evalContext.Error != nil { - fields = append(fields, map[string]interface{}{ - "title": "Error message", - "value": evalContext.Error.Error(), - "short": false, + attributes = append(attributes, map[string]interface{}{ + "label": "Error message", + "value": map[string]interface{}{ + "label": evalContext.Error.Error(), + }, }) } + message := "" if evalContext.Rule.State != models.AlertStateOK { //dont add message when going back to alert state ok. message += " " + evalContext.Rule.Message } + + if message == "" { + message = evalContext.GetNotificationTitle() + " in state " + evalContext.GetStateModel().Text + } + //HipChat has a set list of colors var color string switch evalContext.Rule.State { @@ -123,15 +132,24 @@ func (this *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error { // Add a card with link to the dashboard card := map[string]interface{}{ - "style": "link", + "style": "application", "url": ruleUrl, "id": "1", "title": evalContext.GetNotificationTitle(), - "description": evalContext.GetNotificationTitle() + " in state " + evalContext.GetStateModel().Text, + "description": message, "icon": map[string]interface{}{ "url": "https://grafana.com/assets/img/fav32.png", }, - "date": evalContext.EndTime.Unix(), + "date": evalContext.EndTime.Unix(), + "attributes": attributes, + } + if evalContext.ImagePublicUrl != "" { + card["thumbnail"] = map[string]interface{}{ + "url": evalContext.ImagePublicUrl, + "url@2x": evalContext.ImagePublicUrl, + "width": 1193, + "height": 564, + } } body := map[string]interface{}{ @@ -144,6 +162,7 @@ func (this *HipChatNotifier) Notify(evalContext *alerting.EvalContext) error { hipUrl := fmt.Sprintf("%s/v2/room/%s/notification?auth_token=%s", this.Url, this.RoomId, this.ApiKey) data, _ := json.Marshal(&body) + this.log.Info("Request payload", "json", string(data)) cmd := &models.SendWebhookSync{Url: hipUrl, Body: string(data)} if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil { diff --git a/pkg/services/alerting/notifiers/kafka.go b/pkg/services/alerting/notifiers/kafka.go new file mode 100644 index 00000000000..92f6489106b --- /dev/null +++ b/pkg/services/alerting/notifiers/kafka.go @@ -0,0 +1,120 @@ +package notifiers + +import ( + "strconv" + + "fmt" + + "github.com/grafana/grafana/pkg/bus" + "github.com/grafana/grafana/pkg/components/simplejson" + "github.com/grafana/grafana/pkg/log" + m "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/services/alerting" +) + +func init() { + alerting.RegisterNotifier(&alerting.NotifierPlugin{ + Type: "kafka", + Name: "Kafka REST Proxy", + Description: "Sends notifications to Kafka Rest Proxy", + Factory: NewKafkaNotifier, + OptionsTemplate: ` +

Kafka settings

+
+ Kafka REST Proxy + +
+
+ Topic + +
+ `, + }) +} + +func NewKafkaNotifier(model *m.AlertNotification) (alerting.Notifier, error) { + endpoint := model.Settings.Get("kafkaRestProxy").MustString() + if endpoint == "" { + return nil, alerting.ValidationError{Reason: "Could not find kafka rest proxy endpoint property in settings"} + } + topic := model.Settings.Get("kafkaTopic").MustString() + if topic == "" { + return nil, alerting.ValidationError{Reason: "Could not find kafka topic property in settings"} + } + + return &KafkaNotifier{ + NotifierBase: NewNotifierBase(model.Id, model.IsDefault, model.Name, model.Type, model.Settings), + Endpoint: endpoint, + Topic: topic, + log: log.New("alerting.notifier.kafka"), + }, nil +} + +type KafkaNotifier struct { + NotifierBase + Endpoint string + Topic string + log log.Logger +} + +func (this *KafkaNotifier) Notify(evalContext *alerting.EvalContext) error { + + state := evalContext.Rule.State + + customData := "Triggered metrics:\n\n" + for _, evt := range evalContext.EvalMatches { + customData = customData + fmt.Sprintf("%s: %v\n", evt.Metric, evt.Value) + } + + this.log.Info("Notifying Kafka", "alert_state", state) + + recordJSON := simplejson.New() + records := make([]interface{}, 1) + + bodyJSON := simplejson.New() + bodyJSON.Set("description", evalContext.Rule.Name+" - "+evalContext.Rule.Message) + bodyJSON.Set("client", "Grafana") + bodyJSON.Set("details", customData) + bodyJSON.Set("incident_key", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10)) + + ruleUrl, err := evalContext.GetRuleUrl() + if err != nil { + this.log.Error("Failed get rule link", "error", err) + return err + } + bodyJSON.Set("client_url", ruleUrl) + + if evalContext.ImagePublicUrl != "" { + contexts := make([]interface{}, 1) + imageJSON := simplejson.New() + imageJSON.Set("type", "image") + imageJSON.Set("src", evalContext.ImagePublicUrl) + contexts[0] = imageJSON + bodyJSON.Set("contexts", contexts) + } + + valueJSON := simplejson.New() + valueJSON.Set("value", bodyJSON) + records[0] = valueJSON + recordJSON.Set("records", records) + body, _ := recordJSON.MarshalJSON() + + topicUrl := this.Endpoint + "/topics/" + this.Topic + + cmd := &m.SendWebhookSync{ + Url: topicUrl, + Body: string(body), + HttpMethod: "POST", + HttpHeader: map[string]string{ + "Content-Type": "application/vnd.kafka.json.v2+json", + "Accept": "application/vnd.kafka.v2+json", + }, + } + + if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil { + this.log.Error("Failed to send notification to Kafka", "error", err, "body", string(body)) + return err + } + + return nil +} diff --git a/pkg/services/alerting/notifiers/kafka_test.go b/pkg/services/alerting/notifiers/kafka_test.go new file mode 100644 index 00000000000..045976cb14b --- /dev/null +++ b/pkg/services/alerting/notifiers/kafka_test.go @@ -0,0 +1,55 @@ +package notifiers + +import ( + "testing" + + "github.com/grafana/grafana/pkg/components/simplejson" + m "github.com/grafana/grafana/pkg/models" + . "github.com/smartystreets/goconvey/convey" +) + +func TestKafkaNotifier(t *testing.T) { + Convey("Kafka notifier tests", t, func() { + + Convey("Parsing alert notification from settings", func() { + Convey("empty settings should return error", func() { + json := `{ }` + + settingsJSON, _ := simplejson.NewJson([]byte(json)) + model := &m.AlertNotification{ + Name: "kafka_testing", + Type: "kafka", + Settings: settingsJSON, + } + + _, err := NewKafkaNotifier(model) + So(err, ShouldNotBeNil) + }) + + Convey("settings should send an event to kafka", func() { + json := ` + { + "kafkaRestProxy": "http://localhost:8082", + "kafkaTopic": "topic1" + }` + + settingsJSON, _ := simplejson.NewJson([]byte(json)) + model := &m.AlertNotification{ + Name: "kafka_testing", + Type: "kafka", + Settings: settingsJSON, + } + + not, err := NewKafkaNotifier(model) + kafkaNotifier := not.(*KafkaNotifier) + + So(err, ShouldBeNil) + So(kafkaNotifier.Name, ShouldEqual, "kafka_testing") + So(kafkaNotifier.Type, ShouldEqual, "kafka") + So(kafkaNotifier.Endpoint, ShouldEqual, "http://localhost:8082") + So(kafkaNotifier.Topic, ShouldEqual, "topic1") + }) + + }) + }) +} diff --git a/pkg/services/alerting/notifiers/opsgenie.go b/pkg/services/alerting/notifiers/opsgenie.go index e67ccfb10e5..aea02465177 100644 --- a/pkg/services/alerting/notifiers/opsgenie.go +++ b/pkg/services/alerting/notifiers/opsgenie.go @@ -37,8 +37,7 @@ func init() { } var ( - opsgenieCreateAlertURL string = "https://api.opsgenie.com/v1/json/alert" - opsgenieCloseAlertURL string = "https://api.opsgenie.com/v1/json/alert/close" + opsgenieAlertURL string = "https://api.opsgenie.com/v2/alerts" ) func NewOpsGenieNotifier(model *m.AlertNotification) (alerting.Notifier, error) { @@ -87,7 +86,6 @@ func (this *OpsGenieNotifier) createAlert(evalContext *alerting.EvalContext) err } bodyJSON := simplejson.New() - bodyJSON.Set("apiKey", this.ApiKey) bodyJSON.Set("message", evalContext.Rule.Name) bodyJSON.Set("source", "Grafana") bodyJSON.Set("alias", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10)) @@ -103,9 +101,13 @@ func (this *OpsGenieNotifier) createAlert(evalContext *alerting.EvalContext) err body, _ := bodyJSON.MarshalJSON() cmd := &m.SendWebhookSync{ - Url: opsgenieCreateAlertURL, + Url: opsgenieAlertURL, Body: string(body), HttpMethod: "POST", + HttpHeader: map[string]string{ + "Content-Type": "application/json", + "Authorization": fmt.Sprintf("GenieKey %s", this.ApiKey), + }, } if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil { @@ -119,14 +121,17 @@ func (this *OpsGenieNotifier) closeAlert(evalContext *alerting.EvalContext) erro this.log.Info("Closing OpsGenie alert", "ruleId", evalContext.Rule.Id, "notification", this.Name) bodyJSON := simplejson.New() - bodyJSON.Set("apiKey", this.ApiKey) - bodyJSON.Set("alias", "alertId-"+strconv.FormatInt(evalContext.Rule.Id, 10)) + bodyJSON.Set("source", "Grafana") body, _ := bodyJSON.MarshalJSON() cmd := &m.SendWebhookSync{ - Url: opsgenieCloseAlertURL, + Url: fmt.Sprintf("%s/alertId-%d/close?identifierType=alias", opsgenieAlertURL, evalContext.Rule.Id), Body: string(body), HttpMethod: "POST", + HttpHeader: map[string]string{ + "Content-Type": "application/json", + "Authorization": fmt.Sprintf("GenieKey %s", this.ApiKey), + }, } if err := bus.DispatchCtx(evalContext.Ctx, cmd); err != nil { diff --git a/pkg/services/alerting/result_handler.go b/pkg/services/alerting/result_handler.go index d34dbf5a632..448b4ace5bb 100644 --- a/pkg/services/alerting/result_handler.go +++ b/pkg/services/alerting/result_handler.go @@ -73,10 +73,8 @@ func (handler *DefaultResultHandler) Handle(evalContext *EvalContext) error { OrgId: evalContext.Rule.OrgId, DashboardId: evalContext.Rule.DashboardId, PanelId: evalContext.Rule.PanelId, - Type: annotations.AlertType, AlertId: evalContext.Rule.Id, - Title: evalContext.Rule.Name, - Text: evalContext.GetStateModel().Text, + Text: "", NewState: string(evalContext.Rule.State), PrevState: string(evalContext.PrevAlertState), Epoch: time.Now().Unix(), diff --git a/pkg/services/annotations/annotations.go b/pkg/services/annotations/annotations.go index be9d3f2d4d0..2fdc824f172 100644 --- a/pkg/services/annotations/annotations.go +++ b/pkg/services/annotations/annotations.go @@ -5,7 +5,7 @@ import "github.com/grafana/grafana/pkg/components/simplejson" type Repository interface { Save(item *Item) error Update(item *Item) error - Find(query *ItemQuery) ([]*Item, error) + Find(query *ItemQuery) ([]*ItemDTO, error) Delete(params *DeleteParams) error } @@ -13,11 +13,10 @@ type ItemQuery struct { OrgId int64 `json:"orgId"` From int64 `json:"from"` To int64 `json:"to"` - Type ItemType `json:"type"` AlertId int64 `json:"alertId"` DashboardId int64 `json:"dashboardId"` PanelId int64 `json:"panelId"` - NewState []string `json:"newState"` + Tags []string `json:"tags"` Limit int64 `json:"limit"` } @@ -28,12 +27,15 @@ type PostParams struct { Epoch int64 `json:"epoch"` Title string `json:"title"` Text string `json:"text"` + Icon string `json:"icon"` } type DeleteParams struct { + Id int64 `json:"id"` AlertId int64 `json:"alertId"` DashboardId int64 `json:"dashboardId"` PanelId int64 `json:"panelId"` + RegionId int64 `json:"regionId"` } var repositoryInstance Repository @@ -46,29 +48,41 @@ func SetRepository(rep Repository) { repositoryInstance = rep } -type ItemType string - -const ( - AlertType ItemType = "alert" - EventType ItemType = "event" -) - type Item struct { - Id int64 `json:"id"` - OrgId int64 `json:"orgId"` - DashboardId int64 `json:"dashboardId"` - PanelId int64 `json:"panelId"` - CategoryId int64 `json:"categoryId"` - RegionId int64 `json:"regionId"` - Type ItemType `json:"type"` - Title string `json:"title"` - Text string `json:"text"` - Metric string `json:"metric"` - AlertId int64 `json:"alertId"` - UserId int64 `json:"userId"` - PrevState string `json:"prevState"` - NewState string `json:"newState"` - Epoch int64 `json:"epoch"` + Id int64 `json:"id"` + OrgId int64 `json:"orgId"` + UserId int64 `json:"userId"` + DashboardId int64 `json:"dashboardId"` + PanelId int64 `json:"panelId"` + RegionId int64 `json:"regionId"` + Text string `json:"text"` + AlertId int64 `json:"alertId"` + PrevState string `json:"prevState"` + NewState string `json:"newState"` + Epoch int64 `json:"epoch"` + Tags []string `json:"tags"` + Data *simplejson.Json `json:"data"` - Data *simplejson.Json `json:"data"` + // needed until we remove it from db + Type string + Title string +} + +type ItemDTO struct { + Id int64 `json:"id"` + AlertId int64 `json:"alertId"` + AlertName string `json:"alertName"` + DashboardId int64 `json:"dashboardId"` + PanelId int64 `json:"panelId"` + UserId int64 `json:"userId"` + NewState string `json:"newState"` + PrevState string `json:"prevState"` + Time int64 `json:"time"` + Text string `json:"text"` + RegionId int64 `json:"regionId"` + Tags []string `json:"tags"` + Login string `json:"login"` + Email string `json:"email"` + AvatarUrl string `json:"avatarUrl"` + Data *simplejson.Json `json:"data"` } diff --git a/pkg/services/sqlstore/alert.go b/pkg/services/sqlstore/alert.go index bc589f89c14..33a4cae53c2 100644 --- a/pkg/services/sqlstore/alert.go +++ b/pkg/services/sqlstore/alert.go @@ -100,13 +100,13 @@ func HandleAlertsQuery(query *m.GetAlertsQuery) error { sql.WriteString(")") } + sql.WriteString(" ORDER BY name ASC") + if query.Limit != 0 { sql.WriteString(" LIMIT ?") params = append(params, query.Limit) } - sql.WriteString(" ORDER BY name ASC") - alerts := make([]*m.Alert, 0) if err := x.Sql(sql.String(), params...).Find(&alerts); err != nil { return err diff --git a/pkg/services/sqlstore/annotation.go b/pkg/services/sqlstore/annotation.go index ffad5bf2cad..a2c5d80ac3a 100644 --- a/pkg/services/sqlstore/annotation.go +++ b/pkg/services/sqlstore/annotation.go @@ -2,9 +2,11 @@ package sqlstore import ( "bytes" + "errors" "fmt" "strings" + "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/services/annotations" ) @@ -13,19 +15,94 @@ type SqlAnnotationRepo struct { func (r *SqlAnnotationRepo) Save(item *annotations.Item) error { return inTransaction(func(sess *DBSession) error { - + tags := models.ParseTagPairs(item.Tags) + item.Tags = models.JoinTagPairs(tags) if _, err := sess.Table("annotation").Insert(item); err != nil { return err } + if item.Tags != nil { + if tags, err := r.ensureTagsExist(sess, tags); err != nil { + return err + } else { + for _, tag := range tags { + if _, err := sess.Exec("INSERT INTO annotation_tag (annotation_id, tag_id) VALUES(?,?)", item.Id, tag.Id); err != nil { + return err + } + } + } + } + return nil }) } +// Will insert if needed any new key/value pars and return ids +func (r *SqlAnnotationRepo) ensureTagsExist(sess *DBSession, tags []*models.Tag) ([]*models.Tag, error) { + for _, tag := range tags { + var existingTag models.Tag + + // check if it exists + if exists, err := sess.Table("tag").Where("key=? AND value=?", tag.Key, tag.Value).Get(&existingTag); err != nil { + return nil, err + } else if exists { + tag.Id = existingTag.Id + } else { + if _, err := sess.Table("tag").Insert(tag); err != nil { + return nil, err + } + } + } + + return tags, nil +} + func (r *SqlAnnotationRepo) Update(item *annotations.Item) error { return inTransaction(func(sess *DBSession) error { + var ( + isExist bool + err error + ) + existing := new(annotations.Item) - if _, err := sess.Table("annotation").Id(item.Id).Update(item); err != nil { + if item.Id == 0 && item.RegionId != 0 { + // Update region end time + isExist, err = sess.Table("annotation").Where("region_id=? AND id!=? AND org_id=?", item.RegionId, item.RegionId, item.OrgId).Get(existing) + } else { + isExist, err = sess.Table("annotation").Where("id=? AND org_id=?", item.Id, item.OrgId).Get(existing) + } + + if err != nil { + return err + } + if !isExist { + return errors.New("Annotation not found") + } + + existing.Epoch = item.Epoch + existing.Text = item.Text + if item.RegionId != 0 { + existing.RegionId = item.RegionId + } + + if item.Tags != nil { + if tags, err := r.ensureTagsExist(sess, models.ParseTagPairs(item.Tags)); err != nil { + return err + } else { + if _, err := sess.Exec("DELETE FROM annotation_tag WHERE annotation_id = ?", existing.Id); err != nil { + return err + } + for _, tag := range tags { + if _, err := sess.Exec("INSERT INTO annotation_tag (annotation_id, tag_id) VALUES(?,?)", existing.Id, tag.Id); err != nil { + return err + } + } + } + } + + existing.Tags = item.Tags + + if _, err := sess.Table("annotation").Id(existing.Id).Cols("epoch", "text", "region_id", "tags").Update(existing); err != nil { return err } @@ -33,51 +110,79 @@ func (r *SqlAnnotationRepo) Update(item *annotations.Item) error { }) } -func (r *SqlAnnotationRepo) Find(query *annotations.ItemQuery) ([]*annotations.Item, error) { +func (r *SqlAnnotationRepo) Find(query *annotations.ItemQuery) ([]*annotations.ItemDTO, error) { var sql bytes.Buffer params := make([]interface{}, 0) - sql.WriteString(`SELECT * - from annotation - `) + sql.WriteString(` + SELECT + annotation.id, + annotation.epoch as time, + annotation.dashboard_id, + annotation.panel_id, + annotation.new_state, + annotation.prev_state, + annotation.alert_id, + annotation.region_id, + annotation.text, + annotation.tags, + annotation.data, + usr.email, + usr.login, + alert.name as alert_name + FROM annotation + LEFT OUTER JOIN ` + dialect.Quote("user") + ` as usr on usr.id = annotation.user_id + LEFT OUTER JOIN alert on alert.id = annotation.alert_id + `) - sql.WriteString(`WHERE org_id = ?`) + sql.WriteString(`WHERE annotation.org_id = ?`) params = append(params, query.OrgId) if query.AlertId != 0 { - sql.WriteString(` AND alert_id = ?`) - params = append(params, query.AlertId) - } - - if query.AlertId != 0 { - sql.WriteString(` AND alert_id = ?`) + sql.WriteString(` AND annotation.alert_id = ?`) params = append(params, query.AlertId) } if query.DashboardId != 0 { - sql.WriteString(` AND dashboard_id = ?`) + sql.WriteString(` AND annotation.dashboard_id = ?`) params = append(params, query.DashboardId) } if query.PanelId != 0 { - sql.WriteString(` AND panel_id = ?`) + sql.WriteString(` AND annotation.panel_id = ?`) params = append(params, query.PanelId) } if query.From > 0 && query.To > 0 { - sql.WriteString(` AND epoch BETWEEN ? AND ?`) + sql.WriteString(` AND annotation.epoch BETWEEN ? AND ?`) params = append(params, query.From, query.To) } - if query.Type != "" { - sql.WriteString(` AND type = ?`) - params = append(params, string(query.Type)) - } + if len(query.Tags) > 0 { + keyValueFilters := []string{} - if len(query.NewState) > 0 { - sql.WriteString(` AND new_state IN (?` + strings.Repeat(",?", len(query.NewState)-1) + ")") - for _, v := range query.NewState { - params = append(params, v) + tags := models.ParseTagPairs(query.Tags) + for _, tag := range tags { + if tag.Value == "" { + keyValueFilters = append(keyValueFilters, "(tag.key = ?)") + params = append(params, tag.Key) + } else { + keyValueFilters = append(keyValueFilters, "(tag.key = ? AND tag.value = ?)") + params = append(params, tag.Key, tag.Value) + } + } + + if len(tags) > 0 { + tagsSubQuery := fmt.Sprintf(` + SELECT SUM(1) FROM annotation_tag at + INNER JOIN tag on tag.id = at.tag_id + WHERE at.annotation_id = annotation.id + AND ( + %s + ) + `, strings.Join(keyValueFilters, " OR ")) + + sql.WriteString(fmt.Sprintf(" AND (%s) = %d ", tagsSubQuery, len(tags))) } } @@ -87,7 +192,7 @@ func (r *SqlAnnotationRepo) Find(query *annotations.ItemQuery) ([]*annotations.I sql.WriteString(fmt.Sprintf(" ORDER BY epoch DESC LIMIT %v", query.Limit)) - items := make([]*annotations.Item, 0) + items := make([]*annotations.ItemDTO, 0) if err := x.Sql(sql.String(), params...).Find(&items); err != nil { return nil, err } @@ -97,11 +202,31 @@ func (r *SqlAnnotationRepo) Find(query *annotations.ItemQuery) ([]*annotations.I func (r *SqlAnnotationRepo) Delete(params *annotations.DeleteParams) error { return inTransaction(func(sess *DBSession) error { + var ( + sql string + annoTagSql string + queryParams []interface{} + ) - sql := "DELETE FROM annotation WHERE dashboard_id = ? AND panel_id = ?" + if params.RegionId != 0 { + annoTagSql = "DELETE FROM annotation_tag WHERE annotation_id IN (SELECT id FROM annotation WHERE region_id = ?)" + sql = "DELETE FROM annotation WHERE region_id = ?" + queryParams = []interface{}{params.RegionId} + } else if params.Id != 0 { + annoTagSql = "DELETE FROM annotation_tag WHERE annotation_id IN (SELECT id FROM annotation WHERE id = ?)" + sql = "DELETE FROM annotation WHERE id = ?" + queryParams = []interface{}{params.Id} + } else { + annoTagSql = "DELETE FROM annotation_tag WHERE annotation_id IN (SELECT id FROM annotation WHERE dashboard_id = ? AND panel_id = ?)" + sql = "DELETE FROM annotation WHERE dashboard_id = ? AND panel_id = ?" + queryParams = []interface{}{params.DashboardId, params.PanelId} + } - _, err := sess.Exec(sql, params.DashboardId, params.PanelId) - if err != nil { + if _, err := sess.Exec(annoTagSql, queryParams...); err != nil { + return err + } + + if _, err := sess.Exec(sql, queryParams...); err != nil { return err } diff --git a/pkg/services/sqlstore/annotation_test.go b/pkg/services/sqlstore/annotation_test.go new file mode 100644 index 00000000000..3f7415a952b --- /dev/null +++ b/pkg/services/sqlstore/annotation_test.go @@ -0,0 +1,208 @@ +package sqlstore + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" + + "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/services/annotations" +) + +func TestSavingTags(t *testing.T) { + Convey("Testing annotation saving/loading", t, func() { + InitTestDB(t) + + repo := SqlAnnotationRepo{} + + Convey("Can save tags", func() { + tagPairs := []*models.Tag{ + {Key: "outage"}, + {Key: "type", Value: "outage"}, + {Key: "server", Value: "server-1"}, + {Key: "error"}, + } + tags, err := repo.ensureTagsExist(newSession(), tagPairs) + + So(err, ShouldBeNil) + So(len(tags), ShouldEqual, 4) + }) + }) +} + +func TestAnnotations(t *testing.T) { + Convey("Testing annotation saving/loading", t, func() { + InitTestDB(t) + + repo := SqlAnnotationRepo{} + + Convey("Can save annotation", func() { + err := repo.Save(&annotations.Item{ + OrgId: 1, + UserId: 1, + DashboardId: 1, + Text: "hello", + Epoch: 10, + Tags: []string{"outage", "error", "type:outage", "server:server-1"}, + }) + + So(err, ShouldBeNil) + + Convey("Can query for annotation", func() { + items, err := repo.Find(&annotations.ItemQuery{ + OrgId: 1, + DashboardId: 1, + From: 0, + To: 15, + }) + + So(err, ShouldBeNil) + So(items, ShouldHaveLength, 1) + + Convey("Can read tags", func() { + So(items[0].Tags, ShouldResemble, []string{"outage", "error", "type:outage", "server:server-1"}) + }) + }) + + Convey("Should not find any when item is outside time range", func() { + items, err := repo.Find(&annotations.ItemQuery{ + OrgId: 1, + DashboardId: 1, + From: 12, + To: 15, + }) + + So(err, ShouldBeNil) + So(items, ShouldHaveLength, 0) + }) + + Convey("Should not find one when tag filter does not match", func() { + items, err := repo.Find(&annotations.ItemQuery{ + OrgId: 1, + DashboardId: 1, + From: 1, + To: 15, + Tags: []string{"asd"}, + }) + + So(err, ShouldBeNil) + So(items, ShouldHaveLength, 0) + }) + + Convey("Should find one when all tag filters does match", func() { + items, err := repo.Find(&annotations.ItemQuery{ + OrgId: 1, + DashboardId: 1, + From: 1, + To: 15, + Tags: []string{"outage", "error"}, + }) + + So(err, ShouldBeNil) + So(items, ShouldHaveLength, 1) + }) + + Convey("Should find one when all key value tag filters does match", func() { + items, err := repo.Find(&annotations.ItemQuery{ + OrgId: 1, + DashboardId: 1, + From: 1, + To: 15, + Tags: []string{"type:outage", "server:server-1"}, + }) + + So(err, ShouldBeNil) + So(items, ShouldHaveLength, 1) + }) + + Convey("Can update annotation and remove all tags", func() { + query := &annotations.ItemQuery{ + OrgId: 1, + DashboardId: 1, + From: 0, + To: 15, + } + items, err := repo.Find(query) + + So(err, ShouldBeNil) + + annotationId := items[0].Id + + err = repo.Update(&annotations.Item{ + Id: annotationId, + OrgId: 1, + Text: "something new", + Tags: []string{}, + }) + + So(err, ShouldBeNil) + + items, err = repo.Find(query) + + So(err, ShouldBeNil) + + Convey("Can read tags", func() { + So(items[0].Id, ShouldEqual, annotationId) + So(len(items[0].Tags), ShouldEqual, 0) + So(items[0].Text, ShouldEqual, "something new") + }) + }) + + Convey("Can update annotation with new tags", func() { + query := &annotations.ItemQuery{ + OrgId: 1, + DashboardId: 1, + From: 0, + To: 15, + } + items, err := repo.Find(query) + + So(err, ShouldBeNil) + + annotationId := items[0].Id + + err = repo.Update(&annotations.Item{ + Id: annotationId, + OrgId: 1, + Text: "something new", + Tags: []string{"newtag1", "newtag2"}, + }) + + So(err, ShouldBeNil) + + items, err = repo.Find(query) + + So(err, ShouldBeNil) + + Convey("Can read tags", func() { + So(items[0].Id, ShouldEqual, annotationId) + So(items[0].Tags, ShouldResemble, []string{"newtag1", "newtag2"}) + So(items[0].Text, ShouldEqual, "something new") + }) + }) + + Convey("Can delete annotation", func() { + query := &annotations.ItemQuery{ + OrgId: 1, + DashboardId: 1, + From: 0, + To: 15, + } + items, err := repo.Find(query) + So(err, ShouldBeNil) + + annotationId := items[0].Id + + err = repo.Delete(&annotations.DeleteParams{Id: annotationId}) + + items, err = repo.Find(query) + So(err, ShouldBeNil) + + Convey("Should be deleted", func() { + So(len(items), ShouldEqual, 0) + }) + }) + + }) + }) +} diff --git a/pkg/services/sqlstore/dashboard.go b/pkg/services/sqlstore/dashboard.go index 27812eef32e..d91b4a08aa6 100644 --- a/pkg/services/sqlstore/dashboard.go +++ b/pkg/services/sqlstore/dashboard.go @@ -261,6 +261,7 @@ func DeleteDashboard(cmd *m.DeleteDashboardCommand) error { "DELETE FROM dashboard WHERE id = ?", "DELETE FROM playlist_item WHERE type = 'dashboard_by_id' AND value = ?", "DELETE FROM dashboard_version WHERE dashboard_id = ?", + "DELETE FROM annotation WHERE dashboard_id = ?", } for _, sql := range deletes { diff --git a/pkg/services/sqlstore/dashboard_snapshot.go b/pkg/services/sqlstore/dashboard_snapshot.go index 810189b3246..0ef7f99da67 100644 --- a/pkg/services/sqlstore/dashboard_snapshot.go +++ b/pkg/services/sqlstore/dashboard_snapshot.go @@ -86,9 +86,10 @@ func GetDashboardSnapshot(query *m.GetDashboardSnapshotQuery) error { } func SearchDashboardSnapshots(query *m.GetDashboardSnapshotsQuery) error { - var snapshots = make(m.DashboardSnapshots, 0) + var snapshots = make(m.DashboardSnapshotsList, 0) sess := x.Limit(query.Limit) + sess.Table("dashboard_snapshot") if query.Name != "" { sess.Where("name LIKE ?", query.Name) diff --git a/pkg/services/sqlstore/migrations/annotation_mig.go b/pkg/services/sqlstore/migrations/annotation_mig.go index a9343266863..8d2bf94bc42 100644 --- a/pkg/services/sqlstore/migrations/annotation_mig.go +++ b/pkg/services/sqlstore/migrations/annotation_mig.go @@ -57,4 +57,37 @@ func addAnnotationMig(mg *Migrator) { mg.AddMigration("Add column region_id to annotation table", NewAddColumnMigration(table, &Column{ Name: "region_id", Type: DB_BigInt, Nullable: true, Default: "0", })) + + categoryIdIndex := &Index{Cols: []string{"org_id", "category_id"}, Type: IndexType} + mg.AddMigration("Drop category_id index", NewDropIndexMigration(table, categoryIdIndex)) + + mg.AddMigration("Add column tags to annotation table", NewAddColumnMigration(table, &Column{ + Name: "tags", Type: DB_NVarchar, Nullable: true, Length: 500, + })) + + /// + /// Annotation tag + /// + annotationTagTable := Table{ + Name: "annotation_tag", + Columns: []*Column{ + {Name: "annotation_id", Type: DB_BigInt, Nullable: false}, + {Name: "tag_id", Type: DB_BigInt, Nullable: false}, + }, + Indices: []*Index{ + {Cols: []string{"annotation_id", "tag_id"}, Type: UniqueIndex}, + }, + } + + mg.AddMigration("Create annotation_tag table v2", NewAddTableMigration(annotationTagTable)) + mg.AddMigration("Add unique index annotation_tag.annotation_id_tag_id", NewAddIndexMigration(annotationTagTable, annotationTagTable.Indices[0])) + + // + // clear alert text + // + updateTextFieldSql := "UPDATE annotation SET TEXT = '' WHERE alert_id > 0" + mg.AddMigration("Update alert annotations and set TEXT to empty", new(RawSqlMigration). + Sqlite(updateTextFieldSql). + Postgres(updateTextFieldSql). + Mysql(updateTextFieldSql)) } diff --git a/pkg/services/sqlstore/migrations/migrations.go b/pkg/services/sqlstore/migrations/migrations.go index 38072fe88e4..4984ff18592 100644 --- a/pkg/services/sqlstore/migrations/migrations.go +++ b/pkg/services/sqlstore/migrations/migrations.go @@ -26,6 +26,7 @@ func AddMigrations(mg *Migrator) { addAnnotationMig(mg) addTestDataMigrations(mg) addDashboardVersionMigration(mg) + addTagMigration(mg) } func addMigrationLogMigrations(mg *Migrator) { diff --git a/pkg/services/sqlstore/migrations/tag_mig.go b/pkg/services/sqlstore/migrations/tag_mig.go new file mode 100644 index 00000000000..0303ddd6409 --- /dev/null +++ b/pkg/services/sqlstore/migrations/tag_mig.go @@ -0,0 +1,24 @@ +package migrations + +import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator" + +func addTagMigration(mg *Migrator) { + + tagTable := Table{ + Name: "tag", + Columns: []*Column{ + {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, + {Name: "key", Type: DB_NVarchar, Length: 100, Nullable: false}, + {Name: "value", Type: DB_NVarchar, Length: 100, Nullable: false}, + }, + Indices: []*Index{ + {Cols: []string{"key", "value"}, Type: UniqueIndex}, + }, + } + + // create table + mg.AddMigration("create tag table", NewAddTableMigration(tagTable)) + + // create indices + mg.AddMigration("add index tag.key_value", NewAddIndexMigration(tagTable, tagTable.Indices[0])) +} diff --git a/pkg/services/sqlstore/migrator/postgres_dialect.go b/pkg/services/sqlstore/migrator/postgres_dialect.go index 92109efdfab..8de26194411 100644 --- a/pkg/services/sqlstore/migrator/postgres_dialect.go +++ b/pkg/services/sqlstore/migrator/postgres_dialect.go @@ -104,7 +104,7 @@ func (db *Postgres) SqlType(c *Column) string { func (db *Postgres) TableCheckSql(tableName string) (string, []interface{}) { args := []interface{}{"grafana", tableName} - sql := "SELECT `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? and `TABLE_NAME`=?" + sql := "SELECT table_name FROM information_schema.tables WHERE table_schema=? and table_name=?" return sql, args } diff --git a/pkg/services/sqlstore/user.go b/pkg/services/sqlstore/user.go index 37c3bc9d1d8..3781d83dd96 100644 --- a/pkg/services/sqlstore/user.go +++ b/pkg/services/sqlstore/user.go @@ -96,6 +96,7 @@ func CreateUser(cmd *m.CreateUserCommand) error { EmailVerified: cmd.EmailVerified, Created: time.Now(), Updated: time.Now(), + LastSeenAt: time.Now().AddDate(-10, 0, 0), } if len(cmd.Password) > 0 { diff --git a/pkg/setting/setting.go b/pkg/setting/setting.go index f2ba16fa675..ca65fe581af 100644 --- a/pkg/setting/setting.go +++ b/pkg/setting/setting.go @@ -122,6 +122,9 @@ var ( // Basic Auth BasicAuthEnabled bool + // Plugin settings + PluginAppsSkipVerifyTLS bool + // Session settings. SessionOptions session.Options @@ -560,6 +563,9 @@ func NewConfigContext(args *CommandLineArgs) error { authBasic := Cfg.Section("auth.basic") BasicAuthEnabled = authBasic.Key("enabled").MustBool(true) + // global plugin settings + PluginAppsSkipVerifyTLS = Cfg.Section("plugins").Key("app_tls_skip_verify_insecure").MustBool(false) + // PhantomJS rendering ImagesDir = filepath.Join(DataPath, "png") PhantomDir = filepath.Join(HomePath, "vendor/phantomjs") diff --git a/pkg/setting/setting_oauth.go b/pkg/setting/setting_oauth.go index bc52d2336c3..ee2e812415b 100644 --- a/pkg/setting/setting_oauth.go +++ b/pkg/setting/setting_oauth.go @@ -13,6 +13,7 @@ type OAuthInfo struct { TlsClientCert string TlsClientKey string TlsClientCa string + TlsSkipVerify bool } type OAuther struct { diff --git a/pkg/social/social.go b/pkg/social/social.go index 9d2a53946c7..d40c0a0c965 100644 --- a/pkg/social/social.go +++ b/pkg/social/social.go @@ -66,6 +66,7 @@ func NewOAuthService() { TlsClientCert: sec.Key("tls_client_cert").String(), TlsClientKey: sec.Key("tls_client_key").String(), TlsClientCa: sec.Key("tls_client_ca").String(), + TlsSkipVerify: sec.Key("tls_skip_verify_insecure").MustBool(), } if !info.Enabled { diff --git a/pkg/tsdb/mysql/macros.go b/pkg/tsdb/mysql/macros.go index 5545168afbb..36c38804a01 100644 --- a/pkg/tsdb/mysql/macros.go +++ b/pkg/tsdb/mysql/macros.go @@ -11,26 +11,21 @@ import ( const rsIdentifier = `([_a-zA-Z0-9]+)` const sExpr = `\$` + rsIdentifier + `\(([^\)]*)\)` -type SqlMacroEngine interface { - Interpolate(sql string) (string, error) -} - type MySqlMacroEngine struct { TimeRange *tsdb.TimeRange } -func NewMysqlMacroEngine(timeRange *tsdb.TimeRange) SqlMacroEngine { - return &MySqlMacroEngine{ - TimeRange: timeRange, - } +func NewMysqlMacroEngine() tsdb.SqlMacroEngine { + return &MySqlMacroEngine{} } -func (m *MySqlMacroEngine) Interpolate(sql string) (string, error) { +func (m *MySqlMacroEngine) Interpolate(timeRange *tsdb.TimeRange, sql string) (string, error) { + m.TimeRange = timeRange rExp, _ := regexp.Compile(sExpr) var macroError error - sql = ReplaceAllStringSubmatchFunc(rExp, sql, func(groups []string) string { - res, err := m.EvaluateMacro(groups[1], groups[2:]) + sql = replaceAllStringSubmatchFunc(rExp, sql, func(groups []string) string { + res, err := m.evaluateMacro(groups[1], groups[2:]) if err != nil && macroError == nil { macroError = err return "macro_error()" @@ -45,7 +40,7 @@ func (m *MySqlMacroEngine) Interpolate(sql string) (string, error) { return sql, nil } -func ReplaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]string) string) string { +func replaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]string) string) string { result := "" lastIndex := 0 @@ -62,7 +57,7 @@ func ReplaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]str return result + str[lastIndex:] } -func (m *MySqlMacroEngine) EvaluateMacro(name string, args []string) (string, error) { +func (m *MySqlMacroEngine) evaluateMacro(name string, args []string) (string, error) { switch name { case "__time": if len(args) == 0 { diff --git a/pkg/tsdb/mysql/macros_test.go b/pkg/tsdb/mysql/macros_test.go index d85133afa84..c92020d0aae 100644 --- a/pkg/tsdb/mysql/macros_test.go +++ b/pkg/tsdb/mysql/macros_test.go @@ -9,86 +9,60 @@ import ( func TestMacroEngine(t *testing.T) { Convey("MacroEngine", t, func() { + engine := &MySqlMacroEngine{} + timeRange := &tsdb.TimeRange{From: "5m", To: "now"} Convey("interpolate __time function", func() { - engine := &MySqlMacroEngine{} - - sql, err := engine.Interpolate("select $__time(time_column)") + sql, err := engine.Interpolate(nil, "select $__time(time_column)") So(err, ShouldBeNil) So(sql, ShouldEqual, "select UNIX_TIMESTAMP(time_column) as time_sec") }) Convey("interpolate __time function wrapped in aggregation", func() { - engine := &MySqlMacroEngine{} - - sql, err := engine.Interpolate("select min($__time(time_column))") + sql, err := engine.Interpolate(nil, "select min($__time(time_column))") So(err, ShouldBeNil) So(sql, ShouldEqual, "select min(UNIX_TIMESTAMP(time_column) as time_sec)") }) Convey("interpolate __timeFilter function", func() { - engine := &MySqlMacroEngine{ - TimeRange: &tsdb.TimeRange{From: "5m", To: "now"}, - } - - sql, err := engine.Interpolate("WHERE $__timeFilter(time_column)") + sql, err := engine.Interpolate(timeRange, "WHERE $__timeFilter(time_column)") So(err, ShouldBeNil) So(sql, ShouldEqual, "WHERE time_column >= FROM_UNIXTIME(18446744066914186738) AND time_column <= FROM_UNIXTIME(18446744066914187038)") }) Convey("interpolate __timeFrom function", func() { - engine := &MySqlMacroEngine{ - TimeRange: &tsdb.TimeRange{From: "5m", To: "now"}, - } - - sql, err := engine.Interpolate("select $__timeFrom(time_column)") + sql, err := engine.Interpolate(timeRange, "select $__timeFrom(time_column)") So(err, ShouldBeNil) So(sql, ShouldEqual, "select FROM_UNIXTIME(18446744066914186738)") }) Convey("interpolate __timeTo function", func() { - engine := &MySqlMacroEngine{ - TimeRange: &tsdb.TimeRange{From: "5m", To: "now"}, - } - - sql, err := engine.Interpolate("select $__timeTo(time_column)") + sql, err := engine.Interpolate(timeRange, "select $__timeTo(time_column)") So(err, ShouldBeNil) So(sql, ShouldEqual, "select FROM_UNIXTIME(18446744066914187038)") }) Convey("interpolate __unixEpochFilter function", func() { - engine := &MySqlMacroEngine{ - TimeRange: &tsdb.TimeRange{From: "5m", To: "now"}, - } - - sql, err := engine.Interpolate("select $__unixEpochFilter(18446744066914186738)") + sql, err := engine.Interpolate(timeRange, "select $__unixEpochFilter(18446744066914186738)") So(err, ShouldBeNil) So(sql, ShouldEqual, "select 18446744066914186738 >= 18446744066914186738 AND 18446744066914186738 <= 18446744066914187038") }) Convey("interpolate __unixEpochFrom function", func() { - engine := &MySqlMacroEngine{ - TimeRange: &tsdb.TimeRange{From: "5m", To: "now"}, - } - - sql, err := engine.Interpolate("select $__unixEpochFrom()") + sql, err := engine.Interpolate(timeRange, "select $__unixEpochFrom()") So(err, ShouldBeNil) So(sql, ShouldEqual, "select 18446744066914186738") }) Convey("interpolate __unixEpochTo function", func() { - engine := &MySqlMacroEngine{ - TimeRange: &tsdb.TimeRange{From: "5m", To: "now"}, - } - - sql, err := engine.Interpolate("select $__unixEpochTo()") + sql, err := engine.Interpolate(timeRange, "select $__unixEpochTo()") So(err, ShouldBeNil) So(sql, ShouldEqual, "select 18446744066914187038") diff --git a/pkg/tsdb/mysql/mysql.go b/pkg/tsdb/mysql/mysql.go index 19aa50096b1..bdb48867b6e 100644 --- a/pkg/tsdb/mysql/mysql.go +++ b/pkg/tsdb/mysql/mysql.go @@ -6,142 +6,57 @@ import ( "database/sql" "fmt" "strconv" - "sync" "time" "github.com/go-sql-driver/mysql" "github.com/go-xorm/core" - "github.com/go-xorm/xorm" "github.com/grafana/grafana/pkg/components/null" - "github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/tsdb" ) -type MysqlExecutor struct { - engine *xorm.Engine - log log.Logger -} - -type engineCacheType struct { - cache map[int64]*xorm.Engine - versions map[int64]int - sync.Mutex -} - -var engineCache = engineCacheType{ - cache: make(map[int64]*xorm.Engine), - versions: make(map[int64]int), +type MysqlQueryEndpoint struct { + sqlEngine tsdb.SqlEngine + log log.Logger } func init() { - tsdb.RegisterTsdbQueryEndpoint("mysql", NewMysqlExecutor) + tsdb.RegisterTsdbQueryEndpoint("mysql", NewMysqlQueryEndpoint) } -func NewMysqlExecutor(datasource *models.DataSource) (tsdb.TsdbQueryEndpoint, error) { - executor := &MysqlExecutor{ +func NewMysqlQueryEndpoint(datasource *models.DataSource) (tsdb.TsdbQueryEndpoint, error) { + endpoint := &MysqlQueryEndpoint{ log: log.New("tsdb.mysql"), } - err := executor.initEngine(datasource) - if err != nil { - return nil, err - } - - return executor, nil -} - -func (e *MysqlExecutor) initEngine(dsInfo *models.DataSource) error { - engineCache.Lock() - defer engineCache.Unlock() - - if engine, present := engineCache.cache[dsInfo.Id]; present { - if version, _ := engineCache.versions[dsInfo.Id]; version == dsInfo.Version { - e.engine = engine - return nil - } + endpoint.sqlEngine = &tsdb.DefaultSqlEngine{ + MacroEngine: NewMysqlMacroEngine(), } cnnstr := fmt.Sprintf("%s:%s@%s(%s)/%s?collation=utf8mb4_unicode_ci&parseTime=true&loc=UTC", - dsInfo.User, - dsInfo.Password, + datasource.User, + datasource.Password, "tcp", - dsInfo.Url, - dsInfo.Database) + datasource.Url, + datasource.Database, + ) + endpoint.log.Debug("getEngine", "connection", cnnstr) - e.log.Debug("getEngine", "connection", cnnstr) - - engine, err := xorm.NewEngine("mysql", cnnstr) - engine.SetMaxOpenConns(10) - engine.SetMaxIdleConns(10) - if err != nil { - return err + if err := endpoint.sqlEngine.InitEngine("mysql", datasource, cnnstr); err != nil { + return nil, err } - engineCache.cache[dsInfo.Id] = engine - e.engine = engine - return nil + return endpoint, nil } -func (e *MysqlExecutor) Query(ctx context.Context, dsInfo *models.DataSource, tsdbQuery *tsdb.TsdbQuery) (*tsdb.Response, error) { - result := &tsdb.Response{ - Results: make(map[string]*tsdb.QueryResult), - } - - macroEngine := NewMysqlMacroEngine(tsdbQuery.TimeRange) - session := e.engine.NewSession() - defer session.Close() - db := session.DB() - - for _, query := range tsdbQuery.Queries { - rawSql := query.Model.Get("rawSql").MustString() - if rawSql == "" { - continue - } - - queryResult := &tsdb.QueryResult{Meta: simplejson.New(), RefId: query.RefId} - result.Results[query.RefId] = queryResult - - rawSql, err := macroEngine.Interpolate(rawSql) - if err != nil { - queryResult.Error = err - continue - } - - queryResult.Meta.Set("sql", rawSql) - - rows, err := db.Query(rawSql) - if err != nil { - queryResult.Error = err - continue - } - - defer rows.Close() - - format := query.Model.Get("format").MustString("time_series") - - switch format { - case "time_series": - err := e.TransformToTimeSeries(query, rows, queryResult) - if err != nil { - queryResult.Error = err - continue - } - case "table": - err := e.TransformToTable(query, rows, queryResult) - if err != nil { - queryResult.Error = err - continue - } - } - } - - return result, nil +// Query is the main function for the MysqlExecutor +func (e *MysqlQueryEndpoint) Query(ctx context.Context, dsInfo *models.DataSource, tsdbQuery *tsdb.TsdbQuery) (*tsdb.Response, error) { + return e.sqlEngine.Query(ctx, dsInfo, tsdbQuery, e.transformToTimeSeries, e.transformToTable) } -func (e MysqlExecutor) TransformToTable(query *tsdb.Query, rows *core.Rows, result *tsdb.QueryResult) error { +func (e MysqlQueryEndpoint) transformToTable(query *tsdb.Query, rows *core.Rows, result *tsdb.QueryResult) error { columnNames, err := rows.Columns() columnCount := len(columnNames) @@ -166,7 +81,7 @@ func (e MysqlExecutor) TransformToTable(query *tsdb.Query, rows *core.Rows, resu rowLimit := 1000000 rowCount := 0 - for ; rows.Next(); rowCount += 1 { + for ; rows.Next(); rowCount++ { if rowCount > rowLimit { return fmt.Errorf("MySQL query row limit exceeded, limit %d", rowLimit) } @@ -184,7 +99,7 @@ func (e MysqlExecutor) TransformToTable(query *tsdb.Query, rows *core.Rows, resu return nil } -func (e MysqlExecutor) getTypedRowData(types []*sql.ColumnType, rows *core.Rows) (tsdb.RowValues, error) { +func (e MysqlQueryEndpoint) getTypedRowData(types []*sql.ColumnType, rows *core.Rows) (tsdb.RowValues, error) { values := make([]interface{}, len(types)) for i, stype := range types { @@ -248,7 +163,7 @@ func (e MysqlExecutor) getTypedRowData(types []*sql.ColumnType, rows *core.Rows) return values, nil } -func (e MysqlExecutor) TransformToTimeSeries(query *tsdb.Query, rows *core.Rows, result *tsdb.QueryResult) error { +func (e MysqlQueryEndpoint) transformToTimeSeries(query *tsdb.Query, rows *core.Rows, result *tsdb.QueryResult) error { pointsBySeries := make(map[string]*tsdb.TimeSeries) seriesByQueryOrder := list.New() columnNames, err := rows.Columns() @@ -261,7 +176,7 @@ func (e MysqlExecutor) TransformToTimeSeries(query *tsdb.Query, rows *core.Rows, rowLimit := 1000000 rowCount := 0 - for ; rows.Next(); rowCount += 1 { + for ; rows.Next(); rowCount++ { if rowCount > rowLimit { return fmt.Errorf("MySQL query row limit exceeded, limit %d", rowLimit) } diff --git a/pkg/tsdb/mysql/mysql_test.go b/pkg/tsdb/mysql/mysql_test.go index 988833a7a1f..55def0b4129 100644 --- a/pkg/tsdb/mysql/mysql_test.go +++ b/pkg/tsdb/mysql/mysql_test.go @@ -18,14 +18,16 @@ func TestMySQL(t *testing.T) { SkipConvey("MySQL", t, func() { x := InitMySQLTestDB(t) - executor := &MysqlExecutor{ - engine: x, - log: log.New("tsdb.mysql"), + endpoint := &MysqlQueryEndpoint{ + sqlEngine: &tsdb.DefaultSqlEngine{ + MacroEngine: NewMysqlMacroEngine(), + XormEngine: x, + }, + log: log.New("tsdb.mysql"), } sess := x.NewSession() defer sess.Close() - db := sess.DB() sql := "CREATE TABLE `mysql_types` (" sql += "`atinyint` tinyint(1)," @@ -70,14 +72,23 @@ func TestMySQL(t *testing.T) { _, err = sess.Exec(sql) So(err, ShouldBeNil) - Convey("TransformToTable should map MySQL column types to Go types", func() { - rows, err := db.Query("SELECT * FROM mysql_types") - defer rows.Close() + Convey("Query with Table format should map MySQL column types to Go types", func() { + query := &tsdb.TsdbQuery{ + Queries: []*tsdb.Query{ + { + Model: simplejson.NewFromAny(map[string]interface{}{ + "rawSql": "SELECT * FROM mysql_types", + "format": "table", + }), + RefId: "A", + }, + }, + } + + resp, err := endpoint.Query(nil, nil, query) + queryResult := resp.Results["A"] So(err, ShouldBeNil) - queryResult := &tsdb.QueryResult{Meta: simplejson.New()} - err = executor.TransformToTable(nil, rows, queryResult) - So(err, ShouldBeNil) column := queryResult.Tables[0].Rows[0] So(*column[0].(*int8), ShouldEqual, 1) So(*column[1].(*string), ShouldEqual, "abc") diff --git a/pkg/tsdb/postgres/macros.go b/pkg/tsdb/postgres/macros.go new file mode 100644 index 00000000000..3ca31591cb0 --- /dev/null +++ b/pkg/tsdb/postgres/macros.go @@ -0,0 +1,99 @@ +package postgres + +import ( + "fmt" + "regexp" + "strings" + + "github.com/grafana/grafana/pkg/tsdb" +) + +//const rsString = `(?:"([^"]*)")`; +const rsIdentifier = `([_a-zA-Z0-9]+)` +const sExpr = `\$` + rsIdentifier + `\(([^\)]*)\)` + +type PostgresMacroEngine struct { + TimeRange *tsdb.TimeRange +} + +func NewPostgresMacroEngine() tsdb.SqlMacroEngine { + return &PostgresMacroEngine{} +} + +func (m *PostgresMacroEngine) Interpolate(timeRange *tsdb.TimeRange, sql string) (string, error) { + m.TimeRange = timeRange + rExp, _ := regexp.Compile(sExpr) + var macroError error + + sql = replaceAllStringSubmatchFunc(rExp, sql, func(groups []string) string { + res, err := m.evaluateMacro(groups[1], strings.Split(groups[2], ",")) + if err != nil && macroError == nil { + macroError = err + return "macro_error()" + } + return res + }) + + if macroError != nil { + return "", macroError + } + + return sql, nil +} + +func replaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]string) string) string { + result := "" + lastIndex := 0 + + for _, v := range re.FindAllSubmatchIndex([]byte(str), -1) { + groups := []string{} + for i := 0; i < len(v); i += 2 { + groups = append(groups, str[v[i]:v[i+1]]) + } + + result += str[lastIndex:v[0]] + repl(groups) + lastIndex = v[1] + } + + return result + str[lastIndex:] +} + +func (m *PostgresMacroEngine) evaluateMacro(name string, args []string) (string, error) { + switch name { + case "__time": + if len(args) == 0 { + return "", fmt.Errorf("missing time column argument for macro %v", name) + } + return fmt.Sprintf("%s AS \"time\"", args[0]), nil + case "__timeEpoch": + if len(args) == 0 { + return "", fmt.Errorf("missing time column argument for macro %v", name) + } + return fmt.Sprintf("extract(epoch from %s) as \"time\"", args[0]), nil + case "__timeFilter": + if len(args) == 0 { + return "", fmt.Errorf("missing time column argument for macro %v", name) + } + return fmt.Sprintf("%s >= to_timestamp(%d) AND %s <= to_timestamp(%d)", args[0], uint64(m.TimeRange.GetFromAsMsEpoch()/1000), args[0], uint64(m.TimeRange.GetToAsMsEpoch()/1000)), nil + case "__timeFrom": + return fmt.Sprintf("to_timestamp(%d)", uint64(m.TimeRange.GetFromAsMsEpoch()/1000)), nil + case "__timeTo": + return fmt.Sprintf("to_timestamp(%d)", uint64(m.TimeRange.GetToAsMsEpoch()/1000)), nil + case "__timeGroup": + if len(args) < 2 { + return "", fmt.Errorf("macro %v needs time column and interval", name) + } + return fmt.Sprintf("(extract(epoch from \"%s\")/extract(epoch from %s::interval))::int", args[0], args[1]), nil + case "__unixEpochFilter": + if len(args) == 0 { + return "", fmt.Errorf("missing time column argument for macro %v", name) + } + return fmt.Sprintf("%s >= %d AND %s <= %d", args[0], uint64(m.TimeRange.GetFromAsMsEpoch()/1000), args[0], uint64(m.TimeRange.GetToAsMsEpoch()/1000)), nil + case "__unixEpochFrom": + return fmt.Sprintf("%d", uint64(m.TimeRange.GetFromAsMsEpoch()/1000)), nil + case "__unixEpochTo": + return fmt.Sprintf("%d", uint64(m.TimeRange.GetToAsMsEpoch()/1000)), nil + default: + return "", fmt.Errorf("Unknown macro %v", name) + } +} diff --git a/pkg/tsdb/postgres/macros_test.go b/pkg/tsdb/postgres/macros_test.go new file mode 100644 index 00000000000..f181780e6ea --- /dev/null +++ b/pkg/tsdb/postgres/macros_test.go @@ -0,0 +1,80 @@ +package postgres + +import ( + "testing" + + "github.com/grafana/grafana/pkg/tsdb" + . "github.com/smartystreets/goconvey/convey" +) + +func TestMacroEngine(t *testing.T) { + Convey("MacroEngine", t, func() { + engine := &PostgresMacroEngine{} + timeRange := &tsdb.TimeRange{From: "5m", To: "now"} + + Convey("interpolate __time function", func() { + sql, err := engine.Interpolate(nil, "select $__time(time_column)") + So(err, ShouldBeNil) + + So(sql, ShouldEqual, "select time_column AS \"time\"") + }) + + Convey("interpolate __time function wrapped in aggregation", func() { + sql, err := engine.Interpolate(nil, "select min($__time(time_column))") + So(err, ShouldBeNil) + + So(sql, ShouldEqual, "select min(time_column AS \"time\")") + }) + + Convey("interpolate __timeFilter function", func() { + sql, err := engine.Interpolate(timeRange, "WHERE $__timeFilter(time_column)") + So(err, ShouldBeNil) + + So(sql, ShouldEqual, "WHERE time_column >= to_timestamp(18446744066914186738) AND time_column <= to_timestamp(18446744066914187038)") + }) + + Convey("interpolate __timeFrom function", func() { + sql, err := engine.Interpolate(timeRange, "select $__timeFrom(time_column)") + So(err, ShouldBeNil) + + So(sql, ShouldEqual, "select to_timestamp(18446744066914186738)") + }) + + Convey("interpolate __timeGroup function", func() { + + sql, err := engine.Interpolate(timeRange, "GROUP BY $__timeGroup(time_column,'5m')") + So(err, ShouldBeNil) + + So(sql, ShouldEqual, "GROUP BY (extract(epoch from \"time_column\")/extract(epoch from '5m'::interval))::int") + }) + + Convey("interpolate __timeTo function", func() { + sql, err := engine.Interpolate(timeRange, "select $__timeTo(time_column)") + So(err, ShouldBeNil) + + So(sql, ShouldEqual, "select to_timestamp(18446744066914187038)") + }) + + Convey("interpolate __unixEpochFilter function", func() { + sql, err := engine.Interpolate(timeRange, "select $__unixEpochFilter(18446744066914186738)") + So(err, ShouldBeNil) + + So(sql, ShouldEqual, "select 18446744066914186738 >= 18446744066914186738 AND 18446744066914186738 <= 18446744066914187038") + }) + + Convey("interpolate __unixEpochFrom function", func() { + sql, err := engine.Interpolate(timeRange, "select $__unixEpochFrom()") + So(err, ShouldBeNil) + + So(sql, ShouldEqual, "select 18446744066914186738") + }) + + Convey("interpolate __unixEpochTo function", func() { + sql, err := engine.Interpolate(timeRange, "select $__unixEpochTo()") + So(err, ShouldBeNil) + + So(sql, ShouldEqual, "select 18446744066914187038") + }) + + }) +} diff --git a/pkg/tsdb/postgres/postgres.go b/pkg/tsdb/postgres/postgres.go new file mode 100644 index 00000000000..6fc9c89e7be --- /dev/null +++ b/pkg/tsdb/postgres/postgres.go @@ -0,0 +1,245 @@ +package postgres + +import ( + "container/list" + "context" + "fmt" + "strconv" + "time" + + "github.com/go-xorm/core" + "github.com/grafana/grafana/pkg/components/null" + "github.com/grafana/grafana/pkg/log" + "github.com/grafana/grafana/pkg/models" + "github.com/grafana/grafana/pkg/tsdb" +) + +type PostgresQueryEndpoint struct { + sqlEngine tsdb.SqlEngine + log log.Logger +} + +func init() { + tsdb.RegisterTsdbQueryEndpoint("postgres", NewPostgresQueryEndpoint) +} + +func NewPostgresQueryEndpoint(datasource *models.DataSource) (tsdb.TsdbQueryEndpoint, error) { + endpoint := &PostgresQueryEndpoint{ + log: log.New("tsdb.postgres"), + } + + endpoint.sqlEngine = &tsdb.DefaultSqlEngine{ + MacroEngine: NewPostgresMacroEngine(), + } + + cnnstr := generateConnectionString(datasource) + endpoint.log.Debug("getEngine", "connection", cnnstr) + + if err := endpoint.sqlEngine.InitEngine("postgres", datasource, cnnstr); err != nil { + return nil, err + } + + return endpoint, nil +} + +func generateConnectionString(datasource *models.DataSource) string { + password := "" + for key, value := range datasource.SecureJsonData.Decrypt() { + if key == "password" { + password = value + break + } + } + + sslmode := datasource.JsonData.Get("sslmode").MustString("require") + return fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s", datasource.User, password, datasource.Url, datasource.Database, sslmode) +} + +func (e *PostgresQueryEndpoint) Query(ctx context.Context, dsInfo *models.DataSource, tsdbQuery *tsdb.TsdbQuery) (*tsdb.Response, error) { + return e.sqlEngine.Query(ctx, dsInfo, tsdbQuery, e.transformToTimeSeries, e.transformToTable) +} + +func (e PostgresQueryEndpoint) transformToTable(query *tsdb.Query, rows *core.Rows, result *tsdb.QueryResult) error { + + columnNames, err := rows.Columns() + if err != nil { + return err + } + + table := &tsdb.Table{ + Columns: make([]tsdb.TableColumn, len(columnNames)), + Rows: make([]tsdb.RowValues, 0), + } + + for i, name := range columnNames { + table.Columns[i].Text = name + } + + rowLimit := 1000000 + rowCount := 0 + + for ; rows.Next(); rowCount++ { + if rowCount > rowLimit { + return fmt.Errorf("PostgreSQL query row limit exceeded, limit %d", rowLimit) + } + + values, err := e.getTypedRowData(rows) + if err != nil { + return err + } + + table.Rows = append(table.Rows, values) + } + + result.Tables = append(result.Tables, table) + result.Meta.Set("rowCount", rowCount) + return nil +} + +func (e PostgresQueryEndpoint) getTypedRowData(rows *core.Rows) (tsdb.RowValues, error) { + + types, err := rows.ColumnTypes() + if err != nil { + return nil, err + } + + values := make([]interface{}, len(types)) + valuePtrs := make([]interface{}, len(types)) + + for i := 0; i < len(types); i++ { + valuePtrs[i] = &values[i] + } + + if err := rows.Scan(valuePtrs...); err != nil { + return nil, err + } + + // convert types not handled by lib/pq + // unhandled types are returned as []byte + for i := 0; i < len(types); i++ { + if value, ok := values[i].([]byte); ok == true { + switch types[i].DatabaseTypeName() { + case "NUMERIC": + if v, err := strconv.ParseFloat(string(value), 64); err == nil { + values[i] = v + } else { + e.log.Debug("Rows", "Error converting numeric to float", value) + } + case "UNKNOWN", "CIDR", "INET", "MACADDR": + // char literals have type UNKNOWN + values[i] = string(value) + default: + e.log.Debug("Rows", "Unknown database type", types[i].DatabaseTypeName(), "value", value) + values[i] = string(value) + } + } + } + + return values, nil +} + +func (e PostgresQueryEndpoint) transformToTimeSeries(query *tsdb.Query, rows *core.Rows, result *tsdb.QueryResult) error { + pointsBySeries := make(map[string]*tsdb.TimeSeries) + seriesByQueryOrder := list.New() + columnNames, err := rows.Columns() + + if err != nil { + return err + } + + rowLimit := 1000000 + rowCount := 0 + timeIndex := -1 + metricIndex := -1 + + // check columns of resultset + for i, col := range columnNames { + switch col { + case "time": + timeIndex = i + case "metric": + metricIndex = i + } + } + + if timeIndex == -1 { + return fmt.Errorf("Found no column named time") + } + + for rows.Next() { + var timestamp float64 + var value null.Float + var metric string + + if rowCount > rowLimit { + return fmt.Errorf("PostgreSQL query row limit exceeded, limit %d", rowLimit) + } + + values, err := e.getTypedRowData(rows) + if err != nil { + return err + } + + switch columnValue := values[timeIndex].(type) { + case int64: + timestamp = float64(columnValue * 1000) + case float64: + timestamp = columnValue * 1000 + case time.Time: + timestamp = float64(columnValue.Unix() * 1000) + default: + return fmt.Errorf("Invalid type for column time, must be of type timestamp or unix timestamp") + } + + if metricIndex >= 0 { + if columnValue, ok := values[metricIndex].(string); ok == true { + metric = columnValue + } else { + return fmt.Errorf("Column metric must be of type char,varchar or text") + } + } + + for i, col := range columnNames { + if i == timeIndex || i == metricIndex { + continue + } + + switch columnValue := values[i].(type) { + case int64: + value = null.FloatFrom(float64(columnValue)) + case float64: + value = null.FloatFrom(columnValue) + case nil: + value.Valid = false + default: + return fmt.Errorf("Value column must have numeric datatype, column: %s type: %T value: %v", col, columnValue, columnValue) + } + if metricIndex == -1 { + metric = col + } + e.appendTimePoint(pointsBySeries, seriesByQueryOrder, metric, timestamp, value) + rowCount++ + + } + } + + for elem := seriesByQueryOrder.Front(); elem != nil; elem = elem.Next() { + key := elem.Value.(string) + result.Series = append(result.Series, pointsBySeries[key]) + } + + result.Meta.Set("rowCount", rowCount) + return nil +} + +func (e PostgresQueryEndpoint) appendTimePoint(pointsBySeries map[string]*tsdb.TimeSeries, seriesByQueryOrder *list.List, metric string, timestamp float64, value null.Float) { + if series, exist := pointsBySeries[metric]; exist { + series.Points = append(series.Points, tsdb.TimePoint{value, null.FloatFrom(timestamp)}) + } else { + series := &tsdb.TimeSeries{Name: metric} + series.Points = append(series.Points, tsdb.TimePoint{value, null.FloatFrom(timestamp)}) + pointsBySeries[metric] = series + seriesByQueryOrder.PushBack(metric) + } + e.log.Debug("Rows", "metric", metric, "time", timestamp, "value", value) +} diff --git a/pkg/tsdb/postgres/postgres_test.go b/pkg/tsdb/postgres/postgres_test.go new file mode 100644 index 00000000000..75e8cb77f2e --- /dev/null +++ b/pkg/tsdb/postgres/postgres_test.go @@ -0,0 +1,125 @@ +package postgres + +import ( + "testing" + "time" + + "github.com/go-xorm/xorm" + "github.com/grafana/grafana/pkg/components/simplejson" + "github.com/grafana/grafana/pkg/log" + "github.com/grafana/grafana/pkg/services/sqlstore/sqlutil" + "github.com/grafana/grafana/pkg/tsdb" + _ "github.com/lib/pq" + . "github.com/smartystreets/goconvey/convey" +) + +// To run this test, remove the Skip from SkipConvey +// and set up a PostgreSQL db named grafanatest and a user/password grafanatest/grafanatest +func TestPostgres(t *testing.T) { + SkipConvey("PostgreSQL", t, func() { + x := InitPostgresTestDB(t) + + endpoint := &PostgresQueryEndpoint{ + sqlEngine: &tsdb.DefaultSqlEngine{ + MacroEngine: NewPostgresMacroEngine(), + XormEngine: x, + }, + log: log.New("tsdb.postgres"), + } + + sess := x.NewSession() + defer sess.Close() + + sql := ` + CREATE TABLE postgres_types( + c00_smallint smallint, + c01_integer integer, + c02_bigint bigint, + + c03_real real, + c04_double double precision, + c05_decimal decimal(10,2), + c06_numeric numeric(10,2), + + c07_char char(10), + c08_varchar varchar(10), + c09_text text, + + c10_timestamp timestamp without time zone, + c11_timestamptz timestamp with time zone, + c12_date date, + c13_time time without time zone, + c14_timetz time with time zone, + c15_interval interval + ); + ` + _, err := sess.Exec(sql) + So(err, ShouldBeNil) + + sql = ` + INSERT INTO postgres_types VALUES( + 1,2,3, + 4.5,6.7,1.1,1.2, + 'char10','varchar10','text', + + now(),now(),now(),now(),now(),'15m'::interval + ); + ` + _, err = sess.Exec(sql) + So(err, ShouldBeNil) + + Convey("Query with Table format should map PostgreSQL column types to Go types", func() { + query := &tsdb.TsdbQuery{ + Queries: []*tsdb.Query{ + { + Model: simplejson.NewFromAny(map[string]interface{}{ + "rawSql": "SELECT * FROM postgres_types", + "format": "table", + }), + RefId: "A", + }, + }, + } + + resp, err := endpoint.Query(nil, nil, query) + queryResult := resp.Results["A"] + So(err, ShouldBeNil) + + column := queryResult.Tables[0].Rows[0] + So(column[0].(int64), ShouldEqual, 1) + So(column[1].(int64), ShouldEqual, 2) + So(column[2].(int64), ShouldEqual, 3) + So(column[3].(float64), ShouldEqual, 4.5) + So(column[4].(float64), ShouldEqual, 6.7) + // libpq doesnt properly convert decimal, numeric and char to go types but returns []uint8 instead + // So(column[5].(float64), ShouldEqual, 1.1) + // So(column[6].(float64), ShouldEqual, 1.2) + // So(column[7].(string), ShouldEqual, "char") + So(column[8].(string), ShouldEqual, "varchar10") + So(column[9].(string), ShouldEqual, "text") + + So(column[10].(time.Time), ShouldHaveSameTypeAs, time.Now()) + So(column[11].(time.Time), ShouldHaveSameTypeAs, time.Now()) + So(column[12].(time.Time), ShouldHaveSameTypeAs, time.Now()) + So(column[13].(time.Time), ShouldHaveSameTypeAs, time.Now()) + So(column[14].(time.Time), ShouldHaveSameTypeAs, time.Now()) + + // libpq doesnt properly convert interval to go types but returns []uint8 instead + // So(column[15].(time.Time), ShouldHaveSameTypeAs, time.Now()) + }) + }) +} + +func InitPostgresTestDB(t *testing.T) *xorm.Engine { + x, err := xorm.NewEngine(sqlutil.TestDB_Postgres.DriverName, sqlutil.TestDB_Postgres.ConnStr) + + // x.ShowSQL() + + if err != nil { + t.Fatalf("Failed to init postgres db %v", err) + } + + sqlutil.CleanDB(x) + + return x +} diff --git a/pkg/tsdb/sql_engine.go b/pkg/tsdb/sql_engine.go new file mode 100644 index 00000000000..d79ca938bb4 --- /dev/null +++ b/pkg/tsdb/sql_engine.go @@ -0,0 +1,134 @@ +package tsdb + +import ( + "context" + "sync" + + "github.com/go-xorm/core" + "github.com/go-xorm/xorm" + "github.com/grafana/grafana/pkg/components/simplejson" + "github.com/grafana/grafana/pkg/models" +) + +// SqlEngine is a wrapper class around xorm for relational database data sources. +type SqlEngine interface { + InitEngine(driverName string, dsInfo *models.DataSource, cnnstr string) error + Query( + ctx context.Context, + ds *models.DataSource, + query *TsdbQuery, + transformToTimeSeries func(query *Query, rows *core.Rows, result *QueryResult) error, + transformToTable func(query *Query, rows *core.Rows, result *QueryResult) error, + ) (*Response, error) +} + +// SqlMacroEngine interpolates macros into sql. It takes in the timeRange to be able to +// generate queries that use from and to. +type SqlMacroEngine interface { + Interpolate(timeRange *TimeRange, sql string) (string, error) +} + +type DefaultSqlEngine struct { + MacroEngine SqlMacroEngine + XormEngine *xorm.Engine +} + +type engineCacheType struct { + cache map[int64]*xorm.Engine + versions map[int64]int + sync.Mutex +} + +var engineCache = engineCacheType{ + cache: make(map[int64]*xorm.Engine), + versions: make(map[int64]int), +} + +// InitEngine creates the db connection and inits the xorm engine or loads it from the engine cache +func (e *DefaultSqlEngine) InitEngine(driverName string, dsInfo *models.DataSource, cnnstr string) error { + engineCache.Lock() + defer engineCache.Unlock() + + if engine, present := engineCache.cache[dsInfo.Id]; present { + if version, _ := engineCache.versions[dsInfo.Id]; version == dsInfo.Version { + e.XormEngine = engine + return nil + } + } + + engine, err := xorm.NewEngine(driverName, cnnstr) + engine.SetMaxOpenConns(10) + engine.SetMaxIdleConns(10) + if err != nil { + return err + } + + engineCache.cache[dsInfo.Id] = engine + e.XormEngine = engine + + return nil +} + +// Query is a default implementation of the Query method for an SQL data source. +// The caller of this function must implement transformToTimeSeries and transformToTable and +// pass them in as parameters. +func (e *DefaultSqlEngine) Query( + ctx context.Context, + dsInfo *models.DataSource, + tsdbQuery *TsdbQuery, + transformToTimeSeries func(query *Query, rows *core.Rows, result *QueryResult) error, + transformToTable func(query *Query, rows *core.Rows, result *QueryResult) error, +) (*Response, error) { + result := &Response{ + Results: make(map[string]*QueryResult), + } + + session := e.XormEngine.NewSession() + defer session.Close() + db := session.DB() + + for _, query := range tsdbQuery.Queries { + rawSql := query.Model.Get("rawSql").MustString() + if rawSql == "" { + continue + } + + queryResult := &QueryResult{Meta: simplejson.New(), RefId: query.RefId} + result.Results[query.RefId] = queryResult + + rawSql, err := e.MacroEngine.Interpolate(tsdbQuery.TimeRange, rawSql) + if err != nil { + queryResult.Error = err + continue + } + + queryResult.Meta.Set("sql", rawSql) + + rows, err := db.Query(rawSql) + if err != nil { + queryResult.Error = err + continue + } + + defer rows.Close() + + format := query.Model.Get("format").MustString("time_series") + + switch format { + case "time_series": + err := transformToTimeSeries(query, rows, queryResult) + if err != nil { + queryResult.Error = err + continue + } + case "table": + err := transformToTable(query, rows, queryResult) + if err != nil { + queryResult.Error = err + continue + } + } + } + + return result, nil +} diff --git a/public/app/core/components/colorpicker/ColorPicker.tsx b/public/app/core/components/colorpicker/ColorPicker.tsx index 8ef51ce0be7..baf3f87cf81 100644 --- a/public/app/core/components/colorpicker/ColorPicker.tsx +++ b/public/app/core/components/colorpicker/ColorPicker.tsx @@ -77,5 +77,8 @@ export class ColorPicker extends React.Component { } coreModule.directive('colorPicker', function (reactDirective) { - return reactDirective(ColorPicker, ['color', 'onChange']); + return reactDirective(ColorPicker, [ + 'color', + ['onChange', { watchDepth: 'reference', wrapApply: true }] + ]); }); diff --git a/public/app/core/components/colorpicker/ColorPickerPopover.tsx b/public/app/core/components/colorpicker/ColorPickerPopover.tsx index 49e1b1e2105..09b6b8ec2c2 100644 --- a/public/app/core/components/colorpicker/ColorPickerPopover.tsx +++ b/public/app/core/components/colorpicker/ColorPickerPopover.tsx @@ -1,11 +1,11 @@ import React from 'react'; import $ from 'jquery'; +import tinycolor from 'tinycolor2'; import coreModule from 'app/core/core_module'; import { GfColorPalette } from './ColorPalette'; import { GfSpectrumPicker } from './SpectrumPicker'; -// Spectrum picker uses TinyColor and loads it as a global variable, so we can use it here also -declare var tinycolor; +const DEFAULT_COLOR = '#000000'; export interface IProps { color: string; @@ -19,8 +19,8 @@ export class ColorPickerPopover extends React.Component { super(props); this.state = { tab: 'palette', - color: this.props.color, - colorString: this.props.color + color: this.props.color || DEFAULT_COLOR, + colorString: this.props.color || DEFAULT_COLOR }; } @@ -88,7 +88,7 @@ export class ColorPickerPopover extends React.Component { ); const spectrumTab = (
- +
); const currentTab = this.state.tab === 'palette' ? paletteTab : spectrumTab; diff --git a/public/app/core/components/colorpicker/SeriesColorPicker.tsx b/public/app/core/components/colorpicker/SeriesColorPicker.tsx index e7294aa6281..2ee2d7571b3 100644 --- a/public/app/core/components/colorpicker/SeriesColorPicker.tsx +++ b/public/app/core/components/colorpicker/SeriesColorPicker.tsx @@ -44,7 +44,7 @@ export class SeriesColorPicker extends React.Component { return (
{this.props.series && this.renderAxisSelection()} - +
); } diff --git a/public/app/core/components/colorpicker/spectrum_picker.ts b/public/app/core/components/colorpicker/spectrum_picker.ts new file mode 100644 index 00000000000..c262eaac326 --- /dev/null +++ b/public/app/core/components/colorpicker/spectrum_picker.ts @@ -0,0 +1,23 @@ +/** + * Wrapper for the new ngReact directive for backward compatibility. + * Allows remaining untouched in outdated plugins. + * Technically, it's just a wrapper for react component with two-way data binding support. + */ +import coreModule from '../../core_module'; + +export function spectrumPicker() { + return { + restrict: 'E', + require: 'ngModel', + scope: true, + replace: true, + template: '', + link: function(scope, element, attrs, ngModel) { + scope.ngModel = ngModel; + scope.onColorChange = (color) => { + ngModel.$setViewValue(color); + }; + } + }; +} +coreModule.directive('spectrumPicker', spectrumPicker); diff --git a/public/app/core/components/dashboard_selector.ts b/public/app/core/components/dashboard_selector.ts index f68e70a17c0..7ec9f681520 100644 --- a/public/app/core/components/dashboard_selector.ts +++ b/public/app/core/components/dashboard_selector.ts @@ -4,9 +4,6 @@ import coreModule from 'app/core/core_module'; var template = ` - - Not finding dashboard you want? Star it first, then it should appear in this select box. - `; export class DashboardSelectorCtrl { diff --git a/public/app/core/components/grafana_app.ts b/public/app/core/components/grafana_app.ts index 0eee3ae43fd..8852da4a436 100644 --- a/public/app/core/components/grafana_app.ts +++ b/public/app/core/components/grafana_app.ts @@ -7,6 +7,7 @@ import $ from 'jquery'; import coreModule from 'app/core/core_module'; import {profiler} from 'app/core/profiler'; import appEvents from 'app/core/app_events'; +import Drop from 'tether-drop'; export class GrafanaCtrl { @@ -117,6 +118,11 @@ export function grafanaAppDirective(playlistSrv, contextSrv) { if (data.params.kiosk) { appEvents.emit('toggle-kiosk-mode'); } + + // close all drops + for (let drop of Drop.drops) { + drop.destroy(); + } }); // handle kiosk mode diff --git a/public/app/core/components/info_popover.ts b/public/app/core/components/info_popover.ts index a6ea853b7bb..954e84a3baa 100644 --- a/public/app/core/components/info_popover.ts +++ b/public/app/core/components/info_popover.ts @@ -27,6 +27,8 @@ export function infoPopover() { transclude(function(clone, newScope) { var content = document.createElement("div"); + content.className = 'markdown-html'; + _.each(clone, (node) => { content.appendChild(node); }); diff --git a/public/app/core/components/jsontree/jsontree.ts b/public/app/core/components/jsontree/jsontree.ts index 5c39e2ab53c..52fb64e1c87 100644 --- a/public/app/core/components/jsontree/jsontree.ts +++ b/public/app/core/components/jsontree/jsontree.ts @@ -1,201 +1,22 @@ - -/** Created by: Alex Wendland (me@alexwendland.com), 2014-08-06 - * - * angular-json-tree - * - * Directive for creating a tree-view out of a JS Object. Only loads - * sub-nodes on demand in order to improve performance of rendering large - * objects. - * - * Attributes: - * - object (Object, 2-way): JS object to build the tree from - * - start-expanded (Boolean, 1-way, ?=true): should the tree default to expanded - * - * Usage: - * // In the controller - * scope.someObject = { - * test: 'hello', - * array: [1,1,2,3,5,8] - * }; - * // In the html - * - * - * Dependencies: - * - utils (json-tree.js) - * - ajsRecursiveDirectiveHelper (json-tree.js) - * - * Test: json-tree-test.js - */ - -import angular from 'angular'; import coreModule from 'app/core/core_module'; - -var utils = { - /* See link for possible type values to check against. - * http://stackoverflow.com/questions/4622952/json-object-containing-array - * - * Value Class Type - * ------------------------------------- - * "foo" String string - * new String("foo") String object - * 1.2 Number number - * new Number(1.2) Number object - * true Boolean boolean - * new Boolean(true) Boolean object - * new Date() Date object - * new Error() Error object - * [1,2,3] Array object - * new Array(1, 2, 3) Array object - * new Function("") Function function - * /abc/g RegExp object (function in Nitro/V8) - * new RegExp("meow") RegExp object (function in Nitro/V8) - * {} Object object - * new Object() Object object - */ - is: function is(obj, clazz) { - return Object.prototype.toString.call(obj).slice(8, -1) === clazz; - }, - - // See above for possible values - whatClass: function whatClass(obj) { - return Object.prototype.toString.call(obj).slice(8, -1); - }, - - // Iterate over an objects keyset - forKeys: function forKeys(obj, f) { - for (var key in obj) { - if (obj.hasOwnProperty(key) && typeof obj[key] !== 'function') { - if (f(key, obj[key])) { - break; - } - } - } - } -}; +import {JsonExplorer} from '../json_explorer/json_explorer'; coreModule.directive('jsonTree', [function jsonTreeDirective() { - return { + return{ restrict: 'E', scope: { object: '=', startExpanded: '@', rootName: '@', }, - template: '' - }; -}]); + link: function(scope, elem) { -coreModule.directive('jsonNode', ['ajsRecursiveDirectiveHelper', function jsonNodeDirective(ajsRecursiveDirectiveHelper) { - return { - restrict: 'E', - scope: { - key: '=', - value: '=', - startExpanded: '@' - }, - compile: function jsonNodeDirectiveCompile(elem) { - return ajsRecursiveDirectiveHelper.compile(elem, this); - }, - template: ' {{key}}' + - ' {{value}}' + - ' ' + - ' {{preview}}' + - '
    ' + - '
  • ' + - ' ' + - '
  • ' + - '
', - pre: function jsonNodeDirectiveLink(scope, elem, attrs) { - // Set value's type as Class for CSS styling - elem.addClass(utils.whatClass(scope.value).toLowerCase()); - // If the value is an Array or Object, use expandable view type - if (utils.is(scope.value, 'Object') || utils.is(scope.value, 'Array')) { - scope.isExpandable = true; - // Add expandable class for CSS usage - elem.addClass('expandable'); - // Setup preview text - var isArray = utils.is(scope.value, 'Array'); - scope.preview = isArray ? '[ ' : '{ '; - utils.forKeys(scope.value, function jsonNodeDirectiveLinkForKeys(key, value) { - if (value === null) { scope.value[key] = 'null'; } - if (isArray) { - scope.preview += value + ', '; - } else { - scope.preview += key + ': ' + value + ', '; - } - }); - scope.preview = scope.preview.substring(0, scope.preview.length - (scope.preview.length > 2 ? 2 : 0)) + (isArray ? ' ]' : ' }'); - // If directive initially has isExpanded set, also set shouldRender to true - if (scope.startExpanded) { - scope.shouldRender = true; - elem.addClass('expanded'); - } - // Setup isExpanded state handling - scope.isExpanded = scope.startExpanded; - scope.toggleExpanded = function jsonNodeDirectiveToggleExpanded() { - scope.isExpanded = !scope.isExpanded; - if (scope.isExpanded) { - elem.addClass('expanded'); - } else { - elem.removeClass('expanded'); - } - // For delaying subnode render until requested - scope.shouldRender = true; - }; - } else { - scope.isExpandable = false; - // Add expandable class for CSS usage - elem.addClass('not-expandable'); - } - } - }; -}]); + var jsonExp = new JsonExplorer(scope.object, 3, { + animateOpen: true + }); -/** Added by: Alex Wendland (me@alexwendland.com), 2014-08-09 - * Source: http://stackoverflow.com/questions/14430655/recursion-in-angular-directives - * - * Used to allow for recursion within directives - */ -coreModule.factory('ajsRecursiveDirectiveHelper', ['$compile', function RecursiveDirectiveHelper($compile) { - return { - /** - * Manually compiles the element, fixing the recursion loop. - * @param element - * @param [link] A post-link function, or an object with function(s) registered via pre and post properties. - * @returns An object containing the linking functions. - */ - compile: function RecursiveDirectiveHelperCompile(element, link) { - // Normalize the link parameter - if (angular.isFunction(link)) { - link = { - post: link - }; - } - - // Break the recursion loop by removing the contents - var contents = element.contents().remove(); - var compiledContents; - return { - pre: (link && link.pre) ? link.pre : null, - /** - * Compiles and re-adds the contents - */ - post: function RecursiveDirectiveHelperCompilePost(scope, element) { - // Compile the contents - if (!compiledContents) { - compiledContents = $compile(contents); - } - // Re-add the compiled contents to the element - compiledContents(scope, function (clone) { - element.append(clone); - }); - - // Call the post-linking function, if any - if (link && link.post) { - link.post.apply(null, arguments); - } - } - }; + const html = jsonExp.render(true); + elem.html(html); } }; }]); diff --git a/public/app/core/controllers/error_ctrl.js b/public/app/core/controllers/error_ctrl.js index cc711f07a22..fd4081186be 100644 --- a/public/app/core/controllers/error_ctrl.js +++ b/public/app/core/controllers/error_ctrl.js @@ -1,19 +1,21 @@ define([ 'angular', + 'app/core/config', '../core_module', ], -function (angular, coreModule) { +function (angular, config, coreModule) { 'use strict'; coreModule.default.controller('ErrorCtrl', function($scope, contextSrv, navModelSrv) { $scope.navModel = navModelSrv.getNotFoundNav(); + $scope.appSubUrl = config.appSubUrl; var showSideMenu = contextSrv.sidemenu; contextSrv.sidemenu = false; $scope.$on('$destroy', function() { - $scope.contextSrv.sidemenu = showSideMenu; + contextSrv.sidemenu = showSideMenu; }); }); diff --git a/public/app/core/core.ts b/public/app/core/core.ts index 95b2e20aab6..e28f8d2d7eb 100644 --- a/public/app/core/core.ts +++ b/public/app/core/core.ts @@ -5,7 +5,6 @@ import "./directives/dropdown_typeahead"; import "./directives/metric_segment"; import "./directives/misc"; import "./directives/ng_model_on_blur"; -import "./directives/spectrum_picker"; import "./directives/tags"; import "./directives/value_select_dropdown"; import "./directives/rebuild_on_change"; @@ -18,6 +17,7 @@ import './components/code_editor/code_editor'; import './utils/outline'; import './components/colorpicker/ColorPicker'; import './components/colorpicker/SeriesColorPicker'; +import './components/colorpicker/spectrum_picker'; import {grafanaAppDirective} from './components/grafana_app'; import {sideMenuDirective} from './components/sidemenu/sidemenu'; diff --git a/public/app/core/directives/metric_segment.js b/public/app/core/directives/metric_segment.js index 2e9442c15a0..37352556819 100644 --- a/public/app/core/directives/metric_segment.js +++ b/public/app/core/directives/metric_segment.js @@ -79,7 +79,9 @@ function (_, $, coreModule) { $scope.$apply(function() { $scope.getOptions({ $query: query }).then(function(altSegments) { $scope.altSegments = altSegments; - options = _.map($scope.altSegments, function(alt) { return alt.value; }); + options = _.map($scope.altSegments, function(alt) { + return _.escape(alt.value); + }); // add custom values if (segment.custom !== 'false') { diff --git a/public/app/core/directives/spectrum_picker.js b/public/app/core/directives/spectrum_picker.js deleted file mode 100644 index 612188b71d9..00000000000 --- a/public/app/core/directives/spectrum_picker.js +++ /dev/null @@ -1,41 +0,0 @@ -define([ - 'angular', - '../core_module', - 'vendor/spectrum', -], -function (angular, coreModule) { - 'use strict'; - - coreModule.default.directive('spectrumPicker', function() { - return { - restrict: 'E', - require: 'ngModel', - scope: false, - replace: true, - template: "", - link: function(scope, element, attrs, ngModel) { - var input = element.find('input'); - var options = angular.extend({ - showAlpha: true, - showButtons: false, - color: ngModel.$viewValue, - change: function(color) { - scope.$apply(function() { - ngModel.$setViewValue(color.toRgbString()); - }); - } - }, scope.$eval(attrs.options)); - - ngModel.$render = function() { - input.spectrum('set', ngModel.$viewValue || ''); - }; - - input.spectrum(options); - - scope.$on('$destroy', function() { - input.spectrum('destroy'); - }); - } - }; - }); -}); diff --git a/public/app/core/directives/tags.js b/public/app/core/directives/tags.js index 90a355dea07..a322673a342 100644 --- a/public/app/core/directives/tags.js +++ b/public/app/core/directives/tags.js @@ -88,6 +88,7 @@ function (angular, $, coreModule) { typeahead: { source: angular.isFunction(scope.$parent[attrs.typeaheadSource]) ? scope.$parent[attrs.typeaheadSource] : null }, + widthClass: attrs.widthClass, itemValue: getItemProperty(scope, attrs.itemvalue), itemText : getItemProperty(scope, attrs.itemtext), tagClass : angular.isFunction(scope.$parent[attrs.tagclass]) ? diff --git a/public/app/core/nav_model_srv.ts b/public/app/core/nav_model_srv.ts index 4771f8c191c..dd61db5d346 100644 --- a/public/app/core/nav_model_srv.ts +++ b/public/app/core/nav_model_srv.ts @@ -163,7 +163,7 @@ export class NavModelSrv { menu.push({ title: 'Annotations', - icon: 'fa fa-fw fa-bolt', + icon: 'fa fa-fw fa-comment', clickHandler: () => dashNavCtrl.openEditView('annotations') }); diff --git a/public/app/core/specs/time_series_specs.ts b/public/app/core/specs/time_series_specs.ts new file mode 100644 index 00000000000..ddf08a9f704 --- /dev/null +++ b/public/app/core/specs/time_series_specs.ts @@ -0,0 +1,299 @@ +import {describe, beforeEach, it, expect} from 'test/lib/common'; +import TimeSeries from 'app/core/time_series2'; + +describe("TimeSeries", function() { + var points, series; + var yAxisFormats = ['short', 'ms']; + var testData; + + beforeEach(function() { + testData = { + alias: 'test', + datapoints: [ + [1,2],[null,3],[10,4],[8,5] + ] + }; + }); + + describe('when getting flot pairs', function() { + it('with connected style, should ignore nulls', function() { + series = new TimeSeries(testData); + points = series.getFlotPairs('connected', yAxisFormats); + expect(points.length).to.be(3); + }); + + it('with null as zero style, should replace nulls with zero', function() { + series = new TimeSeries(testData); + points = series.getFlotPairs('null as zero', yAxisFormats); + expect(points.length).to.be(4); + expect(points[1][1]).to.be(0); + }); + + it('if last is null current should pick next to last', function() { + series = new TimeSeries({ + datapoints: [[10,1], [null, 2]] + }); + series.getFlotPairs('null', yAxisFormats); + expect(series.stats.current).to.be(10); + }); + + it('max value should work for negative values', function() { + series = new TimeSeries({ + datapoints: [[-10,1], [-4, 2]] + }); + series.getFlotPairs('null', yAxisFormats); + expect(series.stats.max).to.be(-4); + }); + + it('average value should ignore nulls', function() { + series = new TimeSeries(testData); + series.getFlotPairs('null', yAxisFormats); + expect(series.stats.avg).to.be(6.333333333333333); + }); + + it('the delta value should account for nulls', function() { + series = new TimeSeries({ + datapoints: [[1,2],[3,3],[null,4],[10,5],[15,6]] + }); + series.getFlotPairs('null', yAxisFormats); + expect(series.stats.delta).to.be(14); + }); + + it('the delta value should account for nulls on first', function() { + series = new TimeSeries({ + datapoints: [[null,2],[1,3],[10,4],[15,5]] + }); + series.getFlotPairs('null', yAxisFormats); + expect(series.stats.delta).to.be(14); + }); + + it('the delta value should account for nulls on last', function() { + series = new TimeSeries({ + datapoints: [[1,2],[5,3],[10,4],[null,5]] + }); + series.getFlotPairs('null', yAxisFormats); + expect(series.stats.delta).to.be(9); + }); + + it('the delta value should account for resets', function() { + series = new TimeSeries({ + datapoints: [[1,2],[5,3],[10,4],[0,5],[10,6]] + }); + series.getFlotPairs('null', yAxisFormats); + expect(series.stats.delta).to.be(19); + }); + + it('the delta value should account for resets on last', function() { + series = new TimeSeries({ + datapoints: [[1,2],[2,3],[10,4],[8,5]] + }); + series.getFlotPairs('null', yAxisFormats); + expect(series.stats.delta).to.be(17); + }); + + it('the range value should be max - min', function() { + series = new TimeSeries(testData); + series.getFlotPairs('null', yAxisFormats); + expect(series.stats.range).to.be(9); + }); + + it('first value should ingone nulls', function() { + series = new TimeSeries(testData); + series.getFlotPairs('null', yAxisFormats); + expect(series.stats.first).to.be(1); + series = new TimeSeries({ + datapoints: [[null,2],[1,3],[10,4],[8,5]] + }); + series.getFlotPairs('null', yAxisFormats); + expect(series.stats.first).to.be(1); + }); + + it('with null as zero style, average value should treat nulls as 0', function() { + series = new TimeSeries(testData); + series.getFlotPairs('null as zero', yAxisFormats); + expect(series.stats.avg).to.be(4.75); + }); + + it('average value should be null if all values is null', function() { + series = new TimeSeries({ + datapoints: [[null,2],[null,3],[null,4],[null,5]] + }); + series.getFlotPairs('null'); + expect(series.stats.avg).to.be(null); + }); + }); + + describe('When checking if ms resolution is needed', function() { + describe('msResolution with second resolution timestamps', function() { + beforeEach(function() { + series = new TimeSeries({datapoints: [[45, 1234567890], [60, 1234567899]]}); + }); + + it('should set hasMsResolution to false', function() { + expect(series.hasMsResolution).to.be(false); + }); + }); + + describe('msResolution with millisecond resolution timestamps', function() { + beforeEach(function() { + series = new TimeSeries({datapoints: [[55, 1236547890001], [90, 1234456709000]]}); + }); + + it('should show millisecond resolution tooltip', function() { + expect(series.hasMsResolution).to.be(true); + }); + }); + + describe('msResolution with millisecond resolution timestamps but with trailing zeroes', function() { + beforeEach(function() { + series = new TimeSeries({datapoints: [[45, 1234567890000], [60, 1234567899000]]}); + }); + + it('should not show millisecond resolution tooltip', function() { + expect(series.hasMsResolution).to.be(false); + }); + }); + }); + + describe('can detect if series contains ms precision', function() { + var fakedata; + + beforeEach(function() { + fakedata = testData; + }); + + it('missing datapoint with ms precision', function() { + fakedata.datapoints[0] = [1337, 1234567890000]; + series = new TimeSeries(fakedata); + expect(series.isMsResolutionNeeded()).to.be(false); + }); + + it('contains datapoint with ms precision', function() { + fakedata.datapoints[0] = [1337, 1236547890001]; + series = new TimeSeries(fakedata); + expect(series.isMsResolutionNeeded()).to.be(true); + }); + }); + + describe('series overrides', function() { + var series; + beforeEach(function() { + series = new TimeSeries(testData); + }); + + describe('fill & points', function() { + beforeEach(function() { + series.alias = 'test'; + series.applySeriesOverrides([{ alias: 'test', fill: 0, points: true }]); + }); + + it('should set fill zero, and enable points', function() { + expect(series.lines.fill).to.be(0.001); + expect(series.points.show).to.be(true); + }); + }); + + describe('series option overrides, bars, true & lines false', function() { + beforeEach(function() { + series.alias = 'test'; + series.applySeriesOverrides([{ alias: 'test', bars: true, lines: false }]); + }); + + it('should disable lines, and enable bars', function() { + expect(series.lines.show).to.be(false); + expect(series.bars.show).to.be(true); + }); + }); + + describe('series option overrides, linewidth, stack', function() { + beforeEach(function() { + series.alias = 'test'; + series.applySeriesOverrides([{ alias: 'test', linewidth: 5, stack: false }]); + }); + + it('should disable stack, and set lineWidth', function() { + expect(series.stack).to.be(false); + expect(series.lines.lineWidth).to.be(5); + }); + }); + + describe('series option overrides, dashes and lineWidth', function() { + beforeEach(function() { + series.alias = 'test'; + series.applySeriesOverrides([{ alias: 'test', linewidth: 5, dashes: true }]); + }); + + it('should enable dashes, set dashes lineWidth to 5 and lines lineWidth to 0', function() { + expect(series.dashes.show).to.be(true); + expect(series.dashes.lineWidth).to.be(5); + expect(series.lines.lineWidth).to.be(0); + }); + }); + + describe('series option overrides, fill below to', function() { + beforeEach(function() { + series.alias = 'test'; + series.applySeriesOverrides([{ alias: 'test', fillBelowTo: 'min' }]); + }); + + it('should disable line fill and add fillBelowTo', function() { + expect(series.fillBelowTo).to.be('min'); + }); + }); + + describe('series option overrides, pointradius, steppedLine', function() { + beforeEach(function() { + series.alias = 'test'; + series.applySeriesOverrides([{ alias: 'test', pointradius: 5, steppedLine: true }]); + }); + + it('should set pointradius, and set steppedLine', function() { + expect(series.points.radius).to.be(5); + expect(series.lines.steps).to.be(true); + }); + }); + + describe('override match on regex', function() { + beforeEach(function() { + series.alias = 'test_01'; + series.applySeriesOverrides([{ alias: '/.*01/', lines: false }]); + }); + + it('should match second series', function() { + expect(series.lines.show).to.be(false); + }); + }); + + describe('override series y-axis, and z-index', function() { + beforeEach(function() { + series.alias = 'test'; + series.applySeriesOverrides([{ alias: 'test', yaxis: 2, zindex: 2 }]); + }); + + it('should set yaxis', function() { + expect(series.yaxis).to.be(2); + }); + + it('should set zindex', function() { + expect(series.zindex).to.be(2); + }); + }); + + }); + + describe('value formatter', function() { + var series; + beforeEach(function() { + series = new TimeSeries(testData); + }); + + it('should format non-numeric values as empty string', function() { + expect(series.formatValue(null)).to.be(""); + expect(series.formatValue(undefined)).to.be(""); + expect(series.formatValue(NaN)).to.be(""); + expect(series.formatValue(Infinity)).to.be(""); + expect(series.formatValue(-Infinity)).to.be(""); + }); + }); + +}); diff --git a/public/app/core/time_series2.ts b/public/app/core/time_series2.ts index c8ab8d9efcf..1c4c94da6fb 100644 --- a/public/app/core/time_series2.ts +++ b/public/app/core/time_series2.ts @@ -203,7 +203,7 @@ export default class TimeSeries { if (this.stats.max === -Number.MAX_VALUE) { this.stats.max = null; } if (this.stats.min === Number.MAX_VALUE) { this.stats.min = null; } - if (result.length) { + if (result.length && !this.allIsNull) { this.stats.avg = (this.stats.total / nonNulls); this.stats.current = result[result.length-1][1]; if (this.stats.current === null && result.length > 1) { @@ -228,6 +228,9 @@ export default class TimeSeries { } formatValue(value) { + if (!_.isFinite(value)) { + value = null; // Prevent NaN formatting + } return this.valueFormater(value, this.decimals, this.scaledDecimals); } diff --git a/public/app/core/utils/colors.ts b/public/app/core/utils/colors.ts index b9dd164d635..a38c92a6476 100644 --- a/public/app/core/utils/colors.ts +++ b/public/app/core/utils/colors.ts @@ -1,10 +1,13 @@ import _ from 'lodash'; - -// Spectrum picker uses TinyColor and loads it as a global variable, so we can use it here also -declare var tinycolor; +import tinycolor from 'tinycolor2'; export const PALETTE_ROWS = 4; export const PALETTE_COLUMNS = 14; +export const DEFAULT_ANNOTATION_COLOR = 'rgba(0, 211, 255, 1)'; +export const OK_COLOR = "rgba(11, 237, 50, 1)"; +export const ALERTING_COLOR = "rgba(237, 46, 24, 1)"; +export const NO_DATA_COLOR = "rgba(150, 150, 150, 1)"; +export const REGION_FILL_ALPHA = 0.09; let colors = [ "#7EB26D","#EAB839","#6ED0E0","#EF843C","#E24D42","#1F78C1","#BA43A9","#705DA0", diff --git a/public/app/core/utils/kbn.js b/public/app/core/utils/kbn.js index 8947ce5a902..1daaa2f8ba3 100644 --- a/public/app/core/utils/kbn.js +++ b/public/app/core/utils/kbn.js @@ -402,6 +402,10 @@ function($, _, moment) { kbn.valueFormats.currencyRUB = kbn.formatBuilders.currency('₽'); kbn.valueFormats.currencyUAH = kbn.formatBuilders.currency('₴'); kbn.valueFormats.currencyBRL = kbn.formatBuilders.currency('R$'); + kbn.valueFormats.currencyDKK = kbn.formatBuilders.currency('kr'); + kbn.valueFormats.currencyISK = kbn.formatBuilders.currency('kr'); + kbn.valueFormats.currencyNOK = kbn.formatBuilders.currency('kr'); + kbn.valueFormats.currencySEK = kbn.formatBuilders.currency('kr'); // Data (Binary) kbn.valueFormats.bits = kbn.formatBuilders.binarySIPrefix('b'); @@ -756,6 +760,10 @@ function($, _, moment) { {text: 'Rubles (₽)', value: 'currencyRUB'}, {text: 'Hryvnias (₴)', value: 'currencyUAH'}, {text: 'Real (R$)', value: 'currencyBRL'}, + {text: 'Danish Krone (kr)', value: 'currencyDKK'}, + {text: 'Icelandic Krone (kr)', value: 'currencyISK'}, + {text: 'Norwegian Krone (kr)', value: 'currencyNOK'}, + {text: 'Swedish Krone (kr)', value: 'currencySEK'}, ] }, { diff --git a/public/app/features/alerting/alert_def.ts b/public/app/features/alerting/alert_def.ts index 51cbbd9691f..c86f0dee775 100644 --- a/public/app/features/alerting/alert_def.ts +++ b/public/app/features/alerting/alert_def.ts @@ -128,7 +128,6 @@ function joinEvalMatches(matches, separator: string) { } function getAlertAnnotationInfo(ah) { - // backward compatability, can be removed in grafana 5.x // old way stored evalMatches in data property directly, // new way stores it in evalMatches property on new data object diff --git a/public/app/features/alerting/alert_tab_ctrl.ts b/public/app/features/alerting/alert_tab_ctrl.ts index 677eec31060..25c23580ed7 100644 --- a/public/app/features/alerting/alert_tab_ctrl.ts +++ b/public/app/features/alerting/alert_tab_ctrl.ts @@ -94,6 +94,7 @@ export class AlertTabCtrl { case "opsgenie": return "fa fa-bell"; case "hipchat": return "fa fa-mail-forward"; case "pushover": return "fa fa-mobile"; + case "kafka": return "fa fa-random"; } return 'fa fa-bell'; } diff --git a/public/app/features/alerting/partials/notification_edit.html b/public/app/features/alerting/partials/notification_edit.html index 9b716e02ce6..b1e72a30fbb 100644 --- a/public/app/features/alerting/partials/notification_edit.html +++ b/public/app/features/alerting/partials/notification_edit.html @@ -40,13 +40,11 @@
-
- +
+
-
-
- -
+
+
diff --git a/public/app/features/annotations/annotation_tooltip.ts b/public/app/features/annotations/annotation_tooltip.ts index 39c2ff84acb..c950d3edd55 100644 --- a/public/app/features/annotations/annotation_tooltip.ts +++ b/public/app/features/annotations/annotation_tooltip.ts @@ -1,12 +1,10 @@ -/// - import _ from 'lodash'; import $ from 'jquery'; import coreModule from 'app/core/core_module'; import alertDef from '../alerting/alert_def'; /** @ngInject **/ -export function annotationTooltipDirective($sanitize, dashboardSrv, $compile) { +export function annotationTooltipDirective($sanitize, dashboardSrv, contextSrv, popoverSrv, $compile) { function sanitizeString(str) { try { @@ -21,6 +19,7 @@ export function annotationTooltipDirective($sanitize, dashboardSrv, $compile) { restrict: 'E', scope: { "event": "=", + "onEdit": "&" }, link: function(scope, element) { var event = scope.event; @@ -31,33 +30,46 @@ export function annotationTooltipDirective($sanitize, dashboardSrv, $compile) { var tooltip = '
'; var titleStateClass = ''; - if (event.source.name === 'panel-alert') { + if (event.alertId) { var stateModel = alertDef.getStateDisplayModel(event.newState); titleStateClass = stateModel.stateClass; title = ` ${stateModel.text}`; text = alertDef.getAlertAnnotationInfo(event); + if (event.text) { + text = text + '
' + event.text; + } + } else if (title) { + text = title + '
' + text; + title = ''; } - tooltip += ` -
- ${sanitizeString(title)} - ${dashboard.formatDate(event.min)} -
+ var header = `
`; + if (event.login) { + header += `
`; + } + header += ` + ${sanitizeString(title)} + ${dashboard.formatDate(event.min)} `; - tooltip += '
'; + // Show edit icon only for users with at least Editor role + if (event.id && contextSrv.isEditor) { + header += ` + + + + `; + } + + header += `
`; + tooltip += header; + tooltip += '
'; if (text) { - tooltip += sanitizeString(text).replace(/\n/g, '
') + '
'; + tooltip += '
' + sanitizeString(text.replace(/\n/g, '
')) + '
'; } var tags = event.tags; - if (_.isString(event.tags)) { - tags = event.tags.split(','); - if (tags.length === 1) { - tags = event.tags.split(' '); - } - } if (tags && tags.length) { scope.tags = tags; @@ -65,6 +77,7 @@ export function annotationTooltipDirective($sanitize, dashboardSrv, $compile) { } tooltip += "
"; + tooltip += '
'; var $tooltip = $(tooltip); $tooltip.appendTo(element); diff --git a/public/app/features/annotations/annotations_srv.ts b/public/app/features/annotations/annotations_srv.ts index e6a4c8660ae..2863ecdd843 100644 --- a/public/app/features/annotations/annotations_srv.ts +++ b/public/app/features/annotations/annotations_srv.ts @@ -1,5 +1,3 @@ -/// - import './editor_ctrl'; import angular from 'angular'; @@ -11,11 +9,7 @@ export class AnnotationsSrv { alertStatesPromise: any; /** @ngInject */ - constructor(private $rootScope, - private $q, - private datasourceSrv, - private backendSrv, - private timeSrv) { + constructor(private $rootScope, private $q, private datasourceSrv, private backendSrv, private timeSrv) { $rootScope.onAppEvent('refresh', this.clearCache.bind(this), $rootScope); $rootScope.onAppEvent('dashboard-initialized', this.clearCache.bind(this), $rootScope); } @@ -26,64 +20,40 @@ export class AnnotationsSrv { } getAnnotations(options) { - return this.$q.all([ - this.getGlobalAnnotations(options), - this.getPanelAnnotations(options), - this.getAlertStates(options) - ]).then(results => { + return this.$q + .all([this.getGlobalAnnotations(options), this.getAlertStates(options)]) + .then(results => { + // combine the annotations and flatten results + var annotations = _.flattenDeep(results[0]); - // combine the annotations and flatten results - var annotations = _.flattenDeep([results[0], results[1]]); - - // filter out annotations that do not belong to requesting panel - annotations = _.filter(annotations, item => { - // shownIn === 1 requires annotation matching panel id - if (item.source.showIn === 1) { - if (item.panelId && options.panel.id === item.panelId) { - return true; + // filter out annotations that do not belong to requesting panel + annotations = _.filter(annotations, item => { + // if event has panel id and query is of type dashboard then panel and requesting panel id must match + if (item.panelId && item.source.type === 'dashboard') { + return item.panelId === options.panel.id; } - return false; + return true; + }); + + annotations = dedupAnnotations(annotations); + annotations = makeRegions(annotations, options); + + // look for alert state for this panel + var alertState = _.find(results[1], {panelId: options.panel.id}); + + return { + annotations: annotations, + alertState: alertState, + }; + }) + .catch(err => { + if (!err.message && err.data && err.data.message) { + err.message = err.data.message; } - return true; + console.log('AnnotationSrv.query error', err); + this.$rootScope.appEvent('alert-error', ['Annotation Query Failed', err.message || err]); + return []; }); - - // look for alert state for this panel - var alertState = _.find(results[2], {panelId: options.panel.id}); - - return { - annotations: annotations, - alertState: alertState, - }; - - }).catch(err => { - if (!err.message && err.data && err.data.message) { - err.message = err.data.message; - } - this.$rootScope.appEvent('alert-error', ['Annotation Query Failed', (err.message || err)]); - - return []; - }); - } - - getPanelAnnotations(options) { - var panel = options.panel; - var dashboard = options.dashboard; - - if (dashboard.id && panel && panel.alert) { - return this.backendSrv.get('/api/annotations', { - from: options.range.from.valueOf(), - to: options.range.to.valueOf(), - limit: 100, - panelId: panel.id, - dashboardId: dashboard.id, - }).then(results => { - // this built in annotation source name `panel-alert` is used in annotation tooltip - // to know that this annotation is from panel alert - return this.translateQueryResult({iconColor: '#AA0000', name: 'panel-alert'}, results); - }); - } - - return this.$q.when([]); } getAlertStates(options) { @@ -104,43 +74,55 @@ export class AnnotationsSrv { return this.alertStatesPromise; } - this.alertStatesPromise = this.backendSrv.get('/api/alerts/states-for-dashboard', {dashboardId: options.dashboard.id}); + this.alertStatesPromise = this.backendSrv.get('/api/alerts/states-for-dashboard', { + dashboardId: options.dashboard.id, + }); return this.alertStatesPromise; } getGlobalAnnotations(options) { var dashboard = options.dashboard; - if (dashboard.annotations.list.length === 0) { - return this.$q.when([]); - } - if (this.globalAnnotationsPromise) { return this.globalAnnotationsPromise; } - var annotations = _.filter(dashboard.annotations.list, {enable: true}); var range = this.timeSrv.timeRange(); + var promises = []; + + for (let annotation of dashboard.annotations.list) { + if (!annotation.enable) { + continue; + } - this.globalAnnotationsPromise = this.$q.all(_.map(annotations, annotation => { if (annotation.snapshotData) { return this.translateQueryResult(annotation, annotation.snapshotData); } - return this.datasourceSrv.get(annotation.datasource).then(datasource => { - // issue query against data source - return datasource.annotationQuery({range: range, rangeRaw: range.raw, annotation: annotation}); - }) - .then(results => { - // store response in annotation object if this is a snapshot call - if (dashboard.snapshot) { - annotation.snapshotData = angular.copy(results); - } - // translate result - return this.translateQueryResult(annotation, results); - }); - })); + promises.push( + this.datasourceSrv + .get(annotation.datasource) + .then(datasource => { + // issue query against data source + return datasource.annotationQuery({ + range: range, + rangeRaw: range.raw, + annotation: annotation, + dashboard: dashboard, + }); + }) + .then(results => { + // store response in annotation object if this is a snapshot call + if (dashboard.snapshot) { + annotation.snapshotData = angular.copy(results); + } + // translate result + return this.translateQueryResult(annotation, results); + }), + ); + } + this.globalAnnotationsPromise = this.$q.all(promises); return this.globalAnnotationsPromise; } @@ -149,6 +131,21 @@ export class AnnotationsSrv { return this.backendSrv.post('/api/annotations', annotation); } + updateAnnotationEvent(annotation) { + this.globalAnnotationsPromise = null; + return this.backendSrv.put(`/api/annotations/${annotation.id}`, annotation); + } + + deleteAnnotationEvent(annotation) { + this.globalAnnotationsPromise = null; + let deleteUrl = `/api/annotations/${annotation.id}`; + if (annotation.isRegion) { + deleteUrl = `/api/annotations/region/${annotation.regionId}`; + } + + return this.backendSrv.delete(deleteUrl); + } + translateQueryResult(annotation, results) { // if annotation has snapshotData // make clone and remove it @@ -159,13 +156,88 @@ export class AnnotationsSrv { for (var item of results) { item.source = annotation; - item.min = item.time; - item.max = item.time; - item.scope = 1; - item.eventType = annotation.name; } return results; } } +/** + * This function converts annotation events into set + * of single events and regions (event consist of two) + * @param annotations + * @param options + */ +function makeRegions(annotations, options) { + let [regionEvents, singleEvents] = _.partition(annotations, 'regionId'); + let regions = getRegions(regionEvents, options.range); + annotations = _.concat(regions, singleEvents); + return annotations; +} + +function getRegions(events, range) { + let region_events = _.filter(events, event => { + return event.regionId; + }); + let regions = _.groupBy(region_events, 'regionId'); + regions = _.compact( + _.map(regions, region_events => { + let region_obj = _.head(region_events); + if (region_events && region_events.length > 1) { + region_obj.timeEnd = region_events[1].time; + region_obj.isRegion = true; + return region_obj; + } else { + if (region_events && region_events.length) { + // Don't change proper region object + if (!region_obj.time || !region_obj.timeEnd) { + // This is cut region + if (isStartOfRegion(region_obj)) { + region_obj.timeEnd = range.to.valueOf() - 1; + } else { + // Start time = null + region_obj.timeEnd = region_obj.time; + region_obj.time = range.from.valueOf() + 1; + } + region_obj.isRegion = true; + } + + return region_obj; + } + } + }), + ); + + return regions; +} + +function isStartOfRegion(event): boolean { + return event.id && event.id === event.regionId; +} + +function dedupAnnotations(annotations) { + let dedup = []; + + // Split events by annotationId property existance + let events = _.partition(annotations, 'id'); + + let eventsById = _.groupBy(events[0], 'id'); + dedup = _.map(eventsById, eventGroup => { + if (eventGroup.length > 1 && !_.every(eventGroup, isPanelAlert)) { + // Get first non-panel alert + return _.find(eventGroup, event => { + return event.eventType !== 'panel-alert'; + }); + } else { + return _.head(eventGroup); + } + }); + + dedup = _.concat(dedup, events[1]); + return dedup; +} + +function isPanelAlert(event) { + return event.eventType === 'panel-alert'; +} + coreModule.service('annotationsSrv', AnnotationsSrv); diff --git a/public/app/features/annotations/editor_ctrl.ts b/public/app/features/annotations/editor_ctrl.ts index 74c4768b5ad..a52e241ce35 100644 --- a/public/app/features/annotations/editor_ctrl.ts +++ b/public/app/features/annotations/editor_ctrl.ts @@ -1,5 +1,3 @@ -/// - import angular from 'angular'; import _ from 'lodash'; import $ from 'jquery'; @@ -36,11 +34,7 @@ export class AnnotationsEditorCtrl { this.annotations = $scope.dashboard.annotations.list; this.reset(); - $scope.$watch('mode', newVal => { - if (newVal === 'new') { - this.reset(); - } - }); + this.onColorChange = this.onColorChange.bind(this); } datasourceChanged() { @@ -71,6 +65,11 @@ export class AnnotationsEditorCtrl { this.$scope.broadcastRefresh(); } + setupNew() { + this.mode = 'new'; + this.reset(); + } + add() { this.annotations.push(this.currentAnnotation); this.reset(); @@ -85,6 +84,18 @@ export class AnnotationsEditorCtrl { this.$scope.dashboard.updateSubmenuVisibility(); this.$scope.broadcastRefresh(); } + + onColorChange(newColor) { + this.currentAnnotation.iconColor = newColor; + } + + annotationEnabledChange() { + this.$scope.broadcastRefresh(); + } + + annotationHiddenChanged() { + this.$scope.dashboard.updateSubmenuVisibility(); + } } coreModule.controller('AnnotationsEditorCtrl', AnnotationsEditorCtrl); diff --git a/public/app/features/annotations/event.ts b/public/app/features/annotations/event.ts index 53afbea5b07..24d0edbe1a2 100644 --- a/public/app/features/annotations/event.ts +++ b/public/app/features/annotations/event.ts @@ -2,9 +2,11 @@ export class AnnotationEvent { dashboardId: number; panelId: number; + userId: number; time: any; timeEnd: any; isRegion: boolean; - title: string; text: string; + type: string; + tags: string; } diff --git a/public/app/features/annotations/event_editor.ts b/public/app/features/annotations/event_editor.ts index e5311ef8c76..b8e0a40a7bd 100644 --- a/public/app/features/annotations/event_editor.ts +++ b/public/app/features/annotations/event_editor.ts @@ -1,6 +1,5 @@ -/// - import _ from 'lodash'; +import moment from 'moment'; import {coreModule} from 'app/core/core'; import {MetricsPanelCtrl} from 'app/plugins/sdk'; import {AnnotationEvent} from './event'; @@ -11,11 +10,20 @@ export class EventEditorCtrl { timeRange: {from: number, to: number}; form: any; close: any; + timeFormated: string; /** @ngInject **/ constructor(private annotationsSrv) { this.event.panelId = this.panelCtrl.panel.id; this.event.dashboardId = this.panelCtrl.dashboard.id; + + // Annotations query returns time as Unix timestamp in milliseconds + this.event.time = tryEpochToMoment(this.event.time); + if (this.event.isRegion) { + this.event.timeEnd = tryEpochToMoment(this.event.timeEnd); + } + + this.timeFormated = this.panelCtrl.dashboard.formatDate(this.event.time); } save() { @@ -28,7 +36,7 @@ export class EventEditorCtrl { saveModel.timeEnd = 0; if (saveModel.isRegion) { - saveModel.timeEnd = saveModel.timeEnd.valueOf(); + saveModel.timeEnd = this.event.timeEnd.valueOf(); if (saveModel.timeEnd < saveModel.time) { console.log('invalid time'); @@ -36,14 +44,48 @@ export class EventEditorCtrl { } } - this.annotationsSrv.saveAnnotationEvent(saveModel).then(() => { + if (saveModel.id) { + this.annotationsSrv.updateAnnotationEvent(saveModel) + .then(() => { + this.panelCtrl.refresh(); + this.close(); + }) + .catch(() => { + this.panelCtrl.refresh(); + this.close(); + }); + } else { + this.annotationsSrv.saveAnnotationEvent(saveModel) + .then(() => { + this.panelCtrl.refresh(); + this.close(); + }) + .catch(() => { + this.panelCtrl.refresh(); + this.close(); + }); + } + } + + delete() { + return this.annotationsSrv.deleteAnnotationEvent(this.event) + .then(() => { + this.panelCtrl.refresh(); + this.close(); + }) + .catch(() => { this.panelCtrl.refresh(); this.close(); }); } +} - timeChanged() { - this.panelCtrl.render(); +function tryEpochToMoment(timestamp) { + if (timestamp && _.isNumber(timestamp)) { + let epoch = Number(timestamp); + return moment(epoch); + } else { + return timestamp; } } diff --git a/public/app/features/annotations/event_manager.ts b/public/app/features/annotations/event_manager.ts index 6b8a58f0b57..e8ddd2d95d6 100644 --- a/public/app/features/annotations/event_manager.ts +++ b/public/app/features/annotations/event_manager.ts @@ -1,27 +1,28 @@ import _ from 'lodash'; import moment from 'moment'; +import tinycolor from 'tinycolor2'; import {MetricsPanelCtrl} from 'app/plugins/sdk'; import {AnnotationEvent} from './event'; +import {OK_COLOR, ALERTING_COLOR, NO_DATA_COLOR, DEFAULT_ANNOTATION_COLOR, REGION_FILL_ALPHA} from 'app/core/utils/colors'; export class EventManager { event: AnnotationEvent; + editorOpen: boolean; - constructor(private panelCtrl: MetricsPanelCtrl, private elem, private popoverSrv) { - } + constructor(private panelCtrl: MetricsPanelCtrl) {} editorClosed() { - console.log('editorClosed'); this.event = null; + this.editorOpen = false; this.panelCtrl.render(); } - updateTime(range) { - let newEvent = true; + editorOpened() { + this.editorOpen = true; + } - if (this.event) { - newEvent = false; - } else { - // init new event + updateTime(range) { + if (!this.event) { this.event = new AnnotationEvent(); this.event.dashboardId = this.panelCtrl.dashboard.id; this.event.panelId = this.panelCtrl.panel.id; @@ -35,25 +36,11 @@ export class EventManager { this.event.isRegion = true; } - // newEvent means the editor is not visible - if (!newEvent) { - this.panelCtrl.render(); - return; - } - - this.popoverSrv.show({ - element: this.elem[0], - classNames: 'drop-popover drop-popover--form', - position: 'bottom center', - openOn: null, - template: '', - onClose: this.editorClosed.bind(this), - model: { - event: this.event, - panelCtrl: this.panelCtrl, - }, - }); + this.panelCtrl.render(); + } + editEvent(event, elem?) { + this.event = event; this.panelCtrl.render(); } @@ -63,36 +50,60 @@ export class EventManager { } var types = { - '$__alerting': { - color: 'rgba(237, 46, 24, 1)', + $__alerting: { + color: ALERTING_COLOR, position: 'BOTTOM', markerSize: 5, }, - '$__ok': { - color: 'rgba(11, 237, 50, 1)', + $__ok: { + color: OK_COLOR, position: 'BOTTOM', markerSize: 5, }, - '$__no_data': { - color: 'rgba(150, 150, 150, 1)', + $__no_data: { + color: NO_DATA_COLOR, + position: 'BOTTOM', + markerSize: 5, + }, + $__editing: { + color: DEFAULT_ANNOTATION_COLOR, position: 'BOTTOM', markerSize: 5, }, }; if (this.event) { - annotations = [ - { - min: this.event.time.valueOf(), - title: this.event.title, - text: this.event.text, - eventType: '$__alerting', - } - ]; + if (this.event.isRegion) { + annotations = [ + { + isRegion: true, + min: this.event.time.valueOf(), + timeEnd: this.event.timeEnd.valueOf(), + text: this.event.text, + eventType: '$__editing', + editModel: this.event, + }, + ]; + } else { + annotations = [ + { + min: this.event.time.valueOf(), + text: this.event.text, + editModel: this.event, + eventType: '$__editing', + }, + ]; + } } else { // annotations from query for (var i = 0; i < annotations.length; i++) { var item = annotations[i]; + + // add properties used by jquery flot events + item.min = item.time; + item.max = item.time; + item.eventType = item.source.name; + if (item.newState) { item.eventType = '$__' + item.newState; continue; @@ -108,10 +119,50 @@ export class EventManager { } } + let regions = getRegions(annotations); + addRegionMarking(regions, flotOptions); + + let eventSectionHeight = 20; + let eventSectionMargin = 7; + flotOptions.grid.eventSectionHeight = eventSectionMargin; + flotOptions.xaxis.eventSectionHeight = eventSectionHeight; + flotOptions.events = { levels: _.keys(types).length + 1, data: annotations, types: types, + manager: this, }; } } + +function getRegions(events) { + return _.filter(events, 'isRegion'); +} + +function addRegionMarking(regions, flotOptions) { + let markings = flotOptions.grid.markings; + let defaultColor = DEFAULT_ANNOTATION_COLOR; + let fillColor; + + _.each(regions, region => { + if (region.source) { + fillColor = region.source.iconColor || defaultColor; + } else { + fillColor = defaultColor; + } + + fillColor = addAlphaToRGB(fillColor, REGION_FILL_ALPHA); + markings.push({xaxis: {from: region.min, to: region.timeEnd}, color: fillColor}); + }); +} + +function addAlphaToRGB(colorString: string, alpha: number): string { + let color = tinycolor(colorString); + if (color.isValid()) { + color.setAlpha(alpha); + return color.toRgbString(); + } else { + return colorString; + } +} diff --git a/public/app/features/annotations/partials/editor.html b/public/app/features/annotations/partials/editor.html index 1506e1a0dc5..4c0b8f7b127 100644 --- a/public/app/features/annotations/partials/editor.html +++ b/public/app/features/annotations/partials/editor.html @@ -40,10 +40,11 @@ Annotations provide a way to integrate event data into your graphs. They are visualized as vertical lines and icons on all graph panels. When you hover over an annotation icon you can get title, tags, and text information for the event. In the Queries tab you can add queries that return annotation events. -
-
- Checkout the Annotations documentation for more information.

+

+ You can add annotations directly from grafana by holding CTRL or CMD + click on graph (or drag region). These will be stored in Grafana's annotation database. +

+ Checkout the Annotations documentation for more information.
@@ -53,13 +54,16 @@
- + - @@ -77,60 +81,65 @@
-
-
Options
+
+
+
General
- Name - + Name +
- Data source -
+ Data source +
-
-
- - - - - - - - -
+
+ +
+
+ + + +
- + + +
+
-
Query
- - - - +
Query
+ + + + -
-
- - -
+
+
+ +
+
-
diff --git a/public/app/features/annotations/partials/event_editor.html b/public/app/features/annotations/partials/event_editor.html index 6e44b6f768d..529434755f1 100644 --- a/public/app/features/annotations/partials/event_editor.html +++ b/public/app/features/annotations/partials/event_editor.html @@ -1,38 +1,35 @@ -
Add annotation
- -
-
-
- Title - +
+
+
- -
-
- Time - -
-
- -
-
- Start - -
-
- End - -
-
-
- Description - -
-
- - Cancel -
-
- +
+ Add Annotation + Edit Annotation +
+ +
{{ctrl.timeFormated}}
+
+ +
+
+
+ Description + +
+ +
+ Tags + + +
+ +
+ + + Cancel +
+
+ +
diff --git a/public/app/features/annotations/specs/annotations_srv_specs.ts b/public/app/features/annotations/specs/annotations_srv_specs.ts new file mode 100644 index 00000000000..3c0142ed87f --- /dev/null +++ b/public/app/features/annotations/specs/annotations_srv_specs.ts @@ -0,0 +1,40 @@ +import {describe, beforeEach, it, expect, angularMocks} from 'test/lib/common'; +import '../annotations_srv'; +import helpers from 'test/specs/helpers'; + +describe('AnnotationsSrv', function() { + var ctx = new helpers.ServiceTestContext(); + + beforeEach(angularMocks.module('grafana.core')); + beforeEach(angularMocks.module('grafana.services')); + beforeEach(() => { + ctx.createService('annotationsSrv'); + }); + describe('When translating the query result', () => { + const annotationSource = { + datasource: '-- Grafana --', + enable: true, + hide: false, + limit: 200, + name: 'test', + scope: 'global', + tags: [ + 'test' + ], + type: 'event', + }; + + const time = 1507039543000; + const annotations = [{id: 1, panelId: 1, text: 'text', time: time}]; + let translatedAnnotations; + + beforeEach(() => { + translatedAnnotations = ctx.service.translateQueryResult(annotationSource, annotations); + }); + + it('should set defaults', () => { + expect(translatedAnnotations[0].source).to.eql(annotationSource); + }); + }); +}); + diff --git a/public/app/features/dashboard/model.ts b/public/app/features/dashboard/model.ts index 3eb3d25f4fe..7c4cdfac59e 100644 --- a/public/app/features/dashboard/model.ts +++ b/public/app/features/dashboard/model.ts @@ -5,6 +5,7 @@ import moment from 'moment'; import _ from 'lodash'; import $ from 'jquery'; +import {DEFAULT_ANNOTATION_COLOR} from 'app/core/utils/colors'; import {Emitter, contextSrv, appEvents} from 'app/core/core'; import {DashboardRow} from './row/row_model'; import sortByKeys from 'app/core/utils/sort_by_keys'; @@ -71,10 +72,35 @@ export class DashboardModel { } } + this.addBuiltInAnnotationQuery(); this.updateSchema(data); this.initMeta(meta); } + addBuiltInAnnotationQuery() { + let found = false; + for (let item of this.annotations.list) { + if (item.builtIn === 1) { + found = true; + break; + } + } + + if (found) { + return; + } + + this.annotations.list.unshift({ + datasource: '-- Grafana --', + name: 'Annotations & Alerts', + type: 'dashboard', + iconColor: DEFAULT_ANNOTATION_COLOR, + enable: true, + hide: true, + builtIn: 1, + }); + } + private initMeta(meta) { meta = meta || {}; diff --git a/public/app/features/dashboard/specs/dash_import_ctrl_specs.ts b/public/app/features/dashboard/specs/dash_import_ctrl_specs.ts index 97983d60dc9..c541aca34b2 100644 --- a/public/app/features/dashboard/specs/dash_import_ctrl_specs.ts +++ b/public/app/features/dashboard/specs/dash_import_ctrl_specs.ts @@ -53,9 +53,10 @@ describe('DashImportCtrl', function() { // setup api mock backendSrv.get = sinon.spy(() => { return Promise.resolve({ + json: {} }); }); - ctx.ctrl.checkGnetDashboard(); + return ctx.ctrl.checkGnetDashboard(); }); it('should call gnet api with correct dashboard id', function() { @@ -69,9 +70,10 @@ describe('DashImportCtrl', function() { // setup api mock backendSrv.get = sinon.spy(() => { return Promise.resolve({ + json: {} }); }); - ctx.ctrl.checkGnetDashboard(); + return ctx.ctrl.checkGnetDashboard(); }); it('should call gnet api with correct dashboard id', function() { diff --git a/public/app/features/dashboard/specs/dashboard_model_specs.ts b/public/app/features/dashboard/specs/dashboard_model_specs.ts index 6ca84ba89f3..ca5482bbfc5 100644 --- a/public/app/features/dashboard/specs/dashboard_model_specs.ts +++ b/public/app/features/dashboard/specs/dashboard_model_specs.ts @@ -46,8 +46,8 @@ describe('DashboardModel', function() { var saveModel = model.getSaveModelClone(); var keys = _.keys(saveModel); - expect(keys[0]).to.be('addEmptyRow'); - expect(keys[1]).to.be('addPanel'); + expect(keys[0]).to.be('addBuiltInAnnotationQuery'); + expect(keys[1]).to.be('addEmptyRow'); }); }); @@ -220,26 +220,6 @@ describe('DashboardModel', function() { }); }); - describe('when creating dashboard model with missing list for annoations or templating', function() { - var model; - - beforeEach(function() { - model = new DashboardModel({ - annotations: { - enable: true, - }, - templating: { - enable: true - } - }); - }); - - it('should add empty list', function() { - expect(model.annotations.list.length).to.be(0); - expect(model.templating.list.length).to.be(0); - }); - }); - describe('Given editable false dashboard', function() { var model; @@ -339,7 +319,12 @@ describe('DashboardModel', function() { }); it('should add empty list', function() { - expect(model.annotations.list.length).to.be(0); + expect(model.annotations.list.length).to.be(1); + expect(model.templating.list.length).to.be(0); + }); + + it('should add builtin annotation query', function() { + expect(model.annotations.list[0].builtIn).to.be(1); expect(model.templating.list.length).to.be(0); }); }); diff --git a/public/app/features/dashboard/specs/exporter_specs.ts b/public/app/features/dashboard/specs/exporter_specs.ts index 9364cea8c47..cc2b1ddaf97 100644 --- a/public/app/features/dashboard/specs/exporter_specs.ts +++ b/public/app/features/dashboard/specs/exporter_specs.ts @@ -80,6 +80,10 @@ describe('given dashboard with repeated panels', function() { name: 'mixed', meta: {id: "mixed", info: {version: "1.2.1"}, name: "Mixed", builtIn: true} })); + datasourceSrvStub.get.withArgs('-- Grafana --').returns(Promise.resolve({ + name: '-- Grafana --', + meta: {id: "grafana", info: {version: "1.2.1"}, name: "grafana", builtIn: true} + })); config.panels['graph'] = { id: "graph", @@ -116,7 +120,7 @@ describe('given dashboard with repeated panels', function() { }); it('should replace datasource in annotation query', function() { - expect(exported.annotations.list[0].datasource).to.be("${DS_GFDB}"); + expect(exported.annotations.list[1].datasource).to.be("${DS_GFDB}"); }); it('should add datasource as input', function() { diff --git a/public/app/features/dashboard/specs/history_srv_specs.ts b/public/app/features/dashboard/specs/history_srv_specs.ts index 621e91c8f87..354f41e4a15 100644 --- a/public/app/features/dashboard/specs/history_srv_specs.ts +++ b/public/app/features/dashboard/specs/history_srv_specs.ts @@ -21,50 +21,48 @@ describe('historySrv', function() { return [200, restoreResponse(parsedData.version)]; }); })); + beforeEach(ctx.createService('historySrv')); + function wrapPromise(ctx, angularPromise) { + return new Promise((resolve, reject) => { + angularPromise.then(resolve, reject); + ctx.$httpBackend.flush(); + }); + } + describe('getHistoryList', function() { - it('should return a versions array for the given dashboard id', function(done) { - ctx.service.getHistoryList({ id: 1 }).then(function(versions) { + it('should return a versions array for the given dashboard id', function() { + return wrapPromise(ctx, ctx.service.getHistoryList({ id: 1 }).then(function(versions) { expect(versions).to.eql(versionsResponse); - done(); - }); - ctx.$httpBackend.flush(); + })); }); - it('should return an empty array when not given an id', function(done) { - ctx.service.getHistoryList({ }).then(function(versions) { + it('should return an empty array when not given an id', function() { + return wrapPromise(ctx, ctx.service.getHistoryList({ }).then(function(versions) { expect(versions).to.eql([]); - done(); - }); - ctx.$httpBackend.flush(); + })); }); - it('should return an empty array when not given a dashboard', function(done) { - ctx.service.getHistoryList().then(function(versions) { + it('should return an empty array when not given a dashboard', function() { + return wrapPromise(ctx, ctx.service.getHistoryList().then(function(versions) { expect(versions).to.eql([]); - done(); - }); - ctx.$httpBackend.flush(); + })); }); }); describe('restoreDashboard', function() { - it('should return a success response given valid parameters', function(done) { - var version = 6; - ctx.service.restoreDashboard({ id: 1 }, version).then(function(response) { + it('should return a success response given valid parameters', function() { + let version = 6; + return wrapPromise(ctx, ctx.service.restoreDashboard({ id: 1 }, version).then(function(response) { expect(response).to.eql(restoreResponse(version)); - done(); - }); - ctx.$httpBackend.flush(); + })); }); - it('should return an empty object when not given an id', function(done) { - ctx.service.restoreDashboard({}, 6).then(function(response) { + it('should return an empty object when not given an id', function() { + return wrapPromise(ctx, ctx.service.restoreDashboard({}, 6).then(function(response) { expect(response).to.eql({}); - done(); - }); - ctx.$httpBackend.flush(); + })); }); }); }); diff --git a/public/app/features/dashboard/submenu/submenu.html b/public/app/features/dashboard/submenu/submenu.html index ce3c61f1cc3..9e9d22fb495 100644 --- a/public/app/features/dashboard/submenu/submenu.html +++ b/public/app/features/dashboard/submenu/submenu.html @@ -12,7 +12,7 @@
diff --git a/public/app/features/dashboard/unsaved_changes_modal.ts b/public/app/features/dashboard/unsaved_changes_modal.ts index cacfbe0f045..ab3ece1b8a2 100644 --- a/public/app/features/dashboard/unsaved_changes_modal.ts +++ b/public/app/features/dashboard/unsaved_changes_modal.ts @@ -18,7 +18,7 @@ const template = `
-   + +   {{annotation.name}} +   + {{annotation.name}} (Built-in) + @@ -67,7 +71,7 @@ - +
diff --git a/public/app/plugins/datasource/mysql/partials/config.html b/public/app/plugins/datasource/mysql/partials/config.html index 870d3443908..8cbeece71dd 100644 --- a/public/app/plugins/datasource/mysql/partials/config.html +++ b/public/app/plugins/datasource/mysql/partials/config.html @@ -9,17 +9,17 @@
Database - +
User - +
Password - +
diff --git a/public/app/plugins/datasource/mysql/partials/query.editor.html b/public/app/plugins/datasource/mysql/partials/query.editor.html index cd8b4eee7c4..a7e993afd7f 100644 --- a/public/app/plugins/datasource/mysql/partials/query.editor.html +++ b/public/app/plugins/datasource/mysql/partials/query.editor.html @@ -49,6 +49,7 @@ Macros: - $__time(column) -> UNIX_TIMESTAMP(column) as time_sec - $__timeFilter(column) -> UNIX_TIMESTAMP(time_date_time) ≥ 1492750877 AND UNIX_TIMESTAMP(time_date_time) ≤ 1492750877 - $__unixEpochFilter(column) -> time_unix_epoch > 1492750877 AND time_unix_epoch < 1492750877 +- $__timeGroup(column,'5m') -> (extract(epoch from "dateColumn")/extract(epoch from '5m'::interval))::int Or build your own conditionals using these macros which just return the values: - $__timeFrom() -> FROM_UNIXTIME(1492750877) diff --git a/public/app/plugins/datasource/mysql/response_parser.ts b/public/app/plugins/datasource/mysql/response_parser.ts index 5501e4fc17a..70b7ddd2695 100644 --- a/public/app/plugins/datasource/mysql/response_parser.ts +++ b/public/app/plugins/datasource/mysql/response_parser.ts @@ -106,7 +106,6 @@ export default class ResponseParser { const table = data.data.results[options.annotation.name].tables[0]; let timeColumnIndex = -1; - let titleColumnIndex = -1; let textColumnIndex = -1; let tagsColumnIndex = -1; @@ -114,7 +113,7 @@ export default class ResponseParser { if (table.columns[i].text === 'time_sec') { timeColumnIndex = i; } else if (table.columns[i].text === 'title') { - titleColumnIndex = i; + return this.$q.reject({message: 'The title column for annotations is deprecated, now only a column named text is returned'}); } else if (table.columns[i].text === 'text') { textColumnIndex = i; } else if (table.columns[i].text === 'tags') { @@ -132,7 +131,6 @@ export default class ResponseParser { list.push({ annotation: options.annotation, time: Math.floor(row[timeColumnIndex]) * 1000, - title: row[titleColumnIndex], text: row[textColumnIndex], tags: row[tagsColumnIndex] ? row[tagsColumnIndex].trim().split(/\s*,\s*/) : [] }); diff --git a/public/app/plugins/datasource/mysql/specs/datasource_specs.ts b/public/app/plugins/datasource/mysql/specs/datasource_specs.ts index 08d2f8922a5..6ff1f9d47ac 100644 --- a/public/app/plugins/datasource/mysql/specs/datasource_specs.ts +++ b/public/app/plugins/datasource/mysql/specs/datasource_specs.ts @@ -27,7 +27,7 @@ describe('MySQLDatasource', function() { const options = { annotation: { name: annotationName, - rawQuery: 'select time_sec, title, text, tags from table;' + rawQuery: 'select time_sec, text, tags from table;' }, range: { from: moment(1432288354), @@ -41,11 +41,11 @@ describe('MySQLDatasource', function() { refId: annotationName, tables: [ { - columns: [{text: 'time_sec'}, {text: 'title'}, {text: 'text'}, {text: 'tags'}], + columns: [{text: 'time_sec'}, {text: 'text'}, {text: 'tags'}], rows: [ - [1432288355, 'aTitle', 'some text', 'TagA,TagB'], - [1432288390, 'aTitle2', 'some text2', ' TagB , TagC'], - [1432288400, 'aTitle3', 'some text3'] + [1432288355, 'some text', 'TagA,TagB'], + [1432288390, 'some text2', ' TagB , TagC'], + [1432288400, 'some text3'] ] } ] @@ -64,7 +64,6 @@ describe('MySQLDatasource', function() { it('should return annotation list', function() { expect(results.length).to.be(3); - expect(results[0].title).to.be('aTitle'); expect(results[0].text).to.be('some text'); expect(results[0].tags[0]).to.be('TagA'); expect(results[0].tags[1]).to.be('TagB'); @@ -194,4 +193,24 @@ describe('MySQLDatasource', function() { expect(results[0].value).to.be('same'); }); }); + + describe('When interpolating variables', () => { + describe('and value is a string', () => { + it('should return a quoted value', () => { + expect(ctx.ds.interpolateVariable('abc')).to.eql('\'abc\''); + }); + }); + + describe('and value is a number', () => { + it('should return an unquoted value', () => { + expect(ctx.ds.interpolateVariable(1000)).to.eql(1000); + }); + }); + + describe('and value is an array of strings', () => { + it('should return comma separated quoted values', () => { + expect(ctx.ds.interpolateVariable(['a', 'b', 'c'])).to.eql('\'a\',\'b\',\'c\''); + }); + }); + }); }); diff --git a/public/app/plugins/datasource/opentsdb/datasource.js b/public/app/plugins/datasource/opentsdb/datasource.js index 5228ce26b9c..4d51b117ed4 100644 --- a/public/app/plugins/datasource/opentsdb/datasource.js +++ b/public/app/plugins/datasource/opentsdb/datasource.js @@ -91,9 +91,8 @@ function (angular, _, dateMath) { if(annotationObject) { _.each(annotationObject, function(annotation) { var event = { - title: annotation.description, + text: annotation.description, time: Math.floor(annotation.startTime) * 1000, - text: annotation.notes, annotation: options.annotation }; diff --git a/public/app/plugins/datasource/postgres/README.md b/public/app/plugins/datasource/postgres/README.md new file mode 100644 index 00000000000..7b343ba78ff --- /dev/null +++ b/public/app/plugins/datasource/postgres/README.md @@ -0,0 +1,3 @@ +# Grafana PostgreSQL Datasource - Native Plugin + +This is the built in PostgreSQL Datasource that is used to connect to PostgreSQL databases. diff --git a/public/app/plugins/datasource/postgres/datasource.ts b/public/app/plugins/datasource/postgres/datasource.ts new file mode 100644 index 00000000000..54e3bdc41cd --- /dev/null +++ b/public/app/plugins/datasource/postgres/datasource.ts @@ -0,0 +1,132 @@ +/// + +import _ from 'lodash'; +import ResponseParser from './response_parser'; + +export class PostgresDatasource { + id: any; + name: any; + responseParser: ResponseParser; + + /** @ngInject **/ + constructor(instanceSettings, private backendSrv, private $q, private templateSrv) { + this.name = instanceSettings.name; + this.id = instanceSettings.id; + this.responseParser = new ResponseParser(this.$q); + } + + interpolateVariable(value) { + if (typeof value === 'string') { + return '\'' + value + '\''; + } + + var quotedValues = _.map(value, function(val) { + return '\'' + val + '\''; + }); + return quotedValues.join(','); + } + + query(options) { + var queries = _.filter(options.targets, item => { + return item.hide !== true; + }).map(item => { + return { + refId: item.refId, + intervalMs: options.intervalMs, + maxDataPoints: options.maxDataPoints, + datasourceId: this.id, + rawSql: this.templateSrv.replace(item.rawSql, options.scopedVars, this.interpolateVariable), + format: item.format, + }; + }); + + if (queries.length === 0) { + return this.$q.when({data: []}); + } + + return this.backendSrv.datasourceRequest({ + url: '/api/tsdb/query', + method: 'POST', + data: { + from: options.range.from.valueOf().toString(), + to: options.range.to.valueOf().toString(), + queries: queries, + } + }).then(this.responseParser.processQueryResult); + } + + annotationQuery(options) { + if (!options.annotation.rawQuery) { + return this.$q.reject({message: 'Query missing in annotation definition'}); + } + + const query = { + refId: options.annotation.name, + datasourceId: this.id, + rawSql: this.templateSrv.replace(options.annotation.rawQuery, options.scopedVars, this.interpolateVariable), + format: 'table', + }; + + return this.backendSrv.datasourceRequest({ + url: '/api/tsdb/query', + method: 'POST', + data: { + from: options.range.from.valueOf().toString(), + to: options.range.to.valueOf().toString(), + queries: [query], + } + }).then(data => this.responseParser.transformAnnotationResponse(options, data)); + } + + metricFindQuery(query, optionalOptions) { + let refId = 'tempvar'; + if (optionalOptions && optionalOptions.variable && optionalOptions.variable.name) { + refId = optionalOptions.variable.name; + } + + const interpolatedQuery = { + refId: refId, + datasourceId: this.id, + rawSql: this.templateSrv.replace(query, {}, this.interpolateVariable), + format: 'table', + }; + + return this.backendSrv.datasourceRequest({ + url: '/api/tsdb/query', + method: 'POST', + data: { + queries: [interpolatedQuery], + } + }) + .then(data => this.responseParser.parseMetricFindQueryResult(refId, data)); + } + + testDatasource() { + return this.backendSrv.datasourceRequest({ + url: '/api/tsdb/query', + method: 'POST', + data: { + from: '5m', + to: 'now', + queries: [{ + refId: 'A', + intervalMs: 1, + maxDataPoints: 1, + datasourceId: this.id, + rawSql: "SELECT 1", + format: 'table', + }], + } + }).then(res => { + return { status: "success", message: "Database Connection OK"}; + }).catch(err => { + console.log(err); + if (err.data && err.data.message) { + return { status: "error", message: err.data.message }; + } else { + return { status: "error", message: err.status }; + } + }); + } +} + diff --git a/public/app/plugins/datasource/postgres/img/postgresql_logo.svg b/public/app/plugins/datasource/postgres/img/postgresql_logo.svg new file mode 100644 index 00000000000..d98e3659c39 --- /dev/null +++ b/public/app/plugins/datasource/postgres/img/postgresql_logo.svg @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/public/app/plugins/datasource/postgres/mode-sql.js b/public/app/plugins/datasource/postgres/mode-sql.js new file mode 100644 index 00000000000..20e3d493458 --- /dev/null +++ b/public/app/plugins/datasource/postgres/mode-sql.js @@ -0,0 +1,103 @@ +// jshint ignore: start +// jscs: disable + +ace.define("ace/mode/sql_highlight_rules",["require","exports","module","ace/lib/oop","ace/mode/text_highlight_rules"], function(require, exports, module) { +"use strict"; + +var oop = require("../lib/oop"); +var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules; + +var SqlHighlightRules = function() { + + var keywords = ( + "select|insert|update|delete|from|where|and|or|group|by|order|limit|offset|having|as|case|" + + "when|else|end|type|left|right|join|on|outer|desc|asc|union|create|table|primary|key|if|" + + "foreign|not|references|default|null|inner|cross|natural|database|drop|grant" + ); + + var builtinConstants = ( + "true|false" + ); + + var builtinFunctions = ( + "avg|count|first|last|max|min|sum|upper|lower|substring|char_length|round|rank|now|" + + "coalesce" + ); + + var dataTypes = ( + "int|int2|int4|int8|numeric|decimal|date|varchar|char|bigint|float|bool|bytea|text|timestamp|" + + "time|money|real|integer" + ); + + var keywordMapper = this.createKeywordMapper({ + "support.function": builtinFunctions, + "keyword": keywords, + "constant.language": builtinConstants, + "storage.type": dataTypes + }, "identifier", true); + + this.$rules = { + "start" : [ { + token : "comment", + regex : "--.*$" + }, { + token : "comment", + start : "/\\*", + end : "\\*/" + }, { + token : "string", // " string + regex : '".*?"' + }, { + token : "string", // ' string + regex : "'.*?'" + }, { + token : "constant.numeric", // float + regex : "[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b" + }, { + token : keywordMapper, + regex : "[a-zA-Z_$][a-zA-Z0-9_$]*\\b" + }, { + token : "keyword.operator", + regex : "\\+|\\-|\\/|\\/\\/|%|<@>|@>|<@|&|\\^|~|<|>|<=|=>|==|!=|<>|=" + }, { + token : "paren.lparen", + regex : "[\\(]" + }, { + token : "paren.rparen", + regex : "[\\)]" + }, { + token : "text", + regex : "\\s+" + } ] + }; + this.normalizeRules(); +}; + +oop.inherits(SqlHighlightRules, TextHighlightRules); + +exports.SqlHighlightRules = SqlHighlightRules; +}); + +ace.define("ace/mode/sql",["require","exports","module","ace/lib/oop","ace/mode/text","ace/mode/sql_highlight_rules"], function(require, exports, module) { +"use strict"; + +var oop = require("../lib/oop"); +var TextMode = require("./text").Mode; +var SqlHighlightRules = require("./sql_highlight_rules").SqlHighlightRules; + +var Mode = function() { + this.HighlightRules = SqlHighlightRules; + this.$behaviour = this.$defaultBehaviour; +}; +oop.inherits(Mode, TextMode); + +(function() { + + this.lineCommentStart = "--"; + + this.$id = "ace/mode/sql"; +}).call(Mode.prototype); + +exports.Mode = Mode; + +}); diff --git a/public/app/plugins/datasource/postgres/module.ts b/public/app/plugins/datasource/postgres/module.ts new file mode 100644 index 00000000000..09a3f43c9ea --- /dev/null +++ b/public/app/plugins/datasource/postgres/module.ts @@ -0,0 +1,45 @@ +/// + +import {PostgresDatasource} from './datasource'; +import {PostgresQueryCtrl} from './query_ctrl'; + +class PostgresConfigCtrl { + static templateUrl = 'partials/config.html'; + + current: any; + + /** @ngInject **/ + constructor($scope) { + this.current.jsonData.sslmode = this.current.jsonData.sslmode || 'require'; + } +} + +const defaultQuery = `SELECT + extract(epoch from time_column) AS time, + title_column as title, + description_column as text +FROM + metric_table +WHERE + $__timeFilter(time_column) +`; + +class PostgresAnnotationsQueryCtrl { + static templateUrl = 'partials/annotations.editor.html'; + + annotation: any; + + /** @ngInject **/ + constructor() { + this.annotation.rawQuery = this.annotation.rawQuery || defaultQuery; + } +} + +export { + PostgresDatasource, + PostgresDatasource as Datasource, + PostgresQueryCtrl as QueryCtrl, + PostgresConfigCtrl as ConfigCtrl, + PostgresAnnotationsQueryCtrl as AnnotationsQueryCtrl, +}; + diff --git a/public/app/plugins/datasource/postgres/partials/annotations.editor.html b/public/app/plugins/datasource/postgres/partials/annotations.editor.html new file mode 100644 index 00000000000..07b838e739a --- /dev/null +++ b/public/app/plugins/datasource/postgres/partials/annotations.editor.html @@ -0,0 +1,41 @@ + +
+
+
+ +
+
+ +
+
+ +
+
+ +
+
Annotation Query Format
+An annotation is an event that is overlayed on top of graphs. The query can have up to four columns per row, the time column is mandatory. Annotation rendering is expensive so it is important to limit the number of rows returned. + +- column with alias: time for the annotation event. Format is UTC in seconds, use extract(epoch from column) as "time" +- column with alias title for the annotation title +- column with alias: text for the annotation text +- column with alias: tags for annotation tags. This is a comma separated string of tags e.g. 'tag1,tag2' + + +Macros: +- $__time(column) -> column as "time" +- $__timeFilter(column) -> column ≥ to_timestamp(1492750877) AND column ≤ to_timestamp(1492750877) +- $__unixEpochFilter(column) -> column > 1492750877 AND column < 1492750877 + +Or build your own conditionals using these macros which just return the values: +- $__timeFrom() -> to_timestamp(1492750877) +- $__timeTo() -> to_timestamp(1492750877) +- $__unixEpochFrom() -> 1492750877 +- $__unixEpochTo() -> 1492750877 +
+
+
diff --git a/public/app/plugins/datasource/postgres/partials/config.html b/public/app/plugins/datasource/postgres/partials/config.html new file mode 100644 index 00000000000..77f0dcfa4a5 --- /dev/null +++ b/public/app/plugins/datasource/postgres/partials/config.html @@ -0,0 +1,52 @@ + +

PostgreSQL Connection

+ +
+
+ Host + +
+ +
+ Database + +
+ +
+
+ User + +
+
+ Password + +
+
+ Password + + reset +
+
+
+ +
+ + + This option determines whether or with what priority a secure SSL TCP/IP connection will be negotiated with the server. + +
+
+
+ +
+
+
User Permission
+

+ The database user should only be granted SELECT permissions on the specified database & tables you want to query. + Grafana does not validate that queries are safe so queries can contain any SQL statement. For example, statements + like DELETE FROM user; and DROP TABLE user; would be executed. To protect against this we + Highly recommmend you create a specific PostgreSQL user with restricted permissions. +

+
+
+ diff --git a/public/app/plugins/datasource/postgres/partials/query.editor.html b/public/app/plugins/datasource/postgres/partials/query.editor.html new file mode 100644 index 00000000000..1939fc47ecb --- /dev/null +++ b/public/app/plugins/datasource/postgres/partials/query.editor.html @@ -0,0 +1,79 @@ + +
+
+ + +
+
+ +
+
+ +
+ +
+
+
+ +
+
+ +
+
+
+
+
+ +
+
{{ctrl.lastQueryMeta.sql}}
+
+ +
+
Time series:
+- return column named time (UTC in seconds or timestamp)
+- return column(s) with numeric datatype as values
+- (Optional: return column named metric to represent the series name. If no column named metric is found the column name of the value column is used as series name)
+
+Table:
+- return any set of columns
+
+Macros:
+- $__time(column) -> column as "time"
+- $__timeEpoch -> extract(epoch from column) as "time"
+- $__timeFilter(column) ->  column ≥ to_timestamp(1492750877) AND column ≤ to_timestamp(1492750877)
+- $__unixEpochFilter(column) ->  column > 1492750877 AND column < 1492750877
+
+To group by time use $__timeGroup:
+-> (extract(epoch from column)/extract(epoch from column::interval))::int
+
+Example of group by and order by with $__timeGroup:
+SELECT
+  min(date_time_col) AS time_sec,
+  sum(value_double) as value
+FROM yourtable
+group by $__timeGroup(date_time_col, '1h')
+order by $__timeGroup(date_time_col, '1h') ASC
+
+Or build your own conditionals using these macros which just return the values:
+- $__timeFrom() ->  to_timestamp(1492750877)
+- $__timeTo() ->  to_timestamp(1492750877)
+- $__unixEpochFrom() ->  1492750877
+- $__unixEpochTo() ->  1492750877
+		
+
+ + + +
+
{{ctrl.lastQueryError}}
+
+ +
diff --git a/public/app/plugins/datasource/postgres/plugin.json b/public/app/plugins/datasource/postgres/plugin.json new file mode 100644 index 00000000000..26d050ba8ed --- /dev/null +++ b/public/app/plugins/datasource/postgres/plugin.json @@ -0,0 +1,20 @@ +{ + "type": "datasource", + "name": "PostgreSQL", + "id": "postgres", + + "info": { + "author": { + "name": "Grafana Project", + "url": "https://grafana.com" + }, + "logos": { + "small": "img/postgresql_logo.svg", + "large": "img/postgresql_logo.svg" + } + }, + + "alerting": true, + "annotations": true, + "metrics": true +} diff --git a/public/app/plugins/datasource/postgres/query_ctrl.ts b/public/app/plugins/datasource/postgres/query_ctrl.ts new file mode 100644 index 00000000000..c5ebdd0ad85 --- /dev/null +++ b/public/app/plugins/datasource/postgres/query_ctrl.ts @@ -0,0 +1,84 @@ +/// + +import _ from 'lodash'; +import {QueryCtrl} from 'app/plugins/sdk'; + +export interface PostgresQuery { + refId: string; + format: string; + alias: string; + rawSql: string; +} + +export interface QueryMeta { + sql: string; +} + + +const defaultQuery = `SELECT + $__time(time_column), + value1 +FROM + metric_table +WHERE + $__timeFilter(time_column) +`; + +export class PostgresQueryCtrl extends QueryCtrl { + static templateUrl = 'partials/query.editor.html'; + + showLastQuerySQL: boolean; + formats: any[]; + target: PostgresQuery; + lastQueryMeta: QueryMeta; + lastQueryError: string; + showHelp: boolean; + + /** @ngInject **/ + constructor($scope, $injector) { + super($scope, $injector); + + this.target.format = this.target.format || 'time_series'; + this.target.alias = ""; + this.formats = [ + {text: 'Time series', value: 'time_series'}, + {text: 'Table', value: 'table'}, + ]; + + if (!this.target.rawSql) { + + // special handling when in table panel + if (this.panelCtrl.panel.type === 'table') { + this.target.format = 'table'; + this.target.rawSql = "SELECT 1"; + } else { + this.target.rawSql = defaultQuery; + } + } + + this.panelCtrl.events.on('data-received', this.onDataReceived.bind(this), $scope); + this.panelCtrl.events.on('data-error', this.onDataError.bind(this), $scope); + } + + onDataReceived(dataList) { + this.lastQueryMeta = null; + this.lastQueryError = null; + + let anySeriesFromQuery = _.find(dataList, {refId: this.target.refId}); + if (anySeriesFromQuery) { + this.lastQueryMeta = anySeriesFromQuery.meta; + } + } + + onDataError(err) { + if (err.data && err.data.results) { + let queryRes = err.data.results[this.target.refId]; + if (queryRes) { + this.lastQueryMeta = queryRes.meta; + this.lastQueryError = queryRes.error; + } + } + } +} + + diff --git a/public/app/plugins/datasource/postgres/response_parser.ts b/public/app/plugins/datasource/postgres/response_parser.ts new file mode 100644 index 00000000000..9bc0cf7c626 --- /dev/null +++ b/public/app/plugins/datasource/postgres/response_parser.ts @@ -0,0 +1,141 @@ +/// + +import _ from 'lodash'; + +export default class ResponseParser { + constructor(private $q) {} + + processQueryResult(res) { + var data = []; + + if (!res.data.results) { + return {data: data}; + } + + for (let key in res.data.results) { + let queryRes = res.data.results[key]; + + if (queryRes.series) { + for (let series of queryRes.series) { + data.push({ + target: series.name, + datapoints: series.points, + refId: queryRes.refId, + meta: queryRes.meta, + }); + } + } + + if (queryRes.tables) { + for (let table of queryRes.tables) { + table.type = 'table'; + table.refId = queryRes.refId; + table.meta = queryRes.meta; + data.push(table); + } + } + } + + return {data: data}; + } + + parseMetricFindQueryResult(refId, results) { + if (!results || results.data.length === 0 || results.data.results[refId].meta.rowCount === 0) { return []; } + + const columns = results.data.results[refId].tables[0].columns; + const rows = results.data.results[refId].tables[0].rows; + const textColIndex = this.findColIndex(columns, '__text'); + const valueColIndex = this.findColIndex(columns, '__value'); + + if (columns.length === 2 && textColIndex !== -1 && valueColIndex !== -1) { + return this.transformToKeyValueList(rows, textColIndex, valueColIndex); + } + + return this.transformToSimpleList(rows); + } + + transformToKeyValueList(rows, textColIndex, valueColIndex) { + const res = []; + + for (let i = 0; i < rows.length; i++) { + if (!this.containsKey(res, rows[i][textColIndex])) { + res.push({text: rows[i][textColIndex], value: rows[i][valueColIndex]}); + } + } + + return res; + } + + transformToSimpleList(rows) { + const res = []; + + for (let i = 0; i < rows.length; i++) { + for (let j = 0; j < rows[i].length; j++) { + const value = rows[i][j]; + if ( res.indexOf( value ) === -1 ) { + res.push(value); + } + } + } + + return _.map(res, value => { + return { text: value}; + }); + } + + findColIndex(columns, colName) { + for (let i = 0; i < columns.length; i++) { + if (columns[i].text === colName) { + return i; + } + } + + return -1; + } + + containsKey(res, key) { + for (let i = 0; i < res.length; i++) { + if (res[i].text === key) { + return true; + } + } + return false; + } + + transformAnnotationResponse(options, data) { + const table = data.data.results[options.annotation.name].tables[0]; + + let timeColumnIndex = -1; + let titleColumnIndex = -1; + let textColumnIndex = -1; + let tagsColumnIndex = -1; + + for (let i = 0; i < table.columns.length; i++) { + if (table.columns[i].text === 'time') { + timeColumnIndex = i; + } else if (table.columns[i].text === 'text') { + textColumnIndex = i; + } else if (table.columns[i].text === 'tags') { + tagsColumnIndex = i; + } + } + + if (timeColumnIndex === -1) { + return this.$q.reject({message: 'Missing mandatory time column in annotation query.'}); + } + + const list = []; + for (let i = 0; i < table.rows.length; i++) { + const row = table.rows[i]; + list.push({ + annotation: options.annotation, + time: Math.floor(row[timeColumnIndex]) * 1000, + title: row[titleColumnIndex], + text: row[textColumnIndex], + tags: row[tagsColumnIndex] ? row[tagsColumnIndex].trim().split(/\s*,\s*/) : [] + }); + } + + return list; + } +} diff --git a/public/app/plugins/datasource/postgres/specs/datasource_specs.ts b/public/app/plugins/datasource/postgres/specs/datasource_specs.ts new file mode 100644 index 00000000000..5510e138f63 --- /dev/null +++ b/public/app/plugins/datasource/postgres/specs/datasource_specs.ts @@ -0,0 +1,196 @@ +import {describe, beforeEach, it, expect, angularMocks} from 'test/lib/common'; +import moment from 'moment'; +import helpers from 'test/specs/helpers'; +import {PostgresDatasource} from '../datasource'; + +describe('PostgreSQLDatasource', function() { + var ctx = new helpers.ServiceTestContext(); + var instanceSettings = {name: 'postgresql'}; + + beforeEach(angularMocks.module('grafana.core')); + beforeEach(angularMocks.module('grafana.services')); + beforeEach(ctx.providePhase(['backendSrv'])); + + beforeEach(angularMocks.inject(function($q, $rootScope, $httpBackend, $injector) { + ctx.$q = $q; + ctx.$httpBackend = $httpBackend; + ctx.$rootScope = $rootScope; + ctx.ds = $injector.instantiate(PostgresDatasource, {instanceSettings: instanceSettings}); + $httpBackend.when('GET', /\.html$/).respond(''); + })); + + describe('When performing annotationQuery', function() { + let results; + + const annotationName = 'MyAnno'; + + const options = { + annotation: { + name: annotationName, + rawQuery: 'select time, title, text, tags from table;' + }, + range: { + from: moment(1432288354), + to: moment(1432288401) + } + }; + + const response = { + results: { + MyAnno: { + refId: annotationName, + tables: [ + { + columns: [{text: 'time'}, {text: 'text'}, {text: 'tags'}], + rows: [ + [1432288355, 'some text', 'TagA,TagB'], + [1432288390, 'some text2', ' TagB , TagC'], + [1432288400, 'some text3'] + ] + } + ] + } + } + }; + + beforeEach(function() { + ctx.backendSrv.datasourceRequest = function(options) { + return ctx.$q.when({data: response, status: 200}); + }; + ctx.ds.annotationQuery(options).then(function(data) { results = data; }); + ctx.$rootScope.$apply(); + }); + + it('should return annotation list', function() { + expect(results.length).to.be(3); + + expect(results[0].text).to.be('some text'); + expect(results[0].tags[0]).to.be('TagA'); + expect(results[0].tags[1]).to.be('TagB'); + + expect(results[1].tags[0]).to.be('TagB'); + expect(results[1].tags[1]).to.be('TagC'); + + expect(results[2].tags.length).to.be(0); + }); + }); + + describe('When performing metricFindQuery', function() { + let results; + const query = 'select * from atable'; + const response = { + results: { + tempvar: { + meta: { + rowCount: 3 + }, + refId: 'tempvar', + tables: [ + { + columns: [{text: 'title'}, {text: 'text'}], + rows: [ + ['aTitle', 'some text'], + ['aTitle2', 'some text2'], + ['aTitle3', 'some text3'] + ] + } + ] + } + } + }; + + beforeEach(function() { + ctx.backendSrv.datasourceRequest = function(options) { + return ctx.$q.when({data: response, status: 200}); + }; + ctx.ds.metricFindQuery(query).then(function(data) { results = data; }); + ctx.$rootScope.$apply(); + }); + + it('should return list of all column values', function() { + expect(results.length).to.be(6); + expect(results[0].text).to.be('aTitle'); + expect(results[5].text).to.be('some text3'); + }); + }); + + describe('When performing metricFindQuery with key, value columns', function() { + let results; + const query = 'select * from atable'; + const response = { + results: { + tempvar: { + meta: { + rowCount: 3 + }, + refId: 'tempvar', + tables: [ + { + columns: [{text: '__value'}, {text: '__text'}], + rows: [ + ['value1', 'aTitle'], + ['value2', 'aTitle2'], + ['value3', 'aTitle3'] + ] + } + ] + } + } + }; + + beforeEach(function() { + ctx.backendSrv.datasourceRequest = function(options) { + return ctx.$q.when({data: response, status: 200}); + }; + ctx.ds.metricFindQuery(query).then(function(data) { results = data; }); + ctx.$rootScope.$apply(); + }); + + it('should return list of as text, value', function() { + expect(results.length).to.be(3); + expect(results[0].text).to.be('aTitle'); + expect(results[0].value).to.be('value1'); + expect(results[2].text).to.be('aTitle3'); + expect(results[2].value).to.be('value3'); + }); + }); + + describe('When performing metricFindQuery with key, value columns and with duplicate keys', function() { + let results; + const query = 'select * from atable'; + const response = { + results: { + tempvar: { + meta: { + rowCount: 3 + }, + refId: 'tempvar', + tables: [ + { + columns: [{text: '__text'}, {text: '__value'}], + rows: [ + ['aTitle', 'same'], + ['aTitle', 'same'], + ['aTitle', 'diff'] + ] + } + ] + } + } + }; + + beforeEach(function() { + ctx.backendSrv.datasourceRequest = function(options) { + return ctx.$q.when({data: response, status: 200}); + }; + ctx.ds.metricFindQuery(query).then(function(data) { results = data; }); + ctx.$rootScope.$apply(); + }); + + it('should return list of unique keys', function() { + expect(results.length).to.be(1); + expect(results[0].text).to.be('aTitle'); + expect(results[0].value).to.be('same'); + }); + }); +}); diff --git a/public/app/plugins/datasource/prometheus/completer.ts b/public/app/plugins/datasource/prometheus/completer.ts index 2651f75df98..449a5f34b49 100644 --- a/public/app/plugins/datasource/prometheus/completer.ts +++ b/public/app/plugins/datasource/prometheus/completer.ts @@ -1,30 +1,76 @@ /// import {PrometheusDatasource} from "./datasource"; +import _ from 'lodash'; export class PromCompleter { + labelQueryCache: any; + labelNameCache: any; + labelValueCache: any; + identifierRegexps = [/[\[\]a-zA-Z_0-9=]/]; constructor(private datasource: PrometheusDatasource) { + this.labelQueryCache = {}; + this.labelNameCache = {}; + this.labelValueCache = {}; } getCompletions(editor, session, pos, prefix, callback) { let token = session.getTokenAt(pos.row, pos.column); + var metricName; switch (token.type) { - case 'label.name': - callback(null, ['instance', 'job'].map(function (key) { - return { - caption: key, - value: key, - meta: "label name", - score: Number.MAX_VALUE - }; - })); - return; - case 'label.value': - callback(null, []); - return; + case 'entity.name.tag': + metricName = this.findMetricName(session, pos.row, pos.column); + if (!metricName) { + callback(null, this.transformToCompletions(['__name__', 'instance', 'job'], 'label name')); + return; + } + + if (this.labelNameCache[metricName]) { + callback(null, this.labelNameCache[metricName]); + return; + } + + return this.getLabelNameAndValueForMetric(metricName).then(result => { + var labelNames = this.transformToCompletions( + _.uniq(_.flatten(result.map(r => { + return Object.keys(r.metric); + }))) + , 'label name'); + this.labelNameCache[metricName] = labelNames; + callback(null, labelNames); + }); + case 'string.quoted': + metricName = this.findMetricName(session, pos.row, pos.column); + if (!metricName) { + callback(null, []); + return; + } + + var labelNameToken = this.findToken(session, pos.row, pos.column, 'entity.name.tag', null, 'paren.lparen'); + if (!labelNameToken) { + callback(null, []); + return; + } + var labelName = labelNameToken.value; + + if (this.labelValueCache[metricName] && this.labelValueCache[metricName][labelName]) { + callback(null, this.labelValueCache[metricName][labelName]); + return; + } + + return this.getLabelNameAndValueForMetric(metricName).then(result => { + var labelValues = this.transformToCompletions( + _.uniq(result.map(r => { + return r.metric[labelName]; + })) + , 'label value'); + this.labelValueCache[metricName] = this.labelValueCache[metricName] || {}; + this.labelValueCache[metricName][labelName] = labelValues; + callback(null, labelValues); + }); } if (prefix === '[') { @@ -56,4 +102,87 @@ export class PromCompleter { }); } + getLabelNameAndValueForMetric(metricName) { + if (this.labelQueryCache[metricName]) { + return Promise.resolve(this.labelQueryCache[metricName]); + } + var op = '=~'; + if (/[a-zA-Z_:][a-zA-Z0-9_:]*/.test(metricName)) { + op = '='; + } + var expr = '{__name__' + op + '"' + metricName + '"}'; + return this.datasource.performInstantQuery({ expr: expr }, new Date().getTime() / 1000).then(response => { + this.labelQueryCache[metricName] = response.data.data.result; + return response.data.data.result; + }); + } + + transformToCompletions(words, meta) { + return words.map(name => { + return { + caption: name, + value: name, + meta: meta, + score: Number.MAX_VALUE + }; + }); + } + + findMetricName(session, row, column) { + var metricName = ''; + + var tokens; + var nameLabelNameToken = this.findToken(session, row, column, 'entity.name.tag', '__name__', 'paren.lparen'); + if (nameLabelNameToken) { + tokens = session.getTokens(nameLabelNameToken.row); + var nameLabelValueToken = tokens[nameLabelNameToken.index + 2]; + if (nameLabelValueToken && nameLabelValueToken.type === 'string.quoted') { + metricName = nameLabelValueToken.value.slice(1, -1); // cut begin/end quotation + } + } else { + var metricNameToken = this.findToken(session, row, column, 'identifier', null, null); + if (metricNameToken) { + tokens = session.getTokens(metricNameToken.row); + if (tokens[metricNameToken.index + 1].type === 'paren.lparen') { + metricName = metricNameToken.value; + } + } + } + + return metricName; + } + + findToken(session, row, column, target, value, guard) { + var tokens, idx; + for (var r = row; r >= 0; r--) { + tokens = session.getTokens(r); + if (r === row) { // current row + var c = 0; + for (idx = 0; idx < tokens.length; idx++) { + c += tokens[idx].value.length; + if (c >= column) { + break; + } + } + } else { + idx = tokens.length - 1; + } + + for (; idx >= 0; idx--) { + if (tokens[idx].type === guard) { + return null; + } + + if (tokens[idx].type === target + && (!value || tokens[idx].value === value)) { + tokens[idx].row = r; + tokens[idx].index = idx; + return tokens[idx]; + } + } + } + + return null; + } + } diff --git a/public/app/plugins/datasource/prometheus/datasource.ts b/public/app/plugins/datasource/prometheus/datasource.ts index e3f5cc474f5..e7b7cf36aa0 100644 --- a/public/app/plugins/datasource/prometheus/datasource.ts +++ b/public/app/plugins/datasource/prometheus/datasource.ts @@ -1,15 +1,12 @@ /// import _ from 'lodash'; -import moment from 'moment'; import kbn from 'app/core/utils/kbn'; import * as dateMath from 'app/core/utils/datemath'; import PrometheusMetricFindQuery from './metric_find_query'; import TableModel from 'app/core/table_model'; -var durationSplitRegexp = /(\d+)(ms|s|m|h|d|w|M|y)/; - function prometheusSpecialRegexEscape(value) { return value.replace(/[\\^$*+?.()|[\]{}]/g, '\\\\$&'); } @@ -83,6 +80,7 @@ export class PrometheusDatasource { var self = this; var start = this.getPrometheusTime(options.range.from, false); var end = this.getPrometheusTime(options.range.to, true); + var range = Math.ceil(end - start); var queries = []; var activeTargets = []; @@ -95,18 +93,7 @@ export class PrometheusDatasource { } activeTargets.push(target); - - var query: any = {}; - query.expr = this.templateSrv.replace(target.expr, options.scopedVars, self.interpolateQueryExpr); - query.requestId = options.panelId + target.refId; - query.instant = target.instant; - - var interval = this.templateSrv.replace(target.interval, options.scopedVars) || options.interval; - var intervalFactor = target.intervalFactor || 1; - target.step = query.step = this.calculateInterval(interval, intervalFactor); - var range = Math.ceil(end - start); - target.step = query.step = this.adjustStep(query.step, this.intervalSeconds(options.interval), range); - queries.push(query); + queries.push(this.createQuery(target, options, range)); } // No valid targets, return the empty result to save a round trip. @@ -147,13 +134,41 @@ export class PrometheusDatasource { }); } - adjustStep(step, autoStep, range) { - // Prometheus drop query if range/step > 11000 - // calibrate step if it is too big - if (step !== 0 && range / step > 11000) { - step = Math.ceil(range / 11000); + createQuery(target, options, range) { + var query: any = {}; + query.instant = target.instant; + + var interval = kbn.interval_to_seconds(options.interval); + // Minimum interval ("Min step"), if specified for the query. or same as interval otherwise + var minInterval = kbn.interval_to_seconds(this.templateSrv.replace(target.interval, options.scopedVars) || options.interval); + var intervalFactor = target.intervalFactor || 1; + // Adjust the interval to take into account any specified minimum and interval factor plus Prometheus limits + var adjustedInterval = this.adjustInterval(interval, minInterval, range, intervalFactor); + + var scopedVars = options.scopedVars; + // If the interval was adjusted, make a shallow copy of scopedVars with updated interval vars + if (interval !== adjustedInterval) { + interval = adjustedInterval; + scopedVars = Object.assign({}, options.scopedVars, { + "__interval": {text: interval + "s", value: interval + "s"}, + "__interval_ms": {text: interval * 1000, value: interval * 1000}, + }); } - return Math.max(step, autoStep); + target.step = query.step = interval; + + // Only replace vars in expression after having (possibly) updated interval vars + query.expr = this.templateSrv.replace(target.expr, scopedVars, this.interpolateQueryExpr); + query.requestId = options.panelId + target.refId; + return query; + } + + adjustInterval(interval, minInterval, range, intervalFactor) { + // Prometheus will drop queries that might return more than 11000 data points. + // Calibrate interval if it is too small. + if (interval !== 0 && range / intervalFactor / interval > 11000) { + interval = Math.ceil(range / intervalFactor / 11000); + } + return Math.max(interval * intervalFactor, minInterval); } performTimeSeriesQuery(query, start, end) { @@ -218,7 +233,7 @@ export class PrometheusDatasource { var end = this.getPrometheusTime(options.range.to, true); var query = { expr: interpolated, - step: this.adjustStep(kbn.interval_to_seconds(step), 0, Math.ceil(end - start)) + 's' + step: this.adjustInterval(kbn.interval_to_seconds(step), 0, Math.ceil(end - start), 1) + 's' }; var self = this; @@ -257,21 +272,6 @@ export class PrometheusDatasource { }); } - calculateInterval(interval, intervalFactor) { - return Math.ceil(this.intervalSeconds(interval) * intervalFactor); - } - - intervalSeconds(interval) { - var m = interval.match(durationSplitRegexp); - var dur = moment.duration(parseInt(m[1]), m[2]); - var sec = dur.asSeconds(); - if (sec < 1) { - sec = 1; - } - - return sec; - } - transformMetricData(md, options, start, end) { var dps = [], metricLabel = null; diff --git a/public/app/plugins/datasource/prometheus/mode-prometheus.js b/public/app/plugins/datasource/prometheus/mode-prometheus.js index 78edbbd30d1..165c1a364c4 100644 --- a/public/app/plugins/datasource/prometheus/mode-prometheus.js +++ b/public/app/plugins/datasource/prometheus/mode-prometheus.js @@ -65,13 +65,13 @@ var PrometheusHighlightRules = function() { regex : "\\s+" } ], "start-label-matcher" : [ { - token : "keyword", + token : "entity.name.tag", regex : '[a-zA-Z_][a-zA-Z0-9_]*' }, { token : "keyword.operator", regex : '=~|=|!~|!=' }, { - token : "string", + token : "string.quoted", regex : '"[^"]*"|\'[^\']*\'' }, { token : "punctuation.operator", @@ -401,7 +401,7 @@ var PrometheusCompletions = function() {}; (function() { this.getCompletions = function(state, session, pos, prefix, callback) { var token = session.getTokenAt(pos.row, pos.column); - if (token.type === 'label.name' || token.type === 'label.value') { + if (token.type === 'entity.name.tag' || token.type === 'string.quoted') { return callback(null, []); } diff --git a/public/app/plugins/datasource/prometheus/specs/completer_specs.ts b/public/app/plugins/datasource/prometheus/specs/completer_specs.ts index 37d428859ae..b8b5ef023f0 100644 --- a/public/app/plugins/datasource/prometheus/specs/completer_specs.ts +++ b/public/app/plugins/datasource/prometheus/specs/completer_specs.ts @@ -4,24 +4,115 @@ import {PromCompleter} from '../completer'; import {PrometheusDatasource} from '../datasource'; describe('Prometheus editor completer', function() { + function getSessionStub(data) { + return { + getTokenAt: sinon.stub().returns(data.currentToken), + getTokens: sinon.stub().returns(data.tokens), + getLine: sinon.stub().returns(data.line), + }; + } let editor = {}; - let session = { - getTokenAt: sinon.stub().returns({}), - getLine: sinon.stub().returns(""), + let datasourceStub = { + performInstantQuery: sinon + .stub() + .withArgs({expr: '{__name__="node_cpu"'}) + .returns( + Promise.resolve({ + data: { + data: { + result: [ + { + metric: { + job: 'node', + instance: 'localhost:9100', + }, + }, + ], + }, + }, + }), + ), + performSuggestQuery: sinon + .stub() + .withArgs('node', true) + .returns(Promise.resolve(['node_cpu'])), }; - let datasourceStub = {}; let completer = new PromCompleter(datasourceStub); - describe("When inside brackets", () => { - - it("Should return range vectors", () => { - completer.getCompletions(editor, session, 10, "[", (s, res) => { + describe('When inside brackets', () => { + it('Should return range vectors', () => { + const session = getSessionStub({ + currentToken: {}, + tokens: [], + line: '', + }); + completer.getCompletions(editor, session, {row: 0, column: 10}, '[', (s, res) => { expect(res[0]).to.eql({caption: '1s', value: '[1s', meta: 'range vector'}); }); }); - }); + describe('When inside label matcher, and located at label name', () => { + it('Should return label name list', () => { + const session = getSessionStub({ + currentToken: {type: 'entity.name.tag', value: 'j', index: 2, start: 9}, + tokens: [ + {type: 'identifier', value: 'node_cpu'}, + {type: 'paren.lparen', value: '{'}, + {type: 'entity.name.tag', value: 'j', index: 2, start: 9}, + {type: 'paren.rparen', value: '}'}, + ], + line: 'node_cpu{j}', + }); + + return completer.getCompletions(editor, session, {row: 0, column: 10}, 'j', (s, res) => { + expect(res[0].meta).to.eql('label name'); + }); + }); + }); + + describe('When inside label matcher, and located at label name with __name__ match', () => { + it('Should return label name list', () => { + const session = getSessionStub({ + currentToken: {type: 'entity.name.tag', value: 'j', index: 5, start: 22}, + tokens: [ + {type: 'paren.lparen', value: '{'}, + {type: 'entity.name.tag', value: '__name__'}, + {type: 'keyword.operator', value: '=~'}, + {type: 'string.quoted', value: '"node_cpu"'}, + {type: 'punctuation.operator', value: ','}, + {type: 'entity.name.tag', value: 'j', index: 5, start: 22}, + {type: 'paren.rparen', value: '}'}, + ], + line: '{__name__=~"node_cpu",j}', + }); + + return completer.getCompletions(editor, session, {row: 0, column: 23}, 'j', (s, res) => { + expect(res[0].meta).to.eql('label name'); + }); + }); + }); + + describe('When inside label matcher, and located at label value', () => { + it('Should return label value list', () => { + const session = getSessionStub({ + currentToken: {type: 'string.quoted', value: '"n"', index: 4, start: 13}, + tokens: [ + {type: 'identifier', value: 'node_cpu'}, + {type: 'paren.lparen', value: '{'}, + {type: 'entity.name.tag', value: 'job'}, + {type: 'keyword.operator', value: '='}, + {type: 'string.quoted', value: '"n"', index: 4, start: 13}, + {type: 'paren.rparen', value: '}'}, + ], + line: 'node_cpu{job="n"}', + }); + + return completer.getCompletions(editor, session, {row: 0, column: 15}, 'n', (s, res) => { + expect(res[0].meta).to.eql('label value'); + }); + }); + }); }); diff --git a/public/app/plugins/datasource/prometheus/specs/datasource_specs.ts b/public/app/plugins/datasource/prometheus/specs/datasource_specs.ts index 6cd445d1b36..702ad8b5990 100644 --- a/public/app/plugins/datasource/prometheus/specs/datasource_specs.ts +++ b/public/app/plugins/datasource/prometheus/specs/datasource_specs.ts @@ -269,4 +269,311 @@ describe('PrometheusDatasource', function() { ); }); }); + describe('The "step" query parameter', function() { + var response = { + status: "success", + data: { + resultType: "matrix", + result: [] + } + }; + + it('should be min interval when greater than auto interval', function() { + var query = { + // 6 hour range + range: { from: moment(1443438674760), to: moment(1443460274760) }, + targets: [{ + expr: 'test', + interval: '10s' + }], + interval: '5s' + }; + var urlExpected = 'proxied/api/v1/query_range?query=test' + + '&start=1443438675&end=1443460275&step=10'; + ctx.$httpBackend.expect('GET', urlExpected).respond(response); + ctx.ds.query(query); + ctx.$httpBackend.verifyNoOutstandingExpectation(); + }); + it('should be auto interval when greater than min interval', function() { + var query = { + // 6 hour range + range: { from: moment(1443438674760), to: moment(1443460274760) }, + targets: [{ + expr: 'test', + interval: '5s' + }], + interval: '10s' + }; + var urlExpected = 'proxied/api/v1/query_range?query=test' + + '&start=1443438675&end=1443460275&step=10'; + ctx.$httpBackend.expect('GET', urlExpected).respond(response); + ctx.ds.query(query); + ctx.$httpBackend.verifyNoOutstandingExpectation(); + }); + it('should result in querying fewer than 11000 data points', function() { + var query = { + // 6 hour range + range: { from: moment(1443438674760), to: moment(1443460274760) }, + targets: [{ expr: 'test' }], + interval: '1s' + }; + var urlExpected = 'proxied/api/v1/query_range?query=test' + + '&start=1443438675&end=1443460275&step=2'; + ctx.$httpBackend.expect('GET', urlExpected).respond(response); + ctx.ds.query(query); + ctx.$httpBackend.verifyNoOutstandingExpectation(); + }); + it('should not apply min interval when interval * intervalFactor greater', function() { + var query = { + // 6 hour range + range: { from: moment(1443438674760), to: moment(1443460274760) }, + targets: [{ + expr: 'test', + interval: '10s', + intervalFactor: 10 + }], + interval: '5s' + }; + var urlExpected = 'proxied/api/v1/query_range?query=test' + + '&start=1443438675&end=1443460275&step=50'; + ctx.$httpBackend.expect('GET', urlExpected).respond(response); + ctx.ds.query(query); + ctx.$httpBackend.verifyNoOutstandingExpectation(); + }); + it('should apply min interval when interval * intervalFactor smaller', function() { + var query = { + // 6 hour range + range: { from: moment(1443438674760), to: moment(1443460274760) }, + targets: [{ + expr: 'test', + interval: '15s', + intervalFactor: 2 + }], + interval: '5s' + }; + var urlExpected = 'proxied/api/v1/query_range?query=test' + + '&start=1443438675&end=1443460275&step=15'; + ctx.$httpBackend.expect('GET', urlExpected).respond(response); + ctx.ds.query(query); + ctx.$httpBackend.verifyNoOutstandingExpectation(); + }); + it('should apply intervalFactor to auto interval when greater', function() { + var query = { + // 6 hour range + range: { from: moment(1443438674760), to: moment(1443460274760) }, + targets: [{ + expr: 'test', + interval: '5s', + intervalFactor: 10 + }], + interval: '10s' + }; + var urlExpected = 'proxied/api/v1/query_range?query=test' + + '&start=1443438675&end=1443460275&step=100'; + ctx.$httpBackend.expect('GET', urlExpected).respond(response); + ctx.ds.query(query); + ctx.$httpBackend.verifyNoOutstandingExpectation(); + }); + it('should not not be affected by the 11000 data points limit when large enough', function() { + var query = { + // 1 week range + range: { from: moment(1443438674760), to: moment(1444043474760) }, + targets: [{ + expr: 'test', + intervalFactor: 10 + }], + interval: '10s' + }; + var urlExpected = 'proxied/api/v1/query_range?query=test' + + '&start=1443438675&end=1444043475&step=100'; + ctx.$httpBackend.expect('GET', urlExpected).respond(response); + ctx.ds.query(query); + ctx.$httpBackend.verifyNoOutstandingExpectation(); + }); + it('should be determined by the 11000 data points limit when too small', function() { + var query = { + // 1 week range + range: { from: moment(1443438674760), to: moment(1444043474760) }, + targets: [{ + expr: 'test', + intervalFactor: 10 + }], + interval: '5s' + }; + var urlExpected = 'proxied/api/v1/query_range?query=test' + + '&start=1443438675&end=1444043475&step=60'; + ctx.$httpBackend.expect('GET', urlExpected).respond(response); + ctx.ds.query(query); + ctx.$httpBackend.verifyNoOutstandingExpectation(); + }); + }); + describe('The __interval and __interval_ms template variables', function() { + var response = { + status: "success", + data: { + resultType: "matrix", + result: [] + } + }; + + it('should be unchanged when auto interval is greater than min interval', function() { + var query = { + // 6 hour range + range: { from: moment(1443438674760), to: moment(1443460274760) }, + targets: [{ + expr: 'rate(test[$__interval])', + interval: '5s' + }], + interval: '10s', + scopedVars: { + "__interval": {text: "10s", value: "10s"}, + "__interval_ms": {text: 10 * 1000, value: 10 * 1000}, + } + }; + var urlExpected = 'proxied/api/v1/query_range?query=' + + encodeURIComponent('rate(test[10s])') + + '&start=1443438675&end=1443460275&step=10'; + ctx.$httpBackend.expect('GET', urlExpected).respond(response); + ctx.ds.query(query); + ctx.$httpBackend.verifyNoOutstandingExpectation(); + + expect(query.scopedVars.__interval.text).to.be("10s"); + expect(query.scopedVars.__interval.value).to.be("10s"); + expect(query.scopedVars.__interval_ms.text).to.be(10 * 1000); + expect(query.scopedVars.__interval_ms.value).to.be(10 * 1000); + }); + it('should be min interval when it is greater than auto interval', function() { + var query = { + // 6 hour range + range: { from: moment(1443438674760), to: moment(1443460274760) }, + targets: [{ + expr: 'rate(test[$__interval])', + interval: '10s' + }], + interval: '5s', + scopedVars: { + "__interval": {text: "5s", value: "5s"}, + "__interval_ms": {text: 5 * 1000, value: 5 * 1000}, + } + }; + var urlExpected = 'proxied/api/v1/query_range?query=' + + encodeURIComponent('rate(test[10s])') + + '&start=1443438675&end=1443460275&step=10'; + ctx.$httpBackend.expect('GET', urlExpected).respond(response); + ctx.ds.query(query); + ctx.$httpBackend.verifyNoOutstandingExpectation(); + + expect(query.scopedVars.__interval.text).to.be("5s"); + expect(query.scopedVars.__interval.value).to.be("5s"); + expect(query.scopedVars.__interval_ms.text).to.be(5 * 1000); + expect(query.scopedVars.__interval_ms.value).to.be(5 * 1000); + }); + it('should account for intervalFactor', function() { + var query = { + // 6 hour range + range: { from: moment(1443438674760), to: moment(1443460274760) }, + targets: [{ + expr: 'rate(test[$__interval])', + interval: '5s', + intervalFactor: 10 + }], + interval: '10s', + scopedVars: { + "__interval": {text: "10s", value: "10s"}, + "__interval_ms": {text: 10 * 1000, value: 10 * 1000}, + } + }; + var urlExpected = 'proxied/api/v1/query_range?query=' + + encodeURIComponent('rate(test[100s])') + + '&start=1443438675&end=1443460275&step=100'; + ctx.$httpBackend.expect('GET', urlExpected).respond(response); + ctx.ds.query(query); + ctx.$httpBackend.verifyNoOutstandingExpectation(); + + expect(query.scopedVars.__interval.text).to.be("10s"); + expect(query.scopedVars.__interval.value).to.be("10s"); + expect(query.scopedVars.__interval_ms.text).to.be(10 * 1000); + expect(query.scopedVars.__interval_ms.value).to.be(10 * 1000); + }); + it('should be interval * intervalFactor when greater than min interval', function() { + var query = { + // 6 hour range + range: { from: moment(1443438674760), to: moment(1443460274760) }, + targets: [{ + expr: 'rate(test[$__interval])', + interval: '10s', + intervalFactor: 10 + }], + interval: '5s', + scopedVars: { + "__interval": {text: "5s", value: "5s"}, + "__interval_ms": {text: 5 * 1000, value: 5 * 1000}, + } + }; + var urlExpected = 'proxied/api/v1/query_range?query=' + + encodeURIComponent('rate(test[50s])') + + '&start=1443438675&end=1443460275&step=50'; + ctx.$httpBackend.expect('GET', urlExpected).respond(response); + ctx.ds.query(query); + ctx.$httpBackend.verifyNoOutstandingExpectation(); + + expect(query.scopedVars.__interval.text).to.be("5s"); + expect(query.scopedVars.__interval.value).to.be("5s"); + expect(query.scopedVars.__interval_ms.text).to.be(5 * 1000); + expect(query.scopedVars.__interval_ms.value).to.be(5 * 1000); + }); + it('should be min interval when greater than interval * intervalFactor', function() { + var query = { + // 6 hour range + range: { from: moment(1443438674760), to: moment(1443460274760) }, + targets: [{ + expr: 'rate(test[$__interval])', + interval: '15s', + intervalFactor: 2 + }], + interval: '5s', + scopedVars: { + "__interval": {text: "5s", value: "5s"}, + "__interval_ms": {text: 5 * 1000, value: 5 * 1000}, + } + }; + var urlExpected = 'proxied/api/v1/query_range?query=' + + encodeURIComponent('rate(test[15s])') + + '&start=1443438675&end=1443460275&step=15'; + ctx.$httpBackend.expect('GET', urlExpected).respond(response); + ctx.ds.query(query); + ctx.$httpBackend.verifyNoOutstandingExpectation(); + + expect(query.scopedVars.__interval.text).to.be("5s"); + expect(query.scopedVars.__interval.value).to.be("5s"); + expect(query.scopedVars.__interval_ms.text).to.be(5 * 1000); + expect(query.scopedVars.__interval_ms.value).to.be(5 * 1000); + }); + it('should be determined by the 11000 data points limit, accounting for intervalFactor', function() { + var query = { + // 1 week range + range: { from: moment(1443438674760), to: moment(1444043474760) }, + targets: [{ + expr: 'rate(test[$__interval])', + intervalFactor: 10 + }], + interval: '5s', + scopedVars: { + "__interval": {text: "5s", value: "5s"}, + "__interval_ms": {text: 5 * 1000, value: 5 * 1000}, + } + }; + var urlExpected = 'proxied/api/v1/query_range?query=' + + encodeURIComponent('rate(test[60s])') + + '&start=1443438675&end=1444043475&step=60'; + ctx.$httpBackend.expect('GET', urlExpected).respond(response); + ctx.ds.query(query); + ctx.$httpBackend.verifyNoOutstandingExpectation(); + + expect(query.scopedVars.__interval.text).to.be("5s"); + expect(query.scopedVars.__interval.value).to.be("5s"); + expect(query.scopedVars.__interval_ms.text).to.be(5 * 1000); + expect(query.scopedVars.__interval_ms.value).to.be(5 * 1000); + }); + }); }); diff --git a/public/app/plugins/panel/alertlist/module.html b/public/app/plugins/panel/alertlist/module.html index 39dbb5bbe26..a88c4ebadc7 100644 --- a/public/app/plugins/panel/alertlist/module.html +++ b/public/app/plugins/panel/alertlist/module.html @@ -33,7 +33,7 @@
-

{{al.title}}

+

{{al.alertName}}

{{al.stateModel.text}} {{al.info}} diff --git a/public/app/plugins/panel/graph/axes_editor.html b/public/app/plugins/panel/graph/axes_editor.html index 1ce4c3174b4..6160ef01fec 100644 --- a/public/app/plugins/panel/graph/axes_editor.html +++ b/public/app/plugins/panel/graph/axes_editor.html @@ -31,7 +31,7 @@
- +
diff --git a/public/app/plugins/panel/graph/graph.ts b/public/app/plugins/panel/graph/graph.ts index a203648ae04..f378e52a111 100755 --- a/public/app/plugins/panel/graph/graph.ts +++ b/public/app/plugins/panel/graph/graph.ts @@ -22,7 +22,7 @@ import {EventManager} from 'app/features/annotations/all'; import {convertValuesToHistogram, getSeriesValues} from './histogram'; /** @ngInject **/ -function graphDirective($rootScope, timeSrv, popoverSrv) { +function graphDirective($rootScope, timeSrv, popoverSrv, contextSrv) { return { restrict: 'A', template: '', @@ -37,7 +37,7 @@ function graphDirective($rootScope, timeSrv, popoverSrv) { var legendSideLastValue = null; var rootScope = scope.$root; var panelWidth = 0; - var eventManager = new EventManager(ctrl, elem, popoverSrv); + var eventManager = new EventManager(ctrl); var thresholdManager = new ThresholdManager(ctrl); var tooltip = new GraphTooltip(elem, dashboard, scope, function() { return sortedSeries; @@ -268,6 +268,7 @@ function graphDirective($rootScope, timeSrv, popoverSrv) { clickable: true, color: '#c8c8c8', margin: { left: 0, right: 0 }, + labelMarginX: 0, }, selection: { mode: "x", @@ -496,8 +497,8 @@ function graphDirective($rootScope, timeSrv, popoverSrv) { show: panel.yaxes[0].show, index: 1, logBase: panel.yaxes[0].logBase || 1, - min: panel.yaxes[0].min ? _.toNumber(panel.yaxes[0].min) : null, - max: panel.yaxes[0].max ? _.toNumber(panel.yaxes[0].max) : null, + min: parseNumber(panel.yaxes[0].min), + max: parseNumber(panel.yaxes[0].max), tickDecimals: panel.yaxes[0].decimals }; @@ -509,9 +510,9 @@ function graphDirective($rootScope, timeSrv, popoverSrv) { secondY.show = panel.yaxes[1].show; secondY.logBase = panel.yaxes[1].logBase || 1; secondY.position = 'right'; - secondY.min = panel.yaxes[1].min ? _.toNumber(panel.yaxes[1].min) : null; - secondY.max = panel.yaxes[1].max ? _.toNumber(panel.yaxes[1].max) : null; - secondY.tickDecimals = panel.yaxes[1].decimals !== null ? _.toNumber(panel.yaxes[1].decimals): null; + secondY.min = parseNumber(panel.yaxes[1].min); + secondY.max = parseNumber(panel.yaxes[1].max); + secondY.tickDecimals = panel.yaxes[1].decimals; options.yaxes.push(secondY); applyLogScale(options.yaxes[1], data); @@ -521,6 +522,14 @@ function graphDirective($rootScope, timeSrv, popoverSrv) { configureAxisMode(options.yaxes[0], panel.percentage && panel.stack ? "percent" : panel.yaxes[0].format); } + function parseNumber(value: any) { + if (value === null || typeof value === 'undefined') { + return null; + } + + return _.toNumber(value); + } + function applyLogScale(axis, data) { if (axis.logBase === 1) { return; @@ -651,10 +660,10 @@ function graphDirective($rootScope, timeSrv, popoverSrv) { } elem.bind("plotselected", function (event, ranges) { - if (ranges.ctrlKey || ranges.metaKey) { - // scope.$apply(() => { - // eventManager.updateTime(ranges.xaxis); - // }); + if ((ranges.ctrlKey || ranges.metaKey) && contextSrv.isEditor) { + setTimeout(() => { + eventManager.updateTime(ranges.xaxis); + }, 100); } else { scope.$apply(function() { timeSrv.setTime({ @@ -666,13 +675,13 @@ function graphDirective($rootScope, timeSrv, popoverSrv) { }); elem.bind("plotclick", function (event, pos, item) { - if (pos.ctrlKey || pos.metaKey || eventManager.event) { + if ((pos.ctrlKey || pos.metaKey) && contextSrv.isEditor) { // Skip if range selected (added in "plotselected" event handler) let isRangeSelection = pos.x !== pos.x1; if (!isRangeSelection) { - // scope.$apply(() => { - // eventManager.updateTime({from: pos.x, to: null}); - // }); + setTimeout(() => { + eventManager.updateTime({from: pos.x, to: null}); + }, 100); } } }); diff --git a/public/app/plugins/panel/graph/jquery.flot.events.js b/public/app/plugins/panel/graph/jquery.flot.events.js index 3fc3db0d6d3..1aa79c5056f 100644 --- a/public/app/plugins/panel/graph/jquery.flot.events.js +++ b/public/app/plugins/panel/graph/jquery.flot.events.js @@ -7,14 +7,18 @@ define([ function ($, _, angular, Drop) { 'use strict'; - function createAnnotationToolip(element, event) { + function createAnnotationToolip(element, event, plot) { var injector = angular.element(document).injector(); var content = document.createElement('div'); - content.innerHTML = ''; + content.innerHTML = ''; injector.invoke(["$compile", "$rootScope", function($compile, $rootScope) { + var eventManager = plot.getOptions().events.manager; var tmpScope = $rootScope.$new(true); tmpScope.event = event; + tmpScope.onEdit = function() { + eventManager.editEvent(event); + }; $compile(content)(tmpScope); tmpScope.$digest(); @@ -42,6 +46,69 @@ function ($, _, angular, Drop) { }]); } + var markerElementToAttachTo = null; + + function createEditPopover(element, event, plot) { + var eventManager = plot.getOptions().events.manager; + if (eventManager.editorOpen) { + // update marker element to attach to (needed in case of legend on the right + // when there is a double render pass and the inital marker element is removed) + markerElementToAttachTo = element; + return; + } + + // mark as openend + eventManager.editorOpened(); + // set marker elment to attache to + markerElementToAttachTo = element; + + // wait for element to be attached and positioned + setTimeout(function() { + + var injector = angular.element(document).injector(); + var content = document.createElement('div'); + content.innerHTML = ''; + + injector.invoke(["$compile", "$rootScope", function($compile, $rootScope) { + var scope = $rootScope.$new(true); + var drop; + + scope.event = event; + scope.panelCtrl = eventManager.panelCtrl; + scope.close = function() { + drop.close(); + }; + + $compile(content)(scope); + scope.$digest(); + + drop = new Drop({ + target: markerElementToAttachTo[0], + content: content, + position: "bottom center", + classes: 'drop-popover drop-popover--form', + openOn: 'click', + tetherOptions: { + constraints: [{to: 'window', pin: true, attachment: "both"}] + } + }); + + drop.open(); + eventManager.editorOpened(); + + drop.on('close', function() { + // need timeout here in order call drop.destroy + setTimeout(function() { + eventManager.editorClosed(); + scope.$destroy(); + drop.destroy(); + }); + }); + }]); + + }, 100); + } + /* * jquery.flot.events * @@ -121,11 +188,20 @@ function ($, _, angular, Drop) { */ this.setupEvents = function(events) { var that = this; + var parts = _.partition(events, 'isRegion'); + var regions = parts[0]; + events = parts[1]; + $.each(events, function(index, event) { var ve = new VisualEvent(event, that._buildDiv(event)); _events.push(ve); }); + $.each(regions, function (index, event) { + var vre = new VisualEvent(event, that._buildRegDiv(event)); + _events.push(vre); + }); + _events.sort(function(a, b) { var ao = a.getOptions(), bo = b.getOptions(); if (ao.min > bo.min) { return 1; } @@ -232,7 +308,10 @@ function ($, _, angular, Drop) { lineWidth = this._types[eventTypeId].lineWidth; } - top = o.top + this._plot.height(); + var topOffset = xaxis.options.eventSectionHeight || 0; + topOffset = topOffset / 3; + + top = o.top + this._plot.height() + topOffset; left = xaxis.p2c(event.min) + o.left; var line = $('
').css({ @@ -241,25 +320,27 @@ function ($, _, angular, Drop) { "left": left + 'px', "top": 8, "width": lineWidth + "px", - "height": this._plot.height(), + "height": this._plot.height() + topOffset * 0.8, "border-left-width": lineWidth + "px", "border-left-style": lineStyle, - "border-left-color": color + "border-left-color": color, + "color": color }) .appendTo(container); if (markerShow) { var marker = $('
').css({ "position": "absolute", - "left": (-markerSize-Math.round(lineWidth/2)) + "px", + "left": (-markerSize - Math.round(lineWidth / 2)) + "px", "font-size": 0, "line-height": 0, "width": 0, "height": 0, "border-left": markerSize+"px solid transparent", "border-right": markerSize+"px solid transparent" - }) - .appendTo(line); + }); + + marker.appendTo(line); if (this._types[eventTypeId] && this._types[eventTypeId].position && this._types[eventTypeId].position.toUpperCase() === 'BOTTOM') { marker.css({ @@ -280,9 +361,13 @@ function ($, _, angular, Drop) { }); var mouseenter = function() { - createAnnotationToolip(marker, $(this).data("event")); + createAnnotationToolip(marker, $(this).data("event"), that._plot); }; + if (event.editModel) { + createEditPopover(marker, event.editModel, that._plot); + } + var mouseleave = function() { that._plot.clearSelection(); }; @@ -312,6 +397,127 @@ function ($, _, angular, Drop) { return drawableEvent; }; + /** + * create a DOM element for the given region + */ + this._buildRegDiv = function (event) { + var that = this; + + var container = this._plot.getPlaceholder(); + var o = this._plot.getPlotOffset(); + var axes = this._plot.getAxes(); + var xaxis = this._plot.getXAxes()[this._plot.getOptions().events.xaxis - 1]; + var yaxis, top, left, lineWidth, regionWidth, lineStyle, color, markerTooltip; + + // determine the y axis used + if (axes.yaxis && axes.yaxis.used) { yaxis = axes.yaxis; } + if (axes.yaxis2 && axes.yaxis2.used) { yaxis = axes.yaxis2; } + + // map the eventType to a types object + var eventTypeId = event.eventType; + + if (this._types === null || !this._types[eventTypeId] || !this._types[eventTypeId].color) { + color = '#666'; + } else { + color = this._types[eventTypeId].color; + } + + if (this._types === null || !this._types[eventTypeId] || this._types[eventTypeId].markerTooltip === undefined) { + markerTooltip = true; + } else { + markerTooltip = this._types[eventTypeId].markerTooltip; + } + + if (this._types == null || !this._types[eventTypeId] || this._types[eventTypeId].lineWidth === undefined) { + lineWidth = 1; //default line width + } else { + lineWidth = this._types[eventTypeId].lineWidth; + } + + if (this._types == null || !this._types[eventTypeId] || !this._types[eventTypeId].lineStyle) { + lineStyle = 'dashed'; //default line style + } else { + lineStyle = this._types[eventTypeId].lineStyle.toLowerCase(); + } + + var topOffset = 2; + top = o.top + this._plot.height() + topOffset; + + var timeFrom = Math.min(event.min, event.timeEnd); + var timeTo = Math.max(event.min, event.timeEnd); + left = xaxis.p2c(timeFrom) + o.left; + var right = xaxis.p2c(timeTo) + o.left; + regionWidth = right - left; + + _.each([left, right], function(position) { + var line = $('
').css({ + "position": "absolute", + "opacity": 0.8, + "left": position + 'px', + "top": 8, + "width": lineWidth + "px", + "height": that._plot.height() + topOffset, + "border-left-width": lineWidth + "px", + "border-left-style": lineStyle, + "border-left-color": color, + "color": color + }); + line.appendTo(container); + }); + + var region = $('
').css({ + "position": "absolute", + "opacity": 0.5, + "left": left + 'px', + "top": top, + "width": Math.round(regionWidth + lineWidth) + "px", + "height": "0.5rem", + "border-left-color": color, + "color": color, + "background-color": color + }); + region.appendTo(container); + + region.data({ + "event": event + }); + + var mouseenter = function () { + createAnnotationToolip(region, $(this).data("event"), that._plot); + }; + + if (event.editModel) { + createEditPopover(region, event.editModel, that._plot); + } + + var mouseleave = function () { + that._plot.clearSelection(); + }; + + if (markerTooltip) { + region.css({ "cursor": "help" }); + region.hover(mouseenter, mouseleave); + } + + var drawableEvent = new DrawableEvent( + region, + function drawFunc(obj) { obj.show(); }, + function (obj) { obj.remove(); }, + function (obj, position) { + obj.css({ + top: position.top, + left: position.left + }); + }, + left, + top, + region.width(), + region.height() + ); + + return drawableEvent; + }; + /** * check if the event is inside visible range */ @@ -395,5 +601,4 @@ function ($, _, angular, Drop) { name: "events", version: "0.2.5" }); - }); diff --git a/public/app/plugins/panel/graph/thresholds_form.ts b/public/app/plugins/panel/graph/thresholds_form.ts index acf9c4e0030..c9f6a69c6b2 100644 --- a/public/app/plugins/panel/graph/thresholds_form.ts +++ b/public/app/plugins/panel/graph/thresholds_form.ts @@ -37,6 +37,20 @@ export class ThresholdFormCtrl { render() { this.panelCtrl.render(); } + + onFillColorChange(index) { + return (newColor) => { + this.panel.thresholds[index].fillColor = newColor; + this.render(); + }; + } + + onLineColorChange(index) { + return (newColor) => { + this.panel.thresholds[index].lineColor = newColor; + this.render(); + }; + } } var template = ` @@ -77,7 +91,7 @@ var template = `
- +
@@ -87,7 +101,7 @@ var template = `
- +
diff --git a/public/app/plugins/panel/heatmap/heatmap_ctrl.ts b/public/app/plugins/panel/heatmap/heatmap_ctrl.ts index b564339673f..10a5e1b3d4e 100644 --- a/public/app/plugins/panel/heatmap/heatmap_ctrl.ts +++ b/public/app/plugins/panel/heatmap/heatmap_ctrl.ts @@ -119,6 +119,8 @@ export class HeatmapCtrl extends MetricsPanelCtrl { this.events.on('data-error', this.onDataError.bind(this)); this.events.on('data-snapshot-load', this.onDataReceived.bind(this)); this.events.on('init-edit-mode', this.onInitEditMode.bind(this)); + + this.onCardColorChange = this.onCardColorChange.bind(this); } onInitEditMode() { @@ -236,6 +238,11 @@ export class HeatmapCtrl extends MetricsPanelCtrl { this.render(); } + onCardColorChange(newColor) { + this.panel.color.cardColor = newColor; + this.render(); + } + seriesHandler(seriesData) { let series = new TimeSeries({ datapoints: seriesData.datapoints, diff --git a/public/app/plugins/panel/heatmap/partials/display_editor.html b/public/app/plugins/panel/heatmap/partials/display_editor.html index f161bf6cab4..929cf1fe7d4 100644 --- a/public/app/plugins/panel/heatmap/partials/display_editor.html +++ b/public/app/plugins/panel/heatmap/partials/display_editor.html @@ -12,7 +12,7 @@
- +
diff --git a/public/app/plugins/panel/singlestat/editor.html b/public/app/plugins/panel/singlestat/editor.html index 1981b4357f8..f00d909d39c 100644 --- a/public/app/plugins/panel/singlestat/editor.html +++ b/public/app/plugins/panel/singlestat/editor.html @@ -68,14 +68,8 @@
diff --git a/public/app/plugins/panel/singlestat/module.ts b/public/app/plugins/panel/singlestat/module.ts index b4ee2bf7fb7..744659e5134 100644 --- a/public/app/plugins/panel/singlestat/module.ts +++ b/public/app/plugins/panel/singlestat/module.ts @@ -92,6 +92,9 @@ class SingleStatCtrl extends MetricsPanelCtrl { this.events.on('data-error', this.onDataError.bind(this)); this.events.on('data-snapshot-load', this.onDataReceived.bind(this)); this.events.on('init-edit-mode', this.onInitEditMode.bind(this)); + + this.onSparklineColorChange = this.onSparklineColorChange.bind(this); + this.onSparklineFillChange = this.onSparklineFillChange.bind(this); } onInitEditMode() { @@ -221,6 +224,16 @@ class SingleStatCtrl extends MetricsPanelCtrl { }; } + onSparklineColorChange(newColor) { + this.panel.sparkline.lineColor = newColor; + this.render(); + } + + onSparklineFillChange(newColor) { + this.panel.sparkline.fillColor = newColor; + this.render(); + } + getDecimalsForValue(value) { if (_.isNumber(this.panel.decimals)) { return {decimals: this.panel.decimals, scaledDecimals: null}; @@ -432,7 +445,8 @@ class SingleStatCtrl extends MetricsPanelCtrl { function addGauge() { var width = elem.width(); var height = elem.height(); - var dimension = Math.min(width, height); + // Allow to use a bit more space for wide gauges + var dimension = Math.min(width, height * 1.3); ctrl.invalidGaugeRange = false; if (panel.gauge.minValue > panel.gauge.maxValue) { @@ -469,8 +483,11 @@ class SingleStatCtrl extends MetricsPanelCtrl { var fontScale = parseInt(panel.valueFontSize) / 100; var fontSize = Math.min(dimension/5, 100) * fontScale; - var gaugeWidth = Math.min(dimension/6, 60); + // Reduce gauge width if threshold labels enabled + var gaugeWidthReduceRatio = panel.gauge.thresholdLabels ? 1.5 : 1; + var gaugeWidth = Math.min(dimension/6, 60) / gaugeWidthReduceRatio; var thresholdMarkersWidth = gaugeWidth/5; + var thresholdLabelFontSize = fontSize / 2.5; var options = { series: { @@ -491,8 +508,8 @@ class SingleStatCtrl extends MetricsPanelCtrl { values: thresholds, label: { show: panel.gauge.thresholdLabels, - margin: 8, - font: { size: 18 } + margin: thresholdMarkersWidth + 1, + font: { size: thresholdLabelFontSize } }, show: panel.gauge.thresholdMarkers, width: thresholdMarkersWidth, @@ -589,7 +606,7 @@ class SingleStatCtrl extends MetricsPanelCtrl { var body = panel.gauge.show ? '' : getBigValueHtml(); - if (panel.colorBackground && !isNaN(data.value)) { + if (panel.colorBackground) { var color = getColorForValue(data, data.value); if (color) { $panelContainer.css('background-color', color); @@ -673,6 +690,9 @@ class SingleStatCtrl extends MetricsPanelCtrl { } function getColorForValue(data, value) { + if (!_.isFinite(value)) { + return null; + } for (var i = data.thresholds.length; i > 0; i--) { if (value >= data.thresholds[i-1]) { return data.colorMap[i]; diff --git a/public/app/plugins/panel/table/column_options.html b/public/app/plugins/panel/table/column_options.html index 53e3c477346..996f10960f9 100644 --- a/public/app/plugins/panel/table/column_options.html +++ b/public/app/plugins/panel/table/column_options.html @@ -35,13 +35,15 @@
-
+
- +
+ +
@@ -54,7 +56,7 @@
-
+
@@ -67,7 +69,7 @@
Thresholds
- +
@@ -78,13 +80,13 @@
- + - + - +
Invert diff --git a/public/app/plugins/panel/table/column_options.ts b/public/app/plugins/panel/table/column_options.ts index 2191de2bb8d..23035293080 100644 --- a/public/app/plugins/panel/table/column_options.ts +++ b/public/app/plugins/panel/table/column_options.ts @@ -40,6 +40,7 @@ export class ColumnOptionsCtrl { this.fontSizes = ['80%', '90%', '100%', '110%', '120%', '130%', '150%', '160%', '180%', '200%', '220%', '250%']; this.dateFormats = [ {text: 'YYYY-MM-DD HH:mm:ss', value: 'YYYY-MM-DD HH:mm:ss'}, + {text: 'YYYY-MM-DD HH:mm:ss.SSS', value: 'YYYY-MM-DD HH:mm:ss.SSS'}, {text: 'MM/DD/YY h:mm:ss a', value: 'MM/DD/YY h:mm:ss a'}, {text: 'MMMM D, YYYY LT', value: 'MMMM D, YYYY LT'}, ]; @@ -52,6 +53,8 @@ export class ColumnOptionsCtrl { return col.text; }); }; + + this.onColorChange = this.onColorChange.bind(this); } render() { @@ -103,6 +106,13 @@ export class ColumnOptionsCtrl { ref[2] = copy; this.panelCtrl.render(); } + + onColorChange(styleIndex, colorIndex) { + return (newColor) => { + this.panel.styles[styleIndex].colors[colorIndex] = newColor; + this.render(); + }; + } } /** @ngInject */ diff --git a/public/app/system.conf.js b/public/app/system.conf.js new file mode 100644 index 00000000000..88ab2670e78 --- /dev/null +++ b/public/app/system.conf.js @@ -0,0 +1,83 @@ +System.config({ + defaultJSExtenions: true, + baseURL: 'public', + paths: { + 'virtual-scroll': 'vendor/npm/virtual-scroll/src/index.js', + 'mousetrap': 'vendor/npm/mousetrap/mousetrap.js', + 'remarkable': 'vendor/npm/remarkable/dist/remarkable.js', + 'tether': 'vendor/npm/tether/dist/js/tether.js', + 'eventemitter3': 'vendor/npm/eventemitter3/index.js', + 'tether-drop': 'vendor/npm/tether-drop/dist/js/drop.js', + 'moment': 'vendor/moment.js', + "jquery": "vendor/jquery/dist/jquery.js", + 'lodash-src': 'vendor/lodash/dist/lodash.js', + "lodash": 'app/core/lodash_extended.js', + "angular": "vendor/angular/angular.js", + "bootstrap": "vendor/bootstrap/bootstrap.js", + 'angular-route': 'vendor/angular-route/angular-route.js', + 'angular-sanitize': 'vendor/angular-sanitize/angular-sanitize.js', + "angular-ui": "vendor/angular-ui/ui-bootstrap-tpls.js", + "angular-strap": "vendor/angular-other/angular-strap.js", + "angular-dragdrop": "vendor/angular-native-dragdrop/draganddrop.js", + "angular-bindonce": "vendor/angular-bindonce/bindonce.js", + "spectrum": "vendor/spectrum.js", + "bootstrap-tagsinput": "vendor/tagsinput/bootstrap-tagsinput.js", + "jquery.flot": "vendor/flot/jquery.flot", + "jquery.flot.pie": "vendor/flot/jquery.flot.pie", + "jquery.flot.selection": "vendor/flot/jquery.flot.selection", + "jquery.flot.stack": "vendor/flot/jquery.flot.stack", + "jquery.flot.stackpercent": "vendor/flot/jquery.flot.stackpercent", + "jquery.flot.time": "vendor/flot/jquery.flot.time", + "jquery.flot.crosshair": "vendor/flot/jquery.flot.crosshair", + "jquery.flot.fillbelow": "vendor/flot/jquery.flot.fillbelow", + "jquery.flot.gauge": "vendor/flot/jquery.flot.gauge", + "d3": "vendor/d3/d3.js", + "jquery.flot.dashes": "vendor/flot/jquery.flot.dashes", + "twemoji": "vendor/npm/twemoji/2/twemoji.amd.js", + "ace": "vendor/npm/ace-builds/src-noconflict/ace", + }, + + packages: { + app: { + defaultExtension: 'js', + }, + vendor: { + defaultExtension: 'js', + }, + plugins: { + defaultExtension: 'js', + }, + test: { + defaultExtension: 'js', + }, + }, + + map: { + text: 'vendor/plugin-text/text.js', + css: 'app/core/utils/css_loader.js' + }, + + meta: { + 'vendor/npm/virtual-scroll/src/indx.js': { + format: 'cjs', + exports: 'VirtualScroll', + }, + 'vendor/angular/angular.js': { + format: 'global', + deps: ['jquery'], + exports: 'angular', + }, + 'vendor/npm/eventemitter3/index.js': { + format: 'cjs', + exports: 'EventEmitter' + }, + 'vendor/npm/mousetrap/mousetrap.js': { + format: 'global', + exports: 'Mousetrap' + }, + 'vendor/npm/ace-builds/src-noconflict/ace.js': { + format: 'global', + exports: 'ace' + } + } +}); diff --git a/public/sass/_grafana.scss b/public/sass/_grafana.scss index f2abb2b8b3f..79ee1799b90 100644 --- a/public/sass/_grafana.scss +++ b/public/sass/_grafana.scss @@ -78,6 +78,7 @@ @import "components/jsontree"; @import "components/edit_sidemenu.scss"; @import "components/row.scss"; +@import "components/icon-picker.scss"; @import "components/json_explorer.scss"; @import "components/code_editor.scss"; diff --git a/public/sass/_variables.dark.scss b/public/sass/_variables.dark.scss index 4de87a0aaf0..0dc00cf5a9c 100644 --- a/public/sass/_variables.dark.scss +++ b/public/sass/_variables.dark.scss @@ -251,7 +251,8 @@ $alert-info-bg: linear-gradient(100deg, #1a4552, #00374a); // popover $popover-bg: $panel-bg; $popover-color: $text-color; -$popover-border-color: $gray-1; +$popover-border-color: $dark-4; +$popover-shadow: 0 0 20px black; $popover-help-bg: $btn-secondary-bg; $popover-help-color: $text-color; diff --git a/public/sass/_variables.light.scss b/public/sass/_variables.light.scss index 533daec705b..e6901e1c772 100644 --- a/public/sass/_variables.light.scss +++ b/public/sass/_variables.light.scss @@ -270,9 +270,11 @@ $alert-warning-bg: linear-gradient(90deg, #d44939, #e0603d); $alert-info-bg: $blue-dark; // popover -$popover-bg: $gray-5; +$popover-bg: $panel-bg; $popover-color: $text-color; -$popover-border-color: $gray-3; +$popover-border-color: $gray-5; +$popover-shadow: 0 0 20px $white; + $popover-help-bg: $blue-dark; $popover-help-color: $gray-6; $popover-error-bg: $btn-danger-bg; diff --git a/public/sass/components/_drop.scss b/public/sass/components/_drop.scss index 9e3c884bc68..c1441bd31cb 100644 --- a/public/sass/components/_drop.scss +++ b/public/sass/components/_drop.scss @@ -51,9 +51,16 @@ $easing: cubic-bezier(0, 0, 0.265, 1.00); } } +.drop-element.drop-popover { + .drop-content { + box-shadow: $popover-shadow; + } +} + .drop-element.drop-popover--form { .drop-content { max-width: none; + padding: 0; } } diff --git a/public/sass/components/_icon-picker.scss b/public/sass/components/_icon-picker.scss new file mode 100644 index 00000000000..796f3f95db5 --- /dev/null +++ b/public/sass/components/_icon-picker.scss @@ -0,0 +1,26 @@ +.gf-icon-picker { + width: 400px; + height: 450px; + + .icon-filter { + padding-bottom: 10px; + margin: auto; + width: 50%; + } + + .icon-container { + max-height: 350px; + overflow: auto; + + .gf-event-icon { + margin: 0.4rem; + height: 1.5rem; + } + } +} + +.gf-icon-picker-button { + .gf-event-icon { + height: 1.2rem; + } +} diff --git a/public/sass/components/_panel_graph.scss b/public/sass/components/_panel_graph.scss index 5f9178df2f7..45372f92a65 100644 --- a/public/sass/components/_panel_graph.scss +++ b/public/sass/components/_panel_graph.scss @@ -287,19 +287,27 @@ margin-top: 8px; } - .graph-annotation-header { - background-color: $input-label-bg; + .graph-annotation__header { + background-color: $popover-border-color; padding: 0.40rem 0.65rem; + display: flex; } - .graph-annotation-title { + .graph-annotation__title { font-weight: $font-weight-semi-bold; padding-right: $spacer; - position: relative; - top: 2px; + overflow: hidden; + display: inline-block; + white-space: nowrap; + text-overflow: ellipsis; + flex-grow: 1; } - .graph-annotation-time { + .graph-annotation__edit-icon { + padding-left: $spacer; + } + + .graph-annotation__time { color: $text-muted; font-style: italic; font-weight: normal; @@ -308,15 +316,22 @@ top: 1px; } - .graph-annotation-body { + .graph-annotation__body { padding: 0.65rem; } - a { + .graph-annotation__user { + img { + border-radius: 50%; + width: 16px; + height: 16px; + } + } + + a[href] { color: $blue; text-decoration: underline; } - } .left-yaxis-label { diff --git a/public/sass/mixins/_drop_element.scss b/public/sass/mixins/_drop_element.scss index 7aa51fff256..f1bb69efd98 100644 --- a/public/sass/mixins/_drop_element.scss +++ b/public/sass/mixins/_drop_element.scss @@ -16,10 +16,6 @@ max-width: 20rem; border: 1px solid $border-color; - @if $theme-bg != $border-color { - box-shadow: 0 0 15px $border-color; - } - &:before { content: ""; display: block; diff --git a/public/sass/pages/_errorpage.scss b/public/sass/pages/_errorpage.scss index 63676417274..e18306ea05a 100644 --- a/public/sass/pages/_errorpage.scss +++ b/public/sass/pages/_errorpage.scss @@ -43,7 +43,7 @@ line-height: 1rem; } -.error-link {color: $yellow;} +.error-link {color: $orange;} .error-minus { color: #7eb26d; @@ -57,4 +57,4 @@ line-height: 1rem; } -.graph-text {margin: 0;} \ No newline at end of file +.graph-text {margin: 0;} diff --git a/public/test/core/time_series_specs.js b/public/test/core/time_series_specs.js deleted file mode 100644 index 08434e58b81..00000000000 --- a/public/test/core/time_series_specs.js +++ /dev/null @@ -1,278 +0,0 @@ -define([ - 'app/core/time_series' -], function(TimeSeries) { - 'use strict'; - - describe("TimeSeries", function() { - var points, series; - var yAxisFormats = ['short', 'ms']; - var testData; - - beforeEach(function() { - testData = { - alias: 'test', - datapoints: [ - [1,2],[null,3],[10,4],[8,5] - ] - }; - }); - - describe('when getting flot pairs', function() { - it('with connected style, should ignore nulls', function() { - series = new TimeSeries(testData); - points = series.getFlotPairs('connected', yAxisFormats); - expect(points.length).to.be(3); - }); - - it('with null as zero style, should replace nulls with zero', function() { - series = new TimeSeries(testData); - points = series.getFlotPairs('null as zero', yAxisFormats); - expect(points.length).to.be(4); - expect(points[1][1]).to.be(0); - }); - - it('if last is null current should pick next to last', function() { - series = new TimeSeries({ - datapoints: [[10,1], [null, 2]] - }); - series.getFlotPairs('null', yAxisFormats); - expect(series.stats.current).to.be(10); - }); - - it('max value should work for negative values', function() { - series = new TimeSeries({ - datapoints: [[-10,1], [-4, 2]] - }); - series.getFlotPairs('null', yAxisFormats); - expect(series.stats.max).to.be(-4); - }); - - it('average value should ignore nulls', function() { - series = new TimeSeries(testData); - series.getFlotPairs('null', yAxisFormats); - expect(series.stats.avg).to.be(6.333333333333333); - }); - - it('the delta value should account for nulls', function() { - series = new TimeSeries({ - datapoints: [[1,2],[3,3],[null,4],[10,5],[15,6]] - }); - series.getFlotPairs('null', yAxisFormats); - expect(series.stats.delta).to.be(14); - }); - - it('the delta value should account for nulls on first', function() { - series = new TimeSeries({ - datapoints: [[null,2],[1,3],[10,4],[15,5]] - }); - series.getFlotPairs('null', yAxisFormats); - expect(series.stats.delta).to.be(14); - }); - - it('the delta value should account for nulls on last', function() { - series = new TimeSeries({ - datapoints: [[1,2],[5,3],[10,4],[null,5]] - }); - series.getFlotPairs('null', yAxisFormats); - expect(series.stats.delta).to.be(9); - }); - - it('the delta value should account for resets', function() { - series = new TimeSeries({ - datapoints: [[1,2],[5,3],[10,4],[0,5],[10,6]] - }); - series.getFlotPairs('null', yAxisFormats); - expect(series.stats.delta).to.be(19); - }); - - it('the delta value should account for resets on last', function() { - series = new TimeSeries({ - datapoints: [[1,2],[2,3],[10,4],[8,5]] - }); - series.getFlotPairs('null', yAxisFormats); - expect(series.stats.delta).to.be(17); - }); - - it('the range value should be max - min', function() { - series = new TimeSeries(testData); - series.getFlotPairs('null', yAxisFormats); - expect(series.stats.range).to.be(9); - }); - - it('first value should ingone nulls', function() { - series = new TimeSeries(testData); - series.getFlotPairs('null', yAxisFormats); - expect(series.stats.first).to.be(1); - series = new TimeSeries({ - datapoints: [[null,2],[1,3],[10,4],[8,5]] - }); - series.getFlotPairs('null', yAxisFormats); - expect(series.stats.first).to.be(1); - }); - - it('with null as zero style, average value should treat nulls as 0', function() { - series = new TimeSeries(testData); - series.getFlotPairs('null as zero', yAxisFormats); - expect(series.stats.avg).to.be(4.75); - }); - }); - - describe('When checking if ms resolution is needed', function() { - describe('msResolution with second resolution timestamps', function() { - beforeEach(function() { - series = new TimeSeries({datapoints: [[45, 1234567890], [60, 1234567899]]}); - }); - - it('should set hasMsResolution to false', function() { - expect(series.hasMsResolution).to.be(false); - }); - }); - - describe('msResolution with millisecond resolution timestamps', function() { - beforeEach(function() { - series = new TimeSeries({datapoints: [[55, 1236547890001], [90, 1234456709000]]}); - }); - - it('should show millisecond resolution tooltip', function() { - expect(series.hasMsResolution).to.be(true); - }); - }); - - describe('msResolution with millisecond resolution timestamps but with trailing zeroes', function() { - beforeEach(function() { - series = new TimeSeries({datapoints: [[45, 1234567890000], [60, 1234567899000]]}); - }); - - it('should not show millisecond resolution tooltip', function() { - expect(series.hasMsResolution).to.be(false); - }); - }); - }); - - describe('can detect if series contains ms precision', function() { - var fakedata; - - beforeEach(function() { - fakedata = testData; - }); - - it('missing datapoint with ms precision', function() { - fakedata.datapoints[0] = [1337, 1234567890000]; - series = new TimeSeries(fakedata); - expect(series.isMsResolutionNeeded()).to.be(false); - }); - - it('contains datapoint with ms precision', function() { - fakedata.datapoints[0] = [1337, 1236547890001]; - series = new TimeSeries(fakedata); - expect(series.isMsResolutionNeeded()).to.be(true); - }); - }); - - describe('series overrides', function() { - var series; - beforeEach(function() { - series = new TimeSeries(testData); - }); - - describe('fill & points', function() { - beforeEach(function() { - series.alias = 'test'; - series.applySeriesOverrides([{ alias: 'test', fill: 0, points: true }]); - }); - - it('should set fill zero, and enable points', function() { - expect(series.lines.fill).to.be(0.001); - expect(series.points.show).to.be(true); - }); - }); - - describe('series option overrides, bars, true & lines false', function() { - beforeEach(function() { - series.alias = 'test'; - series.applySeriesOverrides([{ alias: 'test', bars: true, lines: false }]); - }); - - it('should disable lines, and enable bars', function() { - expect(series.lines.show).to.be(false); - expect(series.bars.show).to.be(true); - }); - }); - - describe('series option overrides, linewidth, stack', function() { - beforeEach(function() { - series.alias = 'test'; - series.applySeriesOverrides([{ alias: 'test', linewidth: 5, stack: false }]); - }); - - it('should disable stack, and set lineWidth', function() { - expect(series.stack).to.be(false); - expect(series.lines.lineWidth).to.be(5); - }); - }); - - describe('series option overrides, dashes and lineWidth', function() { - beforeEach(function() { - series.alias = 'test'; - series.applySeriesOverrides([{ alias: 'test', linewidth: 5, dashes: true }]); - }); - - it('should enable dashes, set dashes lineWidth to 5 and lines lineWidth to 0', function() { - expect(series.dashes.show).to.be(true); - expect(series.dashes.lineWidth).to.be(5); - expect(series.lines.lineWidth).to.be(0); - }); - }); - - describe('series option overrides, fill below to', function() { - beforeEach(function() { - series.alias = 'test'; - series.applySeriesOverrides([{ alias: 'test', fillBelowTo: 'min' }]); - }); - - it('should disable line fill and add fillBelowTo', function() { - expect(series.fillBelowTo).to.be('min'); - }); - }); - - describe('series option overrides, pointradius, steppedLine', function() { - beforeEach(function() { - series.alias = 'test'; - series.applySeriesOverrides([{ alias: 'test', pointradius: 5, steppedLine: true }]); - }); - - it('should set pointradius, and set steppedLine', function() { - expect(series.points.radius).to.be(5); - expect(series.lines.steps).to.be(true); - }); - }); - - describe('override match on regex', function() { - beforeEach(function() { - series.alias = 'test_01'; - series.applySeriesOverrides([{ alias: '/.*01/', lines: false }]); - }); - - it('should match second series', function() { - expect(series.lines.show).to.be(false); - }); - }); - - describe('override series y-axis, and z-index', function() { - beforeEach(function() { - series.alias = 'test'; - series.applySeriesOverrides([{ alias: 'test', yaxis: 2, zindex: 2 }]); - }); - - it('should set yaxis', function() { - expect(series.yaxis).to.be(2); - }); - - it('should set zindex', function() { - expect(series.zindex).to.be(2); - }); - }); - - }); - }); -}); diff --git a/public/test/index.ts b/public/test/index.ts index 79c911cd32c..33f24331b67 100644 --- a/public/test/index.ts +++ b/public/test/index.ts @@ -21,7 +21,7 @@ angular.module('grafana.directives', []); angular.module('grafana.filters', []); angular.module('grafana.routes', ['ngRoute']); -const context = (require).context('../', true, /specs/); +const context = (require).context('../', true, /specs\.(tsx?|js)/); for (let key of context.keys()) { context(key); } diff --git a/public/test/specs/helpers.js b/public/test/specs/helpers.js index a091b1a3b70..40c4a75423c 100644 --- a/public/test/specs/helpers.js +++ b/public/test/specs/helpers.js @@ -103,7 +103,7 @@ define([ }; this.createService = function(name) { - return window.inject(function($q, $rootScope, $httpBackend, $injector, $location) { + return window.inject(function($q, $rootScope, $httpBackend, $injector, $location, $timeout) { self.$q = $q; self.$rootScope = $rootScope; self.$httpBackend = $httpBackend; @@ -111,6 +111,7 @@ define([ self.$rootScope.onAppEvent = function() {}; self.$rootScope.appEvent = function() {}; + self.$timeout = $timeout; self.service = $injector.get(name); }); diff --git a/public/test/test-main.js b/public/test/test-main.js new file mode 100644 index 00000000000..1347c421a64 --- /dev/null +++ b/public/test/test-main.js @@ -0,0 +1,130 @@ +(function() { + "use strict"; + + // Tun on full stack traces in errors to help debugging + Error.stackTraceLimit=Infinity; + + window.__karma__.loaded = function() {}; + + System.config({ + baseURL: '/base/', + defaultJSExtensions: true, + paths: { + 'mousetrap': 'vendor/npm/mousetrap/mousetrap.js', + 'eventemitter3': 'vendor/npm/eventemitter3/index.js', + 'remarkable': 'vendor/npm/remarkable/dist/remarkable.js', + 'tether': 'vendor/npm/tether/dist/js/tether.js', + 'tether-drop': 'vendor/npm/tether-drop/dist/js/drop.js', + 'moment': 'vendor/moment.js', + "jquery": "vendor/jquery/dist/jquery.js", + 'lodash-src': 'vendor/lodash/dist/lodash.js', + "lodash": 'app/core/lodash_extended.js', + "angular": 'vendor/angular/angular.js', + 'angular-mocks': 'vendor/angular-mocks/angular-mocks.js', + "bootstrap": "vendor/bootstrap/bootstrap.js", + 'angular-route': 'vendor/angular-route/angular-route.js', + 'angular-sanitize': 'vendor/angular-sanitize/angular-sanitize.js', + "angular-ui": "vendor/angular-ui/ui-bootstrap-tpls.js", + "angular-strap": "vendor/angular-other/angular-strap.js", + "angular-dragdrop": "vendor/angular-native-dragdrop/draganddrop.js", + "angular-bindonce": "vendor/angular-bindonce/bindonce.js", + "spectrum": "vendor/spectrum.js", + "bootstrap-tagsinput": "vendor/tagsinput/bootstrap-tagsinput.js", + "jquery.flot": "vendor/flot/jquery.flot", + "jquery.flot.pie": "vendor/flot/jquery.flot.pie", + "jquery.flot.selection": "vendor/flot/jquery.flot.selection", + "jquery.flot.stack": "vendor/flot/jquery.flot.stack", + "jquery.flot.stackpercent": "vendor/flot/jquery.flot.stackpercent", + "jquery.flot.time": "vendor/flot/jquery.flot.time", + "jquery.flot.crosshair": "vendor/flot/jquery.flot.crosshair", + "jquery.flot.fillbelow": "vendor/flot/jquery.flot.fillbelow", + "jquery.flot.gauge": "vendor/flot/jquery.flot.gauge", + "d3": "vendor/d3/d3.js", + "jquery.flot.dashes": "vendor/flot/jquery.flot.dashes", + "twemoji": "vendor/npm/twemoji/2/twemoji.amd.js", + "ace": "vendor/npm/ace-builds/src-noconflict/ace", + }, + + packages: { + app: { + defaultExtension: 'js', + }, + vendor: { + defaultExtension: 'js', + }, + }, + + map: { + }, + + meta: { + 'vendor/angular/angular.js': { + format: 'global', + deps: ['jquery'], + exports: 'angular', + }, + 'vendor/angular-mocks/angular-mocks.js': { + format: 'global', + deps: ['angular'], + }, + 'vendor/npm/eventemitter3/index.js': { + format: 'cjs', + exports: 'EventEmitter' + }, + 'vendor/npm/mousetrap/mousetrap.js': { + format: 'global', + exports: 'Mousetrap' + }, + 'vendor/npm/ace-builds/src-noconflict/ace.js': { + format: 'global', + exports: 'ace' + }, + } + }); + + function file2moduleName(filePath) { + return filePath.replace(/\\/g, '/') + .replace(/^\/base\//, '') + .replace(/\.\w*$/, ''); + } + + function onlySpecFiles(path) { + return /specs.*/.test(path); + } + + window.grafanaBootData = {settings: {}}; + + var modules = ['angular', 'angular-mocks', 'app/app']; + var promises = modules.map(function(name) { + return System.import(name); + }); + + Promise.all(promises).then(function(deps) { + var angular = deps[0]; + + angular.module('grafana', ['ngRoute']); + angular.module('grafana.services', ['ngRoute', '$strap.directives']); + angular.module('grafana.panels', []); + angular.module('grafana.controllers', []); + angular.module('grafana.directives', []); + angular.module('grafana.filters', []); + angular.module('grafana.routes', ['ngRoute']); + + // load specs + return Promise.all( + Object.keys(window.__karma__.files) // All files served by Karma. + .filter(onlySpecFiles) + .map(file2moduleName) + .map(function(path) { + // console.log(path); + return System.import(path); + })); + }).then(function() { + window.__karma__.start(); + }, function(error) { + window.__karma__.error(error.stack || error); + }).catch(function(error) { + window.__karma__.error(error.stack || error); + }); + +})(); diff --git a/public/vendor/flot/jquery.flot.js b/public/vendor/flot/jquery.flot.js index 2f1b60b0830..41f5ea2fd2d 100644 --- a/public/vendor/flot/jquery.flot.js +++ b/public/vendor/flot/jquery.flot.js @@ -602,6 +602,7 @@ Licensed under the MIT license. tickColor: null, // color for the ticks, e.g. "rgba(0,0,0,0.15)" margin: 0, // distance from the canvas edge to the grid labelMargin: 5, // in pixels + eventSectionHeight: 0, // space for event section axisMargin: 8, // in pixels borderWidth: 2, // in pixels minBorderMargin: null, // in pixels, null means taken from points radius @@ -1450,6 +1451,7 @@ Licensed under the MIT license. tickLength = axis.options.tickLength, axisMargin = options.grid.axisMargin, padding = options.grid.labelMargin, + eventSectionPadding = options.grid.eventSectionHeight, innermost = true, outermost = true, first = true, @@ -1490,7 +1492,9 @@ Licensed under the MIT license. padding += +tickLength; if (isXAxis) { + // Add space for event section lh += padding; + lh += eventSectionPadding; if (pos == "bottom") { plotOffset.bottom += lh + axisMargin; @@ -1518,6 +1522,7 @@ Licensed under the MIT license. axis.position = pos; axis.tickLength = tickLength; axis.box.padding = padding; + axis.box.eventSectionPadding = eventSectionPadding; axis.innermost = innermost; } @@ -2225,7 +2230,7 @@ Licensed under the MIT license. halign = "center"; x = plotOffset.left + axis.p2c(tick.v); if (axis.position == "bottom") { - y = box.top + box.padding; + y = box.top + box.padding + box.eventSectionPadding; } else { y = box.top + box.height - box.padding; valign = "bottom"; diff --git a/public/vendor/tagsinput/bootstrap-tagsinput.js b/public/vendor/tagsinput/bootstrap-tagsinput.js index 702b6416962..06f02c10712 100644 --- a/public/vendor/tagsinput/bootstrap-tagsinput.js +++ b/public/vendor/tagsinput/bootstrap-tagsinput.js @@ -28,15 +28,14 @@ this.$element = $(element); this.$element.hide(); + this.widthClass = options.widthClass || 'width-9'; this.isSelect = (element.tagName === 'SELECT'); this.multiple = (this.isSelect && element.hasAttribute('multiple')); this.objectItems = options && options.itemValue; this.placeholderText = element.hasAttribute('placeholder') ? this.$element.attr('placeholder') : ''; - this.inputSize = Math.max(1, this.placeholderText.length); this.$container = $('
'); - this.$input = $('').appendTo(this.$container); + this.$input = $('').appendTo(this.$container); this.$element.after(this.$container); @@ -292,6 +291,13 @@ self.$input.focus(); }, self)); + self.$container.on('blur', 'input', $.proxy(function(event) { + var $input = $(event.target); + self.add($input.val()); + $input.val(''); + event.preventDefault(); + }, self)); + self.$container.on('keydown', 'input', $.proxy(function(event) { var $input = $(event.target), $inputWrapper = self.findInputWrapper(); @@ -352,6 +358,8 @@ // Remove icon clicked self.$container.on('click', '[data-role=remove]', $.proxy(function(event) { self.remove($(event.target).closest('.tag').data('item')); + // Grafana mod, if tags input used in popover the click event will bubble up and hide popover + event.stopPropagation(); }, self)); // Only add existing value as tags when using strings as tags diff --git a/scripts/build/Dockerfile b/scripts/build/Dockerfile index da9a99706bb..89b0a1a46dd 100644 --- a/scripts/build/Dockerfile +++ b/scripts/build/Dockerfile @@ -21,12 +21,14 @@ RUN gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A170311380 RUN curl --silent --location https://rpm.nodesource.com/setup_6.x | bash - && \ yum install -y nodejs --nogpgcheck +ENV GOLANG_VERSION 1.9.1 + RUN wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo && \ yum install -y yarn --nogpgcheck && \ - wget https://storage.googleapis.com/golang/go1.9.linux-amd64.tar.gz && \ - tar -C /usr/local -xzf go1.9.linux-amd64.tar.gz + wget https://storage.googleapis.com/golang/go${GOLANG_VERSION}.linux-amd64.tar.gz && \ + tar -C /usr/local -xzf go${GOLANG_VERSION}.linux-amd64.tar.gz + -ENV GOLANG_VERSION 1.9 ENV PATH /usr/local/go/bin:$PATH RUN mkdir -p /go/src /go/bin && chmod -R 777 /go diff --git a/scripts/webpack/webpack.common.js b/scripts/webpack/webpack.common.js index fd1114e8bbf..736369a4845 100644 --- a/scripts/webpack/webpack.common.js +++ b/scripts/webpack/webpack.common.js @@ -29,13 +29,14 @@ module.exports = { module: { rules: [ { - test: /\.(ts|tsx)$/, + test: /\.tsx?$/, enforce: 'pre', exclude: /node_modules/, use: { loader: 'tslint-loader', options: { - emitErrors: true + emitErrors: true, + typeCheck: false, } } }, @@ -59,10 +60,6 @@ module.exports = { } ] }, - // { - // test : /\.(ico|png|cur|jpg|ttf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/, - // loader : 'file-loader', - // }, { test: /\.html$/, exclude: /index\.template.html/, diff --git a/scripts/webpack/webpack.dev.js b/scripts/webpack/webpack.dev.js index e852f966cf7..037212c26de 100644 --- a/scripts/webpack/webpack.dev.js +++ b/scripts/webpack/webpack.dev.js @@ -10,7 +10,7 @@ const WebpackCleanupPlugin = require('webpack-cleanup-plugin'); const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; module.exports = merge(common, { - devtool: "source-map", + devtool: "eval-source-map", entry: { dark: './public/sass/grafana.dark.scss', diff --git a/tasks/options/copy.js b/tasks/options/copy.js new file mode 100644 index 00000000000..1ef32af6951 --- /dev/null +++ b/tasks/options/copy.js @@ -0,0 +1,45 @@ +module.exports = function(config) { + return { + // copy source to temp, we will minify in place for the dist build + everything_but_less_to_temp: { + cwd: '<%= srcDir %>', + expand: true, + src: ['**/*', '!**/*.less'], + dest: '<%= tempDir %>' + }, + + public_to_gen: { + cwd: '<%= srcDir %>', + expand: true, + src: ['**/*', '!**/*.less'], + dest: '<%= genDir %>' + }, + + node_modules: { + cwd: './node_modules', + expand: true, + src: [ + 'ace-builds/src-noconflict/**/*', + 'eventemitter3/*.js', + 'systemjs/dist/*.js', + 'es6-promise/**/*', + 'es6-shim/*.js', + 'reflect-metadata/*.js', + 'reflect-metadata/*.ts', + 'reflect-metadata/*.d.ts', + 'rxjs/**/*', + 'tether/**/*', + 'tether-drop/**/*', + 'tether-drop/**/*', + 'remarkable/dist/*', + 'remarkable/dist/*', + 'virtual-scroll/**/*', + 'mousetrap/**/*', + 'twemoji/2/twemoji.amd*', + 'twemoji/2/svg/*.svg', + ], + dest: '<%= srcDir %>/vendor/npm' + } + + }; +}; diff --git a/tsconfig.json b/tsconfig.json index 53b7fe13c27..bc9222ac87d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,8 +9,8 @@ "module": "esnext", "declaration": false, "allowSyntheticDefaultImports": true, - "inlineSourceMap": true, - "sourceMap": false, + "inlineSourceMap": false, + "sourceMap": true, "noEmitOnError": false, "emitDecoratorMetadata": false, "experimentalDecorators": false, @@ -18,6 +18,7 @@ "noImplicitThis": false, "noImplicitUseStrict":false, "noImplicitAny": false, + "noUnusedLocals": true, "baseUrl": "public", "paths": { "app": ["app"] @@ -27,7 +28,5 @@ "public/app/**/*.ts", "public/app/**/*.tsx", "public/test/**/*.ts" - ], - "exclude": [ ] } diff --git a/tslint.json b/tslint.json index 1f74b95bfdd..9e3a4f29747 100644 --- a/tslint.json +++ b/tslint.json @@ -2,11 +2,11 @@ "rules": { "no-string-throw": true, "no-unused-expression": true, - "no-duplicate-variable": true, "no-unused-variable": true, + "no-duplicate-variable": true, "curly": true, "class-name": true, - "semicolon": ["always"], + "semicolon": [true, "always", "ignore-bound-class-methods"], "triple-equals": [true, "allow-null-check"], "comment-format": [false, "check-space"], "eofline": true, @@ -26,7 +26,6 @@ ], "no-construct": true, "no-debugger": true, - "no-duplicate-variable": true, "no-empty": false, "no-eval": true, "no-inferrable-types": true, diff --git a/vendor/github.com/lib/pq/array.go b/vendor/github.com/lib/pq/array.go index e7b2145d67b..e4933e22764 100644 --- a/vendor/github.com/lib/pq/array.go +++ b/vendor/github.com/lib/pq/array.go @@ -13,7 +13,7 @@ import ( var typeByteSlice = reflect.TypeOf([]byte{}) var typeDriverValuer = reflect.TypeOf((*driver.Valuer)(nil)).Elem() -var typeSqlScanner = reflect.TypeOf((*sql.Scanner)(nil)).Elem() +var typeSQLScanner = reflect.TypeOf((*sql.Scanner)(nil)).Elem() // Array returns the optimal driver.Valuer and sql.Scanner for an array or // slice of any dimension. @@ -278,7 +278,7 @@ func (GenericArray) evaluateDestination(rt reflect.Type) (reflect.Type, func([]b // TODO calculate the assign function for other types // TODO repeat this section on the element type of arrays or slices (multidimensional) { - if reflect.PtrTo(rt).Implements(typeSqlScanner) { + if reflect.PtrTo(rt).Implements(typeSQLScanner) { // dest is always addressable because it is an element of a slice. assign = func(src []byte, dest reflect.Value) (err error) { ss := dest.Addr().Interface().(sql.Scanner) @@ -587,7 +587,7 @@ func appendArrayElement(b []byte, rv reflect.Value) ([]byte, string, error) { } } - var del string = "," + var del = "," var err error var iv interface{} = rv.Interface() diff --git a/vendor/github.com/lib/pq/conn.go b/vendor/github.com/lib/pq/conn.go index 3747ffcaf84..338a0bc1879 100644 --- a/vendor/github.com/lib/pq/conn.go +++ b/vendor/github.com/lib/pq/conn.go @@ -27,12 +27,12 @@ var ( ErrNotSupported = errors.New("pq: Unsupported command") ErrInFailedTransaction = errors.New("pq: Could not complete operation in a failed transaction") ErrSSLNotSupported = errors.New("pq: SSL is not enabled on the server") - ErrSSLKeyHasWorldPermissions = errors.New("pq: Private key file has group or world access. Permissions should be u=rw (0600) or less.") - ErrCouldNotDetectUsername = errors.New("pq: Could not detect default username. Please provide one explicitly.") + ErrSSLKeyHasWorldPermissions = errors.New("pq: Private key file has group or world access. Permissions should be u=rw (0600) or less") + ErrCouldNotDetectUsername = errors.New("pq: Could not detect default username. Please provide one explicitly") errUnexpectedReady = errors.New("unexpected ReadyForQuery") errNoRowsAffected = errors.New("no RowsAffected available after the empty statement") - errNoLastInsertId = errors.New("no LastInsertId available after the empty statement") + errNoLastInsertID = errors.New("no LastInsertId available after the empty statement") ) type Driver struct{} @@ -131,7 +131,7 @@ type conn struct { } // Handle driver-side settings in parsed connection string. -func (c *conn) handleDriverSettings(o values) (err error) { +func (cn *conn) handleDriverSettings(o values) (err error) { boolSetting := func(key string, val *bool) error { if value, ok := o[key]; ok { if value == "yes" { @@ -145,18 +145,18 @@ func (c *conn) handleDriverSettings(o values) (err error) { return nil } - err = boolSetting("disable_prepared_binary_result", &c.disablePreparedBinaryResult) + err = boolSetting("disable_prepared_binary_result", &cn.disablePreparedBinaryResult) if err != nil { return err } - err = boolSetting("binary_parameters", &c.binaryParameters) + err = boolSetting("binary_parameters", &cn.binaryParameters) if err != nil { return err } return nil } -func (c *conn) handlePgpass(o values) { +func (cn *conn) handlePgpass(o values) { // if a password was supplied, do not process .pgpass if _, ok := o["password"]; ok { return @@ -229,10 +229,10 @@ func (c *conn) handlePgpass(o values) { } } -func (c *conn) writeBuf(b byte) *writeBuf { - c.scratch[0] = b +func (cn *conn) writeBuf(b byte) *writeBuf { + cn.scratch[0] = b return &writeBuf{ - buf: c.scratch[:5], + buf: cn.scratch[:5], pos: 1, } } @@ -310,9 +310,8 @@ func DialOpen(d Dialer, name string) (_ driver.Conn, err error) { u, err := userCurrent() if err != nil { return nil, err - } else { - o["user"] = u } + o["user"] = u } cn := &conn{ @@ -698,7 +697,7 @@ var emptyRows noRows var _ driver.Result = noRows{} func (noRows) LastInsertId() (int64, error) { - return 0, errNoLastInsertId + return 0, errNoLastInsertID } func (noRows) RowsAffected() (int64, error) { @@ -707,7 +706,7 @@ func (noRows) RowsAffected() (int64, error) { // Decides which column formats to use for a prepared statement. The input is // an array of type oids, one element per result column. -func decideColumnFormats(colTyps []oid.Oid, forceText bool) (colFmts []format, colFmtData []byte) { +func decideColumnFormats(colTyps []fieldDesc, forceText bool) (colFmts []format, colFmtData []byte) { if len(colTyps) == 0 { return nil, colFmtDataAllText } @@ -719,8 +718,8 @@ func decideColumnFormats(colTyps []oid.Oid, forceText bool) (colFmts []format, c allBinary := true allText := true - for i, o := range colTyps { - switch o { + for i, t := range colTyps { + switch t.OID { // This is the list of types to use binary mode for when receiving them // through a prepared statement. If a type appears in this list, it // must also be implemented in binaryDecode in encode.go. @@ -840,16 +839,15 @@ func (cn *conn) query(query string, args []driver.Value) (_ *rows, err error) { rows.colNames, rows.colFmts, rows.colTyps = cn.readPortalDescribeResponse() cn.postExecuteWorkaround() return rows, nil - } else { - st := cn.prepareTo(query, "") - st.exec(args) - return &rows{ - cn: cn, - colNames: st.colNames, - colTyps: st.colTyps, - colFmts: st.colFmts, - }, nil } + st := cn.prepareTo(query, "") + st.exec(args) + return &rows{ + cn: cn, + colNames: st.colNames, + colTyps: st.colTyps, + colFmts: st.colFmts, + }, nil } // Implement the optional "Execer" interface for one-shot queries @@ -876,17 +874,16 @@ func (cn *conn) Exec(query string, args []driver.Value) (res driver.Result, err cn.postExecuteWorkaround() res, _, err = cn.readExecuteResponse("Execute") return res, err - } else { - // Use the unnamed statement to defer planning until bind - // time, or else value-based selectivity estimates cannot be - // used. - st := cn.prepareTo(query, "") - r, err := st.Exec(args) - if err != nil { - panic(err) - } - return r, err } + // Use the unnamed statement to defer planning until bind + // time, or else value-based selectivity estimates cannot be + // used. + st := cn.prepareTo(query, "") + r, err := st.Exec(args) + if err != nil { + panic(err) + } + return r, err } func (cn *conn) send(m *writeBuf) { @@ -1147,10 +1144,10 @@ const formatText format = 0 const formatBinary format = 1 // One result-column format code with the value 1 (i.e. all binary). -var colFmtDataAllBinary []byte = []byte{0, 1, 0, 1} +var colFmtDataAllBinary = []byte{0, 1, 0, 1} // No result-column format codes (i.e. all text). -var colFmtDataAllText []byte = []byte{0, 0} +var colFmtDataAllText = []byte{0, 0} type stmt struct { cn *conn @@ -1158,7 +1155,7 @@ type stmt struct { colNames []string colFmts []format colFmtData []byte - colTyps []oid.Oid + colTyps []fieldDesc paramTyps []oid.Oid closed bool } @@ -1321,7 +1318,7 @@ type rows struct { cn *conn finish func() colNames []string - colTyps []oid.Oid + colTyps []fieldDesc colFmts []format done bool rb readBuf @@ -1409,7 +1406,7 @@ func (rs *rows) Next(dest []driver.Value) (err error) { dest[i] = nil continue } - dest[i] = decode(&conn.parameterStatus, rs.rb.next(l), rs.colTyps[i], rs.colFmts[i]) + dest[i] = decode(&conn.parameterStatus, rs.rb.next(l), rs.colTyps[i].OID, rs.colFmts[i]) } return case 'T': @@ -1515,7 +1512,7 @@ func (cn *conn) sendBinaryModeQuery(query string, args []driver.Value) { cn.send(b) } -func (c *conn) processParameterStatus(r *readBuf) { +func (cn *conn) processParameterStatus(r *readBuf) { var err error param := r.string() @@ -1526,13 +1523,13 @@ func (c *conn) processParameterStatus(r *readBuf) { var minor int _, err = fmt.Sscanf(r.string(), "%d.%d.%d", &major1, &major2, &minor) if err == nil { - c.parameterStatus.serverVersion = major1*10000 + major2*100 + minor + cn.parameterStatus.serverVersion = major1*10000 + major2*100 + minor } case "TimeZone": - c.parameterStatus.currentLocation, err = time.LoadLocation(r.string()) + cn.parameterStatus.currentLocation, err = time.LoadLocation(r.string()) if err != nil { - c.parameterStatus.currentLocation = nil + cn.parameterStatus.currentLocation = nil } default: @@ -1540,8 +1537,8 @@ func (c *conn) processParameterStatus(r *readBuf) { } } -func (c *conn) processReadyForQuery(r *readBuf) { - c.txnStatus = transactionStatus(r.byte()) +func (cn *conn) processReadyForQuery(r *readBuf) { + cn.txnStatus = transactionStatus(r.byte()) } func (cn *conn) readReadyForQuery() { @@ -1556,9 +1553,9 @@ func (cn *conn) readReadyForQuery() { } } -func (c *conn) processBackendKeyData(r *readBuf) { - c.processID = r.int32() - c.secretKey = r.int32() +func (cn *conn) processBackendKeyData(r *readBuf) { + cn.processID = r.int32() + cn.secretKey = r.int32() } func (cn *conn) readParseResponse() { @@ -1576,7 +1573,7 @@ func (cn *conn) readParseResponse() { } } -func (cn *conn) readStatementDescribeResponse() (paramTyps []oid.Oid, colNames []string, colTyps []oid.Oid) { +func (cn *conn) readStatementDescribeResponse() (paramTyps []oid.Oid, colNames []string, colTyps []fieldDesc) { for { t, r := cn.recv1() switch t { @@ -1602,7 +1599,7 @@ func (cn *conn) readStatementDescribeResponse() (paramTyps []oid.Oid, colNames [ } } -func (cn *conn) readPortalDescribeResponse() (colNames []string, colFmts []format, colTyps []oid.Oid) { +func (cn *conn) readPortalDescribeResponse() (colNames []string, colFmts []format, colTyps []fieldDesc) { t, r := cn.recv1() switch t { case 'T': @@ -1698,31 +1695,33 @@ func (cn *conn) readExecuteResponse(protocolState string) (res driver.Result, co } } -func parseStatementRowDescribe(r *readBuf) (colNames []string, colTyps []oid.Oid) { +func parseStatementRowDescribe(r *readBuf) (colNames []string, colTyps []fieldDesc) { n := r.int16() colNames = make([]string, n) - colTyps = make([]oid.Oid, n) + colTyps = make([]fieldDesc, n) for i := range colNames { colNames[i] = r.string() r.next(6) - colTyps[i] = r.oid() - r.next(6) + colTyps[i].OID = r.oid() + colTyps[i].Len = r.int16() + colTyps[i].Mod = r.int32() // format code not known when describing a statement; always 0 r.next(2) } return } -func parsePortalRowDescribe(r *readBuf) (colNames []string, colFmts []format, colTyps []oid.Oid) { +func parsePortalRowDescribe(r *readBuf) (colNames []string, colFmts []format, colTyps []fieldDesc) { n := r.int16() colNames = make([]string, n) colFmts = make([]format, n) - colTyps = make([]oid.Oid, n) + colTyps = make([]fieldDesc, n) for i := range colNames { colNames[i] = r.string() r.next(6) - colTyps[i] = r.oid() - r.next(6) + colTyps[i].OID = r.oid() + colTyps[i].Len = r.int16() + colTyps[i].Mod = r.int32() colFmts[i] = format(r.int16()) } return diff --git a/vendor/github.com/lib/pq/hstore/hstore_test.go b/vendor/github.com/lib/pq/hstore/hstore_test.go deleted file mode 100644 index 8e61e69958f..00000000000 --- a/vendor/github.com/lib/pq/hstore/hstore_test.go +++ /dev/null @@ -1,147 +0,0 @@ -package hstore - -import ( - "database/sql" - _ "github.com/lib/pq" - "os" - "testing" -) - -type Fatalistic interface { - Fatal(args ...interface{}) -} - -func openTestConn(t Fatalistic) *sql.DB { - datname := os.Getenv("PGDATABASE") - sslmode := os.Getenv("PGSSLMODE") - - if datname == "" { - os.Setenv("PGDATABASE", "pqgotest") - } - - if sslmode == "" { - os.Setenv("PGSSLMODE", "disable") - } - - conn, err := sql.Open("postgres", "") - if err != nil { - t.Fatal(err) - } - - return conn -} - -func TestHstore(t *testing.T) { - db := openTestConn(t) - defer db.Close() - - // quitely create hstore if it doesn't exist - _, err := db.Exec("CREATE EXTENSION IF NOT EXISTS hstore") - if err != nil { - t.Skipf("Skipping hstore tests - hstore extension create failed: %s", err.Error()) - } - - hs := Hstore{} - - // test for null-valued hstores - err = db.QueryRow("SELECT NULL::hstore").Scan(&hs) - if err != nil { - t.Fatal(err) - } - if hs.Map != nil { - t.Fatalf("expected null map") - } - - err = db.QueryRow("SELECT $1::hstore", hs).Scan(&hs) - if err != nil { - t.Fatalf("re-query null map failed: %s", err.Error()) - } - if hs.Map != nil { - t.Fatalf("expected null map") - } - - // test for empty hstores - err = db.QueryRow("SELECT ''::hstore").Scan(&hs) - if err != nil { - t.Fatal(err) - } - if hs.Map == nil { - t.Fatalf("expected empty map, got null map") - } - if len(hs.Map) != 0 { - t.Fatalf("expected empty map, got len(map)=%d", len(hs.Map)) - } - - err = db.QueryRow("SELECT $1::hstore", hs).Scan(&hs) - if err != nil { - t.Fatalf("re-query empty map failed: %s", err.Error()) - } - if hs.Map == nil { - t.Fatalf("expected empty map, got null map") - } - if len(hs.Map) != 0 { - t.Fatalf("expected empty map, got len(map)=%d", len(hs.Map)) - } - - // a few example maps to test out - hsOnePair := Hstore{ - Map: map[string]sql.NullString{ - "key1": {"value1", true}, - }, - } - - hsThreePairs := Hstore{ - Map: map[string]sql.NullString{ - "key1": {"value1", true}, - "key2": {"value2", true}, - "key3": {"value3", true}, - }, - } - - hsSmorgasbord := Hstore{ - Map: map[string]sql.NullString{ - "nullstring": {"NULL", true}, - "actuallynull": {"", false}, - "NULL": {"NULL string key", true}, - "withbracket": {"value>42", true}, - "withequal": {"value=42", true}, - `"withquotes1"`: {`this "should" be fine`, true}, - `"withquotes"2"`: {`this "should\" also be fine`, true}, - "embedded1": {"value1=>x1", true}, - "embedded2": {`"value2"=>x2`, true}, - "withnewlines": {"\n\nvalue\t=>2", true}, - "<>": {`this, "should,\" also, => be fine`, true}, - }, - } - - // test encoding in query params, then decoding during Scan - testBidirectional := func(h Hstore) { - err = db.QueryRow("SELECT $1::hstore", h).Scan(&hs) - if err != nil { - t.Fatalf("re-query %d-pair map failed: %s", len(h.Map), err.Error()) - } - if hs.Map == nil { - t.Fatalf("expected %d-pair map, got null map", len(h.Map)) - } - if len(hs.Map) != len(h.Map) { - t.Fatalf("expected %d-pair map, got len(map)=%d", len(h.Map), len(hs.Map)) - } - - for key, val := range hs.Map { - otherval, found := h.Map[key] - if !found { - t.Fatalf(" key '%v' not found in %d-pair map", key, len(h.Map)) - } - if otherval.Valid != val.Valid { - t.Fatalf(" value %v <> %v in %d-pair map", otherval, val, len(h.Map)) - } - if otherval.String != val.String { - t.Fatalf(" value '%v' <> '%v' in %d-pair map", otherval.String, val.String, len(h.Map)) - } - } - } - - testBidirectional(hsOnePair) - testBidirectional(hsThreePairs) - testBidirectional(hsSmorgasbord) -} diff --git a/vendor/github.com/lib/pq/listen_example/doc.go b/vendor/github.com/lib/pq/listen_example/doc.go index 34496f4f960..80f0a9b9701 100644 --- a/vendor/github.com/lib/pq/listen_example/doc.go +++ b/vendor/github.com/lib/pq/listen_example/doc.go @@ -18,11 +18,11 @@ mechanism to avoid polling the database while waiting for more work to arrive. package main import ( - "github.com/lib/pq" - "database/sql" "fmt" "time" + + "github.com/lib/pq" ) func doWork(db *sql.DB, work int64) { @@ -51,21 +51,15 @@ mechanism to avoid polling the database while waiting for more work to arrive. } func waitForNotification(l *pq.Listener) { - for { - select { - case <-l.Notify: - fmt.Println("received notification, new work available") - return - case <-time.After(90 * time.Second): - go func() { - l.Ping() - }() - // Check if there's more work available, just in case it takes - // a while for the Listener to notice connection loss and - // reconnect. - fmt.Println("received no work for 90 seconds, checking for new work") - return - } + select { + case <-l.Notify: + fmt.Println("received notification, new work available") + case <-time.After(90 * time.Second): + go l.Ping() + // Check if there's more work available, just in case it takes + // a while for the Listener to notice connection loss and + // reconnect. + fmt.Println("received no work for 90 seconds, checking for new work") } } diff --git a/vendor/github.com/lib/pq/oid/gen.go b/vendor/github.com/lib/pq/oid/gen.go deleted file mode 100644 index f16a51c0b41..00000000000 --- a/vendor/github.com/lib/pq/oid/gen.go +++ /dev/null @@ -1,74 +0,0 @@ -// +build ignore - -// Generate the table of OID values -// Run with 'go run gen.go'. -package main - -import ( - "fmt" - "log" - "os" - "os/exec" - - "database/sql" - _ "github.com/lib/pq" -) - -func main() { - datname := os.Getenv("PGDATABASE") - sslmode := os.Getenv("PGSSLMODE") - - if datname == "" { - os.Setenv("PGDATABASE", "pqgotest") - } - - if sslmode == "" { - os.Setenv("PGSSLMODE", "disable") - } - - db, err := sql.Open("postgres", "") - if err != nil { - log.Fatal(err) - } - cmd := exec.Command("gofmt") - cmd.Stderr = os.Stderr - w, err := cmd.StdinPipe() - if err != nil { - log.Fatal(err) - } - f, err := os.Create("types.go") - if err != nil { - log.Fatal(err) - } - cmd.Stdout = f - err = cmd.Start() - if err != nil { - log.Fatal(err) - } - fmt.Fprintln(w, "// generated by 'go run gen.go'; do not edit") - fmt.Fprintln(w, "\npackage oid") - fmt.Fprintln(w, "const (") - rows, err := db.Query(` - SELECT typname, oid - FROM pg_type WHERE oid < 10000 - ORDER BY oid; - `) - if err != nil { - log.Fatal(err) - } - var name string - var oid int - for rows.Next() { - err = rows.Scan(&name, &oid) - if err != nil { - log.Fatal(err) - } - fmt.Fprintf(w, "T_%s Oid = %d\n", name, oid) - } - if err = rows.Err(); err != nil { - log.Fatal(err) - } - fmt.Fprintln(w, ")") - w.Close() - cmd.Wait() -} diff --git a/vendor/github.com/lib/pq/oid/types.go b/vendor/github.com/lib/pq/oid/types.go index 03df05a617a..ecc84c2c862 100644 --- a/vendor/github.com/lib/pq/oid/types.go +++ b/vendor/github.com/lib/pq/oid/types.go @@ -1,4 +1,4 @@ -// generated by 'go run gen.go'; do not edit +// Code generated by gen.go. DO NOT EDIT. package oid @@ -18,6 +18,7 @@ const ( T_xid Oid = 28 T_cid Oid = 29 T_oidvector Oid = 30 + T_pg_ddl_command Oid = 32 T_pg_type Oid = 71 T_pg_attribute Oid = 75 T_pg_proc Oid = 81 @@ -28,6 +29,7 @@ const ( T_pg_node_tree Oid = 194 T__json Oid = 199 T_smgr Oid = 210 + T_index_am_handler Oid = 325 T_point Oid = 600 T_lseg Oid = 601 T_path Oid = 602 @@ -133,6 +135,9 @@ const ( T__uuid Oid = 2951 T_txid_snapshot Oid = 2970 T_fdw_handler Oid = 3115 + T_pg_lsn Oid = 3220 + T__pg_lsn Oid = 3221 + T_tsm_handler Oid = 3310 T_anyenum Oid = 3500 T_tsvector Oid = 3614 T_tsquery Oid = 3615 @@ -144,6 +149,8 @@ const ( T__regconfig Oid = 3735 T_regdictionary Oid = 3769 T__regdictionary Oid = 3770 + T_jsonb Oid = 3802 + T__jsonb Oid = 3807 T_anyrange Oid = 3831 T_event_trigger Oid = 3838 T_int4range Oid = 3904 @@ -158,4 +165,179 @@ const ( T__daterange Oid = 3913 T_int8range Oid = 3926 T__int8range Oid = 3927 + T_pg_shseclabel Oid = 4066 + T_regnamespace Oid = 4089 + T__regnamespace Oid = 4090 + T_regrole Oid = 4096 + T__regrole Oid = 4097 ) + +var TypeName = map[Oid]string{ + T_bool: "BOOL", + T_bytea: "BYTEA", + T_char: "CHAR", + T_name: "NAME", + T_int8: "INT8", + T_int2: "INT2", + T_int2vector: "INT2VECTOR", + T_int4: "INT4", + T_regproc: "REGPROC", + T_text: "TEXT", + T_oid: "OID", + T_tid: "TID", + T_xid: "XID", + T_cid: "CID", + T_oidvector: "OIDVECTOR", + T_pg_ddl_command: "PG_DDL_COMMAND", + T_pg_type: "PG_TYPE", + T_pg_attribute: "PG_ATTRIBUTE", + T_pg_proc: "PG_PROC", + T_pg_class: "PG_CLASS", + T_json: "JSON", + T_xml: "XML", + T__xml: "_XML", + T_pg_node_tree: "PG_NODE_TREE", + T__json: "_JSON", + T_smgr: "SMGR", + T_index_am_handler: "INDEX_AM_HANDLER", + T_point: "POINT", + T_lseg: "LSEG", + T_path: "PATH", + T_box: "BOX", + T_polygon: "POLYGON", + T_line: "LINE", + T__line: "_LINE", + T_cidr: "CIDR", + T__cidr: "_CIDR", + T_float4: "FLOAT4", + T_float8: "FLOAT8", + T_abstime: "ABSTIME", + T_reltime: "RELTIME", + T_tinterval: "TINTERVAL", + T_unknown: "UNKNOWN", + T_circle: "CIRCLE", + T__circle: "_CIRCLE", + T_money: "MONEY", + T__money: "_MONEY", + T_macaddr: "MACADDR", + T_inet: "INET", + T__bool: "_BOOL", + T__bytea: "_BYTEA", + T__char: "_CHAR", + T__name: "_NAME", + T__int2: "_INT2", + T__int2vector: "_INT2VECTOR", + T__int4: "_INT4", + T__regproc: "_REGPROC", + T__text: "_TEXT", + T__tid: "_TID", + T__xid: "_XID", + T__cid: "_CID", + T__oidvector: "_OIDVECTOR", + T__bpchar: "_BPCHAR", + T__varchar: "_VARCHAR", + T__int8: "_INT8", + T__point: "_POINT", + T__lseg: "_LSEG", + T__path: "_PATH", + T__box: "_BOX", + T__float4: "_FLOAT4", + T__float8: "_FLOAT8", + T__abstime: "_ABSTIME", + T__reltime: "_RELTIME", + T__tinterval: "_TINTERVAL", + T__polygon: "_POLYGON", + T__oid: "_OID", + T_aclitem: "ACLITEM", + T__aclitem: "_ACLITEM", + T__macaddr: "_MACADDR", + T__inet: "_INET", + T_bpchar: "BPCHAR", + T_varchar: "VARCHAR", + T_date: "DATE", + T_time: "TIME", + T_timestamp: "TIMESTAMP", + T__timestamp: "_TIMESTAMP", + T__date: "_DATE", + T__time: "_TIME", + T_timestamptz: "TIMESTAMPTZ", + T__timestamptz: "_TIMESTAMPTZ", + T_interval: "INTERVAL", + T__interval: "_INTERVAL", + T__numeric: "_NUMERIC", + T_pg_database: "PG_DATABASE", + T__cstring: "_CSTRING", + T_timetz: "TIMETZ", + T__timetz: "_TIMETZ", + T_bit: "BIT", + T__bit: "_BIT", + T_varbit: "VARBIT", + T__varbit: "_VARBIT", + T_numeric: "NUMERIC", + T_refcursor: "REFCURSOR", + T__refcursor: "_REFCURSOR", + T_regprocedure: "REGPROCEDURE", + T_regoper: "REGOPER", + T_regoperator: "REGOPERATOR", + T_regclass: "REGCLASS", + T_regtype: "REGTYPE", + T__regprocedure: "_REGPROCEDURE", + T__regoper: "_REGOPER", + T__regoperator: "_REGOPERATOR", + T__regclass: "_REGCLASS", + T__regtype: "_REGTYPE", + T_record: "RECORD", + T_cstring: "CSTRING", + T_any: "ANY", + T_anyarray: "ANYARRAY", + T_void: "VOID", + T_trigger: "TRIGGER", + T_language_handler: "LANGUAGE_HANDLER", + T_internal: "INTERNAL", + T_opaque: "OPAQUE", + T_anyelement: "ANYELEMENT", + T__record: "_RECORD", + T_anynonarray: "ANYNONARRAY", + T_pg_authid: "PG_AUTHID", + T_pg_auth_members: "PG_AUTH_MEMBERS", + T__txid_snapshot: "_TXID_SNAPSHOT", + T_uuid: "UUID", + T__uuid: "_UUID", + T_txid_snapshot: "TXID_SNAPSHOT", + T_fdw_handler: "FDW_HANDLER", + T_pg_lsn: "PG_LSN", + T__pg_lsn: "_PG_LSN", + T_tsm_handler: "TSM_HANDLER", + T_anyenum: "ANYENUM", + T_tsvector: "TSVECTOR", + T_tsquery: "TSQUERY", + T_gtsvector: "GTSVECTOR", + T__tsvector: "_TSVECTOR", + T__gtsvector: "_GTSVECTOR", + T__tsquery: "_TSQUERY", + T_regconfig: "REGCONFIG", + T__regconfig: "_REGCONFIG", + T_regdictionary: "REGDICTIONARY", + T__regdictionary: "_REGDICTIONARY", + T_jsonb: "JSONB", + T__jsonb: "_JSONB", + T_anyrange: "ANYRANGE", + T_event_trigger: "EVENT_TRIGGER", + T_int4range: "INT4RANGE", + T__int4range: "_INT4RANGE", + T_numrange: "NUMRANGE", + T__numrange: "_NUMRANGE", + T_tsrange: "TSRANGE", + T__tsrange: "_TSRANGE", + T_tstzrange: "TSTZRANGE", + T__tstzrange: "_TSTZRANGE", + T_daterange: "DATERANGE", + T__daterange: "_DATERANGE", + T_int8range: "INT8RANGE", + T__int8range: "_INT8RANGE", + T_pg_shseclabel: "PG_SHSECLABEL", + T_regnamespace: "REGNAMESPACE", + T__regnamespace: "_REGNAMESPACE", + T_regrole: "REGROLE", + T__regrole: "_REGROLE", +} diff --git a/vendor/github.com/lib/pq/rows.go b/vendor/github.com/lib/pq/rows.go new file mode 100644 index 00000000000..c6aa5b9a36a --- /dev/null +++ b/vendor/github.com/lib/pq/rows.go @@ -0,0 +1,93 @@ +package pq + +import ( + "math" + "reflect" + "time" + + "github.com/lib/pq/oid" +) + +const headerSize = 4 + +type fieldDesc struct { + // The object ID of the data type. + OID oid.Oid + // The data type size (see pg_type.typlen). + // Note that negative values denote variable-width types. + Len int + // The type modifier (see pg_attribute.atttypmod). + // The meaning of the modifier is type-specific. + Mod int +} + +func (fd fieldDesc) Type() reflect.Type { + switch fd.OID { + case oid.T_int8: + return reflect.TypeOf(int64(0)) + case oid.T_int4: + return reflect.TypeOf(int32(0)) + case oid.T_int2: + return reflect.TypeOf(int16(0)) + case oid.T_varchar, oid.T_text: + return reflect.TypeOf("") + case oid.T_bool: + return reflect.TypeOf(false) + case oid.T_date, oid.T_time, oid.T_timetz, oid.T_timestamp, oid.T_timestamptz: + return reflect.TypeOf(time.Time{}) + case oid.T_bytea: + return reflect.TypeOf([]byte(nil)) + default: + return reflect.TypeOf(new(interface{})).Elem() + } +} + +func (fd fieldDesc) Name() string { + return oid.TypeName[fd.OID] +} + +func (fd fieldDesc) Length() (length int64, ok bool) { + switch fd.OID { + case oid.T_text, oid.T_bytea: + return math.MaxInt64, true + case oid.T_varchar, oid.T_bpchar: + return int64(fd.Mod - headerSize), true + default: + return 0, false + } +} + +func (fd fieldDesc) PrecisionScale() (precision, scale int64, ok bool) { + switch fd.OID { + case oid.T_numeric, oid.T__numeric: + mod := fd.Mod - headerSize + precision = int64((mod >> 16) & 0xffff) + scale = int64(mod & 0xffff) + return precision, scale, true + default: + return 0, 0, false + } +} + +// ColumnTypeScanType returns the value type that can be used to scan types into. +func (rs *rows) ColumnTypeScanType(index int) reflect.Type { + return rs.colTyps[index].Type() +} + +// ColumnTypeDatabaseTypeName return the database system type name. +func (rs *rows) ColumnTypeDatabaseTypeName(index int) string { + return rs.colTyps[index].Name() +} + +// ColumnTypeLength returns the length of the column type if the column is a +// variable length type. If the column is not a variable length type ok +// should return false. +func (rs *rows) ColumnTypeLength(index int) (length int64, ok bool) { + return rs.colTyps[index].Length() +} + +// ColumnTypePrecisionScale should return the precision and scale for decimal +// types. If not applicable, ok should be false. +func (rs *rows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) { + return rs.colTyps[index].PrecisionScale() +} diff --git a/vendor/vendor.json b/vendor/vendor.json index 273afc6e968..558a11e6921 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -461,10 +461,31 @@ "revisionTime": "2017-02-10T14:05:23Z" }, { - "checksumSHA1": "ZAj/o03zG8Ui4mZ4XmzU4yyKC04=", + "checksumSHA1": "RYMOEINLFNWIJk8aKNifSlPhg9U=", "path": "github.com/lib/pq", - "revision": "dd1fe2071026ce53f36a39112e645b4d4f5793a4", - "revisionTime": "2017-07-07T05:36:02Z" + "revision": "23da1db4f16d9658a86ae9b717c245fc078f10f1", + "revisionTime": "2017-09-18T17:50:43Z" + }, + { + "checksumSHA1": "jaCQF1par6Jl8g+V2Cgp0n/0wSc=", + "origin": "github.com/grafana/grafana/vendor/github.com/lib/pq/hstore", + "path": "github.com/lib/pq/hstore", + "revision": "23da1db4f16d9658a86ae9b717c245fc078f10f1", + "revisionTime": "2017-09-18T17:50:43Z" + }, + { + "checksumSHA1": "mJHrY33tDs2MRhHt+XunkRF/5ek=", + "origin": "github.com/grafana/grafana/vendor/github.com/lib/pq/listen_example", + "path": "github.com/lib/pq/listen_example", + "revision": "23da1db4f16d9658a86ae9b717c245fc078f10f1", + "revisionTime": "2017-09-18T17:50:43Z" + }, + { + "checksumSHA1": "AU3fA8Sm33Vj9PBoRPSeYfxLRuE=", + "origin": "github.com/grafana/grafana/vendor/github.com/lib/pq/oid", + "path": "github.com/lib/pq/oid", + "revision": "23da1db4f16d9658a86ae9b717c245fc078f10f1", + "revisionTime": "2017-09-18T17:50:43Z" }, { "checksumSHA1": "bKMZjd2wPw13VwoE7mBeSv5djFA=", diff --git a/yarn.lock b/yarn.lock index 32e25dd691e..5c47f42682e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1700,7 +1700,7 @@ combined-stream@^1.0.5, combined-stream@~1.0.5: dependencies: delayed-stream "~1.0.0" -commander@2.11.x, commander@~2.11.0: +commander@2.11.0, commander@2.11.x, commander@~2.11.0: version "2.11.0" resolved "https://registry.yarnpkg.com/commander/-/commander-2.11.0.tgz#157152fd1e7a6c8d98a5b715cf376df928004563" @@ -1710,7 +1710,7 @@ commander@2.8.x: dependencies: graceful-readlink ">= 1.0.0" -commander@2.9.0, commander@2.9.x, commander@^2.8.1, commander@^2.9.0, commander@~2.9.0: +commander@2.9.x, commander@^2.8.1, commander@^2.9.0, commander@~2.9.0: version "2.9.0" resolved "https://registry.yarnpkg.com/commander/-/commander-2.9.0.tgz#9c99094176e12240cb22d6c5146098400fe0f7d4" dependencies: @@ -2134,9 +2134,9 @@ debug@2.3.3: dependencies: ms "0.7.2" -debug@2.6.8: - version "2.6.8" - resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.8.tgz#e731531ca2ede27d188222427da17821d68ff4fc" +debug@3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261" dependencies: ms "2.0.0" @@ -2255,14 +2255,18 @@ di@^0.0.1: version "0.0.1" resolved "https://registry.yarnpkg.com/di/-/di-0.0.1.tgz#806649326ceaa7caa3306d75d985ea2748ba913c" -diff@3.2.0, diff@^3.2.0: - version "3.2.0" - resolved "https://registry.yarnpkg.com/diff/-/diff-3.2.0.tgz#c9ce393a4b7cbd0b058a725c93df299027868ff9" +diff@3.3.1: + version "3.3.1" + resolved "https://registry.yarnpkg.com/diff/-/diff-3.3.1.tgz#aa8567a6eed03c531fc89d3f711cd0e5259dec75" diff@^2.0.2: version "2.2.3" resolved "https://registry.yarnpkg.com/diff/-/diff-2.2.3.tgz#60eafd0d28ee906e4e8ff0a52c1229521033bf99" +diff@^3.2.0: + version "3.2.0" + resolved "https://registry.yarnpkg.com/diff/-/diff-3.2.0.tgz#c9ce393a4b7cbd0b058a725c93df299027868ff9" + diffie-hellman@^5.0.0: version "5.0.2" resolved "https://registry.yarnpkg.com/diffie-hellman/-/diffie-hellman-5.0.2.tgz#b5835739270cfe26acf632099fded2a07f209e5e" @@ -3352,14 +3356,14 @@ glob-parent@^2.0.0: dependencies: is-glob "^2.0.0" -glob@7.1.1: - version "7.1.1" - resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.1.tgz#805211df04faaf1c63a3600306cdf5ade50b2ec8" +glob@7.1.2, glob@^7.0.0, glob@^7.0.3, glob@^7.0.5, glob@^7.1.1, glob@^7.1.2, glob@~7.1.1, glob@~7.1.2: + version "7.1.2" + resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15" dependencies: fs.realpath "^1.0.0" inflight "^1.0.4" inherits "2" - minimatch "^3.0.2" + minimatch "^3.0.4" once "^1.3.0" path-is-absolute "^1.0.0" @@ -3373,17 +3377,6 @@ glob@^5.0.1, glob@^5.0.15, glob@~5.0.0: once "^1.3.0" path-is-absolute "^1.0.0" -glob@^7.0.0, glob@^7.0.3, glob@^7.0.5, glob@^7.1.1, glob@^7.1.2, glob@~7.1.1, glob@~7.1.2: - version "7.1.2" - resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15" - dependencies: - fs.realpath "^1.0.0" - inflight "^1.0.4" - inherits "2" - minimatch "^3.0.4" - once "^1.3.0" - path-is-absolute "^1.0.0" - glob@~7.0.0: version "7.0.6" resolved "https://registry.yarnpkg.com/glob/-/glob-7.0.6.tgz#211bafaf49e525b8cd93260d14ab136152b3f57a" @@ -3454,9 +3447,9 @@ graceful-fs@^4.1.0, graceful-fs@^4.1.11, graceful-fs@^4.1.2, graceful-fs@^4.1.6, version "1.0.1" resolved "https://registry.yarnpkg.com/graceful-readlink/-/graceful-readlink-1.0.1.tgz#4cafad76bc62f02fa039b2f94e9a3dd3a391a725" -growl@1.9.2: - version "1.9.2" - resolved "https://registry.yarnpkg.com/growl/-/growl-1.9.2.tgz#0ea7743715db8d8de2c5ede1775e1b45ac85c02f" +growl@1.10.3: + version "1.10.3" + resolved "https://registry.yarnpkg.com/growl/-/growl-1.10.3.tgz#1926ba90cf3edfe2adb4927f5880bc22c66c790f" grunt-angular-templates@^1.1.0: version "1.1.0" @@ -3772,7 +3765,7 @@ hawk@3.1.3, hawk@~3.1.3: hoek "2.x.x" sntp "1.x.x" -he@1.1.x: +he@1.1.1, he@1.1.x: version "1.1.1" resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd" @@ -4937,21 +4930,6 @@ lockfile@~1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/lockfile/-/lockfile-1.0.3.tgz#2638fc39a0331e9cac1a04b71799931c9c50df79" -lodash._baseassign@^3.0.0: - version "3.2.0" - resolved "https://registry.yarnpkg.com/lodash._baseassign/-/lodash._baseassign-3.2.0.tgz#8c38a099500f215ad09e59f1722fd0c52bfe0a4e" - dependencies: - lodash._basecopy "^3.0.0" - lodash.keys "^3.0.0" - -lodash._basecopy@^3.0.0: - version "3.0.1" - resolved "https://registry.yarnpkg.com/lodash._basecopy/-/lodash._basecopy-3.0.1.tgz#8da0e6a876cf344c0ad8a54882111dd3c5c7ca36" - -lodash._basecreate@^3.0.0: - version "3.0.3" - resolved "https://registry.yarnpkg.com/lodash._basecreate/-/lodash._basecreate-3.0.3.tgz#1bc661614daa7fc311b7d03bf16806a0213cf821" - lodash._baseuniq@~4.6.0: version "4.6.0" resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8" @@ -4963,14 +4941,6 @@ lodash._createset@~4.0.0: version "4.0.3" resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26" -lodash._getnative@^3.0.0: - version "3.9.1" - resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5" - -lodash._isiterateecall@^3.0.0: - version "3.0.9" - resolved "https://registry.yarnpkg.com/lodash._isiterateecall/-/lodash._isiterateecall-3.0.9.tgz#5203ad7ba425fae842460e696db9cf3e6aac057c" - lodash._root@~3.0.0: version "3.0.1" resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692" @@ -4991,38 +4961,14 @@ lodash.clonedeep@^4.3.2, lodash.clonedeep@~4.5.0: version "4.5.0" resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef" -lodash.create@3.1.1: - version "3.1.1" - resolved "https://registry.yarnpkg.com/lodash.create/-/lodash.create-3.1.1.tgz#d7f2849f0dbda7e04682bb8cd72ab022461debe7" - dependencies: - lodash._baseassign "^3.0.0" - lodash._basecreate "^3.0.0" - lodash._isiterateecall "^3.0.0" - lodash.flattendeep@^4.4.0: version "4.4.0" resolved "https://registry.yarnpkg.com/lodash.flattendeep/-/lodash.flattendeep-4.4.0.tgz#fb030917f86a3134e5bc9bec0d69e0013ddfedb2" -lodash.isarguments@^3.0.0: - version "3.1.0" - resolved "https://registry.yarnpkg.com/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz#2f573d85c6a24289ff00663b491c1d338ff3458a" - -lodash.isarray@^3.0.0: - version "3.0.4" - resolved "https://registry.yarnpkg.com/lodash.isarray/-/lodash.isarray-3.0.4.tgz#79e4eb88c36a8122af86f844aa9bcd851b5fbb55" - lodash.kebabcase@^4.0.0: version "4.1.1" resolved "https://registry.yarnpkg.com/lodash.kebabcase/-/lodash.kebabcase-4.1.1.tgz#8489b1cb0d29ff88195cceca448ff6d6cc295c36" -lodash.keys@^3.0.0: - version "3.1.2" - resolved "https://registry.yarnpkg.com/lodash.keys/-/lodash.keys-3.1.2.tgz#4dbc0472b156be50a0b286855d1bd0b0c656098a" - dependencies: - lodash._getnative "^3.0.0" - lodash.isarguments "^3.0.0" - lodash.isarray "^3.0.0" - lodash.memoize@^4.1.2: version "4.1.2" resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe" @@ -5393,21 +5339,20 @@ mkdirp@0.5.1, mkdirp@0.5.x, mkdirp@0.x.x, "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdir dependencies: minimist "0.0.8" -mocha@3.5.0: - version "3.5.0" - resolved "https://registry.yarnpkg.com/mocha/-/mocha-3.5.0.tgz#1328567d2717f997030f8006234bce9b8cd72465" +mocha@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/mocha/-/mocha-4.0.1.tgz#0aee5a95cf69a4618820f5e51fa31717117daf1b" dependencies: browser-stdout "1.3.0" - commander "2.9.0" - debug "2.6.8" - diff "3.2.0" + commander "2.11.0" + debug "3.1.0" + diff "3.3.1" escape-string-regexp "1.0.5" - glob "7.1.1" - growl "1.9.2" - json3 "3.3.2" - lodash.create "3.1.1" + glob "7.1.2" + growl "1.10.3" + he "1.1.1" mkdirp "0.5.1" - supports-color "3.1.2" + supports-color "4.4.0" moment@^2.18.1: version "2.18.1" @@ -8010,11 +7955,11 @@ strip-json-comments@~2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a" -supports-color@3.1.2: - version "3.1.2" - resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-3.1.2.tgz#72a262894d9d408b956ca05ff37b2ed8a6e2a2d5" +supports-color@4.4.0, supports-color@^4.0.0, supports-color@^4.2.1, supports-color@^4.4.0: + version "4.4.0" + resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-4.4.0.tgz#883f7ddabc165142b2a61427f3352ded195d1a3e" dependencies: - has-flag "^1.0.0" + has-flag "^2.0.0" supports-color@^2.0.0: version "2.0.0" @@ -8026,12 +7971,6 @@ supports-color@^3.1.0, supports-color@^3.2.3: dependencies: has-flag "^1.0.0" -supports-color@^4.0.0, supports-color@^4.2.1, supports-color@^4.4.0: - version "4.4.0" - resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-4.4.0.tgz#883f7ddabc165142b2a61427f3352ded195d1a3e" - dependencies: - has-flag "^2.0.0" - svgo@^0.7.0: version "0.7.2" resolved "https://registry.yarnpkg.com/svgo/-/svgo-0.7.2.tgz#9f5772413952135c6fefbf40afe6a4faa88b4bb5"