statping/core/services.go

327 lines
9.3 KiB
Go
Raw Normal View History

2018-08-16 06:22:20 +00:00
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statup
//
// 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 (
"encoding/json"
"fmt"
"github.com/hunterlong/statup/notifiers"
2018-06-30 00:57:05 +00:00
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/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
}
func ReturnService(s *types.Service) *Service {
return &Service{s}
2018-06-22 04:02:57 +00:00
}
// SelectService returns a *core.Service from in memory
2018-06-23 00:10:37 +00:00
func SelectService(id int64) *Service {
for _, s := range CoreApp.Services {
if s.(*Service).Id == id {
return s.(*Service)
2018-06-23 00:10:37 +00:00
}
}
return nil
2018-06-10 01:31:13 +00:00
}
// SelectAllServices returns a slice of *core.Service to be store on []*core.Services, should only be called once on startup.
func (c *Core) SelectAllServices() ([]*Service, error) {
var services []*Service
db := servicesDB().Find(&services).Order("order_id desc")
if db.Error != nil {
utils.Log(3, fmt.Sprintf("service error: %v", db.Error))
return nil, db.Error
2018-06-30 00:57:05 +00:00
}
for _, service := range services {
service.Start()
service.AllCheckins()
service.AllFailures()
CoreApp.Services = append(CoreApp.Services, service)
2018-06-22 06:56:44 +00:00
}
reorderServices()
return services, db.Error
}
// reorderServices will sort the services based on 'order_id'
func reorderServices() {
sort.Sort(ServiceOrder(CoreApp.Services))
}
// ToJSON will convert a service to a JSON string
func (s *Service) ToJSON() string {
data, _ := json.Marshal(s)
return string(data)
2018-06-10 01:31:13 +00:00
}
// AvgTime will return the average amount of time for a service to response back successfully
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
}
// Online24 returns the service's uptime percent within last 24 hours
2018-08-19 00:37:00 +00:00
func (s *Service) Online24() float32 {
ago := time.Now().Add(-24 * time.Hour)
return s.OnlineSince(ago)
}
// 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
}
// DateScan struct is for creating the charts.js graph JSON array
2018-06-24 11:51:07 +00:00
type DateScan struct {
CreatedAt time.Time `json:"x"`
2018-06-24 20:56:58 +00:00
Value int64 `json:"y"`
2018-06-24 11:51:07 +00:00
}
// lastFailure returns the last failure a service had
func (s *Service) lastFailure() *Failure {
limited := s.LimitedFailures()
last := limited[len(limited)-1]
return last
2018-07-14 02:37:39 +00:00
}
// SmallText returns a short description about a services status
2018-08-19 00:37:00 +00:00
func (s *Service) SmallText() string {
last := s.LimitedFailures()
hits, _ := s.LimitedHits()
2018-08-19 08:48:02 +00:00
if s.Online {
2018-06-30 03:40:00 +00:00
if len(last) == 0 {
return fmt.Sprintf("Online since %v", s.CreatedAt.Format("Monday 3:04PM, Jan _2 2006"))
} else {
return fmt.Sprintf("Online, last failure was %v", hits[0].CreatedAt.Format("Monday 3:04PM, Jan _2 2006"))
}
}
2018-08-19 08:48:02 +00:00
if len(last) > 0 {
lastFailure := s.lastFailure()
return fmt.Sprintf("%v on %v", lastFailure.ParseError(), last[0].CreatedAt.Format("Monday 3:04PM, Jan _2 2006"))
2018-08-19 08:48:02 +00:00
} else {
return fmt.Sprintf("%v is currently offline", s.Name)
}
2018-06-30 03:40:00 +00:00
}
// GroupDataBy returns a SQL query as a string to group a column by a time
2018-07-04 09:00:16 +00:00
func GroupDataBy(column string, id int64, tm time.Time, increment string) string {
var sql string
switch CoreApp.DbConnection {
case "mysql":
sql = fmt.Sprintf("SELECT CONCAT(date_format(created_at, '%%Y-%%m-%%dT%%H:%%i:00Z')) AS created_at, AVG(latency)*1000 AS value FROM %v WHERE service=%v AND DATE_FORMAT(created_at, '%%Y-%%m-%%dT%%TZ') BETWEEN DATE_FORMAT('%v', '%%Y-%%m-%%dT%%TZ') AND DATE_FORMAT(NOW(), '%%Y-%%m-%%dT%%TZ') GROUP BY 1 ORDER BY created_at ASC;", column, id, tm.Format(time.RFC3339))
case "sqlite":
sql = fmt.Sprintf("SELECT strftime('%%Y-%%m-%%dT%%H:%%M:00Z', created_at), AVG(latency)*1000 as value FROM %v WHERE service=%v AND created_at >= '%v' GROUP BY strftime('%%M:00', created_at) ORDER BY created_at ASC;", column, id, tm.Format(time.RFC3339))
case "postgres":
sql = fmt.Sprintf("SELECT date_trunc('%v', created_at), AVG(latency)*1000 AS value FROM %v WHERE service=%v AND created_at >= '%v' GROUP BY 1 ORDER BY date_trunc ASC;", increment, column, id, tm.Format(time.RFC3339))
}
return sql
}
// GraphData returns the JSON object used by Charts.js to render the chart
2018-08-19 00:37:00 +00:00
func (s *Service) GraphData() string {
2018-07-03 21:39:56 +00:00
var d []*DateScan
since := time.Now().Add(time.Hour*-24 + time.Minute*0 + time.Second*0)
2018-07-04 09:00:16 +00:00
sql := GroupDataBy("hits", s.Id, since, "minute")
rows, err := DbSession.Raw(sql).Rows()
2018-06-24 11:51:07 +00:00
if err != nil {
2018-06-30 00:57:05 +00:00
utils.Log(2, err)
2018-06-25 06:21:18 +00:00
return ""
2018-06-10 01:31:13 +00:00
}
for rows.Next() {
2018-07-03 21:39:56 +00:00
gd := new(DateScan)
var tt string
2018-06-24 11:51:07 +00:00
var ff float64
err := rows.Scan(&tt, &ff)
2018-07-03 21:39:56 +00:00
if err != nil {
utils.Log(2, fmt.Sprintf("Issue loading chart data for service %v, %v", s.Name, err))
}
gd.CreatedAt, err = time.Parse(time.RFC3339, tt)
if err != nil {
utils.Log(2, fmt.Sprintf("Issue parsing time %v", err))
}
2018-06-24 11:51:07 +00:00
gd.Value = int64(ff)
d = append(d, gd)
}
2018-06-25 06:21:18 +00:00
data, err := json.Marshal(d)
2018-06-24 11:51:07 +00:00
if err != nil {
2018-06-30 00:57:05 +00:00
utils.Log(2, err)
2018-06-25 06:21:18 +00:00
return ""
2018-06-24 11:51:07 +00:00
}
2018-06-19 04:48:25 +00:00
return string(data)
2018-06-10 01:31:13 +00:00
}
// AvgUptime24 returns a service's average online status for last 24 hours
func (s *Service) AvgUptime24() string {
ago := time.Now().Add(-24 * time.Hour)
return s.AvgUptime(ago)
}
// AvgUptime returns average online status for last 24 hours
func (s *Service) AvgUptime(ago time.Time) string {
failed, _ := s.TotalFailuresSince(ago)
2018-06-10 01:31:13 +00:00
if failed == 0 {
return "100"
2018-06-10 01:31:13 +00:00
}
total, _ := s.TotalHitsSince(ago)
2018-06-10 04:21:12 +00:00
if total == 0 {
return "0.00"
2018-06-10 04:21:12 +00:00
}
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
}
amount := fmt.Sprintf("%0.2f", percent)
if amount == "100.00" {
amount = "100"
2018-06-22 04:02:57 +00:00
}
return amount
}
// TotalUptime returns the total uptime percent of a service
func (s *Service) TotalUptime() string {
hits, _ := s.TotalHits()
failures, _ := s.TotalFailures()
percent := float64(failures) / float64(hits) * 100
percent = 100 - percent
if percent < 0 {
percent = 0
}
amount := fmt.Sprintf("%0.2f", percent)
if amount == "100.00" {
amount = "100"
}
return amount
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
func updateService(service *Service) {
index := service.index()
CoreApp.Services[index] = service
}
// Delete will remove a service from the database, it will also end the service checking go routine
func (u *Service) Delete() error {
i := u.index()
err := servicesDB().Delete(u)
if err.Error != nil {
utils.Log(3, fmt.Sprintf("Failed to delete service %v. %v", u.Name, err.Error))
return err.Error
2018-06-30 00:57:05 +00:00
}
2018-08-19 00:37:00 +00:00
u.Close()
slice := CoreApp.Services
CoreApp.Services = append(slice[:i], slice[i+1:]...)
reorderServices()
notifiers.OnDeletedService(u.Service)
return err.Error
}
// UpdateSingle will update a single column for a service
func (u *Service) UpdateSingle(attr ...interface{}) error {
return servicesDB().Model(u).Update(attr).Error
2018-06-11 03:41:02 +00:00
}
// Update will update a service in the database, the service's checking routine can be restarted by passing true
func (u *Service) Update(restart bool) error {
err := servicesDB().Update(u)
if err.Error != nil {
utils.Log(3, fmt.Sprintf("Failed to update service %v. %v", u.Name, err))
return err.Error
}
if restart {
u.Close()
u.Start()
u.SleepDuration = time.Duration(u.Interval) * time.Second
go u.CheckQueue(true)
}
reorderServices()
updateService(u)
notifiers.OnUpdatedService(u.Service)
return err.Error
}
// Create will create a service and insert it into the database
func (u *Service) Create() (int64, error) {
2018-06-15 04:30:10 +00:00
u.CreatedAt = time.Now()
db := servicesDB().Create(u)
if db.Error != nil {
utils.Log(3, fmt.Sprintf("Failed to create service %v #%v: %v", u.Name, u.Id, db.Error))
return 0, db.Error
2018-06-10 03:44:47 +00:00
}
2018-08-19 00:37:00 +00:00
u.Start()
go u.CheckQueue(true)
CoreApp.Services = append(CoreApp.Services, u)
reorderServices()
notifiers.OnNewService(u.Service)
return u.Id, nil
2018-06-10 01:31:13 +00:00
}
// ServicesCount returns the amount of services inside the []*core.Services slice
func (c *Core) ServicesCount() int {
return len(c.Services)
}
// CountOnline
func (c *Core) CountOnline() int {
2018-06-11 00:20:42 +00:00
amount := 0
for _, s := range CoreApp.Services {
if s.(*Service).Online {
2018-06-11 00:20:42 +00:00
amount++
}
}
return amount
2018-06-25 07:09:31 +00:00
}