From 48a1803b8c87d85c8b9731401e47f6d3358f8dc3 Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Mon, 6 Dec 2021 11:23:40 +0530 Subject: [PATCH 1/9] added filtering code --- handlers/downtimes.go | 16 +++++++++++- handlers/routes.go | 1 + types/downtimes/database.go | 52 +++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) 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() From b0a5c550246987b2e97eaf109004e072a302fa91 Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Mon, 6 Dec 2021 17:04:09 +0530 Subject: [PATCH 2/9] removed switch cases --- handlers/downtimes.go | 5 +-- types/downtimes/database.go | 72 +++++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/handlers/downtimes.go b/handlers/downtimes.go index 4385a04c..df45cac6 100644 --- a/handlers/downtimes.go +++ b/handlers/downtimes.go @@ -24,10 +24,7 @@ func findDowntime(r *http.Request) (*downtimes.Downtime, error) { 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) + downtime,err := downtimes.FindAll(vars) if err != nil { sendErrorJson(err, w, r) return diff --git a/types/downtimes/database.go b/types/downtimes/database.go index 0286d71b..1c400036 100644 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -3,7 +3,6 @@ package downtimes import ( "fmt" "github.com/statping/statping/database" - "github.com/statping/statping/types/services" "strconv" "time" ) @@ -74,38 +73,49 @@ 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) { +func FindAll(vars map[string]string ) (*[]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) + var start time.Time + var end time.Time + var err error + var count int64 + st,ok1 := vars["start"] + en,ok2 := vars["end"] + if ok1 && ok2 && (en > st){ + start,err = ConvertToUnixTime(vars["start"]) + if err!=nil { + return &downtime,err } + end,err = ConvertToUnixTime(vars["end"]) + if err!=nil { + return &downtime,err + } + }else{ + ninetyDaysAgo := time.Now().Add(time.Duration(-90*24) * time.Hour) + start = ninetyDaysAgo + end = time.Now() + } + q := db.Where("start BETWEEN ? AND ? ", start, end) + subStatus,ok3 := vars["sub_status"] + if ok3{ + q = q.Where(" sub_status = ?", subStatus) + } + service,ok4 := vars["service"] + if ok4{ + q = q.Where(" service = ?", service) + } + ty,ok5 := vars["type"] + if ok5{ + q = q.Where(" type = ?", ty) + } + cnt,ok5 := vars["count"] + if ok5{ + count,err = strconv.ParseInt(cnt,10,64) + if count > 100 { + count = 100 + } + }else { + count = 20 } q = q.Order("id ASC ").Find(&downtime) return &downtime, q.Error() From de0859ade4ea132e4482138c5c63f547c859b938 Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Wed, 8 Dec 2021 12:16:53 +0530 Subject: [PATCH 3/9] modified to r.query --- handlers/downtimes.go | 25 ++++++++++++++++++++++--- types/downtimes/database.go | 24 ++++++++++++------------ 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/handlers/downtimes.go b/handlers/downtimes.go index df45cac6..7e04a8fd 100644 --- a/handlers/downtimes.go +++ b/handlers/downtimes.go @@ -7,6 +7,7 @@ import ( "github.com/statping/statping/types/services" "github.com/statping/statping/utils" "net/http" + "net/url" "time" ) @@ -21,15 +22,33 @@ func findDowntime(r *http.Request) (*downtimes.Downtime, error) { return downtime, nil } +func convertToMap(query url.Values) map[string]string{ + vars := make(map[string]string) + if query.Get("start")!= "" { + vars["start"] = query.Get("start") + } + if query.Get("end")!= "" { + vars["end"] = query.Get("end") + } + if query.Get("sub_status")!= "" { + vars["sub_status"] = query.Get("sub_status") + } + if query.Get("service_id")!= "" { + vars["service_id"] = query.Get("service_id") + } + if query.Get("type")!= "" { + vars["type"] = query.Get("type") + } + return vars +} func apiAllDowntimes(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - fmt.Println(vars) + query := r.URL.Query() + vars:=convertToMap(query) downtime,err := downtimes.FindAll(vars) if err != nil { sendErrorJson(err, w, r) return } - fmt.Println(downtime) sendJsonAction(downtime, "fetch", w, r) } diff --git a/types/downtimes/database.go b/types/downtimes/database.go index 1c400036..e2c609f2 100644 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -79,9 +79,9 @@ func FindAll(vars map[string]string ) (*[]Downtime, error) { var end time.Time var err error var count int64 - st,ok1 := vars["start"] - en,ok2 := vars["end"] - if ok1 && ok2 && (en > st){ + st,err1 := vars["start"] + en,err2 := vars["end"] + if err1 && err2 && (en > st){ start,err = ConvertToUnixTime(vars["start"]) if err!=nil { return &downtime,err @@ -96,20 +96,20 @@ func FindAll(vars map[string]string ) (*[]Downtime, error) { end = time.Now() } q := db.Where("start BETWEEN ? AND ? ", start, end) - subStatus,ok3 := vars["sub_status"] - if ok3{ + subStatus,err3 := vars["sub_status"] + if err3{ q = q.Where(" sub_status = ?", subStatus) } - service,ok4 := vars["service"] - if ok4{ - q = q.Where(" service = ?", service) + serviceId,err4 := vars["service_id"] + if err4{ + q = q.Where(" service = ?", serviceId) } - ty,ok5 := vars["type"] - if ok5{ + ty,err5 := vars["type"] + if err5{ q = q.Where(" type = ?", ty) } - cnt,ok5 := vars["count"] - if ok5{ + cnt,err5 := vars["count"] + if err5{ count,err = strconv.ParseInt(cnt,10,64) if count > 100 { count = 100 From 945beb79e5ed72792e14ded58a04e36efcc108e2 Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Wed, 8 Dec 2021 14:27:52 +0530 Subject: [PATCH 4/9] added skip --- handlers/downtimes.go | 6 ++++++ types/downtimes/database.go | 14 +++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/handlers/downtimes.go b/handlers/downtimes.go index 7e04a8fd..ba374577 100644 --- a/handlers/downtimes.go +++ b/handlers/downtimes.go @@ -39,6 +39,12 @@ func convertToMap(query url.Values) map[string]string{ if query.Get("type")!= "" { vars["type"] = query.Get("type") } + if query.Get("skip")!= "" { + vars["skip"] = query.Get("skip") + } + if query.Get("count")!= "" { + vars["count"] = query.Get("count") + } return vars } func apiAllDowntimes(w http.ResponseWriter, r *http.Request) { diff --git a/types/downtimes/database.go b/types/downtimes/database.go index e2c609f2..e1166e9f 100644 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -68,17 +68,14 @@ func ConvertToUnixTime(str string) (time.Time,error){ 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 ) (*[]Downtime, error) { var downtime []Downtime var start time.Time var end time.Time var err error var count int64 + var skip int64 st,err1 := vars["start"] en,err2 := vars["end"] if err1 && err2 && (en > st){ @@ -117,7 +114,14 @@ func FindAll(vars map[string]string ) (*[]Downtime, error) { }else { count = 20 } - q = q.Order("id ASC ").Find(&downtime) + skp,err6:=vars["skip"] + if err6{ + skip,err = strconv.ParseInt(skp,10,64) + }else { + skip = 0 + } + q = q.Order("id ASC ") + q = q.Limit((int)(count)).Offset((int)(skip)).Find(&downtime) return &downtime, q.Error() } func (c *Downtime) Create() error { From d1a2af6f1a9380c036ba89c05efaa13d94773258 Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Fri, 10 Dec 2021 17:23:17 +0530 Subject: [PATCH 5/9] formating --- handlers/downtimes.go | 12 +++++------ types/downtimes/database.go | 42 +++++++++++++++++-------------------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/handlers/downtimes.go b/handlers/downtimes.go index ba374577..a987fe1f 100644 --- a/handlers/downtimes.go +++ b/handlers/downtimes.go @@ -24,25 +24,25 @@ func findDowntime(r *http.Request) (*downtimes.Downtime, error) { func convertToMap(query url.Values) map[string]string{ vars := make(map[string]string) - if query.Get("start")!= "" { + if query.Get("start") != "" { vars["start"] = query.Get("start") } - if query.Get("end")!= "" { + if query.Get("end") != "" { vars["end"] = query.Get("end") } - if query.Get("sub_status")!= "" { + if query.Get("sub_status") != "" { vars["sub_status"] = query.Get("sub_status") } - if query.Get("service_id")!= "" { + if query.Get("service_id") != "" { vars["service_id"] = query.Get("service_id") } - if query.Get("type")!= "" { + if query.Get("type") != "" { vars["type"] = query.Get("type") } if query.Get("skip")!= "" { vars["skip"] = query.Get("skip") } - if query.Get("count")!= "" { + if query.Get("count") != "" { vars["count"] = query.Get("count") } return vars diff --git a/types/downtimes/database.go b/types/downtimes/database.go index e1166e9f..b06f5882 100644 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -73,18 +73,17 @@ func FindAll(vars map[string]string ) (*[]Downtime, error) { var downtime []Downtime var start time.Time var end time.Time - var err error - var count int64 - var skip int64 st,err1 := vars["start"] en,err2 := vars["end"] - if err1 && err2 && (en > st){ + startInt,err := strconv.ParseInt(st,10,64) + endInt,err := strconv.ParseInt(en,10,64) + if err1 && err2 && (endInt > startInt){ start,err = ConvertToUnixTime(vars["start"]) - if err!=nil { + if err != nil { return &downtime,err } end,err = ConvertToUnixTime(vars["end"]) - if err!=nil { + if err != nil { return &downtime,err } }else{ @@ -92,35 +91,32 @@ func FindAll(vars map[string]string ) (*[]Downtime, error) { start = ninetyDaysAgo end = time.Now() } - q := db.Where("start BETWEEN ? AND ? ", start, end) - subStatus,err3 := vars["sub_status"] - if err3{ - q = q.Where(" sub_status = ?", subStatus) + q := db.Where("start BETWEEN ? AND ?", start, end) + if subStatusVar,subStatusErr := vars["sub_status"]; subStatusErr{ + q = q.Where("sub_status = ?", subStatusVar) } - serviceId,err4 := vars["service_id"] - if err4{ - q = q.Where(" service = ?", serviceId) + if serviceIdVar,serviceIdErr := vars["service_id"]; serviceIdErr{ + q = q.Where("service = ?", serviceIdVar) } - ty,err5 := vars["type"] - if err5{ - q = q.Where(" type = ?", ty) + if typeVar,typeErr := vars["type"]; typeErr{ + q = q.Where("type = ?", typeVar) } - cnt,err5 := vars["count"] - if err5{ - count,err = strconv.ParseInt(cnt,10,64) + var count int64 + if countVar,countErr := vars["count"]; countErr{ + count,err = strconv.ParseInt(countVar,10,64) if count > 100 { count = 100 } }else { count = 20 } - skp,err6:=vars["skip"] - if err6{ - skip,err = strconv.ParseInt(skp,10,64) + var skip int64 + if skipVar,err6 := vars["skip"]; err6{ + skip,err = strconv.ParseInt(skipVar,10,64) }else { skip = 0 } - q = q.Order("id ASC ") + q = q.Order("id ASC") q = q.Limit((int)(count)).Offset((int)(skip)).Find(&downtime) return &downtime, q.Error() } From 5bc476cdbe66576bb2e1893a9a544e88825523a1 Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Thu, 16 Dec 2021 16:44:23 +0530 Subject: [PATCH 6/9] added downtimeWithService section --- handlers/api.go | 4 +++- handlers/downtimes.go | 32 +++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/handlers/api.go b/handlers/api.go index a564bd2e..a306dfbb 100644 --- a/handlers/api.go +++ b/handlers/api.go @@ -167,7 +167,9 @@ func sendJsonAction(obj interface{}, method string, w http.ResponseWriter, r *ht case *downtimes.Downtime: objName = "downtime" objId = v.Id - + case *DowntimeService: + objName = "downtime_with_service" + objId = v.Id default: objName = fmt.Sprintf("%T", v) } diff --git a/handlers/downtimes.go b/handlers/downtimes.go index a987fe1f..f2008f5c 100644 --- a/handlers/downtimes.go +++ b/handlers/downtimes.go @@ -47,15 +47,45 @@ func convertToMap(query url.Values) map[string]string{ } return vars } + +type DowntimeService struct { + Id int64 `gorm:"primary_key;column:id" json:"id"` + Service *services.Service `gorm:"foreignKey:service" json:"service"` + ServiceId int64 `gorm:"index;column:service" json:"service_id"` + SubStatus string `gorm:"column:sub_status" json:"sub_status"` + Failures int `gorm:"column:failures" json:"failures"` + Start *time.Time `gorm:"index;column:start" json:"start"` + End *time.Time `gorm:"column:end" json:"end"` + Type string `gorm:"default:'auto';column:type" json:"type"` +} + func apiAllDowntimes(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() vars:=convertToMap(query) downtime,err := downtimes.FindAll(vars) + var downtimeWithService []DowntimeService + servicesMap := services.All() + if downtime==nil{ + sendJsonAction(downtimeWithService, "fetch", w, r) + return + } + for _,dtime :=range *downtime{ + var downtimeWithServiceVar DowntimeService + downtimeWithServiceVar.Id = dtime.Id + downtimeWithServiceVar.ServiceId = dtime.ServiceId + downtimeWithServiceVar.SubStatus = dtime.SubStatus + downtimeWithServiceVar.Failures = dtime.Failures + downtimeWithServiceVar.Start = dtime.Start + downtimeWithServiceVar.End = dtime.End + downtimeWithServiceVar.Type = dtime.Type + downtimeWithServiceVar.Service = servicesMap[dtime.ServiceId] + downtimeWithService = append(downtimeWithService,downtimeWithServiceVar) + } if err != nil { sendErrorJson(err, w, r) return } - sendJsonAction(downtime, "fetch", w, r) + sendJsonAction(downtimeWithService, "fetch", w, r) } func apiAllDowntimesForServiceHandler(w http.ResponseWriter, r *http.Request) { From 97b58c3f919f2b94e3eb80b78cda7f8fd6c89319 Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Mon, 20 Dec 2021 13:00:16 +0530 Subject: [PATCH 7/9] descending order --- types/downtimes/database.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/downtimes/database.go b/types/downtimes/database.go index b06f5882..d6f27f3a 100644 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -116,7 +116,7 @@ func FindAll(vars map[string]string ) (*[]Downtime, error) { }else { skip = 0 } - q = q.Order("id ASC") + q = q.Order("id DESC") q = q.Limit((int)(count)).Offset((int)(skip)).Find(&downtime) return &downtime, q.Error() } From f0eb5cbb9641e2a6291a9bc8d763ddafabc426ad Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Mon, 20 Dec 2021 16:21:56 +0530 Subject: [PATCH 8/9] time desc order --- types/downtimes/database.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/downtimes/database.go b/types/downtimes/database.go index d6f27f3a..253a2e4b 100644 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -116,7 +116,7 @@ func FindAll(vars map[string]string ) (*[]Downtime, error) { }else { skip = 0 } - q = q.Order("id DESC") + q = q.Order("start DESC") q = q.Limit((int)(count)).Offset((int)(skip)).Find(&downtime) return &downtime, q.Error() } From d7dfdd35277e7da631eced4fa7213bda6dbeee86 Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Mon, 27 Dec 2021 12:33:36 +0530 Subject: [PATCH 9/9] go fmted --- handlers/downtimes.go | 30 ++++++++++++------------ types/downtimes/database.go | 46 ++++++++++++++++++------------------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/handlers/downtimes.go b/handlers/downtimes.go index f2008f5c..c9f26670 100644 --- a/handlers/downtimes.go +++ b/handlers/downtimes.go @@ -22,7 +22,7 @@ func findDowntime(r *http.Request) (*downtimes.Downtime, error) { return downtime, nil } -func convertToMap(query url.Values) map[string]string{ +func convertToMap(query url.Values) map[string]string { vars := make(map[string]string) if query.Get("start") != "" { vars["start"] = query.Get("start") @@ -39,7 +39,7 @@ func convertToMap(query url.Values) map[string]string{ if query.Get("type") != "" { vars["type"] = query.Get("type") } - if query.Get("skip")!= "" { + if query.Get("skip") != "" { vars["skip"] = query.Get("skip") } if query.Get("count") != "" { @@ -49,27 +49,27 @@ func convertToMap(query url.Values) map[string]string{ } type DowntimeService struct { - Id int64 `gorm:"primary_key;column:id" json:"id"` - Service *services.Service `gorm:"foreignKey:service" json:"service"` - ServiceId int64 `gorm:"index;column:service" json:"service_id"` - SubStatus string `gorm:"column:sub_status" json:"sub_status"` - Failures int `gorm:"column:failures" json:"failures"` - Start *time.Time `gorm:"index;column:start" json:"start"` - End *time.Time `gorm:"column:end" json:"end"` - Type string `gorm:"default:'auto';column:type" json:"type"` + Id int64 `gorm:"primary_key;column:id" json:"id"` + Service *services.Service `gorm:"foreignKey:service" json:"service"` + ServiceId int64 `gorm:"index;column:service" json:"service_id"` + SubStatus string `gorm:"column:sub_status" json:"sub_status"` + Failures int `gorm:"column:failures" json:"failures"` + Start *time.Time `gorm:"index;column:start" json:"start"` + End *time.Time `gorm:"column:end" json:"end"` + Type string `gorm:"default:'auto';column:type" json:"type"` } func apiAllDowntimes(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() - vars:=convertToMap(query) - downtime,err := downtimes.FindAll(vars) + vars := convertToMap(query) + downtime, err := downtimes.FindAll(vars) var downtimeWithService []DowntimeService servicesMap := services.All() - if downtime==nil{ + if downtime == nil { sendJsonAction(downtimeWithService, "fetch", w, r) return } - for _,dtime :=range *downtime{ + for _, dtime := range *downtime { var downtimeWithServiceVar DowntimeService downtimeWithServiceVar.Id = dtime.Id downtimeWithServiceVar.ServiceId = dtime.ServiceId @@ -79,7 +79,7 @@ func apiAllDowntimes(w http.ResponseWriter, r *http.Request) { downtimeWithServiceVar.End = dtime.End downtimeWithServiceVar.Type = dtime.Type downtimeWithServiceVar.Service = servicesMap[dtime.ServiceId] - downtimeWithService = append(downtimeWithService,downtimeWithServiceVar) + downtimeWithService = append(downtimeWithService, downtimeWithServiceVar) } if err != nil { sendErrorJson(err, w, r) diff --git a/types/downtimes/database.go b/types/downtimes/database.go index 253a2e4b..addcee4d 100644 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -59,61 +59,61 @@ func FindByService(service int64, start time.Time, end time.Time) (*[]Downtime, return &downtime, q.Error() } -func ConvertToUnixTime(str string) (time.Time,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 + return t, err } tm := time.Unix(i, 0) - return tm,nil + return tm, nil } -func FindAll(vars map[string]string ) (*[]Downtime, error) { +func FindAll(vars map[string]string) (*[]Downtime, error) { var downtime []Downtime var start time.Time var end time.Time - st,err1 := vars["start"] - en,err2 := vars["end"] - startInt,err := strconv.ParseInt(st,10,64) - endInt,err := strconv.ParseInt(en,10,64) - if err1 && err2 && (endInt > startInt){ - start,err = ConvertToUnixTime(vars["start"]) + st, err1 := vars["start"] + en, err2 := vars["end"] + startInt, err := strconv.ParseInt(st, 10, 64) + endInt, err := strconv.ParseInt(en, 10, 64) + if err1 && err2 && (endInt > startInt) { + start, err = ConvertToUnixTime(vars["start"]) if err != nil { - return &downtime,err + return &downtime, err } - end,err = ConvertToUnixTime(vars["end"]) + end, err = ConvertToUnixTime(vars["end"]) if err != nil { - return &downtime,err + return &downtime, err } - }else{ + } else { ninetyDaysAgo := time.Now().Add(time.Duration(-90*24) * time.Hour) start = ninetyDaysAgo end = time.Now() } q := db.Where("start BETWEEN ? AND ?", start, end) - if subStatusVar,subStatusErr := vars["sub_status"]; subStatusErr{ + if subStatusVar, subStatusErr := vars["sub_status"]; subStatusErr { q = q.Where("sub_status = ?", subStatusVar) } - if serviceIdVar,serviceIdErr := vars["service_id"]; serviceIdErr{ + if serviceIdVar, serviceIdErr := vars["service_id"]; serviceIdErr { q = q.Where("service = ?", serviceIdVar) } - if typeVar,typeErr := vars["type"]; typeErr{ + if typeVar, typeErr := vars["type"]; typeErr { q = q.Where("type = ?", typeVar) } var count int64 - if countVar,countErr := vars["count"]; countErr{ - count,err = strconv.ParseInt(countVar,10,64) + if countVar, countErr := vars["count"]; countErr { + count, err = strconv.ParseInt(countVar, 10, 64) if count > 100 { count = 100 } - }else { + } else { count = 20 } var skip int64 - if skipVar,err6 := vars["skip"]; err6{ - skip,err = strconv.ParseInt(skipVar,10,64) - }else { + if skipVar, err6 := vars["skip"]; err6 { + skip, err = strconv.ParseInt(skipVar, 10, 64) + } else { skip = 0 } q = q.Order("start DESC")