Skip to content

Commit

Permalink
feat: Add templates to trigger description #484
Browse files Browse the repository at this point in the history
* Added templates to trigger description
* Added presentation of trigger description in non-editing for api

Closed #484
  • Loading branch information
SantFlamel committed Jan 27, 2020
1 parent e411f20 commit 7ab8f29
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
13 changes: 13 additions & 0 deletions api/handler/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package handler
import (
"fmt"
"net/http"
"strings"
"time"

"github.com/moira-alert/moira"

"github.com/go-chi/chi"
"github.com/go-chi/render"
"github.com/moira-alert/moira/metric_source/local"
Expand Down Expand Up @@ -79,11 +82,21 @@ func getTrigger(writer http.ResponseWriter, request *http.Request) {
if triggerID == "testlog" {
panic("Test for multi line logs")
}

trigger, err := controller.GetTrigger(database, triggerID)
if err != nil {
render.Render(writer, request, err)
return
}

if !strings.Contains(request.Header.Get("Referer"), "/edit") && trigger.Desc != nil {
triggerData := moira.TriggerData{Desc: *trigger.Desc}

triggerData.TemplateDescription(nil)

*trigger.Desc = triggerData.Desc
}

if err := render.Render(writer, request, trigger); err != nil {
render.Render(writer, request, api.ErrorRender(err))
}
Expand Down
16 changes: 16 additions & 0 deletions datatypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math"
"strconv"
"strings"
"text/template"
"time"
)

Expand Down Expand Up @@ -103,6 +104,10 @@ func (event *NotificationEvent) CreateMessage(location *time.Location) string {
// NotificationEvents represents slice of NotificationEvent
type NotificationEvents []NotificationEvent

type TemplateEvent struct {
Metric string
}

// TriggerData represents trigger object
type TriggerData struct {
ID string `json:"id"`
Expand All @@ -115,6 +120,17 @@ type TriggerData struct {
Tags []string `json:"__notifier_trigger_tags"`
}

func (trigger *TriggerData) TemplateDescription(data map[string]interface{}) {
buffer := new(bytes.Buffer)

triggerTemplate := template.Must(template.New("moira").Parse(trigger.Desc))
if err := triggerTemplate.Execute(buffer, data); err != nil {
return
}

trigger.Desc = buffer.String()
}

// GetTriggerURI gets frontUri and returns triggerUrl, returns empty string on selfcheck and test notifications
func (trigger TriggerData) GetTriggerURI(frontURI string) string {
if trigger.ID != "" {
Expand Down
25 changes: 25 additions & 0 deletions datatypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,31 @@ func TestTriggerData_GetTags(t *testing.T) {
})
}

func TestTriggerData_TemplateDescription(t *testing.T) {
Convey("Test templates", t, func() {
var trigger TriggerData
trigger.Desc = "https://grafana.yourhost.com/some-dashboard{{ range $i, $v := .Events }}{{ if ne $i 0 }}&{{ else }}?{{ end }}var-host={{ $v.Metric }}{{ end }}"
var data = map[string]interface{}{"Events": []NotificationEvent{{Metric: "1"}, {Metric: "2"}}}

Convey("Test nil data", func() {
trigger.TemplateDescription(nil)
So(trigger.Desc, ShouldEqual, "https://grafana.yourhost.com/some-dashboard")
})

Convey("Test data", func() {
trigger.TemplateDescription(data)
So(trigger.Desc, ShouldEqual, "https://grafana.yourhost.com/some-dashboard?var-host=1&var-host=2")
})

Convey("Test description whithout templates", func() {
anotherText := "Anther text"
trigger.Desc = anotherText
trigger.TemplateDescription(data)
So(trigger.Desc, ShouldEqual, anotherText)
})
})
}

func TestScheduledNotification_GetKey(t *testing.T) {
Convey("Get key", t, func() {
notification := ScheduledNotification{
Expand Down
12 changes: 12 additions & 0 deletions notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ func (notifier *StandardNotifier) runSender(sender moira.Sender, ch chan Notific
notifier.logger.Errorf(buildErr)
}
}

notifier.BuildTriggerDescription(pkg.Events, &pkg.Trigger)

err = sender.SendEvents(pkg.Events, pkg.Contact, pkg.Trigger, plot, pkg.Throttled)
if err == nil {
if metric, found := notifier.metrics.SendersOkMetrics.GetRegisteredMeter(pkg.Contact.Type); found {
Expand All @@ -170,3 +173,12 @@ func (notifier *StandardNotifier) runSender(sender moira.Sender, ch chan Notific
}
}
}

func (notifier *StandardNotifier) BuildTriggerDescription(events moira.NotificationEvents, trigger *moira.TriggerData) {
templateEvents := make([]moira.TemplateEvent, 0, len(events))
for _, event := range events {
templateEvents = append(templateEvents, moira.TemplateEvent{Metric: event.Metric})
}

trigger.TemplateDescription(map[string]interface{}{"Events": templateEvents})
}

0 comments on commit 7ab8f29

Please sign in to comment.