2018-06-30 00:57:05 +00:00
|
|
|
package core
|
2018-06-22 06:56:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"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-06-30 00:57:05 +00:00
|
|
|
type Checkin types.Checkin
|
|
|
|
|
|
|
|
func (c *Checkin) String() string {
|
|
|
|
return c.Api
|
|
|
|
}
|
|
|
|
|
2018-07-14 02:37:39 +00:00
|
|
|
func FindCheckin(api string) *types.Checkin {
|
|
|
|
for _, ser := range CoreApp.Services {
|
2018-07-17 09:18:20 +00:00
|
|
|
for _, c := range ser.ToService().Checkins {
|
2018-06-30 00:57:05 +00:00
|
|
|
if c.Api == api {
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2018-06-22 06:56:44 +00:00
|
|
|
}
|
|
|
|
|
2018-07-14 02:37:39 +00:00
|
|
|
func SelectAllCheckins(s *types.Service) []*types.Checkin {
|
|
|
|
var checkins []*types.Checkin
|
2018-06-30 00:57:05 +00:00
|
|
|
col := DbSession.Collection("checkins").Find("service", s.Id).OrderBy("-id")
|
2018-06-22 06:56:44 +00:00
|
|
|
col.All(&checkins)
|
|
|
|
s.Checkins = checkins
|
|
|
|
return checkins
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Checkin) Create() (int64, error) {
|
|
|
|
u.CreatedAt = time.Now()
|
2018-06-30 00:57:05 +00:00
|
|
|
uuid, err := DbSession.Collection("checkins").Insert(u)
|
2018-06-22 06:56:44 +00:00
|
|
|
if uuid == nil {
|
2018-06-30 00:57:05 +00:00
|
|
|
utils.Log(2, err)
|
2018-06-22 06:56:44 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
2018-06-24 00:30:59 +00:00
|
|
|
fmt.Println("new checkin: ", uuid)
|
2018-06-22 06:56:44 +00:00
|
|
|
return uuid.(int64), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func SelectCheckinApi(api string) *Checkin {
|
|
|
|
var checkin *Checkin
|
2018-06-30 00:57:05 +00:00
|
|
|
DbSession.Collection("checkins").Find("api", api).One(&checkin)
|
2018-06-22 06:56:44 +00:00
|
|
|
return checkin
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Checkin) Receivehit() {
|
|
|
|
c.Hits++
|
|
|
|
c.Last = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Checkin) RecheckCheckinFailure(guard chan struct{}) {
|
|
|
|
between := time.Now().Sub(c.Last).Seconds()
|
|
|
|
if between > float64(c.Interval) {
|
|
|
|
fmt.Println("rechecking every 15 seconds!")
|
|
|
|
c.CreateFailure()
|
|
|
|
time.Sleep(15 * time.Second)
|
|
|
|
guard <- struct{}{}
|
|
|
|
c.RecheckCheckinFailure(guard)
|
|
|
|
} else {
|
|
|
|
fmt.Println("i recovered!!")
|
|
|
|
}
|
|
|
|
<-guard
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Checkin) CreateFailure() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Checkin) Ago() string {
|
|
|
|
got, _ := timeago.TimeAgoWithTime(time.Now(), f.Last)
|
|
|
|
return got
|
|
|
|
}
|
|
|
|
|
2018-07-14 02:37:39 +00:00
|
|
|
//func (c *Checkin) Run() {
|
|
|
|
// if c.Interval == 0 {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// fmt.Println("checking: ", c.Api)
|
|
|
|
// between := time.Now().Sub(c.Last).Seconds()
|
|
|
|
// if between > float64(c.Interval) {
|
|
|
|
// guard := make(chan struct{})
|
|
|
|
// c.RecheckCheckinFailure(guard)
|
|
|
|
// <-guard
|
|
|
|
// }
|
|
|
|
// time.Sleep(1 * time.Second)
|
|
|
|
// c.Run()
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func (s *Service) StartCheckins() {
|
|
|
|
// for _, c := range s.Checkins {
|
|
|
|
// checkin := c.(*Checkin)
|
|
|
|
// go checkin.Run()
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//func CheckinProcess() {
|
|
|
|
// for _, s := range CoreApp.Services {
|
|
|
|
// for _, c := range s.Checkins {
|
|
|
|
// checkin := c
|
|
|
|
// go checkin.Run()
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//}
|