mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
AlertingAPI: Change list response query prop (#33419)
* Alerting: change to full []AlertQuery as json in a string and not just model.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
@@ -90,10 +91,17 @@ func (srv PrometheusSrv) RouteGetRuleStatuses(c *models.ReqContext) response.Res
|
|||||||
|
|
||||||
stateMap := srv.manager.GetStatesByRuleUID()
|
stateMap := srv.manager.GetStatesByRuleUID()
|
||||||
for _, rule := range alertRuleQuery.Result {
|
for _, rule := range alertRuleQuery.Result {
|
||||||
|
var queryStr string
|
||||||
|
encodedQuery, err := json.Marshal(rule.Data)
|
||||||
|
if err != nil {
|
||||||
|
queryStr = err.Error()
|
||||||
|
} else {
|
||||||
|
queryStr = string(encodedQuery)
|
||||||
|
}
|
||||||
alertingRule := apimodels.AlertingRule{
|
alertingRule := apimodels.AlertingRule{
|
||||||
State: "inactive",
|
State: "inactive",
|
||||||
Name: rule.Title,
|
Name: rule.Title,
|
||||||
Query: rule.DataToString(), // TODO: don't escape <>& etc
|
Query: queryStr, // TODO: don't escape <>& etc
|
||||||
Duration: rule.For.Seconds(),
|
Duration: rule.For.Seconds(),
|
||||||
Annotations: rule.Annotations,
|
Annotations: rule.Annotations,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,18 +70,6 @@ type AlertRule struct {
|
|||||||
Labels map[string]string
|
Labels map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (alertRule *AlertRule) DataToString() string {
|
|
||||||
response := "["
|
|
||||||
for i, part := range alertRule.Data {
|
|
||||||
response += string(part.Model)
|
|
||||||
if i < len(alertRule.Data)-1 {
|
|
||||||
response += ","
|
|
||||||
}
|
|
||||||
}
|
|
||||||
response += "]"
|
|
||||||
return response
|
|
||||||
}
|
|
||||||
|
|
||||||
// AlertRuleKey is the alert definition identifier
|
// AlertRuleKey is the alert definition identifier
|
||||||
type AlertRuleKey struct {
|
type AlertRuleKey struct {
|
||||||
OrgID int64
|
OrgID int64
|
||||||
|
|||||||
@@ -71,8 +71,8 @@ func TestPrometheusRules(t *testing.T) {
|
|||||||
From: ngmodels.Duration(time.Duration(5) * time.Hour),
|
From: ngmodels.Duration(time.Duration(5) * time.Hour),
|
||||||
To: ngmodels.Duration(time.Duration(3) * time.Hour),
|
To: ngmodels.Duration(time.Duration(3) * time.Hour),
|
||||||
},
|
},
|
||||||
|
DatasourceUID: "-100",
|
||||||
Model: json.RawMessage(`{
|
Model: json.RawMessage(`{
|
||||||
"datasourceUid": "-100",
|
|
||||||
"type": "math",
|
"type": "math",
|
||||||
"expression": "2 + 3 > 1"
|
"expression": "2 + 3 > 1"
|
||||||
}`),
|
}`),
|
||||||
@@ -91,8 +91,8 @@ func TestPrometheusRules(t *testing.T) {
|
|||||||
From: ngmodels.Duration(time.Duration(5) * time.Hour),
|
From: ngmodels.Duration(time.Duration(5) * time.Hour),
|
||||||
To: ngmodels.Duration(time.Duration(3) * time.Hour),
|
To: ngmodels.Duration(time.Duration(3) * time.Hour),
|
||||||
},
|
},
|
||||||
|
DatasourceUID: "-100",
|
||||||
Model: json.RawMessage(`{
|
Model: json.RawMessage(`{
|
||||||
"datasourceUid": "-100",
|
|
||||||
"type": "math",
|
"type": "math",
|
||||||
"expression": "2 + 3 > 1"
|
"expression": "2 + 3 > 1"
|
||||||
}`),
|
}`),
|
||||||
@@ -137,6 +137,7 @@ func TestPrometheusRules(t *testing.T) {
|
|||||||
b, err := ioutil.ReadAll(resp.Body)
|
b, err := ioutil.ReadAll(resp.Body)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, 200, resp.StatusCode)
|
require.Equal(t, 200, resp.StatusCode)
|
||||||
|
|
||||||
require.JSONEq(t, `
|
require.JSONEq(t, `
|
||||||
{
|
{
|
||||||
"status": "success",
|
"status": "success",
|
||||||
@@ -147,7 +148,7 @@ func TestPrometheusRules(t *testing.T) {
|
|||||||
"rules": [{
|
"rules": [{
|
||||||
"state": "inactive",
|
"state": "inactive",
|
||||||
"name": "AlwaysFiring",
|
"name": "AlwaysFiring",
|
||||||
"query": "[{\"datasourceUid\":\"-100\",\"expression\":\"2 + 3 \\u003e 1\",\"intervalMs\":1000,\"maxDataPoints\":100,\"type\":\"math\"}]",
|
"query": "[{\"refId\":\"A\",\"queryType\":\"\",\"relativeTimeRange\":{\"from\":18000,\"to\":10800},\"datasourceUid\":\"-100\",\"model\":{\"expression\":\"2 + 3 \\u003e 1\",\"intervalMs\":1000,\"maxDataPoints\":100,\"type\":\"math\"}}]",
|
||||||
"duration": 10,
|
"duration": 10,
|
||||||
"annotations": {
|
"annotations": {
|
||||||
"annotation1": "val1"
|
"annotation1": "val1"
|
||||||
@@ -163,7 +164,7 @@ func TestPrometheusRules(t *testing.T) {
|
|||||||
}, {
|
}, {
|
||||||
"state": "inactive",
|
"state": "inactive",
|
||||||
"name": "AlwaysFiringButSilenced",
|
"name": "AlwaysFiringButSilenced",
|
||||||
"query": "[{\"datasourceUid\":\"-100\",\"expression\":\"2 + 3 \\u003e 1\",\"intervalMs\":1000,\"maxDataPoints\":100,\"type\":\"math\"}]",
|
"query": "[{\"refId\":\"A\",\"queryType\":\"\",\"relativeTimeRange\":{\"from\":18000,\"to\":10800},\"datasourceUid\":\"-100\",\"model\":{\"expression\":\"2 + 3 \\u003e 1\",\"intervalMs\":1000,\"maxDataPoints\":100,\"type\":\"math\"}}]",
|
||||||
"labels": null,
|
"labels": null,
|
||||||
"health": "ok",
|
"health": "ok",
|
||||||
"lastError": "",
|
"lastError": "",
|
||||||
@@ -202,7 +203,7 @@ func TestPrometheusRules(t *testing.T) {
|
|||||||
"rules": [{
|
"rules": [{
|
||||||
"state": "inactive",
|
"state": "inactive",
|
||||||
"name": "AlwaysFiring",
|
"name": "AlwaysFiring",
|
||||||
"query": "[{\"datasourceUid\":\"-100\",\"expression\":\"2 + 3 \\u003e 1\",\"intervalMs\":1000,\"maxDataPoints\":100,\"type\":\"math\"}]",
|
"query": "[{\"refId\":\"A\",\"queryType\":\"\",\"relativeTimeRange\":{\"from\":18000,\"to\":10800},\"datasourceUid\":\"-100\",\"model\":{\"expression\":\"2 + 3 \\u003e 1\",\"intervalMs\":1000,\"maxDataPoints\":100,\"type\":\"math\"}}]",
|
||||||
"duration": 10,
|
"duration": 10,
|
||||||
"annotations": {
|
"annotations": {
|
||||||
"annotation1": "val1"
|
"annotation1": "val1"
|
||||||
@@ -218,7 +219,7 @@ func TestPrometheusRules(t *testing.T) {
|
|||||||
}, {
|
}, {
|
||||||
"state": "inactive",
|
"state": "inactive",
|
||||||
"name": "AlwaysFiringButSilenced",
|
"name": "AlwaysFiringButSilenced",
|
||||||
"query": "[{\"datasourceUid\":\"-100\",\"expression\":\"2 + 3 \\u003e 1\",\"intervalMs\":1000,\"maxDataPoints\":100,\"type\":\"math\"}]",
|
"query": "[{\"refId\":\"A\",\"queryType\":\"\",\"relativeTimeRange\":{\"from\":18000,\"to\":10800},\"datasourceUid\":\"-100\",\"model\":{\"expression\":\"2 + 3 \\u003e 1\",\"intervalMs\":1000,\"maxDataPoints\":100,\"type\":\"math\"}}]",
|
||||||
"labels": null,
|
"labels": null,
|
||||||
"health": "ok",
|
"health": "ok",
|
||||||
"lastError": "",
|
"lastError": "",
|
||||||
|
|||||||
Reference in New Issue
Block a user