2018-08-16 06:22:20 +00:00
|
|
|
// Statup
|
|
|
|
// Copyright (C) 2018. Hunter Long and the project contributors
|
|
|
|
// Written by Hunter Long <info@socialeck.com> and the project contributors
|
|
|
|
//
|
|
|
|
// https://github.com/hunterlong/statup
|
|
|
|
//
|
|
|
|
// The licenses for most software and other practical works are designed
|
|
|
|
// to take away your freedom to share and change the works. By contrast,
|
|
|
|
// the GNU General Public License is intended to guarantee your freedom to
|
|
|
|
// share and change all versions of a program--to make sure it remains free
|
|
|
|
// software for all its users.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2018-06-30 00:57:05 +00:00
|
|
|
package core
|
2018-06-22 06:56:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-10-06 09:50:39 +00:00
|
|
|
"github.com/ararog/timeago"
|
2018-06-30 00:57:05 +00:00
|
|
|
"github.com/hunterlong/statup/types"
|
|
|
|
"github.com/hunterlong/statup/utils"
|
2018-06-22 06:56:44 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-10-07 04:48:33 +00:00
|
|
|
type checkin struct {
|
2018-08-20 07:20:05 +00:00
|
|
|
*types.Checkin
|
|
|
|
}
|
2018-06-30 00:57:05 +00:00
|
|
|
|
2018-10-07 04:48:33 +00:00
|
|
|
type checkinHit struct {
|
2018-10-03 10:47:32 +00:00
|
|
|
*types.CheckinHit
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:48:33 +00:00
|
|
|
// String will return a checkin API string
|
|
|
|
func (c *checkin) String() string {
|
2018-10-02 07:26:49 +00:00
|
|
|
return c.ApiKey
|
2018-06-30 00:57:05 +00:00
|
|
|
}
|
|
|
|
|
2018-10-07 04:48:33 +00:00
|
|
|
// ReturnCheckin converts *types.Checking to *core.checkin
|
|
|
|
func ReturnCheckin(s *types.Checkin) *checkin {
|
|
|
|
return &checkin{Checkin: s}
|
2018-08-20 07:20:05 +00:00
|
|
|
}
|
|
|
|
|
2018-10-07 04:48:33 +00:00
|
|
|
// ReturnCheckinHit converts *types.checkinHit to *core.checkinHit
|
|
|
|
func ReturnCheckinHit(h *types.CheckinHit) *checkinHit {
|
|
|
|
return &checkinHit{CheckinHit: h}
|
2018-10-03 10:47:32 +00:00
|
|
|
}
|
|
|
|
|
2018-10-07 04:48:33 +00:00
|
|
|
// SelectCheckin will find a checkin based on the API supplied
|
|
|
|
func SelectCheckin(api string) *checkin {
|
|
|
|
var checkin checkin
|
2018-10-03 10:47:32 +00:00
|
|
|
checkinDB().Where("api_key = ?", api).First(&checkin)
|
|
|
|
return &checkin
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:48:33 +00:00
|
|
|
// Period will return the duration of the checkin interval
|
|
|
|
func (c *checkin) Period() time.Duration {
|
|
|
|
duration, _ := time.ParseDuration(fmt.Sprintf("%vs", c.Interval))
|
2018-10-04 08:18:55 +00:00
|
|
|
return duration
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:48:33 +00:00
|
|
|
// Grace will return the duration of the checkin Grace Period (after service hasn't responded, wait a bit for a response)
|
|
|
|
func (c *checkin) Grace() time.Duration {
|
|
|
|
duration, _ := time.ParseDuration(fmt.Sprintf("%vs", c.GracePeriod))
|
2018-10-04 08:18:55 +00:00
|
|
|
return duration
|
|
|
|
}
|
|
|
|
|
2018-10-06 06:38:33 +00:00
|
|
|
// Expected returns the duration of when the serviec should receive a checkin
|
2018-10-07 04:48:33 +00:00
|
|
|
func (c *checkin) Expected() time.Duration {
|
|
|
|
last := c.Last().CreatedAt
|
2018-10-04 08:18:55 +00:00
|
|
|
now := time.Now()
|
|
|
|
lastDir := now.Sub(last)
|
2018-10-07 04:48:33 +00:00
|
|
|
sub := time.Duration(c.Period() - lastDir)
|
2018-10-04 08:18:55 +00:00
|
|
|
return sub
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:48:33 +00:00
|
|
|
// Last returns the last checkinHit for a checkin
|
|
|
|
func (c *checkin) Last() checkinHit {
|
|
|
|
var hit checkinHit
|
|
|
|
checkinHitsDB().Where("checkin = ?", c.Id).Last(&hit)
|
2018-10-04 08:18:55 +00:00
|
|
|
return hit
|
2018-06-22 06:56:44 +00:00
|
|
|
}
|
|
|
|
|
2018-10-07 04:48:33 +00:00
|
|
|
// Hits returns all of the CheckinHits for a given checkin
|
|
|
|
func (c *checkin) Hits() []checkinHit {
|
|
|
|
var checkins []checkinHit
|
|
|
|
checkinHitsDB().Where("checkin = ?", c.Id).Order("id DESC").Find(&checkins)
|
2018-10-03 10:47:32 +00:00
|
|
|
return checkins
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:48:33 +00:00
|
|
|
// Create will create a new checkin
|
|
|
|
func (c *checkin) Create() (int64, error) {
|
|
|
|
c.ApiKey = utils.RandomString(7)
|
|
|
|
row := checkinDB().Create(&c)
|
2018-10-06 03:35:53 +00:00
|
|
|
if row.Error != nil {
|
|
|
|
utils.Log(2, row.Error)
|
|
|
|
return 0, row.Error
|
2018-10-04 08:18:55 +00:00
|
|
|
}
|
2018-10-07 04:48:33 +00:00
|
|
|
return c.Id, row.Error
|
2018-10-06 03:35:53 +00:00
|
|
|
}
|
|
|
|
|
2018-10-07 04:48:33 +00:00
|
|
|
// Update will update a checkin
|
|
|
|
func (u *checkin) Update() (int64, error) {
|
2018-10-06 03:35:53 +00:00
|
|
|
row := checkinDB().Update(&u)
|
2018-10-05 04:28:38 +00:00
|
|
|
if row.Error != nil {
|
2018-09-05 10:54:57 +00:00
|
|
|
utils.Log(2, row.Error)
|
|
|
|
return 0, row.Error
|
2018-06-22 06:56:44 +00:00
|
|
|
}
|
2018-09-05 10:54:57 +00:00
|
|
|
return u.Id, row.Error
|
2018-06-22 06:56:44 +00:00
|
|
|
}
|
|
|
|
|
2018-10-07 04:48:33 +00:00
|
|
|
// Create will create a new successful checkinHit
|
|
|
|
func (u *checkinHit) Create() (int64, error) {
|
2018-10-04 08:18:55 +00:00
|
|
|
if u.CreatedAt.IsZero() {
|
|
|
|
u.CreatedAt = time.Now()
|
|
|
|
}
|
2018-10-03 10:47:32 +00:00
|
|
|
row := checkinHitsDB().Create(u)
|
2018-10-05 06:04:26 +00:00
|
|
|
if row.Error != nil {
|
2018-10-03 10:47:32 +00:00
|
|
|
utils.Log(2, row.Error)
|
|
|
|
return 0, row.Error
|
|
|
|
}
|
|
|
|
return u.Id, row.Error
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:48:33 +00:00
|
|
|
// Ago returns the duration of time between now and the last successful checkinHit
|
|
|
|
func (f *checkinHit) Ago() string {
|
2018-10-06 09:50:39 +00:00
|
|
|
got, _ := timeago.TimeAgoWithTime(time.Now(), f.CreatedAt)
|
|
|
|
return got
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:48:33 +00:00
|
|
|
// RecheckCheckinFailure will check if a Service checkin has been reported yet
|
|
|
|
func (c *checkin) RecheckCheckinFailure(guard chan struct{}) {
|
2018-10-02 07:26:49 +00:00
|
|
|
between := time.Now().Sub(time.Now()).Seconds()
|
2018-06-22 06:56:44 +00:00
|
|
|
if between > float64(c.Interval) {
|
|
|
|
fmt.Println("rechecking every 15 seconds!")
|
|
|
|
time.Sleep(15 * time.Second)
|
|
|
|
guard <- struct{}{}
|
|
|
|
c.RecheckCheckinFailure(guard)
|
|
|
|
} else {
|
|
|
|
fmt.Println("i recovered!!")
|
|
|
|
}
|
|
|
|
<-guard
|
|
|
|
}
|