chaged stuff

pull/1097/head
Rhythm 2022-01-04 16:21:41 +05:30
parent aa36f50011
commit b4995bcdab
3 changed files with 31 additions and 35 deletions

View File

@ -22,8 +22,6 @@ type serviceOrder struct {
Order int `json:"order"`
}
var (
zeroTime time.Time
zeroBool bool
@ -44,32 +42,31 @@ func findService(r *http.Request) (*services.Service, error) {
return servicer, nil
}
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 findDowntimeByTime(t string,s services.Service) *downtimes.Downtime {
func findAllDowntimes(t string) []downtimes.Downtime {
var timeVar time.Time
if t == ""{
if t == "" {
timeVar = time.Now()
}else{
} else {
var e error
timeVar,e = ConvertToUnixTime(t)
if e != nil{
timeVar, e = ConvertToUnixTime(t)
if e != nil {
return nil
}
}
downTime := downtimes.FindDowntime(s.Id,timeVar)
downTime := downtimes.FindDowntime(timeVar)
return downTime
}
func findPublicSubService(r *http.Request, service *services.Service) (*services.Service, error) {
vars := mux.Vars(r)
id := utils.ToInt(vars["sub_id"])
@ -558,24 +555,27 @@ func apiAllServicesHandler(r *http.Request) interface{} {
func apiAllServicesStatusHandler(r *http.Request) interface{} {
query := r.URL.Query()
var t string
if query.Get("time") != ""{
if query.Get("time") != "" {
t = query.Get("time")
}
var srvs []services.ServiceWithDowntime
dtime := findAllDowntimes(t)
m := make(map[int64]downtimes.Downtime)
for i := 0; i < len(dtime); i += 1 {
m[dtime[i].ServiceId] = dtime[i]
}
for _, v := range services.AllInOrder() {
if !v.Public.Bool && !IsUser(r) {
continue
}
dtime :=findDowntimeByTime(t,v)// we get status of each service at time t
serviceJson,_ := json.Marshal(&v)
serviceJson, _ := json.Marshal(&v)
var serviceDowntimeMap map[string]interface{}
json.Unmarshal(serviceJson,&serviceDowntimeMap)
jsonString,_ := json.Marshal(serviceDowntimeMap)
json.Unmarshal(serviceJson, &serviceDowntimeMap)
jsonString, _ := json.Marshal(serviceDowntimeMap)
serviceDowntime := services.ServiceWithDowntime{}
json.Unmarshal(jsonString,&serviceDowntime)
if dtime!=nil{
serviceDowntime.Downtime = *dtime
json.Unmarshal(jsonString, &serviceDowntime)
if vv, ok := m[v.Id]; ok == true {
serviceDowntime.Downtime = vv
}
srvs = append(srvs, serviceDowntime)
}

View File

@ -58,17 +58,12 @@ func FindByService(service int64, start time.Time, end time.Time) (*[]Downtime,
return &downtime, q.Error()
}
func FindDowntime(service int64, timeVar time.Time) *Downtime {
func FindDowntime(timeVar time.Time) []Downtime {
var downtime []Downtime
q := db.Where("service = $1 and start BETWEEN $2 AND $3 ", service, time.Time{}, timeVar)
q := db.Where("start <= ? and \"end\" >= ?", timeVar, timeVar)
q = q.Order("id ASC ").Find(&downtime)
downtimeList := *(&downtime)
for _,dtime := range downtimeList{
if (*(dtime.End)).Unix() > timeVar.Unix(){
return &dtime
}
}
return nil
return downtime
}
func (c *Downtime) Create() error {

View File

@ -139,12 +139,13 @@ type ServiceWithDowntime struct {
notifyAfterCount int64 `gorm:"column:notify_after_count" yaml:"-"`
prevOnline bool `gorm:"column:prev_online" yaml:"-"`
FailureCounter int `gorm:"column:failure_counter" json:"-" yaml:"-"`
CurrentDowntime int64 `gorm:"column:current_downtime" json:"-" yaml:"-"`
LastFailureType string `gorm:"-" json:"-" yaml:"-"`
ManualDowntime bool `gorm:"default:false;column:manual_downtime" json:"manual_downtime"`
Downtime downtimes.Downtime `gorm:"-" json:"downtime" yaml:"-"`
FailureCounter int `gorm:"column:failure_counter" json:"-" yaml:"-"`
CurrentDowntime int64 `gorm:"column:current_downtime" json:"-" yaml:"-"`
LastFailureType string `gorm:"-" json:"-" yaml:"-"`
ManualDowntime bool `gorm:"default:false;column:manual_downtime" json:"manual_downtime"`
Downtime downtimes.Downtime `gorm:"-" json:"downtime" yaml:"-"`
}
// ServiceOrder will reorder the services based on 'order_id' (Order)
type ServiceOrder []Service