From aa36f5001148780c43671b3fc96be91f08733559 Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Tue, 28 Dec 2021 14:36:10 +0530 Subject: [PATCH] added status route --- handlers/routes.go | 1 + handlers/services.go | 45 +++++++++++++++++++------ types/downtimes/database.go | 6 ++-- types/services/struct.go | 67 +++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 14 deletions(-) diff --git a/handlers/routes.go b/handlers/routes.go index cf070068..992e2ff6 100755 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -131,6 +131,7 @@ func Router() *mux.Router { // API SERVICE Routes api.Handle("/api/services", scoped(apiAllServicesHandler)).Methods("GET") + api.Handle("/api/services/status", scoped(apiAllServicesStatusHandler)).Methods("GET") api.Handle("/api/services", authenticated(apiCreateServiceHandler, false)).Methods("POST") api.Handle("/api/services/{id}", authenticatedV2(apiServiceHandler)).Methods("GET") api.Handle("/api/services/{id}/sub_services", scoped(apiAllSubServicesHandler)).Methods("GET") diff --git a/handlers/services.go b/handlers/services.go index 7b825af2..0b5cb21c 100755 --- a/handlers/services.go +++ b/handlers/services.go @@ -1,6 +1,7 @@ package handlers import ( + "encoding/json" "fmt" "github.com/gorilla/mux" "github.com/statping/statping/database" @@ -21,6 +22,8 @@ type serviceOrder struct { Order int `json:"order"` } + + var ( zeroTime time.Time zeroBool bool @@ -51,7 +54,7 @@ func ConvertToUnixTime(str string) (time.Time,error){ return tm,nil } -func findServiceStatus(t string,s services.Service) string{ +func findDowntimeByTime(t string,s services.Service) *downtimes.Downtime { var timeVar time.Time if t == ""{ timeVar = time.Now() @@ -59,11 +62,11 @@ func findServiceStatus(t string,s services.Service) string{ var e error timeVar,e = ConvertToUnixTime(t) if e != nil{ - return "" + return nil } } - serviceStatus := downtimes.FindStatusByTime(s.Id,timeVar) - return serviceStatus + downTime := downtimes.FindDowntime(s.Id,timeVar) + return downTime } @@ -542,23 +545,43 @@ 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 } +func apiAllServicesStatusHandler(r *http.Request) interface{} { + query := r.URL.Query() + var t string + if query.Get("time") != ""{ + t = query.Get("time") + } + var srvs []services.ServiceWithDowntime + 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) + var serviceDowntimeMap map[string]interface{} + json.Unmarshal(serviceJson,&serviceDowntimeMap) + + jsonString,_ := json.Marshal(serviceDowntimeMap) + serviceDowntime := services.ServiceWithDowntime{} + json.Unmarshal(jsonString,&serviceDowntime) + if dtime!=nil{ + serviceDowntime.Downtime = *dtime + } + srvs = append(srvs, serviceDowntime) + } + return srvs +} + func apiAllSubServicesHandler(r *http.Request) interface{} { var srvs []services.Service service, err := findService(r) diff --git a/types/downtimes/database.go b/types/downtimes/database.go index f60130fe..2f6b3cb9 100755 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -58,17 +58,17 @@ func FindByService(service int64, start time.Time, end time.Time) (*[]Downtime, return &downtime, q.Error() } -func FindStatusByTime(service int64, timeVar time.Time) (string) { +func FindDowntime(service int64, timeVar time.Time) *Downtime { 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 &dtime } } - return "" + return nil } func (c *Downtime) Create() error { diff --git a/types/services/struct.go b/types/services/struct.go index a6d99ce1..7b7597cd 100755 --- a/types/services/struct.go +++ b/types/services/struct.go @@ -4,6 +4,7 @@ import ( "database/sql/driver" "encoding/json" "github.com/statping/statping/types/checkins" + "github.com/statping/statping/types/downtimes" "github.com/statping/statping/types/failures" "github.com/statping/statping/types/incidents" "github.com/statping/statping/types/messages" @@ -78,6 +79,72 @@ type Service struct { ManualDowntime bool `gorm:"default:false;column:manual_downtime" json:"manual_downtime"` } +type ServiceWithDowntime struct { + Id int64 `gorm:"primary_key;column:id" json:"id" yaml:"id"` + Name string `gorm:"column:name" json:"name" yaml:"name"` + Description string `gorm:"column:description" json:"description" yaml:"-"` + DisplayName string `json:"display_name"` + Domain string `gorm:"column:domain" json:"domain" yaml:"domain" private:"true" scope:"user,admin"` + Expected null.NullString `gorm:"column:expected" json:"expected" yaml:"expected" scope:"user,admin"` + ExpectedStatus int `gorm:"default:200;column:expected_status" json:"expected_status" yaml:"expected_status" scope:"user,admin"` + Interval int `gorm:"default:30;column:check_interval" json:"check_interval" yaml:"check_interval"` + Type string `gorm:"column:check_type" json:"type" yaml:"type"` + Method string `gorm:"column:method" json:"method" scope:"user,admin" yaml:"method"` + PostData null.NullString `gorm:"column:post_data" json:"post_data" scope:"user,admin" yaml:"post_data"` + Port int `gorm:"not null;column:port" json:"port" scope:"user,admin" yaml:"port"` + Timeout int `gorm:"default:30;column:timeout" json:"timeout" scope:"user,admin" yaml:"timeout"` + Order int `gorm:"default:0;column:order_id" json:"order_id" yaml:"order_id"` + Ftc int `gorm:"default:3;column:ftc" json:"ftc" yaml:"ftc"` + VerifySSL null.NullBool `gorm:"default:false;column:verify_ssl" json:"verify_ssl" scope:"user,admin" yaml:"verify_ssl"` + GrpcHealthCheck null.NullBool `gorm:"default:false;column:grpc_health_check" json:"grpc_health_check" scope:"user,admin" yaml:"grpc_health_check"` + Public null.NullBool `gorm:"default:true;column:public" json:"public" yaml:"public"` + GroupId int `gorm:"default:0;column:group_id" json:"group_id" yaml:"group_id"` + TLSCert null.NullString `gorm:"column:tls_cert" json:"tls_cert" scope:"user,admin" yaml:"tls_cert"` + TLSCertKey null.NullString `gorm:"column:tls_cert_key" json:"tls_cert_key" scope:"user,admin" yaml:"tls_cert_key"` + TLSCertRoot null.NullString `gorm:"column:tls_cert_root" json:"tls_cert_root" scope:"user,admin" yaml:"tls_cert_root"` + Headers null.NullString `gorm:"column:headers" json:"headers" scope:"user,admin" yaml:"headers"` + Permalink null.NullString `gorm:"column:permalink" json:"permalink" yaml:"permalink"` + Redirect null.NullBool `gorm:"default:false;column:redirect" json:"redirect" scope:"user,admin" yaml:"redirect"` + SubServicesDetails SubServicesDetail `gorm:"column:sub_services_detail;type:json;DEFAULT:null" json:"sub_services_detail" yaml:"sub_services_detail"` + CreatedAt time.Time `gorm:"column:created_at" json:"created_at" yaml:"-"` + UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" yaml:"-"` + Online bool `gorm:"column:online" json:"online" yaml:"-"` + Latency int64 `gorm:"-" json:"latency" yaml:"-"` + PingTime int64 `gorm:"-" json:"ping_time" yaml:"-"` + Online24Hours float32 `gorm:"-" json:"online_24_hours" yaml:"-"` + Online7Days float32 `gorm:"-" json:"online_7_days" yaml:"-"` + AvgResponse int64 `gorm:"-" json:"avg_response" yaml:"-"` + FailuresLast24Hours int `gorm:"-" json:"failures_24_hours" yaml:"-"` + Running chan bool `gorm:"-" json:"-" yaml:"-"` + Checkpoint time.Time `gorm:"-" json:"-" yaml:"-"` + SleepDuration time.Duration `gorm:"-" json:"-" yaml:"-"` + LastResponse string `gorm:"-" json:"-" yaml:"-"` + NotifyAfter int64 `gorm:"column:notify_after" json:"notify_after" yaml:"notify_after" scope:"user,admin"` + AllowNotifications null.NullBool `gorm:"default:true;column:allow_notifications" json:"allow_notifications" yaml:"allow_notifications" scope:"user,admin"` + UpdateNotify null.NullBool `gorm:"default:true;column:notify_all_changes" json:"notify_all_changes" yaml:"notify_all_changes" scope:"user,admin"` // This Variable is a simple copy of `core.CoreApp.UpdateNotify.Bool` + DownText string `gorm:"-" json:"-" yaml:"-"` // Contains the current generated Downtime Text // Is 'true' if the user has already be informed that the Services now again available // Is 'true' if the user has already be informed that the Services now again available + LastStatusCode int `gorm:"-" json:"status_code" yaml:"-"` + LastLookupTime int64 `gorm:"-" json:"-" yaml:"-"` + LastLatency int64 `gorm:"-" json:"-" yaml:"-"` + LastCheck time.Time `gorm:"column:last_check" json:"last_check" yaml:"-"` + LastOnline time.Time `gorm:"column:last_success" json:"last_success" yaml:"-"` + LastOffline time.Time `gorm:"column:last_error" json:"last_error" yaml:"-"` + Stats *Stats `gorm:"-" json:"stats,omitempty" yaml:"-"` + Messages []*messages.Message `gorm:"foreignkey:service;association_foreignkey:id" json:"messages,omitempty" yaml:"messages"` + Incidents []*incidents.Incident `gorm:"foreignkey:service;association_foreignkey:id" json:"incidents,omitempty" yaml:"incidents"` + Checkins []*checkins.Checkin `gorm:"foreignkey:service;association_foreignkey:id" json:"checkins,omitempty" yaml:"-" scope:"user,admin"` + Failures []*failures.Failure `gorm:"-" json:"failures,omitempty" yaml:"-" scope:"user,admin"` + LastProcessingTime time.Time `gorm:"column:last_processing_time" json:"-"` + + 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:"-"` +} // ServiceOrder will reorder the services based on 'order_id' (Order) type ServiceOrder []Service