statping/types/notifications/methods.go

172 lines
4.0 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"
)
2020-03-06 03:03:18 +00:00
func (n *Notification) Name() string {
newName := strings.ToLower(n.Method)
newName = strings.ReplaceAll(newName, " ", "_")
return newName
}
2020-03-04 10:29:00 +00:00
// AfterFind for Notification will set the timezone
func (n *Notification) AfterFind() (err error) {
n.CreatedAt = utils.Now()
n.UpdatedAt = utils.Now()
return
}
// AddQueue will add any type of interface (json, string, struct, etc) into the Notifiers queue
2020-03-14 03:13:20 +00:00
//func (n *Notification) AddQueue(uid string, msg interface{}) {
// data := &QueueData{uid, msg}
// n.Queue = append(n.Queue, data)
// log.WithFields(utils.ToFields(data, n)).Debug(fmt.Sprintf("Notifier '%v' added new item (%v) to the queue. (%v queued)", n.Method, uid, len(n.Queue)))
//}
2020-03-04 10:29:00 +00:00
// CanTest returns true if the notifier implements the OnTest interface
2020-03-14 03:13:20 +00:00
//func (n *Notification) CanTest() bool {
// return n.testable
//}
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 {
2020-03-14 03:13:20 +00:00
since := time.Since(n.lastSent)
2020-03-04 10:29:00 +00:00
return since
}
2020-03-17 03:13:07 +00:00
func SelectNotifier(n *Notification) *Notification {
notif, err := Find(n.Method)
if err != nil {
log.Errorln(err)
return n
}
n.Host = notif.Host
n.Username = notif.Username
n.Password = notif.Password
n.ApiSecret = notif.ApiSecret
n.ApiKey = notif.ApiKey
n.Var1 = notif.Var1
n.Host = notif.Var2
return n
}
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
fmt.Println("Last sent: ", n.lastSent.String())
fmt.Println("Last count: ", n.lastSentCount)
fmt.Println("Last sent before now: ", n.lastSent.Add(60*time.Second).Before(utils.Now()))
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)
if n.lastSent.Add(60 * time.Second).Before(utils.Now()) {
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 {
dbField = strings.ToLower(dbField)
switch dbField {
case "host":
return n.Host
case "port":
return fmt.Sprintf("%v", n.Port)
case "username":
return n.Username
case "password":
if n.Password != "" {
return "##########"
}
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(int(n.Limits))
}
return ""
}
// Init accepts the Notifier interface to initialize the notifier
2020-03-09 15:15:15 +00:00
//func Init(n Notifier) (*Notification, error) {
// if Exists(n.Select().Method) {
// AllCommunications = append(AllCommunications, n)
// } else {
// _, err := insertDatabase(n)
// if err != nil {
// log.Errorln(err)
// return nil, err
// }
// AllCommunications = append(AllCommunications, n)
// }
//
// notify, err := SelectNotification(n)
// if err != nil {
// return nil, errors.Wrap(err, "error selecting notification")
// }
//
// notify.CreatedAt = time.Now().UTC()
// notify.UpdatedAt = time.Now().UTC()
// if notify.Delay.Seconds() == 0 {
// notify.Delay = 1 * time.Second
// }
// notify.testable = utils.IsType(n, new(Tester))
// notify.Form = n.Select().Form
//
// AllCommunications = append(AllCommunications, n)
//
// return nil, err
//}
2020-03-04 10:29:00 +00:00
// ResetQueue will clear the notifiers Queue
func (n *Notification) ResetQueue() {
n.Queue = nil
}
// 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
}
}