mirror of https://github.com/statping/statping
go fmted
parent
f0eb5cbb96
commit
d7dfdd3527
|
@ -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)
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue