statping/types/notifications/methods.go

94 lines
1.8 KiB
Go
Raw Normal View History

2020-03-14 03:13:20 +00:00
package notifications
2020-03-04 10:29:00 +00:00
import (
"fmt"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/utils"
2020-03-04 10:29:00 +00:00
"strings"
"time"
)
func (n Notification) Name() string {
2020-03-06 03:03:18 +00:00
newName := strings.ToLower(n.Method)
newName = strings.ReplaceAll(newName, " ", "_")
return newName
}
2020-03-04 10:29:00 +00:00
// LastSent returns a time.Duration of the last sent notification for the notifier
func (n Notification) LastSent() time.Duration {
return time.Since(n.lastSent)
2020-03-04 10:29:00 +00:00
}
2020-03-14 03:13:20 +00:00
func (n *Notification) CanSend() bool {
if !n.Enabled.Bool {
return false
}
2020-03-04 10:29:00 +00:00
2020-03-14 03:13:20 +00:00
// the last sent notification was past 1 minute (limit per minute)
2020-03-25 18:46:50 +00:00
if n.lastSent.Add(60 * time.Minute).Before(utils.Now()) {
2020-03-14 03:13:20 +00:00
if n.lastSentCount != 0 {
n.lastSentCount--
2020-03-04 10:29:00 +00:00
}
}
2020-03-14 03:13:20 +00:00
// dont send if already beyond the notifier's limit
if n.lastSentCount >= n.Limits {
return false
}
// action to do since notifier is able to send
n.lastSentCount++
n.lastSent = utils.Now()
return true
2020-03-04 10:29:00 +00:00
}
// GetValue returns the database value of a accept DbField value.
func (n *Notification) GetValue(dbField string) string {
switch strings.ToLower(dbField) {
2020-03-04 10:29:00 +00:00
case "host":
return n.Host
case "port":
return fmt.Sprintf("%d", n.Port)
2020-03-04 10:29:00 +00:00
case "username":
return n.Username
case "password":
return n.Password
2020-03-04 10:29:00 +00:00
case "var1":
return n.Var1
case "var2":
return n.Var2
case "api_key":
return n.ApiKey
case "api_secret":
return n.ApiSecret
case "limits":
return utils.ToString(n.Limits)
default:
return ""
2020-03-04 10:29:00 +00:00
}
}
// start will start the go routine for the notifier queue
func (n *Notification) Start() {
n.Running = make(chan bool)
}
// close will stop the go routine for queue
func (n *Notification) Close() {
if n.IsRunning() {
close(n.Running)
}
}
// IsRunning will return true if the notifier is currently running a queue
func (n *Notification) IsRunning() bool {
if n.Running == nil {
return false
}
select {
case <-n.Running:
return false
default:
return true
}
}