2020-03-04 10:29:00 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2020-03-04 14:20:47 +00:00
|
|
|
"errors"
|
2020-03-04 10:29:00 +00:00
|
|
|
"fmt"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/database"
|
|
|
|
"github.com/statping/statping/utils"
|
2020-03-06 03:03:18 +00:00
|
|
|
"sort"
|
2020-03-04 10:29:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = utils.Log
|
|
|
|
|
|
|
|
func DB() database.Database {
|
|
|
|
return database.DB().Model(&Service{})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Find(id int64) (*Service, error) {
|
2020-03-04 14:20:47 +00:00
|
|
|
srv := allServices[id]
|
|
|
|
if srv == nil {
|
|
|
|
return nil, errors.New("service not found")
|
|
|
|
}
|
|
|
|
return srv, nil
|
2020-03-04 10:29:00 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 08:27:51 +00:00
|
|
|
func all() []*Service {
|
2020-03-04 10:29:00 +00:00
|
|
|
var services []*Service
|
|
|
|
DB().Find(&services)
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
2020-03-05 08:27:51 +00:00
|
|
|
func All() map[int64]*Service {
|
|
|
|
return allServices
|
|
|
|
}
|
|
|
|
|
2020-03-06 03:03:18 +00:00
|
|
|
func AllInOrder() []Service {
|
|
|
|
var services []Service
|
|
|
|
for _, service := range allServices {
|
|
|
|
services = append(services, *service)
|
|
|
|
}
|
|
|
|
sort.Sort(ServiceOrder(services))
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
2020-03-04 10:29:00 +00:00
|
|
|
func (s *Service) Create() error {
|
2020-03-06 03:03:18 +00:00
|
|
|
err := DB().Create(s)
|
2020-03-04 14:20:47 +00:00
|
|
|
if err.Error() != nil {
|
2020-03-04 10:29:00 +00:00
|
|
|
log.Errorln(fmt.Sprintf("Failed to create service %v #%v: %v", s.Name, s.Id, err))
|
2020-03-04 14:20:47 +00:00
|
|
|
return err.Error()
|
2020-03-04 10:29:00 +00:00
|
|
|
}
|
|
|
|
allServices[s.Id] = s
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Update() error {
|
2020-03-06 03:03:18 +00:00
|
|
|
db := DB().Update(s)
|
2020-03-04 10:29:00 +00:00
|
|
|
|
|
|
|
allServices[s.Id] = s
|
|
|
|
|
|
|
|
if !s.AllowNotifications.Bool {
|
|
|
|
//for _, n := range CoreApp.Notifications {
|
|
|
|
// notif := n.(notifier.Notifier).Select()
|
|
|
|
// notif.ResetUniqueQueue(fmt.Sprintf("service_%v", s.Id))
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
s.Close()
|
|
|
|
s.SleepDuration = s.Duration()
|
2020-03-04 14:20:47 +00:00
|
|
|
go ServiceCheckQueue(allServices[s.Id], true)
|
2020-03-04 10:29:00 +00:00
|
|
|
|
|
|
|
//notifier.OnUpdatedService(s.Service)
|
|
|
|
|
|
|
|
return db.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Delete() error {
|
2020-03-06 03:03:18 +00:00
|
|
|
db := database.DB().Delete(s)
|
2020-03-04 10:29:00 +00:00
|
|
|
|
|
|
|
s.Close()
|
|
|
|
delete(allServices, s.Id)
|
|
|
|
//notifier.OnDeletedService(s.Service)
|
|
|
|
|
|
|
|
return db.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) DeleteFailures() error {
|
|
|
|
query := database.DB().Exec(`DELETE FROM failures WHERE service = ?`, s.Id)
|
|
|
|
return query.Error()
|
|
|
|
}
|