pull/78/head
Hunter Long 2018-10-02 00:26:49 -07:00
parent ed6fb12423
commit b7940e7270
5 changed files with 32 additions and 17 deletions

View File

@ -28,7 +28,7 @@ type Checkin struct {
}
func (c *Checkin) String() string {
return c.Api
return c.ApiKey
}
func ReturnCheckin(s *types.Checkin) *Checkin {
@ -39,7 +39,7 @@ func FindCheckin(api string) *types.Checkin {
for _, ser := range CoreApp.Services {
service := ser.Select()
for _, c := range service.Checkins {
if c.Api == api {
if c.ApiKey == api {
return c
}
}
@ -71,13 +71,18 @@ func SelectCheckinApi(api string) *Checkin {
return checkin
}
func (c *Checkin) Receivehit() {
c.Hits++
c.Last = time.Now()
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
}
func (c *Checkin) RecheckCheckinFailure(guard chan struct{}) {
between := time.Now().Sub(c.Last).Seconds()
between := time.Now().Sub(time.Now()).Seconds()
if between > float64(c.Interval) {
fmt.Println("rechecking every 15 seconds!")
c.CreateFailure()
@ -95,6 +100,6 @@ func (f *Checkin) CreateFailure() {
}
func (f *Checkin) Ago() string {
got, _ := timeago.TimeAgoWithTime(time.Now(), f.Last)
got, _ := timeago.TimeAgoWithTime(time.Now(), time.Now())
return got
}

View File

@ -277,6 +277,7 @@ func (c *DbConfig) CreateCore() *Core {
func (db *DbConfig) DropDatabase() error {
utils.Log(1, "Dropping Database Tables...")
err := DbSession.DropTableIfExists("checkins")
err = DbSession.DropTableIfExists("checkins_hits")
err = DbSession.DropTableIfExists("notifications")
err = DbSession.DropTableIfExists("core")
err = DbSession.DropTableIfExists("failures")
@ -290,6 +291,7 @@ func (db *DbConfig) DropDatabase() error {
func (db *DbConfig) CreateDatabase() error {
utils.Log(1, "Creating Database Tables...")
err := DbSession.CreateTable(&types.Checkin{})
err = DbSession.CreateTable(&types.CheckinHit{})
err = DbSession.CreateTable(&notifier.Notification{})
err = DbSession.Table("core").CreateTable(&types.Core{})
err = DbSession.CreateTable(&types.Failure{})
@ -315,7 +317,7 @@ func (db *DbConfig) MigrateDatabase() error {
if tx.Error != nil {
return tx.Error
}
tx = tx.AutoMigrate(&types.Service{}, &types.User{}, &types.Hit{}, &types.Failure{}, &types.Checkin{}, &notifier.Notification{}).Table("core").AutoMigrate(&types.Core{})
tx = tx.AutoMigrate(&types.Service{}, &types.User{}, &types.Hit{}, &types.Failure{}, &types.Checkin{}, &types.CheckinHit{}, &notifier.Notification{}).Table("core").AutoMigrate(&types.Core{})
if tx.Error != nil {
tx.Rollback()
utils.Log(3, fmt.Sprintf("Statup Database could not be migrated: %v", tx.Error))

View File

@ -266,7 +266,7 @@ func checkinCreateUpdateHandler(w http.ResponseWriter, r *http.Request) {
checkin := &types.Checkin{
Service: service.Id,
Interval: interval,
Api: utils.NewSHA1Hash(18),
ApiKey: utils.NewSHA1Hash(18),
}
checkin.Create()
executeResponse(w, r, "service.html", service, "/services")

View File

@ -20,17 +20,24 @@ import (
)
type Checkin struct {
Id int64 `gorm:"primary_key;column:id"`
Service int64 `gorm:"index;column:service"`
Interval int64 `gorm:"column:check_interval"`
Api string `gorm:"column:api"`
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
Hits int64 `json:"hits"`
Last time.Time `json:"last"`
Id int64 `gorm:"primary_key;column:id"`
Service int64 `gorm:"index;column:service"`
Interval int64 `gorm:"column:check_interval"`
GracePeriod int64 `gorm:"column:grace_period"`
ApiKey string `gorm:"column:api_key"`
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
Hits []CheckinHit `json:"hits"`
CheckinInterface `json:"-"`
}
type CheckinHit struct {
Id int64 `gorm:"primary_key;column:id"`
Checkin int64 `gorm:"index;column:checkin"`
From string `gorm:"column:from_location"`
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
}
type CheckinInterface interface {
// Database functions
Create() (int64, error)

View File

@ -26,6 +26,7 @@ type Hit struct {
Id int64 `gorm:"primary_key;column:id"`
Service int64 `gorm:"column:service"`
Latency float64 `gorm:"column:latency"`
PingTime float64 `gorm:"column:ping_time"`
CreatedAt time.Time `gorm:"column:created_at"`
}