diff --git a/core/checker.go b/core/checker.go index 45933cf4..f23bfae8 100644 --- a/core/checker.go +++ b/core/checker.go @@ -106,13 +106,13 @@ func (s *Service) checkTcp(record bool) *Service { conn, err := net.DialTimeout("tcp", domain, time.Duration(s.Timeout)*time.Second) if err != nil { if record { - RecordFailure(s, fmt.Sprintf("TCP Dial Error %v", err)) + recordFailure(s, fmt.Sprintf("TCP Dial Error %v", err)) } return s } if err := conn.Close(); err != nil { if record { - RecordFailure(s, fmt.Sprintf("TCP Socket Close Error %v", err)) + recordFailure(s, fmt.Sprintf("TCP Socket Close Error %v", err)) } return s } @@ -120,7 +120,7 @@ func (s *Service) checkTcp(record bool) *Service { s.Latency = t2.Sub(t1).Seconds() s.LastResponse = "" if record { - RecordSuccess(s) + recordSuccess(s) } return s } @@ -130,7 +130,7 @@ func (s *Service) checkHttp(record bool) *Service { dnsLookup, err := s.dnsCheck() if err != nil { if record { - RecordFailure(s, fmt.Sprintf("Could not get IP address for domain %v, %v", s.Domain, err)) + recordFailure(s, fmt.Sprintf("Could not get IP address for domain %v, %v", s.Domain, err)) } return s } @@ -149,7 +149,7 @@ func (s *Service) checkHttp(record bool) *Service { } if err != nil { if record { - RecordFailure(s, fmt.Sprintf("HTTP Error %v", err)) + recordFailure(s, fmt.Sprintf("HTTP Error %v", err)) } return s } @@ -159,7 +159,7 @@ func (s *Service) checkHttp(record bool) *Service { s.Latency = t2.Sub(t1).Seconds() if err != nil { if record { - RecordFailure(s, fmt.Sprintf("HTTP Error %v", err)) + recordFailure(s, fmt.Sprintf("HTTP Error %v", err)) } return s } @@ -178,7 +178,7 @@ func (s *Service) checkHttp(record bool) *Service { s.LastResponse = string(contents) s.LastStatusCode = response.StatusCode if record { - RecordFailure(s, fmt.Sprintf("HTTP Response Body did not match '%v'", s.Expected)) + recordFailure(s, fmt.Sprintf("HTTP Response Body did not match '%v'", s.Expected)) } return s } @@ -187,14 +187,14 @@ func (s *Service) checkHttp(record bool) *Service { //s.LastResponse = string(contents) s.LastStatusCode = response.StatusCode if record { - RecordFailure(s, fmt.Sprintf("HTTP Status Code %v did not match %v", response.StatusCode, s.ExpectedStatus)) + recordFailure(s, fmt.Sprintf("HTTP Status Code %v did not match %v", response.StatusCode, s.ExpectedStatus)) } return s } s.LastStatusCode = response.StatusCode s.Online = true if record { - RecordSuccess(s) + recordSuccess(s) } return s } @@ -209,12 +209,8 @@ func (s *Service) Check(record bool) { } } -type HitData struct { - Latency float64 -} - -// RecordSuccess will create a new 'hit' record in the database for a successful/online service -func RecordSuccess(s *Service) { +// recordSuccess will create a new 'hit' record in the database for a successful/online service +func recordSuccess(s *Service) { s.Online = true s.LastOnline = time.Now() hit := &types.Hit{ @@ -227,8 +223,8 @@ func RecordSuccess(s *Service) { notifier.OnSuccess(s.Service) } -// RecordFailure will create a new 'failure' record in the database for a offline service -func RecordFailure(s *Service, issue string) { +// recordFailure will create a new 'failure' record in the database for a offline service +func recordFailure(s *Service, issue string) { s.Online = false fail := &types.Failure{ Service: s.Id, diff --git a/core/checkin.go b/core/checkin.go index 1a3b7459..1e0d11a5 100644 --- a/core/checkin.go +++ b/core/checkin.go @@ -98,34 +98,3 @@ func (f *Checkin) Ago() string { got, _ := timeago.TimeAgoWithTime(time.Now(), f.Last) return got } - -//func (c *Checkin) Run() { -// if c.Interval == 0 { -// return -// } -// fmt.Println("checking: ", c.Api) -// between := time.Now().Sub(c.Last).Seconds() -// if between > float64(c.Interval) { -// guard := make(chan struct{}) -// c.RecheckCheckinFailure(guard) -// <-guard -// } -// time.Sleep(1 * time.Second) -// c.Run() -//} -// -//func (s *Service) StartCheckins() { -// for _, c := range s.Checkins { -// checkin := c.(*Checkin) -// go checkin.Run() -// } -//} -// -//func CheckinProcess() { -// for _, s := range CoreApp.DbServices { -// for _, c := range s.Checkins { -// checkin := c -// go checkin.Run() -// } -// } -//} diff --git a/core/configs.go b/core/configs.go index 67d58c62..a569086e 100644 --- a/core/configs.go +++ b/core/configs.go @@ -25,6 +25,10 @@ import ( "os" ) +type ErrorResponse struct { + Error string +} + // LoadConfig will attempt to load the 'config.yml' file in a specific directory func LoadConfig(directory string) (*DbConfig, error) { var configs *types.DbConfig @@ -131,7 +135,3 @@ func DeleteConfig() { utils.Log(3, err) } } - -type ErrorResponse struct { - Error string -} diff --git a/core/database.go b/core/database.go index 27953727..8a7751a9 100644 --- a/core/database.go +++ b/core/database.go @@ -34,9 +34,8 @@ var ( DbSession *gorm.DB ) -func (s *Service) allHits() *gorm.DB { - var hits []*Hit - return servicesDB().Find(s).Related(&hits) +type DbConfig struct { + *types.DbConfig } // failuresDB returns the 'failures' database column @@ -64,20 +63,12 @@ func usersDB() *gorm.DB { return DbSession.Model(&types.User{}) } -// commDB returns the 'communications' database column -func commDB() *gorm.DB { - return DbSession.Model(¬ifier.Notification{}) -} - // hitsDB returns the 'hits' database column func checkinDB() *gorm.DB { return DbSession.Model(&types.Checkin{}) } -type DbConfig struct { - *types.DbConfig -} - +// HitsBetween returns the gorm database query for a collection of service hits between a time range func (s *Service) HitsBetween(t1, t2 time.Time) *gorm.DB { selector := Dbtimestamp(3600) return DbSession.Debug().Model(&types.Hit{}).Select(selector).Where("service = ? AND created_at BETWEEN ? AND ?", s.Id, t1.UTC().Format(types.TIME), t2.UTC().Format(types.TIME)).Group("timeframe") diff --git a/core/hits.go b/core/hits.go index f730a3b2..9719f01c 100644 --- a/core/hits.go +++ b/core/hits.go @@ -59,14 +59,6 @@ func reverseHits(input []*types.Hit) []*types.Hit { return append(reverseHits(input[1:]), input[0]) } -// SelectHitsGroupBy returns all hits from the group by function -func (s *Service) SelectHitsGroupBy(group string) ([]*types.Hit, error) { - var hits []*types.Hit - col := hitsDB().Where("service = ?", s.Id) - err := col.Find(&hits) - return hits, err.Error -} - // TotalHits returns the total amount of successful hits a service has func (s *Service) TotalHits() (uint64, error) { var count uint64 diff --git a/core/sample.go b/core/sample.go index 5d6a234f..0ca38a81 100644 --- a/core/sample.go +++ b/core/sample.go @@ -112,7 +112,7 @@ func InsertSampleHits() error { return nil } -func InsertSampleCore() error { +func insertSampleCore() error { core := &types.Core{ Name: "Statup Sample Data", Description: "This data is only used to testing", @@ -148,7 +148,7 @@ func insertSampleUsers() { // InsertSampleData will create the example/dummy services for a brand new Statup installation func InsertLargeSampleData() error { - InsertSampleCore() + insertSampleCore() InsertSampleData() insertSampleUsers() s6 := ReturnService(&types.Service{ diff --git a/handlers/dashboard.go b/handlers/dashboard.go index b3f932f3..a3395c68 100644 --- a/handlers/dashboard.go +++ b/handlers/dashboard.go @@ -19,6 +19,7 @@ import ( "encoding/json" "fmt" "github.com/hunterlong/statup/core" + "github.com/hunterlong/statup/core/notifier" "github.com/hunterlong/statup/source" "github.com/hunterlong/statup/types" "github.com/hunterlong/statup/utils" @@ -98,7 +99,8 @@ func logsLineHandler(w http.ResponseWriter, r *http.Request) { } type exportData struct { - Services []types.ServiceInterface + Core *core.Core `json:"core"` + Notifiers types.AllNotifiers `json:"notifiers"` } func exportHandler(w http.ResponseWriter, r *http.Request) { @@ -107,8 +109,19 @@ func exportHandler(w http.ResponseWriter, r *http.Request) { return } - data := exportData{core.CoreApp.Services} + var notifiers []*notifier.Notification + for _, v := range core.CoreApp.Notifications { + notifier := v.(notifier.Notifier) + notifiers = append(notifiers, notifier.Select()) + } + + data := exportData{core.CoreApp, notifiers} w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(data) + + w.Header().Set("Content-Disposition", "attachment; filename=Wiki.png") + w.Header().Set("Content-Type", r.Header.Get("Content-Type")) + w.Header().Set("Content-Length", r.Header.Get("Content-Length")) + } diff --git a/handlers/handlers.go b/handlers/handlers.go index 83ad95c7..000669d2 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -39,6 +39,7 @@ var ( httpServer *http.Server ) +// RunHTTPServer will start a HTTP server on a specific IP and port func RunHTTPServer(ip string, port int) error { host := fmt.Sprintf("%v:%v", ip, port) utils.Log(1, "Statup HTTP Server running on http://"+host) @@ -62,6 +63,7 @@ func RunHTTPServer(ip string, port int) error { return httpServer.ListenAndServe() } +// IsAuthenticated returns true if the HTTP request is authenticated func IsAuthenticated(r *http.Request) bool { if os.Getenv("GO_ENV") == "test" { return true @@ -82,6 +84,7 @@ func IsAuthenticated(r *http.Request) bool { return session.Values["authenticated"].(bool) } +// executeResponse will render a HTTP response for the front end user func executeResponse(w http.ResponseWriter, r *http.Request, file string, data interface{}, redirect interface{}) { utils.Http(r) if url, ok := redirect.(string); ok { @@ -172,6 +175,7 @@ func executeResponse(w http.ResponseWriter, r *http.Request, file string, data i } } +// executeJSResponse will render a Javascript response func executeJSResponse(w http.ResponseWriter, r *http.Request, file string, data interface{}) { render, err := source.JsBox.String(file) if err != nil { @@ -187,9 +191,8 @@ func executeJSResponse(w http.ResponseWriter, r *http.Request, file string, data t.Execute(w, data) } +// error404Handler is a HTTP handler for 404 error pages func error404Handler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) executeResponse(w, r, "error_404.html", nil, nil) } - -type DbConfig types.DbConfig