diff --git a/api.go b/api.go index f67abb5a..bd556179 100644 --- a/api.go +++ b/api.go @@ -10,6 +10,14 @@ func ApiIndexHandler(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(core) } +func ApiCheckinHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + checkin := FindCheckin(vars["api"]) + checkin.Receivehit() + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(checkin) +} + func ApiServiceHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) service, _ := SelectService(StringInt(vars["id"])) diff --git a/checker.go b/checker.go index 382a1497..b3841b6a 100644 --- a/checker.go +++ b/checker.go @@ -12,6 +12,7 @@ func CheckServices() { services, _ = SelectAllServices() for _, v := range services { obj := v + go obj.StartCheckins() go obj.CheckQueue() } } diff --git a/checkin.go b/checkin.go new file mode 100644 index 00000000..ed32ea43 --- /dev/null +++ b/checkin.go @@ -0,0 +1,111 @@ +package main + +import ( + "fmt" + "github.com/ararog/timeago" + "time" +) + +type Checkin struct { + Id int `db:"id,omitempty"` + Service int64 `db:"service"` + Interval int64 `db:"check_interval"` + Api string `db:"api"` + CreatedAt time.Time `db:"created_at"` + Hits int64 `json:"hits"` + Last time.Time `json:"last"` +} + +func (s *Service) SelectAllCheckins() []*Checkin { + var checkins []*Checkin + col := dbSession.Collection("checkins").Find("service", s.Id).OrderBy("-id") + col.All(&checkins) + s.Checkins = checkins + return checkins +} + +func (u *Checkin) Create() (int64, error) { + u.CreatedAt = time.Now() + uuid, err := dbSession.Collection("checkins").Insert(u) + if uuid == nil { + return 0, err + } + fmt.Println(uuid) + return uuid.(int64), err +} + +func SelectCheckinApi(api string) *Checkin { + var checkin *Checkin + dbSession.Collection("checkins").Find("api", api).One(&checkin) + return checkin +} + +func (c *Checkin) Receivehit() { + c.Hits++ + c.Last = time.Now() +} + +func (c *Checkin) RecheckCheckinFailure(guard chan struct{}) { + between := time.Now().Sub(c.Last).Seconds() + if between > float64(c.Interval) { + fmt.Println("rechecking every 15 seconds!") + c.CreateFailure() + time.Sleep(15 * time.Second) + guard <- struct{}{} + c.RecheckCheckinFailure(guard) + } else { + fmt.Println("i recovered!!") + } + <-guard +} + +func (f *Checkin) CreateFailure() { + +} + +func (f *Checkin) Ago() string { + got, _ := timeago.TimeAgoWithTime(time.Now(), f.Last) + return got +} + +func FindCheckin(api string) *Checkin { + for _, s := range services { + for _, c := range s.Checkins { + if c.Api == api { + return c + } + } + } + return nil +} + +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 + go checkin.Run() + } +} + +func CheckinProcess() { + for _, s := range services { + for _, c := range s.Checkins { + checkin := c + go checkin.Run() + } + } +} diff --git a/database.go b/database.go index b1f4abe6..bfcdca76 100644 --- a/database.go +++ b/database.go @@ -2,8 +2,6 @@ package main import ( "fmt" - "strings" - "time" "upper.io/db.v3/lib/sqlbuilder" "upper.io/db.v3/mysql" "upper.io/db.v3/postgresql" @@ -63,90 +61,3 @@ func DbConnection(dbType string) error { OnLoad(dbSession) return err } - -func DropDatabase() { - fmt.Println("Dropping Tables...") - down, _ := sqlBox.String("down.sql") - requests := strings.Split(down, ";") - for _, request := range requests { - _, err := dbSession.Exec(request) - if err != nil { - fmt.Println(err) - } - } -} - -func LoadSampleData() error { - fmt.Println("Inserting Sample Data...") - s1 := &Service{ - Name: "Google", - Domain: "https://google.com", - ExpectedStatus: 200, - Interval: 10, - Port: 0, - Type: "https", - Method: "GET", - } - s2 := &Service{ - Name: "Statup.io", - Domain: "https://statup.io", - ExpectedStatus: 200, - Interval: 15, - Port: 0, - Type: "https", - Method: "GET", - } - s3 := &Service{ - Name: "Statup.io SSL Check", - Domain: "https://statup.io", - ExpectedStatus: 200, - Interval: 15, - Port: 443, - Type: "tcp", - } - s4 := &Service{ - Name: "Github Failing Check", - Domain: "https://github.com/thisisnotausernamemaybeitis", - ExpectedStatus: 200, - Interval: 15, - Port: 0, - Type: "https", - Method: "GET", - } - s1.Create() - s2.Create() - s3.Create() - s4.Create() - - for i := 0; i < 100; i++ { - s1.Check() - s2.Check() - s3.Check() - s4.Check() - time.Sleep(250 * time.Millisecond) - } - - return nil -} - -func CreateDatabase() { - fmt.Println("Creating Tables...") - sql := "postgres_up.sql" - if dbServer == "mysql" { - sql = "mysql_up.sql" - } else if dbServer == "sqlite3" { - sql = "sqlite_up.sql" - } - up, _ := sqlBox.String(sql) - requests := strings.Split(up, ";") - for _, request := range requests { - _, err := dbSession.Exec(request) - if err != nil { - fmt.Println(err) - } - } - //secret := NewSHA1Hash() - //db.QueryRow("INSERT INTO core (secret, version) VALUES ($1, $2);", secret, VERSION).Scan() - fmt.Println("Database Created") - //SampleData() -} diff --git a/hits.go b/hits.go index c4d636fc..4b1bd1d6 100644 --- a/hits.go +++ b/hits.go @@ -38,7 +38,7 @@ func (s *Service) Hits() ([]Hit, error) { func (s *Service) LimitedHits() ([]Hit, error) { var hits []Hit - col := hitCol().Find("service", s.Id).Limit(256).OrderBy("-id") + col := hitCol().Find("service", s.Id).Limit(1056).OrderBy("-id") err := col.All(&hits) return hits, err } diff --git a/html/css/base.css b/html/css/base.css index 51658b1e..78b83a45 100644 --- a/html/css/base.css +++ b/html/css/base.css @@ -1,15 +1,14 @@ HTML,BODY { background-color: #efefef; - margin: 40px 0; } .container { padding-top: 20px; padding-bottom: 20px; + max-width: 860px; } .navbar { - margin-top: -50px; margin-bottom: 30px; } @@ -94,7 +93,8 @@ HTML,BODY { @media (max-width: 767px) { .container { - padding: 0px; + margin-top: 0 !important; + padding: 0 !important; } .navbar { diff --git a/html/tmpl/dashboard.html b/html/tmpl/dashboard.html index 544d3ebc..a2daccdc 100644 --- a/html/tmpl/dashboard.html +++ b/html/tmpl/dashboard.html @@ -44,14 +44,18 @@ {{ range .Services }} - {{$name := .Name}} - - {{ range .Failures }} -
-

{{$name}}

-

{{.ParseError}}

- -
+ {{ if .Failures }} +
+ {{ range .Failures }} + +
+
{{.ParseError}}
+ Reported {{.Ago}} +
+

{{.Issue}}

+
+ {{ end }} +
{{ end }} diff --git a/html/tmpl/footer.html b/html/tmpl/footer.html index c4f602d8..e52eb558 100644 --- a/html/tmpl/footer.html +++ b/html/tmpl/footer.html @@ -1,5 +1,5 @@ {{ define "footer"}} -