Add alert interface

pull/342/head
Chris Goller 2016-10-31 19:19:32 -05:00
parent b7437da989
commit 476277f219
4 changed files with 40 additions and 13 deletions

1
.gitignore vendored
View File

@ -9,3 +9,4 @@ node_modules/
build/
chronograf.db
npm-debug.log
.vscode

View File

@ -68,7 +68,7 @@ type Source struct {
Name string `json:"name"` // Name is the user-defined name for the source
Type string `json:"type,omitempty"` // Type specifies which kinds of source (enterprise vs oss)
Username string `json:"username,omitempty"` // Username is the username to connect to the source
Password string `json:"password,omitempty"` // Password is in CLEARTEXT FIXME
Password string `json:"password,omitempty"` // Password is in CLEARTEXT // TODO: fixme
URL string `json:"url"` // URL are the connections to the source
Default bool `json:"default"` // Default specifies the default source for the application
}
@ -87,13 +87,22 @@ type SourcesStore interface {
Update(context.Context, Source) error
}
// TickTemplate task to be used by kapacitor
type TickTemplate string
// Alert generates tickscript templates for kapacitor
type Alert interface {
// Generate will create the tickscript to be used as a kapacitor template
Generate() (TickTemplate, error)
}
// Server represents a proxy connection to an HTTP server
type Server struct {
ID int // ID is the unique ID of the server
SrcID int // SrcID of the data source
Name string // Name is the user-defined name for the server
Username string // Username is the username to connect to the server
Password string // Password is in CLEARTEXT FIXME
Password string // Password is in CLEARTEXT // TODO: FIXME
URL string // URL are the connections to the server
}

View File

@ -7,23 +7,36 @@ import (
"text/template"
"time"
"github.com/influxdata/chronograf"
"github.com/influxdata/kapacitor/pipeline"
"github.com/influxdata/kapacitor/tick/ast"
"github.com/influxdata/kapacitor/tick/stateful"
)
var _ chronograf.Alert = &Alert{}
// Alert defines alerting strings in template rendering
type Alert struct {
Trigger string // Specifies the type of alert
Service string // Alerting service
Operator string // Operator for alert comparison
Aggregate string // Statistic aggregate over window of data
}
// TickTemplate task to be used by kapacitor
type TickTemplate string
func (a *Alert) Generate() (chronograf.TickTemplate, error) {
switch a.Trigger {
case "threshold":
return a.Threshold()
case "relative":
return a.Relative()
case "deadman":
return a.Deadman()
}
return "", fmt.Errorf("Unknown tigger mechanism %s", a.Trigger)
}
// Threshold generates a tickscript template with an alert
func (a *Alert) Threshold() (TickTemplate, error) {
func (a *Alert) Threshold() (chronograf.TickTemplate, error) {
if err := ValidateAlert(a); err != nil {
return "", err
}
@ -41,7 +54,7 @@ func (a *Alert) Threshold() (TickTemplate, error) {
}
// Relative creates a tickscript that alerts on relative changes over windows of data
func (a *Alert) Relative() (TickTemplate, error) {
func (a *Alert) Relative() (chronograf.TickTemplate, error) {
if err := ValidateAlert(a); err != nil {
return "", err
}
@ -59,7 +72,7 @@ func (a *Alert) Relative() (TickTemplate, error) {
}
// Deadman creates a tickscript that alerts when no data has been received for a time.
func (a *Alert) Deadman() (TickTemplate, error) {
func (a *Alert) Deadman() (chronograf.TickTemplate, error) {
if err := ValidateAlert(a); err != nil {
return "", err
}
@ -85,7 +98,7 @@ func ValidateAlert(alert *Alert) error {
return validateTick(script)
}
func formatTick(tickscript string) (TickTemplate, error) {
func formatTick(tickscript string) (chronograf.TickTemplate, error) {
node, err := ast.Parse(tickscript)
if err != nil {
log.Fatalf("parse execution: %s", err)
@ -94,7 +107,7 @@ func formatTick(tickscript string) (TickTemplate, error) {
output := new(bytes.Buffer)
node.Format(output, "", true)
return TickTemplate(output.String()), nil
return chronograf.TickTemplate(output.String()), nil
}
func validateTick(script string) error {

View File

@ -1,6 +1,10 @@
package tickscripts
import "testing"
import (
"testing"
"github.com/influxdata/chronograf"
)
func TestValidateAlert(t *testing.T) {
tests := []struct {
@ -65,7 +69,7 @@ func TestThreshold(t *testing.T) {
tests := []struct {
name string
alert Alert
want TickTemplate
want chronograf.TickTemplate
wantErr bool
}{
{
@ -139,7 +143,7 @@ func TestRelative(t *testing.T) {
tests := []struct {
name string
alert Alert
want TickTemplate
want chronograf.TickTemplate
wantErr bool
}{
{
@ -256,7 +260,7 @@ func TestDeadman(t *testing.T) {
tests := []struct {
name string
alert Alert
want TickTemplate
want chronograf.TickTemplate
wantErr bool
}{
{