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 01/10] 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 02/10] 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 03/10] 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 04/10] 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 05/10] 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 06/10] 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 07/10] 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 08/10] 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 09/10] 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") From 77abd9fe2520f84705fdacb7e0e70f2adbb3b3db Mon Sep 17 00:00:00 2001 From: Monark <50051074+dmonark@users.noreply.github.com> Date: Tue, 28 Dec 2021 19:43:18 +0530 Subject: [PATCH 10/10] Downtime create/edit (#16) * done with listing and pagination * added badge to status * intergrated filter for downtimes * refactored code for naming convention and fixed some style * implemented edit/create/delete * removed .vscode folder * removed some eslint file changes * removed app.vue lint changes * added space at the end of app.vue comp * removed servicelist * removed login * removed main * removed page/login * added launch * added launch * added launch * removed linting from api.js * fixed filtering on ui * removed linting from top nav * removed linting from icon * removed linting from english * removed linting from yarn-lock * removed linting from yarn-lock * removed linting from yarn-lock * removed linting from mixin * removed linting from mixin * removed linting from routes * removed linting from routes * fixed the date range filter error * fixed the skip issue in the filter * fixed issue in date range * changed delete downtime btn text * delete icon to bin * changed start and end time format in the downtime listing * prefilled duration filter with past 30 days * added back button to downtime create and edit * highlighted bold active route * fixed issues on dates * removed eslint from store.js * removed eslint from store.js * added lock file to gitignore * removed linting issues * removed linting issues * removed linting issues * fixed extra parameters passing in downtime create/update req body * removed end date validation * removed end date validation * coverted to milllisec in a readable format * removed commented style * restored vue.config file * added max date config to filters and forms and modified validation on end date * added try catch on create and edit downtime * added try catch on create and edit downtime * removed the empty string params from filter Co-authored-by: smit95tpatel --- .gitignore | 3 +- frontend/.gitignore | 3 + frontend/src/API.js | 21 ++ frontend/src/assets/scss/layout.scss | 4 + .../Dashboard/DashboardDowntimes.vue | 176 ++++++++++ .../components/Dashboard/DowntimesList.vue | 172 +++++++++ .../src/components/Dashboard/EditDowntime.vue | 60 ++++ frontend/src/components/Dashboard/TopNav.vue | 3 + .../src/components/Elements/Pagination.vue | 59 ++++ frontend/src/forms/Downtime.vue | 325 ++++++++++++++++++ frontend/src/forms/DowntimeFilters.vue | 173 ++++++++++ frontend/src/icons.js | 2 +- frontend/src/languages/english.js | 14 + frontend/src/mixin.js | 5 +- frontend/src/routes.js | 25 ++ frontend/src/store.js | 8 + 16 files changed, 1050 insertions(+), 3 deletions(-) create mode 100644 frontend/src/components/Dashboard/DashboardDowntimes.vue create mode 100644 frontend/src/components/Dashboard/DowntimesList.vue create mode 100644 frontend/src/components/Dashboard/EditDowntime.vue create mode 100644 frontend/src/components/Elements/Pagination.vue create mode 100644 frontend/src/forms/Downtime.vue create mode 100644 frontend/src/forms/DowntimeFilters.vue diff --git a/.gitignore b/.gitignore index 59dce8cf..5b458bcc 100644 --- a/.gitignore +++ b/.gitignore @@ -41,4 +41,5 @@ tmp /frontend/cypress/videos/ services.yml statping.wiki -assets/ \ No newline at end of file +assets/ +.vscode/settings.json diff --git a/frontend/.gitignore b/frontend/.gitignore index a0dddc6f..b4a193e4 100644 --- a/frontend/.gitignore +++ b/frontend/.gitignore @@ -19,3 +19,6 @@ yarn-error.log* *.njsproj *.sln *.sw? + +# Package lock file +package-lock.json diff --git a/frontend/src/API.js b/frontend/src/API.js index e6f3fc7d..2646a0e7 100644 --- a/frontend/src/API.js +++ b/frontend/src/API.js @@ -282,6 +282,27 @@ class Api { await axios.all([all]) } + async downtimes ({ serviceId, start, end, skip, count, subStatus }) { + return axios.get('api/downtimes', { + params: { service_id: serviceId, start, end, skip, count, sub_status: subStatus } + }).then((response) => response.data); + } + + async downtime (id) { + return axios.get(`api/downtimes/${id}`).then((response) => response.data); + } + + async downtime_create (data) { + return axios.post('/api/downtimes', data).then((response) => response.data); + } + + async downtime_update ({ id, data }) { + return axios.patch(`/api/downtimes/${id}`, data).then((response) => response.data); + } + + async downtime_delete (id) { + return axios.delete(`/api/downtimes/${id}`).then((response) => response.data); + } } const api = new Api() export default api diff --git a/frontend/src/assets/scss/layout.scss b/frontend/src/assets/scss/layout.scss index 59b82352..dc6da19b 100644 --- a/frontend/src/assets/scss/layout.scss +++ b/frontend/src/assets/scss/layout.scss @@ -85,6 +85,10 @@ A { .nav-link { color: $navbar-color; + + &.router-link-exact-active { + font-weight: bold; + } } .form-control { diff --git a/frontend/src/components/Dashboard/DashboardDowntimes.vue b/frontend/src/components/Dashboard/DashboardDowntimes.vue new file mode 100644 index 00000000..adf38bdc --- /dev/null +++ b/frontend/src/components/Dashboard/DashboardDowntimes.vue @@ -0,0 +1,176 @@ + + + \ No newline at end of file diff --git a/frontend/src/components/Dashboard/DowntimesList.vue b/frontend/src/components/Dashboard/DowntimesList.vue new file mode 100644 index 00000000..ecdcf23e --- /dev/null +++ b/frontend/src/components/Dashboard/DowntimesList.vue @@ -0,0 +1,172 @@ + + + \ No newline at end of file diff --git a/frontend/src/components/Dashboard/EditDowntime.vue b/frontend/src/components/Dashboard/EditDowntime.vue new file mode 100644 index 00000000..4bcc8549 --- /dev/null +++ b/frontend/src/components/Dashboard/EditDowntime.vue @@ -0,0 +1,60 @@ + + + \ No newline at end of file diff --git a/frontend/src/components/Dashboard/TopNav.vue b/frontend/src/components/Dashboard/TopNav.vue index 8136fff8..45a1394b 100644 --- a/frontend/src/components/Dashboard/TopNav.vue +++ b/frontend/src/components/Dashboard/TopNav.vue @@ -14,6 +14,9 @@ + diff --git a/frontend/src/components/Elements/Pagination.vue b/frontend/src/components/Elements/Pagination.vue new file mode 100644 index 00000000..5b4687f4 --- /dev/null +++ b/frontend/src/components/Elements/Pagination.vue @@ -0,0 +1,59 @@ + + + \ No newline at end of file diff --git a/frontend/src/forms/Downtime.vue b/frontend/src/forms/Downtime.vue new file mode 100644 index 00000000..a23cb788 --- /dev/null +++ b/frontend/src/forms/Downtime.vue @@ -0,0 +1,325 @@ + + + \ No newline at end of file diff --git a/frontend/src/forms/DowntimeFilters.vue b/frontend/src/forms/DowntimeFilters.vue new file mode 100644 index 00000000..e8317227 --- /dev/null +++ b/frontend/src/forms/DowntimeFilters.vue @@ -0,0 +1,173 @@ + + + \ No newline at end of file diff --git a/frontend/src/icons.js b/frontend/src/icons.js index 91e7c3bc..86024a5c 100644 --- a/frontend/src/icons.js +++ b/frontend/src/icons.js @@ -6,4 +6,4 @@ import Vue from "vue"; library.add(fas, fab) -Vue.component('font-awesome-icon', FontAwesomeIcon) +Vue.component('FontAwesomeIcon', FontAwesomeIcon); diff --git a/frontend/src/languages/english.js b/frontend/src/languages/english.js index db54f907..33d73f4e 100644 --- a/frontend/src/languages/english.js +++ b/frontend/src/languages/english.js @@ -140,6 +140,20 @@ const english = { notify_all: "Notify All Changes", service_update: "Update Service", service_create: "Create Service", + start_time: 'Start Time', + end_time: "End Time", + actions: "Actions", + services: "Services", + downtimes: "Downtimes", + downtime_info: "Downtime Info", + downtime_status: "Downtime Status", + downtime_date_range: "Downtime Date Range", + downtime_create: "Create Downtime", + downtime_update: "Update Downtime", + filters: "Filters", + service: "Service", + clear: "Clear", + search: "Search", }; export default english; diff --git a/frontend/src/mixin.js b/frontend/src/mixin.js index 373239e6..32776900 100644 --- a/frontend/src/mixin.js +++ b/frontend/src/mixin.js @@ -256,6 +256,9 @@ export default Vue.mixin({ }, addSeconds(date, amount) { return addSeconds(date, amount) - } + }, + niceDateWithYear (val) { + return format(parseISO(val), 'do MMM, yyyy h:mma'); + }, } }); diff --git a/frontend/src/routes.js b/frontend/src/routes.js index 2b2a82ad..2cebe256 100644 --- a/frontend/src/routes.js +++ b/frontend/src/routes.js @@ -16,6 +16,9 @@ const Checkins = () => import(/* webpackChunkName: "dashboard" */ '@/components/ const Failures = () => import(/* webpackChunkName: "dashboard" */ '@/components/Dashboard/Failures') const NotFound = () => import(/* webpackChunkName: "index" */ '@/pages/NotFound') const Importer = () => import(/* webpackChunkName: "index" */ '@/components/Dashboard/Importer') +const DashboardDowntimes = () => import(/* webpackChunkName: "dashboard" */ '@/components/Dashboard/DashboardDowntimes') +const EditDowntime = () => import(/* webpackChunkName: "dashboard" */ '@/components/Dashboard/EditDowntime') + import VueRouter from "vue-router"; import Api from "./API"; @@ -137,6 +140,27 @@ const routes = [ requiresAuth: true, title: 'Statping - Service Failures', } + },{ + path: 'downtimes', + component: DashboardDowntimes, + meta: { + requiresAuth: true, + title: 'Statping - Downtimes', + } + },{ + path: 'create_downtime', + component: EditDowntime, + meta: { + requiresAuth: true, + title: 'Statping - Create Downtime', + } + },{ + path: 'edit_downtime/:id', + component: EditDowntime, + meta: { + requiresAuth: true, + title: 'Statping - Update Downtime', + } },{ path: 'messages', component: DashboardMessages, @@ -228,3 +252,4 @@ router.beforeEach((to, from, next) => { }); export default router + diff --git a/frontend/src/store.js b/frontend/src/store.js index 66d923e1..c8b328c0 100644 --- a/frontend/src/store.js +++ b/frontend/src/store.js @@ -23,6 +23,7 @@ export default new Vuex.Store({ oauth: {}, token: null, services: [], + downtimes: [], service: null, groups: [], messages: [], @@ -152,12 +153,19 @@ export default new Vuex.Store({ setModal(state, modal) { state.modal = modal }, + setDowntimes (state, downtimes) { + state.downtimes = downtimes; + } }, actions: { async getAllServices(context) { const services = await Api.services() context.commit("setServices", services); }, + async getDowntimes (context, { payload }) { + const { output } = await Api.downtimes(payload); + context.commit('setDowntimes', output ?? []); + }, async loadCore(context) { const core = await Api.core() const token = await Api.token()