refactor ComposeNotification()

master
Norwin 5 years ago
parent 3a48d3ae5a
commit 287f26a37c

@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"net/smtp"
"strings"
"time"
"github.com/spf13/viper"
@ -55,38 +54,6 @@ func (n EmailNotifier) New(config TransportConfig) (AbstractNotifier, error) {
}, nil
}
func (n EmailNotifier) ComposeNotification(box *Box, checks []CheckResult) Notification {
errTexts := []string{}
resolvedTexts := []string{}
for _, check := range checks {
if check.Status == CheckErr {
errTexts = append(errTexts, check.String())
} else {
resolvedTexts = append(resolvedTexts, check.String())
}
}
var (
resolved string
resolvedList string
errList string
)
if len(resolvedTexts) != 0 {
resolvedList = fmt.Sprintf("Resolved issue(s):\n\n%s\n\n", strings.Join(resolvedTexts, "\n"))
}
if len(errTexts) != 0 {
errList = fmt.Sprintf("New issue(s):\n\n%s\n\n", strings.Join(errTexts, "\n"))
} else {
resolved = "resolved "
}
return Notification{
Subject: fmt.Sprintf("Issues %swith your box \"%s\" on opensensemap.org!", resolved, box.Name),
Body: fmt.Sprintf("A check at %s identified the following updates for your box \"%s\":\n\n%s%sYou may visit https://opensensemap.org/explore/%s for more details.\n\n--\nSent automatically by osem_notify (https://github.com/noerw/osem_notify)",
time.Now().Round(time.Minute), box.Name, errList, resolvedList, box.Id),
}
}
func (n EmailNotifier) Submit(notification Notification) error {
// :TransportConfSourceHack
auth := smtp.PlainAuth(

@ -3,8 +3,6 @@ package core
import (
"errors"
"fmt"
"strings"
"time"
xmpp "github.com/mattn/go-xmpp"
"github.com/spf13/viper"
@ -54,38 +52,6 @@ func (n XmppNotifier) New(config TransportConfig) (AbstractNotifier, error) {
}, nil
}
func (n XmppNotifier) ComposeNotification(box *Box, checks []CheckResult) Notification {
errTexts := []string{}
resolvedTexts := []string{}
for _, check := range checks {
if check.Status == CheckErr {
errTexts = append(errTexts, check.String())
} else {
resolvedTexts = append(resolvedTexts, check.String())
}
}
var (
resolved string
resolvedList string
errList string
)
if len(resolvedTexts) != 0 {
resolvedList = fmt.Sprintf("Resolved issue(s):\n\n%s\n\n", strings.Join(resolvedTexts, "\n"))
}
if len(errTexts) != 0 {
errList = fmt.Sprintf("New issue(s):\n\n%s\n\n", strings.Join(errTexts, "\n"))
} else {
resolved = "resolved "
}
return Notification{
Subject: fmt.Sprintf("Issues %swith your box \"%s\" on opensensemap.org!", resolved, box.Name),
Body: fmt.Sprintf("A check at %s identified the following updates for your box \"%s\":\n\n%s%sYou may visit https://opensensemap.org/explore/%s for more details.\n\n--\nSent automatically by osem_notify (https://github.com/noerw/osem_notify)",
time.Now().Round(time.Minute), box.Name, errList, resolvedList, box.Id),
}
}
func (n XmppNotifier) Submit(notification Notification) error {
// :TransportConfSourceHack
xmppOpts := xmpp.Options{

@ -15,7 +15,6 @@ var Notifiers = map[string]AbstractNotifier{
type AbstractNotifier interface {
New(config TransportConfig) (AbstractNotifier, error)
ComposeNotification(box *Box, checks []CheckResult) Notification
Submit(notification Notification) error
}
@ -81,7 +80,7 @@ func (results BoxCheckResults) SendNotifications(notifyTypes []string, useCache
continue
}
notification := notifier.ComposeNotification(box, resultsDue)
notification := ComposeNotification(box, resultsDue)
var submitErr error
submitErr = notifier.Submit(notification)
@ -116,3 +115,36 @@ func (results BoxCheckResults) SendNotifications(notifyTypes []string, useCache
}
return nil
}
func ComposeNotification(box *Box, checks []CheckResult) Notification {
errTexts := []string{}
resolvedTexts := []string{}
for _, check := range checks {
if check.Status == CheckErr {
errTexts = append(errTexts, check.String())
} else {
resolvedTexts = append(resolvedTexts, check.String())
}
}
var (
resolved string
resolvedList string
errList string
)
if len(resolvedTexts) != 0 {
resolvedList = fmt.Sprintf("Resolved issue(s):\n\n%s\n\n", strings.Join(resolvedTexts, "\n"))
}
if len(errTexts) != 0 {
errList = fmt.Sprintf("New issue(s):\n\n%s\n\n", strings.Join(errTexts, "\n"))
} else {
resolved = "resolved "
}
return Notification{
Subject: fmt.Sprintf("Issues %swith your box \"%s\" on opensensemap.org!", resolved, box.Name),
Body: fmt.Sprintf("A check at %s identified the following updates for your box \"%s\":\n\n%s%sYou may visit https://opensensemap.org/explore/%s for more details.\n\n--\nSent automatically by osem_notify (https://github.com/noerw/osem_notify)",
time.Now().Round(time.Minute), box.Name, errList, resolvedList, box.Id),
}
}

Loading…
Cancel
Save