pull/78/head
Hunter Long 2018-10-06 22:04:06 -07:00
parent 9061a97955
commit 2f3c9a2884
10 changed files with 97 additions and 94 deletions

View File

@ -31,9 +31,10 @@ var (
// VERSION stores the current version of Statup
VERSION string
// COMMIT stores the git commit hash for this version of Statup
COMMIT string
ipAddress string
port int
COMMIT string
ipAddress string
UsingDotEnv bool
port int
)
func init() {
@ -87,7 +88,7 @@ func LoadDotEnvs() error {
err := godotenv.Load()
if err == nil {
utils.Log(1, "Environment file '.env' Loaded")
usingEnv = true
UsingDotEnv = true
}
return err
}

View File

@ -37,13 +37,13 @@ func (c *checkin) String() string {
}
// ReturnCheckin converts *types.Checking to *core.checkin
func ReturnCheckin(s *types.Checkin) *checkin {
return &checkin{Checkin: s}
func ReturnCheckin(c *types.Checkin) *checkin {
return &checkin{Checkin: c}
}
// ReturnCheckinHit converts *types.checkinHit to *core.checkinHit
func ReturnCheckinHit(h *types.CheckinHit) *checkinHit {
return &checkinHit{CheckinHit: h}
func ReturnCheckinHit(c *types.CheckinHit) *checkinHit {
return &checkinHit{CheckinHit: c}
}
// SelectCheckin will find a checkin based on the API supplied
@ -100,31 +100,31 @@ func (c *checkin) Create() (int64, error) {
}
// Update will update a checkin
func (u *checkin) Update() (int64, error) {
row := checkinDB().Update(&u)
func (c *checkin) Update() (int64, error) {
row := checkinDB().Update(&c)
if row.Error != nil {
utils.Log(2, row.Error)
return 0, row.Error
}
return u.Id, row.Error
return c.Id, row.Error
}
// Create will create a new successful checkinHit
func (u *checkinHit) Create() (int64, error) {
if u.CreatedAt.IsZero() {
u.CreatedAt = time.Now()
func (c *checkinHit) Create() (int64, error) {
if c.CreatedAt.IsZero() {
c.CreatedAt = time.Now()
}
row := checkinHitsDB().Create(u)
row := checkinHitsDB().Create(c)
if row.Error != nil {
utils.Log(2, row.Error)
return 0, row.Error
}
return u.Id, row.Error
return c.Id, row.Error
}
// Ago returns the duration of time between now and the last successful checkinHit
func (f *checkinHit) Ago() string {
got, _ := timeago.TimeAgoWithTime(time.Now(), f.CreatedAt)
func (c *checkinHit) Ago() string {
got, _ := timeago.TimeAgoWithTime(time.Now(), c.CreatedAt)
return got
}

View File

@ -31,9 +31,11 @@ import (
)
var (
// DbSession stores the Statup database session
DbSession *gorm.DB
)
// DbConfig stores the config.yml file for the statup configuration
type DbConfig types.DbConfig
// failuresDB returns the 'failures' database column
@ -96,8 +98,8 @@ func (s *Service) AfterFind() (err error) {
}
// AfterFind for Hit will set the timezone
func (s *Hit) AfterFind() (err error) {
s.CreatedAt = utils.Timezoner(s.CreatedAt, CoreApp.Timezone)
func (h *Hit) AfterFind() (err error) {
h.CreatedAt = utils.Timezoner(h.CreatedAt, CoreApp.Timezone)
return
}
@ -114,29 +116,29 @@ func (u *user) AfterFind() (err error) {
}
// AfterFind for checkin will set the timezone
func (s *checkin) AfterFind() (err error) {
s.CreatedAt = utils.Timezoner(s.CreatedAt, CoreApp.Timezone)
func (c *checkin) AfterFind() (err error) {
c.CreatedAt = utils.Timezoner(c.CreatedAt, CoreApp.Timezone)
return
}
// AfterFind for checkinHit will set the timezone
func (s *checkinHit) AfterFind() (err error) {
s.CreatedAt = utils.Timezoner(s.CreatedAt, CoreApp.Timezone)
func (c *checkinHit) AfterFind() (err error) {
c.CreatedAt = utils.Timezoner(c.CreatedAt, CoreApp.Timezone)
return
}
// BeforeCreate for Hit will set CreatedAt to UTC
func (u *Hit) BeforeCreate() (err error) {
if u.CreatedAt.IsZero() {
u.CreatedAt = time.Now().UTC()
func (h *Hit) BeforeCreate() (err error) {
if h.CreatedAt.IsZero() {
h.CreatedAt = time.Now().UTC()
}
return
}
// BeforeCreate for failure will set CreatedAt to UTC
func (u *failure) BeforeCreate() (err error) {
if u.CreatedAt.IsZero() {
u.CreatedAt = time.Now().UTC()
func (f *failure) BeforeCreate() (err error) {
if f.CreatedAt.IsZero() {
f.CreatedAt = time.Now().UTC()
}
return
}
@ -150,25 +152,25 @@ func (u *user) BeforeCreate() (err error) {
}
// BeforeCreate for Service will set CreatedAt to UTC
func (u *Service) BeforeCreate() (err error) {
if u.CreatedAt.IsZero() {
u.CreatedAt = time.Now().UTC()
func (s *Service) BeforeCreate() (err error) {
if s.CreatedAt.IsZero() {
s.CreatedAt = time.Now().UTC()
}
return
}
// BeforeCreate for checkin will set CreatedAt to UTC
func (u *checkin) BeforeCreate() (err error) {
if u.CreatedAt.IsZero() {
u.CreatedAt = time.Now().UTC()
func (c *checkin) BeforeCreate() (err error) {
if c.CreatedAt.IsZero() {
c.CreatedAt = time.Now().UTC()
}
return
}
// BeforeCreate for checkinHit will set CreatedAt to UTC
func (u *checkinHit) BeforeCreate() (err error) {
if u.CreatedAt.IsZero() {
u.CreatedAt = time.Now().UTC()
func (c *checkinHit) BeforeCreate() (err error) {
if c.CreatedAt.IsZero() {
c.CreatedAt = time.Now().UTC()
}
return
}
@ -263,14 +265,14 @@ func DeleteAllSince(table string, date time.Time) {
}
// Update will save the config.yml file
func (c *DbConfig) Update() error {
func (db *DbConfig) Update() error {
var err error
config, err := os.Create(utils.Directory + "/config.yml")
if err != nil {
utils.Log(4, err)
return err
}
data, err := yaml.Marshal(c)
data, err := yaml.Marshal(db)
if err != nil {
utils.Log(3, err)
return err
@ -281,23 +283,23 @@ func (c *DbConfig) Update() error {
}
// Save will initially create the config.yml file
func (c *DbConfig) Save() (*DbConfig, error) {
func (db *DbConfig) Save() (*DbConfig, error) {
var err error
config, err := os.Create(utils.Directory + "/config.yml")
if err != nil {
utils.Log(4, err)
return nil, err
}
c.ApiKey = utils.NewSHA1Hash(16)
c.ApiSecret = utils.NewSHA1Hash(16)
data, err := yaml.Marshal(c)
db.ApiKey = utils.NewSHA1Hash(16)
db.ApiSecret = utils.NewSHA1Hash(16)
data, err := yaml.Marshal(db)
if err != nil {
utils.Log(3, err)
return nil, err
}
config.WriteString(string(data))
defer config.Close()
return c, err
return db, err
}
// CreateCore will initialize the global variable 'CoreApp". This global variable contains most of Statup app.

View File

@ -16,7 +16,6 @@
package notifier
import (
"errors"
"fmt"
"strings"
)
@ -30,7 +29,7 @@ func checkNotifierForm(n Notifier) error {
for _, f := range notifier.Form {
contains := contains(f.DbField, allowedVars)
if !contains {
return errors.New(fmt.Sprintf("the DbField '%v' is not allowed, allowed vars: %v", f.DbField, allowedVars))
return fmt.Errorf("the DbField '%v' is not allowed, allowed vars: %v", f.DbField, allowedVars)
}
}
return nil

View File

@ -1,4 +1,4 @@
// Package Notifier contains the main functionality for the Statup Notification system
// Package notifier contains the main functionality for the Statup Notification system
//
// More info on: https://github.com/hunterlong/statup/wiki/Notifiers
package notifier

View File

@ -318,59 +318,59 @@ func updateService(service *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)
func (s *Service) Delete() error {
i := s.index()
err := servicesDB().Delete(s)
if err.Error != nil {
utils.Log(3, fmt.Sprintf("Failed to delete service %v. %v", u.Name, err.Error))
utils.Log(3, fmt.Sprintf("Failed to delete service %v. %v", s.Name, err.Error))
return err.Error
}
u.Close()
s.Close()
slice := CoreApp.Services
CoreApp.Services = append(slice[:i], slice[i+1:]...)
reorderServices()
notifier.OnDeletedService(u.Service)
notifier.OnDeletedService(s.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
func (s *Service) UpdateSingle(attr ...interface{}) error {
return servicesDB().Model(s).Update(attr).Error
}
// 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)
func (s *Service) Update(restart bool) error {
err := servicesDB().Update(s)
if err.Error != nil {
utils.Log(3, fmt.Sprintf("Failed to update service %v. %v", u.Name, err))
utils.Log(3, fmt.Sprintf("Failed to update service %v. %v", s.Name, err))
return err.Error
}
if restart {
u.Close()
u.Start()
u.SleepDuration = time.Duration(u.Interval) * time.Second
go u.CheckQueue(true)
s.Close()
s.Start()
s.SleepDuration = time.Duration(s.Interval) * time.Second
go s.CheckQueue(true)
}
reorderServices()
updateService(u)
notifier.OnUpdatedService(u.Service)
updateService(s)
notifier.OnUpdatedService(s.Service)
return err.Error
}
// Create will create a service and insert it into the database
func (u *Service) Create(check bool) (int64, error) {
u.CreatedAt = time.Now()
db := servicesDB().Create(u)
func (s *Service) Create(check bool) (int64, error) {
s.CreatedAt = time.Now()
db := servicesDB().Create(s)
if db.Error != nil {
utils.Log(3, fmt.Sprintf("Failed to create service %v #%v: %v", u.Name, u.Id, db.Error))
utils.Log(3, fmt.Sprintf("Failed to create service %v #%v: %v", s.Name, s.Id, db.Error))
return 0, db.Error
}
u.Start()
go u.CheckQueue(check)
CoreApp.Services = append(CoreApp.Services, u)
s.Start()
go s.CheckQueue(check)
CoreApp.Services = append(CoreApp.Services, s)
reorderServices()
notifier.OnNewService(u.Service)
return u.Id, nil
notifier.OnNewService(s.Service)
return s.Id, nil
}
// ServicesCount returns the amount of services inside the []*core.Services slice

View File

@ -19,6 +19,7 @@ import (
"time"
)
// Checkin struct will allow an application to send a recurring HTTP GET to confirm a service is online
type Checkin struct {
Id int64 `gorm:"primary_key;column:id"`
Service int64 `gorm:"index;column:service"`
@ -29,6 +30,7 @@ type Checkin struct {
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
}
// CheckinHit is a successful response from a Checkin
type CheckinHit struct {
Id int64 `gorm:"primary_key;column:id"`
Checkin int64 `gorm:"index;column:checkin"`

View File

@ -19,6 +19,7 @@ import (
"time"
)
// AllNotifiers contains all the Notifiers loaded
type AllNotifiers interface{}
// Core struct contains all the required fields for Statup. All application settings
@ -46,12 +47,11 @@ type Core struct {
Repos []PluginJSON `gorm:"-" json:"-"`
AllPlugins []PluginActions `gorm:"-" json:"-"`
Notifications []AllNotifiers `gorm:"-" json:"-"`
CoreInterface `gorm:"-" json:"-"`
}
type CoreInterface interface {
SelectAllServices() ([]*Service, error)
Count24HFailures() uint64
ServicesCount() int
CountOnline() int
}
//type CoreInterface interface {
// SelectAllServices() ([]*Service, error)
// Count24HFailures() uint64
// ServicesCount() int
// CountOnline() int
//}

View File

@ -20,18 +20,17 @@ import (
)
const (
TIME_NANOZ = "2006-01-02 15:04:05.999999-0700 MST"
TIME_NANO = "2006-01-02T15:04:05Z"
TIME = "2006-01-02 15:04:05"
TIME_DAY = "2006-01-02"
TIME_NANO = "2006-01-02T15:04:05Z"
TIME = "2006-01-02 15:04:05"
TIME_DAY = "2006-01-02"
)
var (
NOW = func() time.Time { return time.Now() }()
HOUR_1_AGO = time.Now().Add(-1 * time.Hour)
HOUR_24_AGO = time.Now().Add(-24 * time.Hour)
HOUR_72_AGO = time.Now().Add(-72 * time.Hour)
DAY_7_AGO = NOW.AddDate(0, 0, -7)
MONTH_1_AGO = NOW.AddDate(0, -1, 0)
YEAR_1_AGO = NOW.AddDate(-1, 0, 0)
NOW = func() time.Time { return time.Now() }()
//HOUR_1_AGO = time.Now().Add(-1 * time.Hour)
//HOUR_24_AGO = time.Now().Add(-24 * time.Hour)
//HOUR_72_AGO = time.Now().Add(-72 * time.Hour)
//DAY_7_AGO = NOW.AddDate(0, 0, -7)
//MONTH_1_AGO = NOW.AddDate(0, -1, 0)
//YEAR_1_AGO = NOW.AddDate(-1, 0, 0)
)

View File

@ -70,8 +70,8 @@ func FormatDuration(d time.Duration) string {
if rev(d.Seconds()) >= 2 {
out += "s"
}
return out
}
return out
}
func rev(f float64) float64 {