statping/failures.go

55 lines
1.1 KiB
Go
Raw Normal View History

2018-06-10 01:31:13 +00:00
package main
2018-06-11 00:20:42 +00:00
import (
"github.com/ararog/timeago"
"time"
)
2018-06-10 01:31:13 +00:00
type Failure struct {
Id int
Issue string
Service int
CreatedAt time.Time
2018-06-11 00:20:42 +00:00
Ago string
2018-06-10 01:31:13 +00:00
}
2018-06-11 00:20:42 +00:00
func (s *Service) SelectAllFailures() []*Failure {
var tks []*Failure
rows, err := db.Query("SELECT * FROM failures WHERE service=$1 ORDER BY id DESC LIMIT 10", s.Id)
2018-06-10 01:31:13 +00:00
if err != nil {
panic(err)
}
for rows.Next() {
2018-06-11 00:20:42 +00:00
var tk Failure
err = rows.Scan(&tk.Id, &tk.Issue, &tk.Service, &tk.CreatedAt)
2018-06-10 01:31:13 +00:00
if err != nil {
panic(err)
}
2018-06-11 00:20:42 +00:00
tk.Ago, _ = timeago.TimeAgoWithTime(time.Now(), tk.CreatedAt)
tks = append(tks, &tk)
2018-06-10 01:31:13 +00:00
}
return tks
}
2018-06-11 00:20:42 +00:00
func CountFailures() int {
var amount int
db.QueryRow("SELECT COUNT(id) FROM failures;").Scan(&amount)
return amount
}
2018-06-10 01:31:13 +00:00
func (s *Service) TotalFailures() int {
var amount int
db.QueryRow("SELECT COUNT(id) FROM failures WHERE service=$1;", s.Id).Scan(&amount)
return amount
}
2018-06-10 03:44:47 +00:00
func (s *Service) TotalFailures24Hours() int {
var amount int
t := time.Now()
x := t.AddDate(0, 0, -1)
2018-06-11 00:20:42 +00:00
db.QueryRow("SELECT COUNT(id) FROM failures WHERE service=$1 AND created_at>=$2 AND created_at<$3;", s.Id, x, t).Scan(&amount)
2018-06-10 03:44:47 +00:00
return amount
}