mirror of https://github.com/statping/statping
docs
parent
f5e15258c8
commit
62f159695a
|
@ -227,6 +227,32 @@ func ExampleNotification_OnSuccess() {
|
|||
// Output: 1
|
||||
}
|
||||
|
||||
// Add a new message into the queue OnSuccess
|
||||
func ExampleOnSuccess() {
|
||||
msg := fmt.Sprintf("received a count trigger for service: %v\n", service.Name)
|
||||
example.AddQueue(msg)
|
||||
}
|
||||
|
||||
// Add a new message into the queue OnFailure
|
||||
func ExampleOnFailure() {
|
||||
msg := fmt.Sprintf("received a failing service: %v\n", service.Name)
|
||||
example.AddQueue(msg)
|
||||
}
|
||||
|
||||
// OnTest allows your notifier to be testable
|
||||
func ExampleOnTest() {
|
||||
err := example.OnTest()
|
||||
fmt.Print(err)
|
||||
// Output <nil>
|
||||
}
|
||||
|
||||
// Implement the Test interface to give your notifier testing abilities
|
||||
func ExampleNotification_CanTest() {
|
||||
testable := example.CanTest()
|
||||
fmt.Print(testable)
|
||||
// Output: false
|
||||
}
|
||||
|
||||
// Add any type of interface to the AddQueue function to be ran in the queue
|
||||
func ExampleNotification_AddQueue() {
|
||||
msg := fmt.Sprintf("this is a failing message as a string passing into AddQueue function")
|
||||
|
|
|
@ -28,10 +28,11 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
AllCommunications []types.AllNotifiers
|
||||
db *gorm.DB
|
||||
AllCommunications []types.AllNotifiers // AllCommunications holds all the loaded notifiers
|
||||
db *gorm.DB // db holds the Statup database connection
|
||||
)
|
||||
|
||||
// Notification contains all the fields for a Statup Notifier.
|
||||
type Notification struct {
|
||||
Id int64 `gorm:"primary_key;column:id" json:"id"`
|
||||
Method string `gorm:"column:method" json:"method"`
|
||||
|
@ -61,25 +62,29 @@ type Notification struct {
|
|||
testable bool
|
||||
}
|
||||
|
||||
// NotificationForm contains the HTML fields for each variable/input you want the notifier to accept.
|
||||
type NotificationForm struct {
|
||||
Type string
|
||||
Title string
|
||||
Placeholder string
|
||||
DbField string
|
||||
SmallText string
|
||||
Required bool
|
||||
Type string // the html input type (text, password, email)
|
||||
Title string // include a title for ease of use
|
||||
Placeholder string // add a placeholder for the input
|
||||
DbField string // true variable key for input
|
||||
SmallText string // insert small text under a html input
|
||||
Required bool // require this input on the html form
|
||||
}
|
||||
|
||||
// NotificationLog contains the normalized message from previously sent notifications
|
||||
type NotificationLog struct {
|
||||
Message string
|
||||
Time utils.Timestamp
|
||||
Timestamp time.Time
|
||||
}
|
||||
|
||||
// AddQueue will add any type of interface (json, string, struct, etc) into the Notifiers queue
|
||||
func (n *Notification) AddQueue(msg interface{}) {
|
||||
n.Queue = append(n.Queue, msg)
|
||||
}
|
||||
|
||||
// CanTest returns true if the notifier implements the OnTest interface
|
||||
func (n *Notification) CanTest() bool {
|
||||
return n.testable
|
||||
}
|
||||
|
@ -94,6 +99,7 @@ func SetDB(d *gorm.DB) {
|
|||
db = d
|
||||
}
|
||||
|
||||
// asNotification accepts a Notifier and returns a Notification struct
|
||||
func asNotification(n Notifier) *Notification {
|
||||
return n.Select()
|
||||
}
|
||||
|
@ -124,6 +130,7 @@ func Load() []types.AllNotifiers {
|
|||
return notifiers
|
||||
}
|
||||
|
||||
// normalizeType will accept multiple interfaces and converts it into a string for logging
|
||||
func normalizeType(ty interface{}) string {
|
||||
switch v := ty.(type) {
|
||||
case int, int32, int64:
|
||||
|
@ -144,6 +151,7 @@ func normalizeType(ty interface{}) string {
|
|||
}
|
||||
}
|
||||
|
||||
// removeQueue will remove a specific notification and return the new one
|
||||
func (n *Notification) removeQueue(msg interface{}) interface{} {
|
||||
var newArr []interface{}
|
||||
for _, q := range n.Queue {
|
||||
|
@ -239,6 +247,7 @@ func Init(n Notifier) (*Notification, error) {
|
|||
return notify, err
|
||||
}
|
||||
|
||||
// startAllNotifiers will start the go routine for each loaded notifier
|
||||
func startAllNotifiers() {
|
||||
for _, comm := range AllCommunications {
|
||||
if isType(comm, new(Notifier)) {
|
||||
|
@ -252,6 +261,7 @@ func startAllNotifiers() {
|
|||
}
|
||||
}
|
||||
|
||||
// Queue is the FIFO go routine to send notifications when objects are triggered
|
||||
func Queue(n Notifier) {
|
||||
notification := n.Select()
|
||||
rateLimit := notification.Delay
|
||||
|
@ -304,17 +314,19 @@ func (f *Notification) LastSent() time.Duration {
|
|||
return since
|
||||
}
|
||||
|
||||
// SentLastHour returns the total amount of notifications sent in last 1 hour
|
||||
func (f *Notification) SentLastHour() int {
|
||||
since := time.Now().Add(-1 * time.Hour)
|
||||
return f.SentLast(since)
|
||||
}
|
||||
|
||||
// SentLastMinute returns the total amount of notifications sent in last 1 minute
|
||||
func (f *Notification) SentLastMinute() int {
|
||||
since := time.Now().Add(-1 * time.Minute)
|
||||
return f.SentLast(since)
|
||||
}
|
||||
|
||||
// SentLastHour returns the amount of sent notifications within the last hour
|
||||
// SentLast accept a time.Time and returns the amount of sent notifications within your time to current
|
||||
func (f *Notification) SentLast(since time.Time) int {
|
||||
sent := 0
|
||||
for _, v := range f.Logs() {
|
||||
|
@ -367,12 +379,14 @@ func isEnabled(n interface{}) bool {
|
|||
return notifier.Enabled
|
||||
}
|
||||
|
||||
func inLimits(n interface{}) bool {
|
||||
notifier := n.(Notifier).Select()
|
||||
// inLimits will return true if the notifier is within sending limits
|
||||
func inLimits(n Notifier) bool {
|
||||
notifier := n.Select()
|
||||
ok, _ := notifier.WithinLimits()
|
||||
return ok
|
||||
}
|
||||
|
||||
// WithinLimits returns true if the notifier is within its sending limits
|
||||
func (notify *Notification) WithinLimits() (bool, error) {
|
||||
if notify.SentLastMinute() == 0 {
|
||||
return true, nil
|
||||
|
@ -392,20 +406,24 @@ func (notify *Notification) WithinLimits() (bool, error) {
|
|||
return true, nil
|
||||
}
|
||||
|
||||
// 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
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
// Package plugin contains the interfaces to build your own Golang Plugin that will receive triggers on Statup events.
|
||||
package plugin
|
Loading…
Reference in New Issue