From eeca8c9a777a659dbe015547f05532b0ed58ceb8 Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Mon, 20 Dec 2021 01:48:02 +0530 Subject: [PATCH] added some changes --- handlers/services.go | 42 +++++++++++++++++++++++++++++++++++-- types/downtimes/database.go | 7 +++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/handlers/services.go b/handlers/services.go index 2b16f596..9c7b6242 100755 --- a/handlers/services.go +++ b/handlers/services.go @@ -8,10 +8,12 @@ import ( "github.com/statping/statping/types/failures" "github.com/statping/statping/types/hits" "github.com/statping/statping/types/services" + "github.com/statping/statping/types/downtimes" "github.com/statping/statping/utils" "net/http" "net/url" "sort" + "strconv" "time" ) @@ -40,6 +42,38 @@ func findService(r *http.Request) (*services.Service, error) { return servicer, nil } +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 +} + +func findServiceStatus(t string,s services.Service) string{ + var timeVar time.Time + if t == ""{ + timeVar = time.Now() + }else{ + var err error + timeVar,err = ConvertToUnixTime(t) + if err != nil{ + return "" + } + } + var downtimes []Downtime + start := time.Time{} + end := timeVar + q := db.Where("start BETWEEN ? AND ?", start, end) + + + + return "" +} + + func findPublicSubService(r *http.Request, service *services.Service) (*services.Service, error) { vars := mux.Vars(r) id := utils.ToInt(vars["sub_id"]) @@ -522,17 +556,21 @@ func convertToMap(query url.Values) map[string]string{ return vars } + + func apiAllServicesHandler(r *http.Request) interface{} { query := r.URL.Query() - timeVar := time.Now().Unix() + var t string if query.Get("time") != ""{ - timeVar = + t = query.Get("time") } + var srvs []services.Service for _, v := range services.AllInOrder() { if !v.Public.Bool && !IsUser(r) { continue } + serviceStatus :=findServiceStatus(t,v) srvs = append(srvs, v) } return srvs diff --git a/types/downtimes/database.go b/types/downtimes/database.go index 0d0f1941..703ef3bc 100755 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -58,6 +58,13 @@ func FindByService(service int64, start time.Time, end time.Time) (*[]Downtime, return &downtime, q.Error() } +func FindAllByService(service int64) (*[]Downtime, error) { + var downtime []Downtime + q := db.Where("service = ? ", service) + q = q.Order("id ASC ").Find(&downtime) + return &downtime, q.Error() +} + func (c *Downtime) Create() error { q := db.Create(c) return q.Error()