added time contraint

pull/1097/head
Rhythm 2021-12-20 03:45:39 +05:30
parent d2c95aeb52
commit 13f7cd3b70
2 changed files with 48 additions and 0 deletions

View File

@ -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

View File

@ -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()