statping/services.go

211 lines
4.5 KiB
Go
Raw Normal View History

2018-06-10 01:31:13 +00:00
package main
import (
"crypto/sha1"
"encoding/json"
"fmt"
"math/rand"
2018-06-11 00:20:42 +00:00
"strconv"
2018-06-10 01:31:13 +00:00
"time"
2018-06-22 04:02:57 +00:00
"upper.io/db.v3"
2018-06-10 01:31:13 +00:00
)
2018-06-10 03:44:47 +00:00
var (
2018-06-10 04:09:20 +00:00
services []*Service
2018-06-10 03:44:47 +00:00
)
2018-06-10 01:31:13 +00:00
type Service struct {
2018-06-18 09:53:18 +00:00
Id int64 `db:"id,omitempty" json:"id"`
Name string `db:"name" json:"name"`
Domain string `db:"domain" json:"domain"`
Expected string `db:"expected" json:"expected"`
ExpectedStatus int `db:"expected_status" json:"expected_status"`
Interval int `db:"check_interval" json:"check_interval"`
Type string `db:"check_type" json:"type"`
Method string `db:"method" json:"method"`
2018-06-23 00:10:37 +00:00
PostData string `db:"post_data" json:"post_data"`
2018-06-18 09:53:18 +00:00
Port int `db:"port" json:"port"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
2018-06-15 04:30:10 +00:00
Online bool `json:"online"`
Latency float64 `json:"latency"`
Online24Hours float32 `json:"24_hours_online"`
AvgResponse string `json:"avg_response"`
TotalUptime string `json:"uptime"`
2018-06-23 00:10:37 +00:00
OrderId int64 `json:"order_id"`
2018-06-15 04:30:10 +00:00
Failures []*Failure `json:"failures"`
2018-06-22 06:56:44 +00:00
Checkins []*Checkin `json:"checkins"`
2018-06-23 00:10:37 +00:00
runRoutine bool
2018-06-23 08:42:50 +00:00
LastResponse string
LastStatusCode int
LastOnline time.Time
2018-06-10 01:31:13 +00:00
}
2018-06-22 04:02:57 +00:00
func serviceCol() db.Collection {
return dbSession.Collection("services")
}
2018-06-23 00:10:37 +00:00
func SelectService(id int64) *Service {
for _, s := range services {
if s.Id == id {
return s
}
}
return nil
2018-06-10 01:31:13 +00:00
}
2018-06-15 04:30:10 +00:00
func SelectAllServices() ([]*Service, error) {
2018-06-23 00:10:37 +00:00
var srvcs []*Service
2018-06-22 04:02:57 +00:00
col := serviceCol().Find()
2018-06-23 00:10:37 +00:00
err := col.All(&srvcs)
for _, s := range srvcs {
2018-06-22 06:56:44 +00:00
s.Checkins = s.SelectAllCheckins()
2018-06-23 00:10:37 +00:00
s.Failures = s.SelectAllFailures()
2018-06-22 06:56:44 +00:00
}
2018-06-23 00:10:37 +00:00
return srvcs, err
2018-06-10 01:31:13 +00:00
}
func (s *Service) AvgTime() float64 {
2018-06-15 04:30:10 +00:00
total, _ := s.TotalHits()
if total == 0 {
return float64(0)
}
sum, _ := s.Sum()
2018-06-10 01:31:13 +00:00
avg := sum / float64(total) * 100
2018-06-15 04:30:10 +00:00
amount := fmt.Sprintf("%0.0f", avg*10)
val, _ := strconv.ParseFloat(amount, 10)
return val
2018-06-10 01:31:13 +00:00
}
2018-06-11 00:20:42 +00:00
func (s *Service) Online24() float32 {
2018-06-15 04:30:10 +00:00
total, _ := s.TotalHits()
failed, _ := s.TotalFailures24Hours()
2018-06-10 03:44:47 +00:00
if failed == 0 {
s.Online24Hours = 100.00
return s.Online24Hours
}
2018-06-10 04:21:12 +00:00
if total == 0 {
s.Online24Hours = 0
return s.Online24Hours
}
2018-06-10 03:44:47 +00:00
avg := float64(failed) / float64(total) * 100
2018-06-11 00:20:42 +00:00
avg = 100 - avg
if avg < 0 {
avg = 0
}
amount, _ := strconv.ParseFloat(fmt.Sprintf("%0.2f", avg), 10)
s.Online24Hours = float32(amount)
return s.Online24Hours
2018-06-10 03:44:47 +00:00
}
2018-06-10 01:31:13 +00:00
type GraphJson struct {
X string `json:"x"`
Y float64 `json:"y"`
}
func (s *Service) GraphData() string {
2018-06-15 04:30:10 +00:00
var d []GraphJson
2018-06-22 04:02:57 +00:00
hits, _ := s.LimitedHits()
2018-06-15 04:30:10 +00:00
for _, h := range hits {
2018-06-10 01:31:13 +00:00
val := h.CreatedAt
2018-06-15 04:30:10 +00:00
o := GraphJson{
2018-06-10 01:31:13 +00:00
X: val.String(),
2018-06-15 04:30:10 +00:00
Y: h.Latency * 1000,
2018-06-10 01:31:13 +00:00
}
d = append(d, o)
}
data, _ := json.Marshal(d)
2018-06-19 04:48:25 +00:00
return string(data)
2018-06-10 01:31:13 +00:00
}
2018-06-11 00:20:42 +00:00
func (s *Service) AvgUptime() string {
2018-06-15 04:30:10 +00:00
failed, _ := s.TotalFailures()
total, _ := s.TotalHits()
2018-06-10 01:31:13 +00:00
if failed == 0 {
2018-06-22 04:02:57 +00:00
s.TotalUptime = "100"
2018-06-10 01:31:13 +00:00
return s.TotalUptime
}
2018-06-10 04:21:12 +00:00
if total == 0 {
2018-06-11 00:20:42 +00:00
s.TotalUptime = "0"
2018-06-10 04:21:12 +00:00
return s.TotalUptime
}
2018-06-10 01:31:13 +00:00
percent := float64(failed) / float64(total) * 100
2018-06-11 00:20:42 +00:00
percent = 100 - percent
if percent < 0 {
percent = 0
}
s.TotalUptime = fmt.Sprintf("%0.2f", percent)
2018-06-22 04:02:57 +00:00
if s.TotalUptime == "100.00" {
s.TotalUptime = "100"
}
2018-06-11 00:20:42 +00:00
return s.TotalUptime
2018-06-10 01:31:13 +00:00
}
2018-06-23 00:10:37 +00:00
func (u *Service) RemoveArray() []*Service {
var srvcs []*Service
for _, s := range services {
if s.Id != u.Id {
srvcs = append(srvcs, s)
}
}
services = srvcs
return srvcs
}
2018-06-15 04:30:10 +00:00
func (u *Service) Delete() error {
2018-06-22 04:02:57 +00:00
res := serviceCol().Find("id", u.Id)
2018-06-15 04:30:10 +00:00
err := res.Delete()
2018-06-23 00:10:37 +00:00
u.RemoveArray()
2018-06-19 04:48:25 +00:00
OnDeletedService(u)
2018-06-15 04:30:10 +00:00
return err
2018-06-11 03:41:02 +00:00
}
func (u *Service) Update() {
2018-06-19 04:48:25 +00:00
OnUpdateService(u)
2018-06-11 03:41:02 +00:00
}
2018-06-15 04:30:10 +00:00
func (u *Service) Create() (int64, error) {
u.CreatedAt = time.Now()
2018-06-22 04:02:57 +00:00
uuid, err := serviceCol().Insert(u)
2018-06-15 04:30:10 +00:00
if uuid == nil {
return 0, err
2018-06-10 03:44:47 +00:00
}
2018-06-22 08:48:47 +00:00
u.Id = uuid.(int64)
services = append(services, u)
2018-06-23 00:10:37 +00:00
go u.CheckQueue()
2018-06-19 04:48:25 +00:00
OnNewService(u)
2018-06-15 04:30:10 +00:00
return uuid.(int64), err
2018-06-10 01:31:13 +00:00
}
2018-06-11 00:20:42 +00:00
func CountOnline() int {
amount := 0
for _, v := range services {
if v.Online {
amount++
}
}
return amount
}
2018-06-10 01:31:13 +00:00
func NewSHA1Hash(n ...int) string {
noRandomCharacters := 32
if len(n) > 0 {
noRandomCharacters = n[0]
}
randString := RandomString(noRandomCharacters)
hash := sha1.New()
hash.Write([]byte(randString))
bs := hash.Sum(nil)
return fmt.Sprintf("%x", bs)
}
var characterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
// RandomString generates a random string of n length
func RandomString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = characterRunes[rand.Intn(len(characterRunes))]
}
return string(b)
}