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"
|
|
|
|
"github.com/hunterlong/statping/database"
|
|
|
|
"github.com/hunterlong/statping/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func All() []*Service {
|
|
|
|
var services []*Service
|
|
|
|
DB().Find(&services)
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Create() error {
|
2020-03-04 14:20:47 +00:00
|
|
|
err := DB().Create(&s)
|
|
|
|
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
|
|
|
|
|
2020-03-04 14:20:47 +00:00
|
|
|
go ServiceCheckQueue(allServices[s.Id], true)
|
2020-03-04 10:29:00 +00:00
|
|
|
reorderServices()
|
|
|
|
//notifications.OnNewService(s)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Update() error {
|
|
|
|
db := DB().Update(&s)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
reorderServices()
|
|
|
|
//notifier.OnUpdatedService(s.Service)
|
|
|
|
|
|
|
|
return db.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) Delete() error {
|
|
|
|
db := database.DB().Delete(&s)
|
|
|
|
|
|
|
|
s.Close()
|
|
|
|
delete(allServices, s.Id)
|
|
|
|
reorderServices()
|
|
|
|
//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()
|
|
|
|
}
|