From c24def95983af0289027f0635f4176713c09999a Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Thu, 24 Feb 2022 16:05:18 +0530 Subject: [PATCH 01/14] bug_fix --- handlers/services.go | 22 +++++++++++++++++++--- types/downtimes/database.go | 7 +++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/handlers/services.go b/handlers/services.go index e4640de4..525a380c 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -545,11 +545,27 @@ func apiAllServicesStatusHandler(w http.ResponseWriter, r *http.Request) { 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] + if t == "" { + dtime:=downtimes.FindDowntime2() + timeNow := time.Now() + for i := 0; i < len(dtime); i++ { + downtimeVar := dtime[i] + serviceVar,_ := services.Find(downtimeVar.ServiceId) + checkInterval := time.Duration(serviceVar.Interval) + timeInstance := timeNow.Add(-time.Second * checkInterval) + if(timeInstance.After(*(downtimeVar.Start)) || timeInstance.Equal(*(downtimeVar.Start)) ) && (downtimeVar.End == nil || (timeInstance.Before(*(downtimeVar.End)) || timeInstance.Equal(*(downtimeVar.End))) ){ + m[downtimeVar.ServiceId] = downtimeVar + } + } + }else{ + dtime := findAllDowntimes(t) + for i := 0; i < len(dtime); i += 1 { + m[dtime[i].ServiceId] = dtime[i] + } } + + for _, v := range services.AllInOrder() { var serviceDowntimeVar services.ServiceWithDowntime serviceDowntimeVar.Service = v diff --git a/types/downtimes/database.go b/types/downtimes/database.go index 0c12c90e..f11abfa1 100644 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -67,6 +67,13 @@ func FindDowntime(timeVar time.Time) []Downtime { return downtime } +func FindDowntime2()[]Downtime { + var downtime []Downtime + db.Raw("SELECT DISTINCT ON (downtimes.service) downtimes.* FROM downtimes ORDER BY service ASC, start DESC;").Scan(&downtime) + //db.Select("downtimes.*").Joins("left join downtimes as d2 on d2.service = downtimes.service AND downtimes.start < d2.start").Where("d2.end = ?",nil).Find(&downtime) + return downtime +} + func FindAll(vars map[string]string) (*[]Downtime, error) { var downtime []Downtime var start time.Time From b64811a991fa529b604a1a87c82cf5fa95a96771 Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Thu, 24 Feb 2022 17:31:52 +0530 Subject: [PATCH 02/14] adjusted the condition --- handlers/services.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/handlers/services.go b/handlers/services.go index 525a380c..4aa41043 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -547,14 +547,14 @@ func apiAllServicesStatusHandler(w http.ResponseWriter, r *http.Request) { var srvs []services.ServiceWithDowntime m := make(map[int64]downtimes.Downtime) if t == "" { - dtime:=downtimes.FindDowntime2() timeNow := time.Now() + dtime:=downtimes.FindDowntime2() for i := 0; i < len(dtime); i++ { downtimeVar := dtime[i] serviceVar,_ := services.Find(downtimeVar.ServiceId) checkInterval := time.Duration(serviceVar.Interval) timeInstance := timeNow.Add(-time.Second * checkInterval) - if(timeInstance.After(*(downtimeVar.Start)) || timeInstance.Equal(*(downtimeVar.Start)) ) && (downtimeVar.End == nil || (timeInstance.Before(*(downtimeVar.End)) || timeInstance.Equal(*(downtimeVar.End))) ){ + if downtimeVar.End == nil || timeInstance.Before(*(downtimeVar.End)) || timeInstance.Equal(*(downtimeVar.End)) { m[downtimeVar.ServiceId] = downtimeVar } } From a8e8d2da06b94f8c05e57036df6f2886ac7e8497 Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Fri, 25 Feb 2022 09:22:36 +0530 Subject: [PATCH 03/14] updated logic --- handlers/services.go | 12 ++++-------- types/downtimes/database.go | 2 +- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/handlers/services.go b/handlers/services.go index 4aa41043..c6d99ed7 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -547,25 +547,21 @@ func apiAllServicesStatusHandler(w http.ResponseWriter, r *http.Request) { var srvs []services.ServiceWithDowntime m := make(map[int64]downtimes.Downtime) if t == "" { - timeNow := time.Now() - dtime:=downtimes.FindDowntime2() + dtime := downtimes.FindDowntime2() for i := 0; i < len(dtime); i++ { downtimeVar := dtime[i] - serviceVar,_ := services.Find(downtimeVar.ServiceId) - checkInterval := time.Duration(serviceVar.Interval) - timeInstance := timeNow.Add(-time.Second * checkInterval) - if downtimeVar.End == nil || timeInstance.Before(*(downtimeVar.End)) || timeInstance.Equal(*(downtimeVar.End)) { + serviceVar, _ := services.Find(downtimeVar.ServiceId) + if serviceVar.Online == false { m[downtimeVar.ServiceId] = downtimeVar } } - }else{ + } else { dtime := findAllDowntimes(t) for i := 0; i < len(dtime); i += 1 { m[dtime[i].ServiceId] = dtime[i] } } - for _, v := range services.AllInOrder() { var serviceDowntimeVar services.ServiceWithDowntime serviceDowntimeVar.Service = v diff --git a/types/downtimes/database.go b/types/downtimes/database.go index f11abfa1..86ad1d76 100644 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -67,7 +67,7 @@ func FindDowntime(timeVar time.Time) []Downtime { return downtime } -func FindDowntime2()[]Downtime { +func FindDowntime2() []Downtime { var downtime []Downtime db.Raw("SELECT DISTINCT ON (downtimes.service) downtimes.* FROM downtimes ORDER BY service ASC, start DESC;").Scan(&downtime) //db.Select("downtimes.*").Joins("left join downtimes as d2 on d2.service = downtimes.service AND downtimes.start < d2.start").Where("d2.end = ?",nil).Find(&downtime) From f12391df252bbc0997daaa09ba6a1069061ff137 Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Fri, 25 Feb 2022 09:48:06 +0530 Subject: [PATCH 04/14] checking db query --- handlers/services.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/handlers/services.go b/handlers/services.go index c6d99ed7..957ed860 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -548,12 +548,11 @@ func apiAllServicesStatusHandler(w http.ResponseWriter, r *http.Request) { m := make(map[int64]downtimes.Downtime) if t == "" { dtime := downtimes.FindDowntime2() - for i := 0; i < len(dtime); i++ { + for i := 0; i < len(dtime); i+=1 { downtimeVar := dtime[i] - serviceVar, _ := services.Find(downtimeVar.ServiceId) - if serviceVar.Online == false { - m[downtimeVar.ServiceId] = downtimeVar - } + //serviceVar, _ := services.Find(downtimeVar.ServiceId) + m[downtimeVar.ServiceId] = downtimeVar + } } else { dtime := findAllDowntimes(t) From bfca2113ccd4900d06aca82b5965e2f2b1f3475a Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Fri, 25 Feb 2022 10:49:42 +0530 Subject: [PATCH 05/14] added logs for stage --- handlers/services.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/handlers/services.go b/handlers/services.go index 957ed860..64f3a08a 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -548,11 +548,13 @@ func apiAllServicesStatusHandler(w http.ResponseWriter, r *http.Request) { m := make(map[int64]downtimes.Downtime) if t == "" { dtime := downtimes.FindDowntime2() + log.Println(len(dtime)) + log.Println(dtime) for i := 0; i < len(dtime); i+=1 { downtimeVar := dtime[i] - //serviceVar, _ := services.Find(downtimeVar.ServiceId) + /*serviceVar, _ := services.Find(downtimeVar.ServiceId) + */ m[downtimeVar.ServiceId] = downtimeVar - } } else { dtime := findAllDowntimes(t) From a21b72b15437045e714d1ef5aa68670203d2a4a5 Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Fri, 25 Feb 2022 13:14:53 +0530 Subject: [PATCH 06/14] changed studd --- handlers/services.go | 10 +++++----- types/downtimes/database.go | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/handlers/services.go b/handlers/services.go index 64f3a08a..0b2829d7 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -548,14 +548,14 @@ func apiAllServicesStatusHandler(w http.ResponseWriter, r *http.Request) { m := make(map[int64]downtimes.Downtime) if t == "" { dtime := downtimes.FindDowntime2() - log.Println(len(dtime)) + log.Println("FindDowntime2 results") log.Println(dtime) - for i := 0; i < len(dtime); i+=1 { + /*for i := 0; i < len(dtime); i+=1 { downtimeVar := dtime[i] - /*serviceVar, _ := services.Find(downtimeVar.ServiceId) - */ + serviceVar, _ := services.Find(downtimeVar.ServiceId) + m[downtimeVar.ServiceId] = downtimeVar - } + }*/ } else { dtime := findAllDowntimes(t) for i := 0; i < len(dtime); i += 1 { diff --git a/types/downtimes/database.go b/types/downtimes/database.go index 86ad1d76..62a7a600 100644 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -67,8 +67,8 @@ func FindDowntime(timeVar time.Time) []Downtime { return downtime } -func FindDowntime2() []Downtime { - var downtime []Downtime +func FindDowntime2() interface{} { + var downtime interface{} db.Raw("SELECT DISTINCT ON (downtimes.service) downtimes.* FROM downtimes ORDER BY service ASC, start DESC;").Scan(&downtime) //db.Select("downtimes.*").Joins("left join downtimes as d2 on d2.service = downtimes.service AND downtimes.start < d2.start").Where("d2.end = ?",nil).Find(&downtime) return downtime From 48decee356195b1bc9b2bc7a99d729a07d6ea10d Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Fri, 25 Feb 2022 14:47:45 +0530 Subject: [PATCH 07/14] changed stuff --- handlers/services.go | 10 ++++++---- types/downtimes/database.go | 6 +++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/handlers/services.go b/handlers/services.go index 0b2829d7..6e97420d 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -550,12 +550,14 @@ func apiAllServicesStatusHandler(w http.ResponseWriter, r *http.Request) { dtime := downtimes.FindDowntime2() log.Println("FindDowntime2 results") log.Println(dtime) - /*for i := 0; i < len(dtime); i+=1 { + fmt.Println(dtime) + for i := 0; i < len(dtime); i+=1 { downtimeVar := dtime[i] serviceVar, _ := services.Find(downtimeVar.ServiceId) - - m[downtimeVar.ServiceId] = downtimeVar - }*/ + if serviceVar.Online ==false { + m[downtimeVar.ServiceId] = downtimeVar + } + } } else { dtime := findAllDowntimes(t) for i := 0; i < len(dtime); i += 1 { diff --git a/types/downtimes/database.go b/types/downtimes/database.go index 62a7a600..b14fe553 100644 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -67,9 +67,9 @@ func FindDowntime(timeVar time.Time) []Downtime { return downtime } -func FindDowntime2() interface{} { - var downtime interface{} - db.Raw("SELECT DISTINCT ON (downtimes.service) downtimes.* FROM downtimes ORDER BY service ASC, start DESC;").Scan(&downtime) +func FindDowntime2() []Downtime { + var downtime []Downtime + db.Raw("select d1.* from downtimes d1 left join downtimes d2 on d1.service = d2.service and d2.start > d1.start where d2.service is NULL;").Scan(&downtime) //db.Select("downtimes.*").Joins("left join downtimes as d2 on d2.service = downtimes.service AND downtimes.start < d2.start").Where("d2.end = ?",nil).Find(&downtime) return downtime } From 8b7e17ed897d6f061aa14857661f922830be3881 Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Fri, 25 Feb 2022 16:12:24 +0530 Subject: [PATCH 08/14] changed stuff --- handlers/services.go | 13 ++++--------- types/downtimes/database.go | 7 +++---- 2 files changed, 7 insertions(+), 13 deletions(-) diff --git a/handlers/services.go b/handlers/services.go index 6e97420d..94ea5b81 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -547,15 +547,10 @@ func apiAllServicesStatusHandler(w http.ResponseWriter, r *http.Request) { var srvs []services.ServiceWithDowntime m := make(map[int64]downtimes.Downtime) if t == "" { - dtime := downtimes.FindDowntime2() - log.Println("FindDowntime2 results") - log.Println(dtime) - fmt.Println(dtime) - for i := 0; i < len(dtime); i+=1 { - downtimeVar := dtime[i] - serviceVar, _ := services.Find(downtimeVar.ServiceId) - if serviceVar.Online ==false { - m[downtimeVar.ServiceId] = downtimeVar + for _, v := range services.AllInOrder() { + if v.Online == false { + downtime:= downtimes.FindDowntime3(v.Id) + m[v.Id] = downtime } } } else { diff --git a/types/downtimes/database.go b/types/downtimes/database.go index b14fe553..55d5a97d 100644 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -67,10 +67,9 @@ func FindDowntime(timeVar time.Time) []Downtime { return downtime } -func FindDowntime2() []Downtime { - var downtime []Downtime - db.Raw("select d1.* from downtimes d1 left join downtimes d2 on d1.service = d2.service and d2.start > d1.start where d2.service is NULL;").Scan(&downtime) - //db.Select("downtimes.*").Joins("left join downtimes as d2 on d2.service = downtimes.service AND downtimes.start < d2.start").Where("d2.end = ?",nil).Find(&downtime) +func FindDowntime3(service int64) Downtime { + var downtime Downtime + db.Raw("select * from downtimes where service = ? order by start desc limit 1;",service).Scan(&downtime) return downtime } From b126b661182270b764c8241602512fc251e1fcbe Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Fri, 25 Feb 2022 16:47:39 +0530 Subject: [PATCH 09/14] changed stuff --- types/downtimes/database.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/types/downtimes/database.go b/types/downtimes/database.go index 55d5a97d..05e09a76 100644 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -69,7 +69,8 @@ func FindDowntime(timeVar time.Time) []Downtime { func FindDowntime3(service int64) Downtime { var downtime Downtime - db.Raw("select * from downtimes where service = ? order by start desc limit 1;",service).Scan(&downtime) + q := db.Where("service = ?",service) + q = q.Order("start desc").First(&downtime) return downtime } From aa32a6c5e3d1c1b24a7e9e78306a45403a3c8a1a Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Fri, 25 Feb 2022 17:19:12 +0530 Subject: [PATCH 10/14] fixed the time.now part --- handlers/services.go | 2 +- types/downtimes/database.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/handlers/services.go b/handlers/services.go index 94ea5b81..e553c06a 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -549,7 +549,7 @@ func apiAllServicesStatusHandler(w http.ResponseWriter, r *http.Request) { if t == "" { for _, v := range services.AllInOrder() { if v.Online == false { - downtime:= downtimes.FindDowntime3(v.Id) + downtime := downtimes.FindDowntime2(v.Id) m[v.Id] = downtime } } diff --git a/types/downtimes/database.go b/types/downtimes/database.go index 05e09a76..8b21c86d 100644 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -67,9 +67,9 @@ func FindDowntime(timeVar time.Time) []Downtime { return downtime } -func FindDowntime3(service int64) Downtime { +func FindDowntime2(service int64) Downtime { var downtime Downtime - q := db.Where("service = ?",service) + q := db.Where("service = ?", service) q = q.Order("start desc").First(&downtime) return downtime } From 7f0d3a94a834de1cc6608a64de3441942d4f2436 Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Fri, 25 Feb 2022 18:56:01 +0530 Subject: [PATCH 11/14] updated names --- handlers/downtimes.go | 2 +- handlers/services.go | 7 ++++--- types/downtimes/database.go | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/handlers/downtimes.go b/handlers/downtimes.go index c9f26670..a409b535 100644 --- a/handlers/downtimes.go +++ b/handlers/downtimes.go @@ -93,7 +93,7 @@ func apiAllDowntimesForServiceHandler(w http.ResponseWriter, r *http.Request) { serviceId := utils.ToInt(vars["service_id"]) ninetyDaysAgo := time.Now().Add(time.Duration(-90*24) * time.Hour) - downtime, err := downtimes.FindByService(serviceId, ninetyDaysAgo, time.Now()) + downtime, err := downtimes.FindByServiceAndDuration(serviceId, ninetyDaysAgo, time.Now()) if err != nil { sendErrorJson(err, w, r) return diff --git a/handlers/services.go b/handlers/services.go index e553c06a..3c93d593 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -545,11 +545,12 @@ func apiAllServicesStatusHandler(w http.ResponseWriter, r *http.Request) { t = query.Get("time") } var srvs []services.ServiceWithDowntime + servicesList := services.AllInOrder() m := make(map[int64]downtimes.Downtime) if t == "" { - for _, v := range services.AllInOrder() { + for _, v := range servicesList { if v.Online == false { - downtime := downtimes.FindDowntime2(v.Id) + downtime := downtimes.FindLatestDowntimeOfService(v.Id) m[v.Id] = downtime } } @@ -560,7 +561,7 @@ func apiAllServicesStatusHandler(w http.ResponseWriter, r *http.Request) { } } - for _, v := range services.AllInOrder() { + for _, v := range servicesList { var serviceDowntimeVar services.ServiceWithDowntime serviceDowntimeVar.Service = v if vv, ok := m[v.Id]; ok == true { diff --git a/types/downtimes/database.go b/types/downtimes/database.go index 8b21c86d..3a63b70e 100644 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -53,7 +53,7 @@ func (c *Downtime) BeforeUpdate() error { return c.Validate() } -func FindByService(service int64, start time.Time, end time.Time) (*[]Downtime, error) { +func FindByServiceAndDuration(service int64, start time.Time, end time.Time) (*[]Downtime, error) { var downtime []Downtime q := db.Where("service = ? and start BETWEEN ? AND ? ", service, start, end) q = q.Order("id ASC ").Find(&downtime) @@ -67,7 +67,7 @@ func FindDowntime(timeVar time.Time) []Downtime { return downtime } -func FindDowntime2(service int64) Downtime { +func FindLatestDowntimeOfService(service int64) Downtime { var downtime Downtime q := db.Where("service = ?", service) q = q.Order("start desc").First(&downtime) From cf596b9d700fd8a540aef47aa8e65b056afacb15 Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Fri, 25 Feb 2022 18:58:22 +0530 Subject: [PATCH 12/14] changed names --- types/services/methods.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/services/methods.go b/types/services/methods.go index b4ac3be7..78555ef3 100644 --- a/types/services/methods.go +++ b/types/services/methods.go @@ -68,7 +68,7 @@ func (s Service) Duration() time.Duration { func (s Service) DowntimeData(start time.Time, end time.Time) (*UptimeSeries, *[]downtimes.Downtime, error) { - downtimesList, _ := downtimes.FindByService(s.Id, start, end) + downtimesList, _ := downtimes.FindByServiceAndDuration(s.Id, start, end) response := &UptimeSeries{ Start: start, From 52d345a0d8d0320640e63a683d4db688a9cc165f Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Mon, 28 Feb 2022 13:14:17 +0530 Subject: [PATCH 13/14] added spaces --- types/downtimes/database.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/types/downtimes/database.go b/types/downtimes/database.go index 3a63b70e..431f576d 100644 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -69,8 +69,8 @@ func FindDowntime(timeVar time.Time) []Downtime { func FindLatestDowntimeOfService(service int64) Downtime { var downtime Downtime - q := db.Where("service = ?", service) - q = q.Order("start desc").First(&downtime) + q := db.Where("service = ? ", service) + q = q.Order("start desc ").First(&downtime) return downtime } From 747ba753829e57cfc30266b6912fdd3a4276a306 Mon Sep 17 00:00:00 2001 From: Rhythm <35167328+kRhythm@users.noreply.github.com> Date: Mon, 28 Feb 2022 15:09:07 +0530 Subject: [PATCH 14/14] removed spaces --- types/downtimes/database.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/types/downtimes/database.go b/types/downtimes/database.go index 431f576d..ab4e237f 100644 --- a/types/downtimes/database.go +++ b/types/downtimes/database.go @@ -55,8 +55,8 @@ func (c *Downtime) BeforeUpdate() error { func FindByServiceAndDuration(service int64, start time.Time, end time.Time) (*[]Downtime, error) { var downtime []Downtime - q := db.Where("service = ? and start BETWEEN ? AND ? ", service, start, end) - q = q.Order("id ASC ").Find(&downtime) + q := db.Where("service = ? and start BETWEEN ? AND ?", service, start, end) + q = q.Order("id ASC").Find(&downtime) return &downtime, q.Error() } @@ -69,8 +69,8 @@ func FindDowntime(timeVar time.Time) []Downtime { func FindLatestDowntimeOfService(service int64) Downtime { var downtime Downtime - q := db.Where("service = ? ", service) - q = q.Order("start desc ").First(&downtime) + q := db.Where("service = ?", service) + q = q.Order("start desc").First(&downtime) return downtime }