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-06-26 04:08:12 +00:00
|
|
|
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
|
2020-07-30 23:14:41 +00:00
|
|
|
func (n Notification) LastSentDur() 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
|
|
|
|
}
|
|
|
|
// the last sent notification was past 1 minute (limit per minute)
|
2020-07-30 23:14:41 +00:00
|
|
|
if n.LastSent.Add(60 * time.Minute).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
|
2020-07-30 23:14:41 +00:00
|
|
|
if n.LastSentCount >= n.Limits {
|
2020-03-14 03:13:20 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2020-03-23 02:50:30 +00:00
|
|
|
switch strings.ToLower(dbField) {
|
2020-03-04 10:29:00 +00:00
|
|
|
case "host":
|
2020-07-22 19:07:42 +00:00
|
|
|
return n.Host.String
|
2020-03-04 10:29:00 +00:00
|
|
|
case "port":
|
2020-07-22 19:07:42 +00:00
|
|
|
return fmt.Sprintf("%d", n.Port.Int64)
|
2020-03-04 10:29:00 +00:00
|
|
|
case "username":
|
2020-07-22 19:07:42 +00:00
|
|
|
return n.Username.String
|
2020-03-04 10:29:00 +00:00
|
|
|
case "password":
|
2020-07-22 19:07:42 +00:00
|
|
|
return n.Password.String
|
2020-03-04 10:29:00 +00:00
|
|
|
case "var1":
|
2020-07-22 19:07:42 +00:00
|
|
|
return n.Var1.String
|
2020-03-04 10:29:00 +00:00
|
|
|
case "var2":
|
2020-07-22 19:07:42 +00:00
|
|
|
return n.Var2.String
|
2020-03-04 10:29:00 +00:00
|
|
|
case "api_key":
|
2020-07-22 19:07:42 +00:00
|
|
|
return n.ApiKey.String
|
2020-03-04 10:29:00 +00:00
|
|
|
case "api_secret":
|
2020-07-22 19:07:42 +00:00
|
|
|
return n.ApiSecret.String
|
2020-03-04 10:29:00 +00:00
|
|
|
case "limits":
|
2020-06-06 00:36:39 +00:00
|
|
|
return utils.ToString(n.Limits)
|
2020-04-04 00:28:09 +00:00
|
|
|
default:
|
|
|
|
return ""
|
2020-03-04 10:29:00 +00:00
|
|
|
}
|
2020-03-23 02:50:30 +00:00
|
|
|
}
|