mirror of https://github.com/statping/statping
checkins
parent
ed6fb12423
commit
b7940e7270
|
@ -28,7 +28,7 @@ type Checkin struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Checkin) String() string {
|
func (c *Checkin) String() string {
|
||||||
return c.Api
|
return c.ApiKey
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReturnCheckin(s *types.Checkin) *Checkin {
|
func ReturnCheckin(s *types.Checkin) *Checkin {
|
||||||
|
@ -39,7 +39,7 @@ func FindCheckin(api string) *types.Checkin {
|
||||||
for _, ser := range CoreApp.Services {
|
for _, ser := range CoreApp.Services {
|
||||||
service := ser.Select()
|
service := ser.Select()
|
||||||
for _, c := range service.Checkins {
|
for _, c := range service.Checkins {
|
||||||
if c.Api == api {
|
if c.ApiKey == api {
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,13 +71,18 @@ func SelectCheckinApi(api string) *Checkin {
|
||||||
return checkin
|
return checkin
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Checkin) Receivehit() {
|
func (c *Checkin) CreateHit() (int64, error) {
|
||||||
c.Hits++
|
c.CreatedAt = time.Now()
|
||||||
c.Last = 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{}) {
|
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) {
|
if between > float64(c.Interval) {
|
||||||
fmt.Println("rechecking every 15 seconds!")
|
fmt.Println("rechecking every 15 seconds!")
|
||||||
c.CreateFailure()
|
c.CreateFailure()
|
||||||
|
@ -95,6 +100,6 @@ func (f *Checkin) CreateFailure() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Checkin) Ago() string {
|
func (f *Checkin) Ago() string {
|
||||||
got, _ := timeago.TimeAgoWithTime(time.Now(), f.Last)
|
got, _ := timeago.TimeAgoWithTime(time.Now(), time.Now())
|
||||||
return got
|
return got
|
||||||
}
|
}
|
||||||
|
|
|
@ -277,6 +277,7 @@ func (c *DbConfig) CreateCore() *Core {
|
||||||
func (db *DbConfig) DropDatabase() error {
|
func (db *DbConfig) DropDatabase() error {
|
||||||
utils.Log(1, "Dropping Database Tables...")
|
utils.Log(1, "Dropping Database Tables...")
|
||||||
err := DbSession.DropTableIfExists("checkins")
|
err := DbSession.DropTableIfExists("checkins")
|
||||||
|
err = DbSession.DropTableIfExists("checkins_hits")
|
||||||
err = DbSession.DropTableIfExists("notifications")
|
err = DbSession.DropTableIfExists("notifications")
|
||||||
err = DbSession.DropTableIfExists("core")
|
err = DbSession.DropTableIfExists("core")
|
||||||
err = DbSession.DropTableIfExists("failures")
|
err = DbSession.DropTableIfExists("failures")
|
||||||
|
@ -290,6 +291,7 @@ func (db *DbConfig) DropDatabase() error {
|
||||||
func (db *DbConfig) CreateDatabase() error {
|
func (db *DbConfig) CreateDatabase() error {
|
||||||
utils.Log(1, "Creating Database Tables...")
|
utils.Log(1, "Creating Database Tables...")
|
||||||
err := DbSession.CreateTable(&types.Checkin{})
|
err := DbSession.CreateTable(&types.Checkin{})
|
||||||
|
err = DbSession.CreateTable(&types.CheckinHit{})
|
||||||
err = DbSession.CreateTable(¬ifier.Notification{})
|
err = DbSession.CreateTable(¬ifier.Notification{})
|
||||||
err = DbSession.Table("core").CreateTable(&types.Core{})
|
err = DbSession.Table("core").CreateTable(&types.Core{})
|
||||||
err = DbSession.CreateTable(&types.Failure{})
|
err = DbSession.CreateTable(&types.Failure{})
|
||||||
|
@ -315,7 +317,7 @@ func (db *DbConfig) MigrateDatabase() error {
|
||||||
if tx.Error != nil {
|
if tx.Error != nil {
|
||||||
return tx.Error
|
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 {
|
if tx.Error != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
utils.Log(3, fmt.Sprintf("Statup Database could not be migrated: %v", tx.Error))
|
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{
|
checkin := &types.Checkin{
|
||||||
Service: service.Id,
|
Service: service.Id,
|
||||||
Interval: interval,
|
Interval: interval,
|
||||||
Api: utils.NewSHA1Hash(18),
|
ApiKey: utils.NewSHA1Hash(18),
|
||||||
}
|
}
|
||||||
checkin.Create()
|
checkin.Create()
|
||||||
executeResponse(w, r, "service.html", service, "/services")
|
executeResponse(w, r, "service.html", service, "/services")
|
||||||
|
|
|
@ -23,14 +23,21 @@ type Checkin struct {
|
||||||
Id int64 `gorm:"primary_key;column:id"`
|
Id int64 `gorm:"primary_key;column:id"`
|
||||||
Service int64 `gorm:"index;column:service"`
|
Service int64 `gorm:"index;column:service"`
|
||||||
Interval int64 `gorm:"column:check_interval"`
|
Interval int64 `gorm:"column:check_interval"`
|
||||||
Api string `gorm:"column:api"`
|
GracePeriod int64 `gorm:"column:grace_period"`
|
||||||
|
ApiKey string `gorm:"column:api_key"`
|
||||||
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
|
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
|
||||||
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
|
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
|
||||||
Hits int64 `json:"hits"`
|
Hits []CheckinHit `json:"hits"`
|
||||||
Last time.Time `json:"last"`
|
|
||||||
CheckinInterface `json:"-"`
|
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 {
|
type CheckinInterface interface {
|
||||||
// Database functions
|
// Database functions
|
||||||
Create() (int64, error)
|
Create() (int64, error)
|
||||||
|
|
|
@ -26,6 +26,7 @@ type Hit struct {
|
||||||
Id int64 `gorm:"primary_key;column:id"`
|
Id int64 `gorm:"primary_key;column:id"`
|
||||||
Service int64 `gorm:"column:service"`
|
Service int64 `gorm:"column:service"`
|
||||||
Latency float64 `gorm:"column:latency"`
|
Latency float64 `gorm:"column:latency"`
|
||||||
|
PingTime float64 `gorm:"column:ping_time"`
|
||||||
CreatedAt time.Time `gorm:"column:created_at"`
|
CreatedAt time.Time `gorm:"column:created_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue