mirror of https://github.com/statping/statping
checkins
parent
ed6fb12423
commit
b7940e7270
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"`
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue