pull/1097/head
Rhythm 2021-12-27 12:33:36 +05:30
parent f0eb5cbb96
commit d7dfdd3527
2 changed files with 38 additions and 38 deletions

View File

@ -22,7 +22,7 @@ func findDowntime(r *http.Request) (*downtimes.Downtime, error) {
return downtime, nil return downtime, nil
} }
func convertToMap(query url.Values) map[string]string{ func convertToMap(query url.Values) map[string]string {
vars := make(map[string]string) vars := make(map[string]string)
if query.Get("start") != "" { if query.Get("start") != "" {
vars["start"] = query.Get("start") vars["start"] = query.Get("start")
@ -39,7 +39,7 @@ func convertToMap(query url.Values) map[string]string{
if query.Get("type") != "" { if query.Get("type") != "" {
vars["type"] = query.Get("type") vars["type"] = query.Get("type")
} }
if query.Get("skip")!= "" { if query.Get("skip") != "" {
vars["skip"] = query.Get("skip") vars["skip"] = query.Get("skip")
} }
if query.Get("count") != "" { if query.Get("count") != "" {
@ -49,27 +49,27 @@ func convertToMap(query url.Values) map[string]string{
} }
type DowntimeService struct { type DowntimeService struct {
Id int64 `gorm:"primary_key;column:id" json:"id"` Id int64 `gorm:"primary_key;column:id" json:"id"`
Service *services.Service `gorm:"foreignKey:service" json:"service"` Service *services.Service `gorm:"foreignKey:service" json:"service"`
ServiceId int64 `gorm:"index;column:service" json:"service_id"` ServiceId int64 `gorm:"index;column:service" json:"service_id"`
SubStatus string `gorm:"column:sub_status" json:"sub_status"` SubStatus string `gorm:"column:sub_status" json:"sub_status"`
Failures int `gorm:"column:failures" json:"failures"` Failures int `gorm:"column:failures" json:"failures"`
Start *time.Time `gorm:"index;column:start" json:"start"` Start *time.Time `gorm:"index;column:start" json:"start"`
End *time.Time `gorm:"column:end" json:"end"` End *time.Time `gorm:"column:end" json:"end"`
Type string `gorm:"default:'auto';column:type" json:"type"` Type string `gorm:"default:'auto';column:type" json:"type"`
} }
func apiAllDowntimes(w http.ResponseWriter, r *http.Request) { func apiAllDowntimes(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query() query := r.URL.Query()
vars:=convertToMap(query) vars := convertToMap(query)
downtime,err := downtimes.FindAll(vars) downtime, err := downtimes.FindAll(vars)
var downtimeWithService []DowntimeService var downtimeWithService []DowntimeService
servicesMap := services.All() servicesMap := services.All()
if downtime==nil{ if downtime == nil {
sendJsonAction(downtimeWithService, "fetch", w, r) sendJsonAction(downtimeWithService, "fetch", w, r)
return return
} }
for _,dtime :=range *downtime{ for _, dtime := range *downtime {
var downtimeWithServiceVar DowntimeService var downtimeWithServiceVar DowntimeService
downtimeWithServiceVar.Id = dtime.Id downtimeWithServiceVar.Id = dtime.Id
downtimeWithServiceVar.ServiceId = dtime.ServiceId downtimeWithServiceVar.ServiceId = dtime.ServiceId
@ -79,7 +79,7 @@ func apiAllDowntimes(w http.ResponseWriter, r *http.Request) {
downtimeWithServiceVar.End = dtime.End downtimeWithServiceVar.End = dtime.End
downtimeWithServiceVar.Type = dtime.Type downtimeWithServiceVar.Type = dtime.Type
downtimeWithServiceVar.Service = servicesMap[dtime.ServiceId] downtimeWithServiceVar.Service = servicesMap[dtime.ServiceId]
downtimeWithService = append(downtimeWithService,downtimeWithServiceVar) downtimeWithService = append(downtimeWithService, downtimeWithServiceVar)
} }
if err != nil { if err != nil {
sendErrorJson(err, w, r) sendErrorJson(err, w, r)

View File

@ -59,61 +59,61 @@ func FindByService(service int64, start time.Time, end time.Time) (*[]Downtime,
return &downtime, q.Error() 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) i, err := strconv.ParseInt(str, 10, 64)
var t time.Time var t time.Time
if err != nil { if err != nil {
return t,err return t, err
} }
tm := time.Unix(i, 0) 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 downtime []Downtime
var start time.Time var start time.Time
var end time.Time var end time.Time
st,err1 := vars["start"] st, err1 := vars["start"]
en,err2 := vars["end"] en, err2 := vars["end"]
startInt,err := strconv.ParseInt(st,10,64) startInt, err := strconv.ParseInt(st, 10, 64)
endInt,err := strconv.ParseInt(en,10,64) endInt, err := strconv.ParseInt(en, 10, 64)
if err1 && err2 && (endInt > startInt){ if err1 && err2 && (endInt > startInt) {
start,err = ConvertToUnixTime(vars["start"]) start, err = ConvertToUnixTime(vars["start"])
if err != nil { if err != nil {
return &downtime,err return &downtime, err
} }
end,err = ConvertToUnixTime(vars["end"]) end, err = ConvertToUnixTime(vars["end"])
if err != nil { if err != nil {
return &downtime,err return &downtime, err
} }
}else{ } else {
ninetyDaysAgo := time.Now().Add(time.Duration(-90*24) * time.Hour) ninetyDaysAgo := time.Now().Add(time.Duration(-90*24) * time.Hour)
start = ninetyDaysAgo start = ninetyDaysAgo
end = time.Now() end = time.Now()
} }
q := db.Where("start BETWEEN ? AND ?", start, end) 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) 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) q = q.Where("service = ?", serviceIdVar)
} }
if typeVar,typeErr := vars["type"]; typeErr{ if typeVar, typeErr := vars["type"]; typeErr {
q = q.Where("type = ?", typeVar) q = q.Where("type = ?", typeVar)
} }
var count int64 var count int64
if countVar,countErr := vars["count"]; countErr{ if countVar, countErr := vars["count"]; countErr {
count,err = strconv.ParseInt(countVar,10,64) count, err = strconv.ParseInt(countVar, 10, 64)
if count > 100 { if count > 100 {
count = 100 count = 100
} }
}else { } else {
count = 20 count = 20
} }
var skip int64 var skip int64
if skipVar,err6 := vars["skip"]; err6{ if skipVar, err6 := vars["skip"]; err6 {
skip,err = strconv.ParseInt(skipVar,10,64) skip, err = strconv.ParseInt(skipVar, 10, 64)
}else { } else {
skip = 0 skip = 0
} }
q = q.Order("start DESC") q = q.Order("start DESC")