diff --git a/handlers/downtimes.go b/handlers/downtimes.go index 3a451684..4385a04c 100644 --- a/handlers/downtimes.go +++ b/handlers/downtimes.go @@ -21,10 +21,24 @@ func findDowntime(r *http.Request) (*downtimes.Downtime, error) { return downtime, nil } +func apiAllDowntimes(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + fmt.Println(vars) + ninetyDaysAgo := time.Now().Add(time.Duration(-90*24) * time.Hour) + start := ninetyDaysAgo + end := time.Now() + downtime,err := downtimes.FindAll(vars,start, end) + if err != nil { + sendErrorJson(err, w, r) + return + } + fmt.Println(downtime) + sendJsonAction(downtime, "fetch", w, r) +} + func apiAllDowntimesForServiceHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) serviceId := utils.ToInt(vars["service_id"]) - ninetyDaysAgo := time.Now().Add(time.Duration(-90*24) * time.Hour) downtime, err := downtimes.FindByService(serviceId, ninetyDaysAgo, time.Now()) diff --git a/handlers/routes.go b/handlers/routes.go index cf070068..04dae630 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -193,6 +193,7 @@ func Router() *mux.Router { //r.Handle("/checkin/{api}", http.HandlerFunc(checkinHitHandler)) // API DOWNTIME Routes + api.Handle("/api/downtimes", authenticated(apiAllDowntimes, false)).Methods("GET") api.Handle("/api/service/{service_id}/downtimes", authenticated(apiAllDowntimesForServiceHandler, false)).Methods("GET") api.Handle("/api/downtimes", authenticated(apiCreateDowntimeHandler, false)).Methods("POST") api.Handle("/api/downtimes/{id}", authenticated(apiDowntimeHandler, false)).Methods("GET") diff --git a/types/downtimes/database.go b/types/downtimes/database.go index 0d0f1941..0286d71b 100644 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -3,6 +3,8 @@ package downtimes import ( "fmt" "github.com/statping/statping/database" + "github.com/statping/statping/types/services" + "strconv" "time" ) @@ -58,6 +60,56 @@ func FindByService(service int64, start time.Time, end time.Time) (*[]Downtime, return &downtime, q.Error() } +func ConvertToUnixTime(str string) (time.Time,error){ + i, err := strconv.ParseInt(str, 10, 64) + var t time.Time + if err != nil { + return t,err + } + tm := time.Unix(i, 0) + return tm,nil +} +type invalidTimeDurationError struct{} + +func (m *invalidTimeDurationError) Error() string { + return "invalid time duration" +} +func FindAll(vars map[string]string ,start time.Time, end time.Time) (*[]Downtime, error) { + var downtime []Downtime + q := db.Where("start BETWEEN ? AND ? ", start, end) + for key,val :=range vars{ + switch key{ + case "start": + _,ok := vars["end"] + if ok && (vars["end"]>vars["start"]) { + start,err := ConvertToUnixTime(vars["start"]) + if err!=nil { + return &downtime,err + } + end,err := ConvertToUnixTime(vars["end"]) + if err!=nil { + return &downtime,err + } + q = q.Where("start BETWEEN ? AND ? ", start, end) + }else { + return &downtime,&invalidTimeDurationError{} + } + case "sub_status": + q = q.Where(" sub_status = ?",val) + case "service": + allServices := services.All() + for k,v := range allServices{ + if v.Name == val { + q = q.Where(" service = ?",k) + } + } + case "type": + q = q.Where(" type = ?",val) + } + } + q = q.Order("id ASC ").Find(&downtime) + return &downtime, q.Error() +} func (c *Downtime) Create() error { q := db.Create(c) return q.Error()