Explorar o código

feat(alerting): abstract graphite from executor

bergquist %!s(int64=9) %!d(string=hai) anos
pai
achega
422234d03a

+ 1 - 1
pkg/services/alerting/types.go → pkg/models/timeseries.go

@@ -1,4 +1,4 @@
-package alerting
+package models
 
 type TimeSeries struct {
 	Name   string       `json:"name"`

+ 1 - 1
pkg/services/alerting/alerting.go

@@ -21,7 +21,7 @@ func Init() {
 
 	scheduler := NewScheduler()
 	go scheduler.Dispatch(&AlertRuleReader{})
-	go scheduler.Executor(&GraphiteExecutor{})
+	go scheduler.Executor(&ExecutorImpl{})
 	go scheduler.HandleResponses()
 }
 

+ 0 - 4
pkg/services/alerting/dummie_executor.go

@@ -6,10 +6,6 @@ import (
 	"time"
 )
 
-type Executor interface {
-	Execute(rule m.AlertRule, responseQueue chan *AlertResult)
-}
-
 type DummieExecutor struct{}
 
 func (this *DummieExecutor) Execute(rule m.AlertRule, responseQueue chan *AlertResult) {

+ 20 - 3
pkg/services/alerting/rule_executor.go → pkg/services/alerting/executor.go

@@ -2,17 +2,34 @@ package alerting
 
 import (
 	m "github.com/grafana/grafana/pkg/models"
+	"github.com/grafana/grafana/pkg/services/alerting/graphite"
 )
 
-func (this *GraphiteExecutor) executeRules(series []GraphiteSerie, rule m.AlertRule) *AlertResult {
+type Executor interface {
+	Execute(rule m.AlertRule, responseQueue chan *AlertResult)
+}
+
+type ExecutorImpl struct{}
+
+func (this *ExecutorImpl) Execute(rule m.AlertRule, responseQueue chan *AlertResult) {
+	response, err := graphite.GraphiteClient{}.GetSeries(rule)
+
+	if err != nil {
+		responseQueue <- &AlertResult{State: "CRITICAL", Id: rule.Id}
+	}
+
+	responseQueue <- this.executeRules(response, rule)
+}
+
+func (this *ExecutorImpl) executeRules(series m.TimeSeriesSlice, rule m.AlertRule) *AlertResult {
 	for _, v := range series {
 		var avg float64
 		var sum float64
-		for _, dp := range v.Datapoints {
+		for _, dp := range v.Points {
 			sum += dp[0]
 		}
 
-		avg = sum / float64(len(v.Datapoints))
+		avg = sum / float64(len(v.Points))
 
 		if float64(rule.CritLevel) < avg {
 			return &AlertResult{State: m.AlertStateCritical, Id: rule.Id, ActualValue: avg}

+ 13 - 14
pkg/services/alerting/graphite_executor.go → pkg/services/alerting/graphite/graphite.go

@@ -1,4 +1,4 @@
-package alerting
+package graphite
 
 import (
 	"fmt"
@@ -11,7 +11,7 @@ import (
 	"time"
 )
 
-type GraphiteExecutor struct{}
+type GraphiteClient struct{}
 
 type GraphiteSerie struct {
 	Datapoints [][2]float64
@@ -20,17 +20,7 @@ type GraphiteSerie struct {
 
 type GraphiteResponse []GraphiteSerie
 
-func (this *GraphiteExecutor) Execute(rule m.AlertRule, responseQueue chan *AlertResult) {
-	response, err := this.getSeries(rule)
-
-	if err != nil {
-		responseQueue <- &AlertResult{State: "CRITICAL", Id: rule.Id}
-	}
-
-	responseQueue <- this.executeRules(response, rule)
-}
-
-func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (GraphiteResponse, error) {
+func (this GraphiteClient) GetSeries(rule m.AlertRule) (m.TimeSeriesSlice, error) {
 	query := &m.GetDataSourceByIdQuery{Id: rule.DatasourceId, OrgId: rule.OrgId}
 	if err := bus.Dispatch(query); err != nil {
 		return nil, err
@@ -61,7 +51,16 @@ func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (GraphiteResponse, err
 		return nil, fmt.Errorf("error!")
 	}
 
-	return response, nil
+	timeSeries := make([]*m.TimeSeries, 0)
+
+	for _, v := range response {
+		timeSeries = append(timeSeries, &m.TimeSeries{
+			Name:   v.Target,
+			Points: v.Datapoints,
+		})
+	}
+
+	return timeSeries, nil
 }
 
 func getTargetFromRule(rule m.AlertRule) string {