feat: publish standalone worker
Separate worker packaging and service lifecycle from the control plane.
Этот коммит содержится в:
76
internal/notifyrender/text_up_one.go
Обычный файл
76
internal/notifyrender/text_up_one.go
Обычный файл
@@ -0,0 +1,76 @@
|
||||
package notifyrender
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"html/template"
|
||||
"strings"
|
||||
|
||||
"rsgit.ru/rsmon/rsmon/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:")
|
||||
}
|
||||
Ссылка в новой задаче
Block a user