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