From b7940e72702b0329919c98f773f8b203e328f5a6 Mon Sep 17 00:00:00 2001 From: Hunter Long Date: Tue, 2 Oct 2018 00:26:49 -0700 Subject: [PATCH] checkins --- core/checkin.go | 19 ++++++++++++------- core/database.go | 4 +++- handlers/services.go | 2 +- types/checkin.go | 23 +++++++++++++++-------- types/types.go | 1 + 5 files changed, 32 insertions(+), 17 deletions(-) diff --git a/core/checkin.go b/core/checkin.go index 1e0d11a5..d89d26de 100644 --- a/core/checkin.go +++ b/core/checkin.go @@ -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 } diff --git a/core/database.go b/core/database.go index ff6dd0f1..0131184a 100644 --- a/core/database.go +++ b/core/database.go @@ -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(¬ifier.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{}, ¬ifier.Notification{}).Table("core").AutoMigrate(&types.Core{}) + tx = tx.AutoMigrate(&types.Service{}, &types.User{}, &types.Hit{}, &types.Failure{}, &types.Checkin{}, &types.CheckinHit{}, ¬ifier.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)) diff --git a/handlers/services.go b/handlers/services.go index 9e000d81..d2b7383f 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -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") diff --git a/types/checkin.go b/types/checkin.go index bcdaf027..9caa72d6 100644 --- a/types/checkin.go +++ b/types/checkin.go @@ -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) diff --git a/types/types.go b/types/types.go index 9dea420b..5f3a3082 100644 --- a/types/types.go +++ b/types/types.go @@ -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"` }