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"
|
|
|
|
"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-08-20 07:20:05 +00:00
|
|
|
type Checkin struct {
|
|
|
|
*types.Checkin
|
|
|
|
}
|
2018-06-30 00:57:05 +00:00
|
|
|
|
|
|
|
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-08-20 07:20:05 +00:00
|
|
|
func ReturnCheckin(s *types.Checkin) *Checkin {
|
|
|
|
return &Checkin{Checkin: s}
|
|
|
|
}
|
|
|
|
|
2018-07-14 02:37:39 +00:00
|
|
|
func FindCheckin(api string) *types.Checkin {
|
2018-09-08 22:16:26 +00:00
|
|
|
for _, ser := range CoreApp.Services {
|
2018-09-12 04:14:22 +00:00
|
|
|
service := ser.Select()
|
2018-09-08 22:16:26 +00:00
|
|
|
for _, c := range service.Checkins {
|
2018-10-02 07:26:49 +00:00
|
|
|
if c.ApiKey == api {
|
2018-06-30 00:57:05 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2018-06-22 06:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Checkin) Create() (int64, error) {
|
|
|
|
u.CreatedAt = time.Now()
|
2018-09-06 05:28:35 +00:00
|
|
|
row := checkinDB().Create(u)
|
2018-09-05 10:54:57 +00:00
|
|
|
if row.Error == nil {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func SelectCheckinApi(api string) *Checkin {
|
|
|
|
var checkin *Checkin
|
2018-09-05 10:54:57 +00:00
|
|
|
checkinDB().Where("api = ?", api).Find(&checkin)
|
2018-06-22 06:56:44 +00:00
|
|
|
return checkin
|
|
|
|
}
|
|
|
|
|
2018-10-02 07:26:49 +00:00
|
|
|
func (c *Checkin) CreateHit() (int64, error) {
|
|
|
|
c.CreatedAt = time.Now()
|
|
|
|
row := checkinDB().Create(c)
|
|
|
|
if row.Error == nil {
|
|
|
|
utils.Log(2, row.Error)
|
|
|
|
return 0, row.Error
|
|
|
|
}
|
|
|
|
return c.Id, row.Error
|
2018-06-22 06:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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!")
|
|
|
|
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 {
|
2018-10-02 07:26:49 +00:00
|
|
|
got, _ := timeago.TimeAgoWithTime(time.Now(), time.Now())
|
2018-06-22 06:56:44 +00:00
|
|
|
return got
|
|
|
|
}
|