StatusApp/db/ping.go

33 lines
721 B
Go

package db
import (
"time"
"status/check"
)
//PingEntry represents an entry of Ping that is persisted in the DB.
type PingEntry struct {
2020-04-23 07:41:57 +07:00
ServiceName string `json:"service_name"`
Time time.Time `json:"query_time"`
Ping int64 `json:"ping"`
Status check.Result `json:"status"`
}
func (p PingEntry) AddToDB() {
if db == nil {
panic("DB is nil")
}
db.Create(&p)
}
2020-04-23 07:41:57 +07:00
func GetFromDB(serviceName string, from, to time.Time) []PingEntry {
entries := make([]PingEntry, 0)
if serviceName == "" {
db.Where("time BETWEEN ? AND ?", from, to).Find(&entries)
} else {
db.Where("service_name = ? AND time BETWEEN ? AND ?", serviceName, from, to).Find(&entries)
}
return entries
}