removed switch cases

pull/1097/head
Rhythm 2021-12-06 17:04:09 +05:30
parent 48a1803b8c
commit b0a5c55024
2 changed files with 42 additions and 35 deletions

View File

@ -24,10 +24,7 @@ func findDowntime(r *http.Request) (*downtimes.Downtime, error) {
func apiAllDowntimes(w http.ResponseWriter, r *http.Request) { func apiAllDowntimes(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
fmt.Println(vars) fmt.Println(vars)
ninetyDaysAgo := time.Now().Add(time.Duration(-90*24) * time.Hour) downtime,err := downtimes.FindAll(vars)
start := ninetyDaysAgo
end := time.Now()
downtime,err := downtimes.FindAll(vars,start, end)
if err != nil { if err != nil {
sendErrorJson(err, w, r) sendErrorJson(err, w, r)
return return

View File

@ -3,7 +3,6 @@ package downtimes
import ( import (
"fmt" "fmt"
"github.com/statping/statping/database" "github.com/statping/statping/database"
"github.com/statping/statping/types/services"
"strconv" "strconv"
"time" "time"
) )
@ -74,38 +73,49 @@ type invalidTimeDurationError struct{}
func (m *invalidTimeDurationError) Error() string { func (m *invalidTimeDurationError) Error() string {
return "invalid time duration" 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 var downtime []Downtime
q := db.Where("start BETWEEN ? AND ? ", start, end) var start time.Time
for key,val :=range vars{ var end time.Time
switch key{ var err error
case "start": var count int64
_,ok := vars["end"] st,ok1 := vars["start"]
if ok && (vars["end"]>vars["start"]) { en,ok2 := vars["end"]
start,err := ConvertToUnixTime(vars["start"]) if ok1 && ok2 && (en > st){
if err!=nil { start,err = ConvertToUnixTime(vars["start"])
return &downtime,err 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)
} }
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) q = q.Order("id ASC ").Find(&downtime)
return &downtime, q.Error() return &downtime, q.Error()