statping/core/services.go

368 lines
9.9 KiB
Go
Raw Normal View History

2018-12-04 05:57:11 +00:00
// Statping
2018-08-16 06:22:20 +00:00
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
2018-12-04 04:17:29 +00:00
// https://github.com/hunterlong/statping
2018-08-16 06:22:20 +00:00
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2018-06-30 00:57:05 +00:00
package core
2018-06-10 01:31:13 +00:00
import (
"fmt"
2018-12-04 04:17:29 +00:00
"github.com/hunterlong/statping/core/notifier"
2020-02-22 23:52:05 +00:00
"github.com/hunterlong/statping/database"
2018-12-04 04:17:29 +00:00
"github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils"
"sort"
2018-06-11 00:20:42 +00:00
"strconv"
2018-06-10 01:31:13 +00:00
"time"
)
type Service struct {
2018-08-19 00:37:00 +00:00
*types.Service
2018-07-14 02:37:39 +00:00
}
// Select will return the *types.Service struct for Service
2018-09-12 04:14:22 +00:00
func (s *Service) Select() *types.Service {
return s.Service
}
// ReturnService will convert *types.Service to *core.Service
func ReturnService(s *types.Service) *Service {
return &Service{s}
2018-06-22 04:02:57 +00:00
}
2018-10-11 16:53:13 +00:00
func Services() []types.ServiceInterface {
return CoreApp.Services
}
// SelectService returns a *core.Service from in memory
2018-06-23 00:10:37 +00:00
func SelectService(id int64) *Service {
2018-11-09 05:29:01 +00:00
for _, s := range Services() {
2018-09-12 04:14:22 +00:00
if s.Select().Id == id {
2020-02-04 03:49:17 +00:00
service := s.(*Service)
service = service.UpdateStats()
return service
2018-06-23 00:10:37 +00:00
}
}
return nil
2018-06-10 01:31:13 +00:00
}
2020-02-24 05:53:15 +00:00
func (s *Service) GetFailures(count int) []*Failure {
var fails []*Failure
db := Database(&types.Failure{}).Where("service = ?", s.Id)
db.Limit(count).Find(&fails)
return fails
}
2020-02-04 03:49:17 +00:00
func (s *Service) UpdateStats() *Service {
s.Online24Hours = s.OnlineDaysPercent(1)
s.Online7Days = s.OnlineDaysPercent(7)
s.AvgResponse = s.AvgTime()
s.FailuresLast24Hours = s.FailuresDaysAgo(1)
s.LastFailure = s.lastFailure()
return s
}
func SelectServices(auth bool) []*Service {
var validServices []*Service
for _, sr := range CoreApp.Services {
s := sr.(*Service)
if !s.Public.Bool {
if auth {
validServices = append(validServices, s)
}
} else {
validServices = append(validServices, s)
}
}
return validServices
}
2019-02-06 18:51:30 +00:00
// SelectServiceLink returns a *core.Service from the service permalink
func SelectServiceLink(permalink string) *Service {
for _, s := range Services() {
if s.Select().Permalink.String == permalink {
return s.(*Service)
}
}
return nil
}
2018-10-07 22:38:56 +00:00
// CheckinProcess runs the checkin routine for each checkin attached to service
func (s *Service) CheckinProcess() {
2018-12-06 19:03:55 +00:00
checkins := s.AllCheckins()
2018-10-07 22:38:56 +00:00
for _, c := range checkins {
c.Start()
go c.Routine()
}
}
2018-12-06 19:03:55 +00:00
// AllCheckins will return a slice of AllCheckins for a Service
func (s *Service) AllCheckins() []*Checkin {
2018-10-07 22:38:56 +00:00
var checkin []*Checkin
2020-02-19 04:07:22 +00:00
Database(&Checkin{}).Where("service = ?", s.Id).Find(&checkin)
2018-10-06 03:35:53 +00:00
return checkin
2018-10-02 08:39:47 +00:00
}
2020-02-24 05:53:15 +00:00
// SelectAllServices returns a slice of *core.Service to be store on []*core.Services
// should only be called once on startup.
2018-10-11 16:53:13 +00:00
func (c *Core) SelectAllServices(start bool) ([]*Service, error) {
var services []*Service
2020-02-19 04:07:22 +00:00
db := Database(&Service{}).Find(&services).Order("order_id desc")
2020-01-26 21:01:43 +00:00
if db.Error() != nil {
log.Errorln(fmt.Sprintf("service error: %v", db.Error()))
return nil, db.Error()
2018-06-30 00:57:05 +00:00
}
CoreApp.Services = nil
for _, service := range services {
2018-10-11 16:53:13 +00:00
if start {
service.Start()
service.CheckinProcess()
}
2020-02-24 05:53:15 +00:00
fails := service.GetFailures(limitedFailures)
for _, f := range fails {
service.Failures = append(service.Failures, f)
2018-11-08 10:50:06 +00:00
}
2018-12-06 19:03:55 +00:00
checkins := service.AllCheckins()
for _, c := range checkins {
2020-02-24 05:53:15 +00:00
c.Failures = c.GetFailures(limitedFailures)
c.Hits = c.LimitedHits(limitedHits)
2018-12-06 19:03:55 +00:00
service.Checkins = append(service.Checkins, c)
}
2020-02-04 03:49:17 +00:00
// collect initial service stats
service = service.UpdateStats()
CoreApp.Services = append(CoreApp.Services, service)
2018-06-22 06:56:44 +00:00
}
2018-12-06 19:03:55 +00:00
reorderServices()
2020-01-26 21:01:43 +00:00
return services, db.Error()
}
// reorderServices will sort the services based on 'order_id'
func reorderServices() {
sort.Sort(ServiceOrder(CoreApp.Services))
}
// AvgTime will return the average amount of time for a service to response back successfully
2020-02-04 03:49:17 +00:00
func (s *Service) AvgTime() float64 {
2018-06-15 04:30:10 +00:00
total, _ := s.TotalHits()
if total == 0 {
2020-02-04 03:49:17 +00:00
return 0
2018-06-15 04:30:10 +00:00
}
2020-02-04 03:49:17 +00:00
avg := s.Sum() / float64(total) * 100
f, _ := strconv.ParseFloat(fmt.Sprintf("%0.0f", avg*10), 32)
return f
}
// OnlineDaysPercent returns the service's uptime percent within last 24 hours
func (s *Service) OnlineDaysPercent(days int) float32 {
2020-01-04 02:03:59 +00:00
ago := time.Now().UTC().Add((-24 * time.Duration(days)) * time.Hour)
return s.OnlineSince(ago)
2018-06-10 01:31:13 +00:00
}
// OnlineSince accepts a time since parameter to return the percent of a service's uptime.
func (s *Service) OnlineSince(ago time.Time) float32 {
failed, _ := s.TotalFailuresSince(ago)
2018-06-10 03:44:47 +00:00
if failed == 0 {
s.Online24Hours = 100.00
return s.Online24Hours
}
total, _ := s.TotalHitsSince(ago)
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-12-06 19:03:55 +00:00
// lastFailure returns the last Failure a service had
2020-02-24 05:53:15 +00:00
func (s *Service) lastFailure() types.FailureInterface {
limited := s.GetFailures(1)
2018-09-18 22:02:27 +00:00
if len(limited) == 0 {
return nil
}
last := limited[len(limited)-1]
return last
2018-07-14 02:37:39 +00:00
}
// DowntimeText will return the amount of downtime for a service based on the duration
2018-10-08 04:27:36 +00:00
// service.DowntimeText()
// // Service has been offline for 15 minutes
2018-09-18 22:02:27 +00:00
func (s *Service) DowntimeText() string {
2018-09-19 06:12:42 +00:00
return fmt.Sprintf("%v has been offline for %v", s.Name, utils.DurationReadable(s.Downtime()))
2018-09-18 22:02:27 +00:00
}
// Dbtimestamp will return a SQL query for grouping by date
func Dbtimestamp(group string, column string) string {
2018-11-25 03:56:09 +00:00
seconds := 3600
2018-11-02 23:19:52 +00:00
switch group {
case "minute":
seconds = 60
2018-11-02 23:19:52 +00:00
case "hour":
seconds = 3600
2018-11-02 23:19:52 +00:00
case "day":
seconds = 86400
2018-11-02 23:19:52 +00:00
case "week":
seconds = 604800
case "month":
seconds = 2592000
case "year":
seconds = 31557600
2018-11-02 23:19:52 +00:00
default:
seconds = 60
}
2019-12-30 08:08:51 +00:00
switch CoreApp.Config.DbConn {
case "mysql":
return fmt.Sprintf("CONCAT(date_format(created_at, '%%Y-%%m-%%d %%H:00:00')) AS timeframe, AVG(%v) AS value", column)
case "postgres":
return fmt.Sprintf("date_trunc('%v', created_at) AS timeframe, AVG(%v) AS value", group, column)
default:
2018-11-02 23:19:52 +00:00
return fmt.Sprintf("datetime((strftime('%%s', created_at) / %v) * %v, 'unixepoch') AS timeframe, AVG(%v) as value", seconds, seconds, column)
}
}
2018-09-18 22:02:27 +00:00
// Downtime returns the amount of time of a offline service
2018-09-16 10:29:52 +00:00
func (s *Service) Downtime() time.Duration {
hits, _ := s.Hits()
2018-10-27 09:27:09 +00:00
fail := s.lastFailure()
if fail == nil {
2018-09-16 10:29:52 +00:00
return time.Duration(0)
}
2018-09-19 06:12:42 +00:00
if len(hits) == 0 {
2020-02-24 05:53:15 +00:00
return time.Now().UTC().Sub(fail.Select().CreatedAt.UTC())
2018-09-19 06:12:42 +00:00
}
2020-02-24 05:53:15 +00:00
since := fail.Select().CreatedAt.UTC().Sub(hits[0].CreatedAt.UTC())
2018-09-16 10:29:52 +00:00
return since
}
2020-02-24 05:53:15 +00:00
// GraphData will return all hits or failures
func GraphData(q *database.GroupQuery, dbType interface{}, by database.By) []*database.TimeValue {
dbQuery, err := q.Database().GroupQuery(q, by).ToTimeValue(dbType)
2020-02-21 05:36:23 +00:00
if err != nil {
log.Error(err)
2020-02-22 23:52:05 +00:00
return nil
2020-02-21 05:36:23 +00:00
}
2020-02-24 05:53:15 +00:00
if q.FillEmpty {
return dbQuery.FillMissing(q.Start, q.End)
}
2020-02-24 05:53:15 +00:00
return dbQuery.ToValues()
2018-06-10 01:31:13 +00:00
}
// index returns a services index int for updating the []*core.Services slice
func (s *Service) index() int {
for k, service := range CoreApp.Services {
if s.Id == service.(*Service).Id {
return k
}
}
return 0
}
// updateService will update a service in the []*core.Services slice
2019-01-09 04:20:43 +00:00
func updateService(s *Service) {
CoreApp.Services[s.index()] = s
}
// Delete will remove a service from the database, it will also end the service checking go routine
2018-10-07 05:04:06 +00:00
func (s *Service) Delete() error {
i := s.index()
2020-02-19 04:07:22 +00:00
err := Database(&Service{}).Delete(s)
2020-01-26 21:01:43 +00:00
if err.Error() != nil {
2020-02-19 04:07:22 +00:00
log.Errorln(fmt.Sprintf("Failed to delete service %v. %v", s.Name, err.Error()))
2020-01-26 21:01:43 +00:00
return err.Error()
2018-06-30 00:57:05 +00:00
}
2018-10-07 05:04:06 +00:00
s.Close()
slice := CoreApp.Services
CoreApp.Services = append(slice[:i], slice[i+1:]...)
reorderServices()
2018-10-07 05:04:06 +00:00
notifier.OnDeletedService(s.Service)
2020-01-26 21:01:43 +00:00
return err.Error()
}
// Update will update a service in the database, the service's checking routine can be restarted by passing true
2018-10-07 05:04:06 +00:00
func (s *Service) Update(restart bool) error {
2020-02-19 04:07:22 +00:00
err := Database(&Service{}).Update(&s)
2020-01-26 21:01:43 +00:00
if err.Error() != nil {
log.Errorln(fmt.Sprintf("Failed to update service %v. %v", s.Name, err))
2020-01-26 21:01:43 +00:00
return err.Error()
}
// clear the notification queue for a service
if !s.AllowNotifications.Bool {
for _, n := range CoreApp.Notifications {
2018-11-29 16:26:42 +00:00
notif := n.(notifier.Notifier).Select()
2019-03-04 17:18:50 +00:00
notif.ResetUniqueQueue(fmt.Sprintf("service_%v", s.Id))
}
}
if restart {
2018-10-07 05:04:06 +00:00
s.Close()
s.Start()
s.SleepDuration = time.Duration(s.Interval) * time.Second
go s.CheckQueue(true)
}
reorderServices()
2018-10-07 05:04:06 +00:00
updateService(s)
notifier.OnUpdatedService(s.Service)
2020-01-26 21:01:43 +00:00
return err.Error()
}
// Create will create a service and insert it into the database
2018-10-07 05:04:06 +00:00
func (s *Service) Create(check bool) (int64, error) {
2020-01-04 02:03:59 +00:00
s.CreatedAt = time.Now().UTC()
2020-02-19 04:07:22 +00:00
db := Database(&Service{}).Create(s)
2020-01-26 21:01:43 +00:00
if db.Error() != nil {
log.Errorln(fmt.Sprintf("Failed to create service %v #%v: %v", s.Name, s.Id, db.Error()))
return 0, db.Error()
2018-06-10 03:44:47 +00:00
}
2018-10-07 05:04:06 +00:00
s.Start()
go s.CheckQueue(check)
CoreApp.Services = append(CoreApp.Services, s)
reorderServices()
2018-10-07 05:04:06 +00:00
notifier.OnNewService(s.Service)
return s.Id, nil
2018-06-10 01:31:13 +00:00
}
// Messages returns all Messages for a Service
func (s *Service) Messages() []*Message {
messages := SelectServiceMessages(s.Id)
return messages
}
2019-02-01 19:57:59 +00:00
// ActiveMessages returns all service messages that are available based on the current time
func (s *Service) ActiveMessages() []*Message {
var messages []*Message
msgs := SelectServiceMessages(s.Id)
for _, m := range msgs {
if m.StartOn.UTC().After(time.Now().UTC()) {
messages = append(messages, m)
}
}
return messages
}
2019-02-01 19:57:59 +00:00
// CountOnline returns the amount of services online
func (c *Core) CountOnline() int {
2018-06-11 00:20:42 +00:00
amount := 0
for _, s := range CoreApp.Services {
2018-09-12 04:14:22 +00:00
if s.Select().Online {
2018-06-11 00:20:42 +00:00
amount++
}
}
return amount
2018-06-25 07:09:31 +00:00
}