Некоторые проверки не удались
CI / test (push) Successful in 2m5s
Docker / Build and publish worker image (push) Failing after 31s
77 строки
2.8 KiB
Go
77 строки
2.8 KiB
Go
package notifyrender
|
|
|
|
import (
|
|
"bytes"
|
|
"html/template"
|
|
"strings"
|
|
|
|
"rocketgit.ru/rsmon/worker/app/models"
|
|
)
|
|
|
|
var (
|
|
// UpOneSubject provides functionality.
|
|
UpOneSubject *template.Template
|
|
// UpOneBody provides functionality.
|
|
UpOneBody *template.Template
|
|
// UpOneHTML provides functionality.
|
|
UpOneHTML *template.Template
|
|
)
|
|
|
|
func init() {
|
|
UpOneSubject = template.Must(template.New("up_one_subject").Parse(`Снова доступен {{.Monitor.GetLabel}}`))
|
|
// UpOneBody provides functionality.
|
|
UpOneBody = template.Must(template.New("up_one_body").Parse(`Монитор {{.Monitor.GetLabel}} снова доступен :white_check_mark:
|
|
|
|
Проверки:
|
|
{{range .Checks}}
|
|
{{.Kind}} - {{if .Name}}{{.Name}}{{else}}Нет имени{{end}} - {{if .URL}}{{.URL}}{{else}}URL не указан{{end}} {{if .Error}}({{.Error}}){{end}}
|
|
{{end}}
|
|
|
|
Он был недоступен {{.FormatDuration}} по причине ошибки {{.Reason}}
|
|
|
|
{{if .StartTime}}Начало события: {{.StartTime.Format "02.01.2006 15:04:05"}}{{end}}
|
|
{{if .EndTime}}Окончание события: {{.EndTime.Format "02.01.2006 15:04:05"}}{{end}}
|
|
`))
|
|
UpOneHTML = template.Must(template.New("up_one_html").Parse(`<h4>Монитор {{.Monitor.GetLabel}} снова доступен</h4>.
|
|
|
|
<h5>Проверки:<h5>
|
|
{{range .Checks}}
|
|
<div>
|
|
{{.Kind}} - {{if .Name}}{{.Name}}{{else}}Нет имени{{end}} - {{if .URL}}{{.URL}}{{else}}URL не указан{{end}} {{if .Error}}({{.Error}}){{end}}
|
|
</div>
|
|
{{end}}
|
|
|
|
<div>Он был недоступен {{.FormatDuration}} по причине ошибки <code>{{.Reason}}</code></div>
|
|
|
|
{{if .StartTime}}<div>Начало события: {{.StartTime.Format "02.01.2006 15:04:05"}}</div>{{end}}
|
|
{{if .EndTime}}<div>Окончание события: {{.EndTime.Format "02.01.2006 15:04:05"}}</div>{{end}}
|
|
`))
|
|
}
|
|
|
|
// executeTemplates is a helper function that executes subject, body, and HTML templates
|
|
// and removes the specified emoji string from the text body for non-HTML output
|
|
func executeTemplates(event *models.Event, subject, body, html *template.Template, emojiToRemove string) (bytes.Buffer, bytes.Buffer, bytes.Buffer, bytes.Buffer) { //nolint:lll
|
|
var err error
|
|
var subjectBuf, textBodyBuf, htmlBodyBuf bytes.Buffer
|
|
|
|
err = subject.Execute(&subjectBuf, event)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
err = body.Execute(&textBodyBuf, event)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
err = html.Execute(&htmlBodyBuf, event)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
txt := bytes.NewBufferString(strings.ReplaceAll(textBodyBuf.String(), emojiToRemove, ""))
|
|
return subjectBuf, *txt, textBodyBuf, htmlBodyBuf
|
|
}
|
|
|
|
// TextUpOne provides functionality.
|
|
func TextUpOne(event *models.Event) (bytes.Buffer, bytes.Buffer, bytes.Buffer, bytes.Buffer) {
|
|
return executeTemplates(event, UpOneSubject, UpOneBody, UpOneHTML, " :white_check_mark:")
|
|
}
|