compile template message
This commit is contained in:
@@ -84,5 +84,6 @@ func getTemplateData(monitor *AbstractMonitor) map[string]interface{} {
|
||||
"SystemName": monitor.config.SystemName,
|
||||
"API": monitor.config.API,
|
||||
"Monitor": monitor,
|
||||
"now": time.Now().Format(monitor.config.DateFormat),
|
||||
}
|
||||
}
|
||||
|
||||
6
http.go
6
http.go
@@ -15,14 +15,14 @@ import (
|
||||
// Investigating template
|
||||
var defaultHTTPInvestigatingTpl = MessageTemplate{
|
||||
Subject: `{{ .Monitor.Name }} - {{ .SystemName }}`,
|
||||
Message: `{{ .Name }} check **failed** - {{ .now }}
|
||||
Message: `{{ .Monitor.Name }} check **failed** - {{ .now }}
|
||||
|
||||
{{ .lastFailReason }}`,
|
||||
{{ .FailReason }}`,
|
||||
}
|
||||
|
||||
// Fixed template
|
||||
var defaultHTTPFixedTpl = MessageTemplate{
|
||||
Subject: `{{ .Name }} - {{ .config.SystemName }}`,
|
||||
Subject: `{{ .Monitor.Name }} - {{ .SystemName }}`,
|
||||
Message: `**Resolved** - {{ .now }}
|
||||
|
||||
- - -
|
||||
|
||||
@@ -192,7 +192,10 @@ func (mon *AbstractMonitor) AnalyseData() {
|
||||
|
||||
if triggered && mon.incident == nil {
|
||||
// create incident
|
||||
subject, message := mon.Template.Investigating.Exec(getTemplateData(mon))
|
||||
tplData := getTemplateData(mon)
|
||||
tplData["FailReason"] = mon.lastFailReason
|
||||
|
||||
subject, message := mon.Template.Investigating.Exec(tplData)
|
||||
mon.incident = &Incident{
|
||||
Name: subject,
|
||||
ComponentID: mon.ComponentID,
|
||||
|
||||
13
template.go
13
template.go
@@ -3,8 +3,6 @@ package cachet
|
||||
import (
|
||||
"bytes"
|
||||
"text/template"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
)
|
||||
|
||||
type MessageTemplate struct {
|
||||
@@ -39,13 +37,14 @@ func (t *MessageTemplate) Compile() error {
|
||||
}
|
||||
|
||||
func (t *MessageTemplate) Exec(data interface{}) (string, string) {
|
||||
return t.exec(t.subjectTpl, data), t.exec(t.messageTpl, data)
|
||||
}
|
||||
|
||||
func (t *MessageTemplate) exec(tpl *template.Template, data interface{}) string {
|
||||
buf := new(bytes.Buffer)
|
||||
|
||||
logrus.Warnf("%#v", t.subjectTpl)
|
||||
t.subjectTpl.Execute(buf, data)
|
||||
subject := buf.String()
|
||||
|
||||
return subject, ""
|
||||
tpl.Execute(buf, data)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func compileTemplate(text string) (*template.Template, error) {
|
||||
|
||||
Reference in New Issue
Block a user