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
|
|
|
|
2018-06-11 00:20:42 +00:00
|
|
|
import (
|
2018-06-29 05:32:50 +00:00
|
|
|
"fmt"
|
2018-06-22 04:02:57 +00:00
|
|
|
"github.com/ararog/timeago"
|
2018-07-14 02:37:39 +00:00
|
|
|
"github.com/hunterlong/statup/types"
|
2018-06-30 00:57:05 +00:00
|
|
|
"github.com/hunterlong/statup/utils"
|
2018-06-29 05:32:50 +00:00
|
|
|
"strings"
|
2018-06-11 00:20:42 +00:00
|
|
|
"time"
|
|
|
|
)
|
2018-06-10 01:31:13 +00:00
|
|
|
|
2018-08-19 00:37:00 +00:00
|
|
|
func CreateServiceFailure(s *Service, data FailureData) (int64, error) {
|
2018-07-14 02:37:39 +00:00
|
|
|
fail := &types.Failure{
|
2018-06-15 04:30:10 +00:00
|
|
|
Issue: data.Issue,
|
|
|
|
Service: s.Id,
|
|
|
|
CreatedAt: time.Now(),
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
2018-06-15 04:30:10 +00:00
|
|
|
s.Failures = append(s.Failures, fail)
|
2018-06-30 00:57:05 +00:00
|
|
|
col := DbSession.Collection("failures")
|
2018-06-15 04:30:10 +00:00
|
|
|
uuid, err := col.Insert(fail)
|
2018-07-03 21:39:56 +00:00
|
|
|
if err != nil {
|
|
|
|
utils.Log(3, err)
|
|
|
|
}
|
2018-06-15 04:30:10 +00:00
|
|
|
if uuid == nil {
|
|
|
|
return 0, err
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
2018-06-15 04:30:10 +00:00
|
|
|
return uuid.(int64), err
|
|
|
|
}
|
|
|
|
|
2018-07-14 02:37:39 +00:00
|
|
|
func SelectAllFailures(s *types.Service) []*types.Failure {
|
|
|
|
var fails []*types.Failure
|
2018-06-30 00:57:05 +00:00
|
|
|
col := DbSession.Collection("failures").Find("service", s.Id).OrderBy("-id")
|
2018-07-03 21:39:56 +00:00
|
|
|
err := col.All(&fails)
|
|
|
|
if err != nil {
|
|
|
|
utils.Log(3, fmt.Sprintf("Issue getting failures for service %v, %v", s.Name, err))
|
|
|
|
}
|
2018-06-23 00:10:37 +00:00
|
|
|
return fails
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
|
|
|
|
2018-08-19 00:37:00 +00:00
|
|
|
func DeleteFailures(u *Service) {
|
2018-06-22 04:02:57 +00:00
|
|
|
var fails []*Failure
|
2018-06-30 00:57:05 +00:00
|
|
|
col := DbSession.Collection("failures")
|
2018-06-22 04:02:57 +00:00
|
|
|
col.Find("service", u.Id).All(&fails)
|
|
|
|
for _, fail := range fails {
|
|
|
|
fail.Delete()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-19 00:37:00 +00:00
|
|
|
func (s *Service) LimitedFailures() []*Failure {
|
2018-07-14 02:37:39 +00:00
|
|
|
var fails []*types.Failure
|
2018-07-17 09:18:20 +00:00
|
|
|
var failArr []*Failure
|
2018-06-30 00:57:05 +00:00
|
|
|
col := DbSession.Collection("failures").Find("service", s.Id).OrderBy("-id").Limit(10)
|
2018-06-24 22:53:48 +00:00
|
|
|
col.All(&fails)
|
2018-07-17 09:18:20 +00:00
|
|
|
for _, f := range fails {
|
|
|
|
failArr = append(failArr, MakeFailure(f))
|
|
|
|
}
|
|
|
|
return failArr
|
2018-06-19 04:48:25 +00:00
|
|
|
}
|
|
|
|
|
2018-07-14 02:37:39 +00:00
|
|
|
func reverseFailures(input []*types.Failure) []*types.Failure {
|
2018-06-24 11:51:07 +00:00
|
|
|
if len(input) == 0 {
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
return append(reverseFailures(input[1:]), input[0])
|
|
|
|
}
|
|
|
|
|
2018-07-14 02:37:39 +00:00
|
|
|
func (fail *Failure) Ago() string {
|
|
|
|
f := fail.ToFailure()
|
2018-06-22 04:02:57 +00:00
|
|
|
got, _ := timeago.TimeAgoWithTime(time.Now(), f.CreatedAt)
|
|
|
|
return got
|
|
|
|
}
|
|
|
|
|
2018-07-14 02:37:39 +00:00
|
|
|
func (fail *Failure) Delete() error {
|
|
|
|
f := fail.ToFailure()
|
2018-06-30 00:57:05 +00:00
|
|
|
col := DbSession.Collection("failures").Find("id", f.Id)
|
2018-06-19 04:48:25 +00:00
|
|
|
return col.Delete()
|
2018-06-19 00:01:03 +00:00
|
|
|
}
|
|
|
|
|
2018-06-24 06:17:31 +00:00
|
|
|
func CountFailures() uint64 {
|
2018-06-30 00:57:05 +00:00
|
|
|
col := DbSession.Collection("failures").Find()
|
2018-06-15 04:30:10 +00:00
|
|
|
amount, err := col.Count()
|
2018-06-24 06:17:31 +00:00
|
|
|
if err != nil {
|
2018-06-30 00:57:05 +00:00
|
|
|
utils.Log(2, err)
|
2018-06-24 06:17:31 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return amount
|
2018-06-11 00:20:42 +00:00
|
|
|
}
|
|
|
|
|
2018-08-19 00:37:00 +00:00
|
|
|
func (s *Service) TotalFailures() (uint64, error) {
|
2018-06-30 00:57:05 +00:00
|
|
|
col := DbSession.Collection("failures").Find("service", s.Id)
|
2018-06-15 04:30:10 +00:00
|
|
|
amount, err := col.Count()
|
|
|
|
return amount, err
|
2018-06-10 01:31:13 +00:00
|
|
|
}
|
2018-06-10 03:44:47 +00:00
|
|
|
|
2018-08-19 00:37:00 +00:00
|
|
|
func (s *Service) TotalFailures24Hours() (uint64, error) {
|
2018-06-30 00:57:05 +00:00
|
|
|
col := DbSession.Collection("failures").Find("service", s.Id)
|
2018-06-15 04:30:10 +00:00
|
|
|
amount, err := col.Count()
|
|
|
|
return amount, err
|
2018-06-10 03:44:47 +00:00
|
|
|
}
|
2018-06-29 05:32:50 +00:00
|
|
|
|
2018-07-14 02:37:39 +00:00
|
|
|
func (f *Failure) ToFailure() *types.Failure {
|
|
|
|
return f.F.(*types.Failure)
|
|
|
|
}
|
|
|
|
|
|
|
|
func MakeFailure(f *types.Failure) *Failure {
|
|
|
|
fail := &Failure{f}
|
|
|
|
return fail
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fail *Failure) ParseError() string {
|
|
|
|
f := fail.ToFailure()
|
2018-06-29 05:32:50 +00:00
|
|
|
err := strings.Contains(f.Issue, "operation timed out")
|
|
|
|
if err {
|
|
|
|
return fmt.Sprintf("HTTP Request Timed Out")
|
|
|
|
}
|
|
|
|
err = strings.Contains(f.Issue, "x509: certificate is valid")
|
|
|
|
if err {
|
|
|
|
return fmt.Sprintf("SSL Certificate invalid")
|
|
|
|
}
|
2018-07-01 03:54:28 +00:00
|
|
|
err = strings.Contains(f.Issue, "Client.Timeout exceeded while awaiting headers")
|
|
|
|
if err {
|
|
|
|
return fmt.Sprintf("Connection Timed Out")
|
|
|
|
}
|
2018-06-29 05:32:50 +00:00
|
|
|
err = strings.Contains(f.Issue, "no such host")
|
|
|
|
if err {
|
|
|
|
return fmt.Sprintf("Domain is offline or not found")
|
|
|
|
}
|
|
|
|
err = strings.Contains(f.Issue, "HTTP Status Code")
|
|
|
|
if err {
|
|
|
|
return fmt.Sprintf("Incorrect HTTP Status Code")
|
|
|
|
}
|
2018-06-30 00:57:05 +00:00
|
|
|
err = strings.Contains(f.Issue, "connection refused")
|
|
|
|
if err {
|
|
|
|
return fmt.Sprintf("Connection Failed")
|
|
|
|
}
|
2018-07-01 03:54:28 +00:00
|
|
|
err = strings.Contains(f.Issue, "can't assign requested address")
|
|
|
|
if err {
|
|
|
|
return fmt.Sprintf("Unable to Request Address")
|
|
|
|
}
|
|
|
|
err = strings.Contains(f.Issue, "no route to host")
|
|
|
|
if err {
|
|
|
|
return fmt.Sprintf("Domain is offline or not found")
|
|
|
|
}
|
2018-06-29 05:32:50 +00:00
|
|
|
return f.Issue
|
|
|
|
}
|