pull/78/head
Hunter Long 2018-10-05 21:10:11 -07:00
parent f5e15258c8
commit 62f159695a
3 changed files with 57 additions and 11 deletions

View File

@ -227,6 +227,32 @@ func ExampleNotification_OnSuccess() {
// Output: 1 // 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 // Add any type of interface to the AddQueue function to be ran in the queue
func ExampleNotification_AddQueue() { func ExampleNotification_AddQueue() {
msg := fmt.Sprintf("this is a failing message as a string passing into AddQueue function") msg := fmt.Sprintf("this is a failing message as a string passing into AddQueue function")

View File

@ -28,10 +28,11 @@ import (
) )
var ( var (
AllCommunications []types.AllNotifiers AllCommunications []types.AllNotifiers // AllCommunications holds all the loaded notifiers
db *gorm.DB db *gorm.DB // db holds the Statup database connection
) )
// Notification contains all the fields for a Statup Notifier.
type Notification struct { type Notification struct {
Id int64 `gorm:"primary_key;column:id" json:"id"` Id int64 `gorm:"primary_key;column:id" json:"id"`
Method string `gorm:"column:method" json:"method"` Method string `gorm:"column:method" json:"method"`
@ -61,25 +62,29 @@ type Notification struct {
testable bool testable bool
} }
// NotificationForm contains the HTML fields for each variable/input you want the notifier to accept.
type NotificationForm struct { type NotificationForm struct {
Type string Type string // the html input type (text, password, email)
Title string Title string // include a title for ease of use
Placeholder string Placeholder string // add a placeholder for the input
DbField string DbField string // true variable key for input
SmallText string SmallText string // insert small text under a html input
Required bool Required bool // require this input on the html form
} }
// NotificationLog contains the normalized message from previously sent notifications
type NotificationLog struct { type NotificationLog struct {
Message string Message string
Time utils.Timestamp Time utils.Timestamp
Timestamp time.Time Timestamp time.Time
} }
// AddQueue will add any type of interface (json, string, struct, etc) into the Notifiers queue
func (n *Notification) AddQueue(msg interface{}) { func (n *Notification) AddQueue(msg interface{}) {
n.Queue = append(n.Queue, msg) n.Queue = append(n.Queue, msg)
} }
// CanTest returns true if the notifier implements the OnTest interface
func (n *Notification) CanTest() bool { func (n *Notification) CanTest() bool {
return n.testable return n.testable
} }
@ -94,6 +99,7 @@ func SetDB(d *gorm.DB) {
db = d db = d
} }
// asNotification accepts a Notifier and returns a Notification struct
func asNotification(n Notifier) *Notification { func asNotification(n Notifier) *Notification {
return n.Select() return n.Select()
} }
@ -124,6 +130,7 @@ func Load() []types.AllNotifiers {
return notifiers return notifiers
} }
// normalizeType will accept multiple interfaces and converts it into a string for logging
func normalizeType(ty interface{}) string { func normalizeType(ty interface{}) string {
switch v := ty.(type) { switch v := ty.(type) {
case int, int32, int64: 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{} { func (n *Notification) removeQueue(msg interface{}) interface{} {
var newArr []interface{} var newArr []interface{}
for _, q := range n.Queue { for _, q := range n.Queue {
@ -239,6 +247,7 @@ func Init(n Notifier) (*Notification, error) {
return notify, err return notify, err
} }
// startAllNotifiers will start the go routine for each loaded notifier
func startAllNotifiers() { func startAllNotifiers() {
for _, comm := range AllCommunications { for _, comm := range AllCommunications {
if isType(comm, new(Notifier)) { 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) { func Queue(n Notifier) {
notification := n.Select() notification := n.Select()
rateLimit := notification.Delay rateLimit := notification.Delay
@ -304,17 +314,19 @@ func (f *Notification) LastSent() time.Duration {
return since return since
} }
// SentLastHour returns the total amount of notifications sent in last 1 hour
func (f *Notification) SentLastHour() int { func (f *Notification) SentLastHour() int {
since := time.Now().Add(-1 * time.Hour) since := time.Now().Add(-1 * time.Hour)
return f.SentLast(since) return f.SentLast(since)
} }
// SentLastMinute returns the total amount of notifications sent in last 1 minute
func (f *Notification) SentLastMinute() int { func (f *Notification) SentLastMinute() int {
since := time.Now().Add(-1 * time.Minute) since := time.Now().Add(-1 * time.Minute)
return f.SentLast(since) 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 { func (f *Notification) SentLast(since time.Time) int {
sent := 0 sent := 0
for _, v := range f.Logs() { for _, v := range f.Logs() {
@ -367,12 +379,14 @@ func isEnabled(n interface{}) bool {
return notifier.Enabled return notifier.Enabled
} }
func inLimits(n interface{}) bool { // inLimits will return true if the notifier is within sending limits
notifier := n.(Notifier).Select() func inLimits(n Notifier) bool {
notifier := n.Select()
ok, _ := notifier.WithinLimits() ok, _ := notifier.WithinLimits()
return ok return ok
} }
// WithinLimits returns true if the notifier is within its sending limits
func (notify *Notification) WithinLimits() (bool, error) { func (notify *Notification) WithinLimits() (bool, error) {
if notify.SentLastMinute() == 0 { if notify.SentLastMinute() == 0 {
return true, nil return true, nil
@ -392,20 +406,24 @@ func (notify *Notification) WithinLimits() (bool, error) {
return true, nil return true, nil
} }
// ResetQueue will clear the notifiers Queue
func (n *Notification) ResetQueue() { func (n *Notification) ResetQueue() {
n.Queue = nil n.Queue = nil
} }
// start will start the go routine for the notifier queue
func (n *Notification) start() { func (n *Notification) start() {
n.Running = make(chan bool) n.Running = make(chan bool)
} }
// close will stop the go routine for queue
func (n *Notification) close() { func (n *Notification) close() {
if n.IsRunning() { if n.IsRunning() {
close(n.Running) close(n.Running)
} }
} }
// IsRunning will return true if the notifier is currently running a queue
func (n *Notification) IsRunning() bool { func (n *Notification) IsRunning() bool {
if n.Running == nil { if n.Running == nil {
return false return false

2
plugin/doc.go Normal file
View File

@ -0,0 +1,2 @@
// Package plugin contains the interfaces to build your own Golang Plugin that will receive triggers on Statup events.
package plugin