From de9d89ab71dda4266449ad14ce374a80864ac3b8 Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Mon, 20 Dec 2021 03:45:39 +0530 Subject: [PATCH] added time contraint --- handlers/services.go | 35 +++++++++++++++++++++++++++++++++++ types/downtimes/database.go | 13 +++++++++++++ 2 files changed, 48 insertions(+) diff --git a/handlers/services.go b/handlers/services.go index 2e67238b..7b825af2 100755 --- a/handlers/services.go +++ b/handlers/services.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/gorilla/mux" "github.com/statping/statping/database" + "github.com/statping/statping/types/downtimes" "github.com/statping/statping/types/errors" "github.com/statping/statping/types/failures" "github.com/statping/statping/types/hits" @@ -11,6 +12,7 @@ import ( "github.com/statping/statping/utils" "net/http" "sort" + "strconv" "time" ) @@ -39,6 +41,32 @@ 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 e error + timeVar,e = ConvertToUnixTime(t) + if e != nil{ + return "" + } + } + serviceStatus := downtimes.FindStatusByTime(s.Id,timeVar) + return serviceStatus +} + + func findPublicSubService(r *http.Request, service *services.Service) (*services.Service, error) { vars := mux.Vars(r) id := utils.ToInt(vars["sub_id"]) @@ -514,11 +542,18 @@ func apiServiceDeleteHandler(w http.ResponseWriter, r *http.Request) { } func apiAllServicesHandler(r *http.Request) interface{} { + query := r.URL.Query() + var t string + if query.Get("time") != ""{ + t = query.Get("time") + } var srvs []services.Service for _, v := range services.AllInOrder() { if !v.Public.Bool && !IsUser(r) { continue } + serviceStatus :=findServiceStatus(t,v)// we get status of each service at time t + fmt.Println(serviceStatus) srvs = append(srvs, v) } return srvs diff --git a/types/downtimes/database.go b/types/downtimes/database.go index 0d0f1941..f60130fe 100755 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -58,6 +58,19 @@ func FindByService(service int64, start time.Time, end time.Time) (*[]Downtime, return &downtime, q.Error() } +func FindStatusByTime(service int64, timeVar time.Time) (string) { + var downtime []Downtime + q := db.Where("service = $1 and start BETWEEN $2 AND $3 ", service, time.Time{}, timeVar) + q = q.Order("id ASC ").Find(&downtime) + downtimeList := *(&downtime) + for _,dtime := range downtimeList{ + if (*(dtime.End)).Unix() > timeVar.Unix(){ + return dtime.SubStatus + } + } + return "" +} + func (c *Downtime) Create() error { q := db.Create(c) return q.Error()