From 2cd0fd85f8be4f403500019cbad102bd06eb6a48 Mon Sep 17 00:00:00 2001 From: akash-sachan Date: Thu, 11 May 2023 17:47:47 +0530 Subject: [PATCH 1/8] sort --- handlers/incident.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/handlers/incident.go b/handlers/incident.go index 3f60d76d..32898af7 100644 --- a/handlers/incident.go +++ b/handlers/incident.go @@ -8,6 +8,7 @@ import ( "github.com/razorpay/statping/types/services" "github.com/razorpay/statping/utils" "net/http" + "sort" "time" ) @@ -71,7 +72,8 @@ func getVisibleIncidentsOfService(service *services.Service) []incidents.Inciden visibleIncidentIds = append(visibleIncidentIds, incident.Id) } else if checkResolvedVisibility(incident.Updates) { incidentVar := *incident - reverse(incidentVar.Updates) + sortUpdates(incidentVar.Updates) + //reverse(incidentVar.Updates) visibleIncidents = append(visibleIncidents, incidentVar) visibleIncidentIds = append(visibleIncidentIds, incident.Id) } @@ -80,11 +82,17 @@ func getVisibleIncidentsOfService(service *services.Service) []incidents.Inciden return visibleIncidents } -func reverse(incidents []*incidents.IncidentUpdate) { +func sortUpdates(updates []*incidents.IncidentUpdate) { + sort.Slice(updates, func(i, j int) bool { + return updates[i].CreatedAt.Unix() >= updates[j].CreatedAt.Unix() + }) +} + +/*func reverse(incidents []*incidents.IncidentUpdate) { for i, j := 0, len(incidents)-1; i < j; i, j = i+1, j-1 { incidents[i], incidents[j] = incidents[j], incidents[i] } -} +}*/ func hasZeroUpdates(Updates []*incidents.IncidentUpdate) bool { if len(Updates) == 0 { @@ -93,8 +101,10 @@ func hasZeroUpdates(Updates []*incidents.IncidentUpdate) bool { return false } +// NOT [(last update is resolved) AND (last update was more than 2 hrs ago)] func checkResolvedVisibility(incidentUpdates []*incidents.IncidentUpdate) bool { - if !(incidentUpdates[len(incidentUpdates)-1].Type == resolved && getTimeDiff(incidentUpdates[len(incidentUpdates)-1]) > incidentsTimeoutInMinutes) { + if !(incidentUpdates[len(incidentUpdates)-1].Type == resolved && + getTimeDiff(incidentUpdates[len(incidentUpdates)-1]) > incidentsTimeoutInMinutes) { return true } return false From ad4c02858c8e55d2c32af232d08f81fbeeb44280 Mon Sep 17 00:00:00 2001 From: akash-sachan Date: Thu, 11 May 2023 18:03:24 +0530 Subject: [PATCH 2/8] remove commented code --- handlers/incident.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/handlers/incident.go b/handlers/incident.go index 32898af7..1b478dbb 100644 --- a/handlers/incident.go +++ b/handlers/incident.go @@ -73,7 +73,6 @@ func getVisibleIncidentsOfService(service *services.Service) []incidents.Inciden } else if checkResolvedVisibility(incident.Updates) { incidentVar := *incident sortUpdates(incidentVar.Updates) - //reverse(incidentVar.Updates) visibleIncidents = append(visibleIncidents, incidentVar) visibleIncidentIds = append(visibleIncidentIds, incident.Id) } @@ -88,12 +87,6 @@ func sortUpdates(updates []*incidents.IncidentUpdate) { }) } -/*func reverse(incidents []*incidents.IncidentUpdate) { - for i, j := 0, len(incidents)-1; i < j; i, j = i+1, j-1 { - incidents[i], incidents[j] = incidents[j], incidents[i] - } -}*/ - func hasZeroUpdates(Updates []*incidents.IncidentUpdate) bool { if len(Updates) == 0 { return true From 2f4c5187054c31dd7a746b9e44c5e928a29d2957 Mon Sep 17 00:00:00 2001 From: akash-sachan Date: Thu, 11 May 2023 18:19:56 +0530 Subject: [PATCH 3/8] sort by created_at --- types/incidents/database.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/incidents/database.go b/types/incidents/database.go index b38ae461..5af1cb07 100644 --- a/types/incidents/database.go +++ b/types/incidents/database.go @@ -34,7 +34,7 @@ func (i *Incident) BeforeCreate() error { } func (i *Incident) AfterFind() { - db.Model(i).Related(&i.Updates).Order("id DESC") + db.Model(i).Related(&i.Updates).Order("created_at DESC") metrics.Query("incident", "find") } From dc1607b998d8e4d933ae1ab1bf65ede0a9341e53 Mon Sep 17 00:00:00 2001 From: akash-sachan Date: Thu, 11 May 2023 18:44:43 +0530 Subject: [PATCH 4/8] test fix afterfind --- types/incidents/database.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/incidents/database.go b/types/incidents/database.go index 5af1cb07..3135c650 100644 --- a/types/incidents/database.go +++ b/types/incidents/database.go @@ -34,7 +34,7 @@ func (i *Incident) BeforeCreate() error { } func (i *Incident) AfterFind() { - db.Model(i).Related(&i.Updates).Order("created_at DESC") + db.Model(i).Related(i.Updates).Order("created_at DESC") metrics.Query("incident", "find") } From 5799f53b6b797502abb7a6c23ed1ea668421db3c Mon Sep 17 00:00:00 2001 From: akash-sachan Date: Thu, 11 May 2023 19:42:52 +0530 Subject: [PATCH 5/8] sort --- handlers/incident.go | 1 + types/incidents/database.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/handlers/incident.go b/handlers/incident.go index 1b478dbb..b79ee21b 100644 --- a/handlers/incident.go +++ b/handlers/incident.go @@ -113,6 +113,7 @@ func apiIncidentUpdatesHandler(w http.ResponseWriter, r *http.Request) { sendErrorJson(err, w, r) return } + sortUpdates(incid.Updates) returnJson(incid.Updates, w, r) } diff --git a/types/incidents/database.go b/types/incidents/database.go index 3135c650..b38ae461 100644 --- a/types/incidents/database.go +++ b/types/incidents/database.go @@ -34,7 +34,7 @@ func (i *Incident) BeforeCreate() error { } func (i *Incident) AfterFind() { - db.Model(i).Related(i.Updates).Order("created_at DESC") + db.Model(i).Related(&i.Updates).Order("id DESC") metrics.Query("incident", "find") } From eb0a632ece8a2f72591511b2884230e29ed49f8c Mon Sep 17 00:00:00 2001 From: akash-sachan Date: Thu, 11 May 2023 19:52:17 +0530 Subject: [PATCH 6/8] order --- handlers/incident.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/handlers/incident.go b/handlers/incident.go index b79ee21b..294a3711 100644 --- a/handlers/incident.go +++ b/handlers/incident.go @@ -72,7 +72,7 @@ func getVisibleIncidentsOfService(service *services.Service) []incidents.Inciden visibleIncidentIds = append(visibleIncidentIds, incident.Id) } else if checkResolvedVisibility(incident.Updates) { incidentVar := *incident - sortUpdates(incidentVar.Updates) + sortUpdates(incidentVar.Updates, "DESC") visibleIncidents = append(visibleIncidents, incidentVar) visibleIncidentIds = append(visibleIncidentIds, incident.Id) } @@ -81,9 +81,15 @@ func getVisibleIncidentsOfService(service *services.Service) []incidents.Inciden return visibleIncidents } -func sortUpdates(updates []*incidents.IncidentUpdate) { +func sortUpdates(updates []*incidents.IncidentUpdate, order string) { sort.Slice(updates, func(i, j int) bool { - return updates[i].CreatedAt.Unix() >= updates[j].CreatedAt.Unix() + switch order { + case "DESC": + return updates[i].CreatedAt.Unix() >= updates[j].CreatedAt.Unix() + case "ASC": + return updates[i].CreatedAt.Unix() <= updates[j].CreatedAt.Unix() + } + return false }) } @@ -113,7 +119,7 @@ func apiIncidentUpdatesHandler(w http.ResponseWriter, r *http.Request) { sendErrorJson(err, w, r) return } - sortUpdates(incid.Updates) + sortUpdates(incid.Updates, "ASC") returnJson(incid.Updates, w, r) } From f9c134bfd2c67628a450361044d9ad48c57aa4ae Mon Sep 17 00:00:00 2001 From: akash-sachan Date: Fri, 12 May 2023 11:08:39 +0530 Subject: [PATCH 7/8] add logs --- handlers/incident.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/handlers/incident.go b/handlers/incident.go index 294a3711..dea0c211 100644 --- a/handlers/incident.go +++ b/handlers/incident.go @@ -104,8 +104,11 @@ func hasZeroUpdates(Updates []*incidents.IncidentUpdate) bool { func checkResolvedVisibility(incidentUpdates []*incidents.IncidentUpdate) bool { if !(incidentUpdates[len(incidentUpdates)-1].Type == resolved && getTimeDiff(incidentUpdates[len(incidentUpdates)-1]) > incidentsTimeoutInMinutes) { + log.Info(fmt.Sprintf("Incident update resolved visibility: %v", true)) return true } + // This log will also given insight into number of unnecessary incidents fetched from DB + log.Info(fmt.Sprintf("Incident update resolved visibility: %v", false)) return false } From d0e811439aa3da526ffd29b8d81f33ac8dd61c35 Mon Sep 17 00:00:00 2001 From: akash-sachan Date: Fri, 12 May 2023 11:26:21 +0530 Subject: [PATCH 8/8] add UT --- handlers/incidents_test.go | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/handlers/incidents_test.go b/handlers/incidents_test.go index c0154394..bcca6342 100644 --- a/handlers/incidents_test.go +++ b/handlers/incidents_test.go @@ -1,8 +1,11 @@ package handlers import ( + "github.com/razorpay/statping/types/incidents" + "github.com/razorpay/statping/utils" "github.com/stretchr/testify/assert" "testing" + "time" ) func TestUnAuthenticatedIncidentRoutes(t *testing.T) { @@ -153,3 +156,41 @@ func TestIncidentsAPIRoutes(t *testing.T) { }) } } + +func TestIncidentUpdateOrder(t *testing.T) { + now := utils.Now() + t0 := now.Add(-5 * time.Minute) + t1 := now.Add(-5*time.Minute + 1*time.Second) + t2 := now.Add(-5*time.Minute + 2*time.Second) + + updates := []*incidents.IncidentUpdate{ + { + Id: 78, + IncidentId: 23, + Message: "Update 2", + Type: "update", + CreatedAt: t1, + UpdatedAt: t2, + }, + { + Id: 79, + IncidentId: 23, + Message: "Resolved 1", + Type: "resolved", + CreatedAt: t2, + UpdatedAt: t2, + }, + { + Id: 76, + IncidentId: 23, + Message: "Update 1", + Type: "update", + CreatedAt: t0, + UpdatedAt: t2, + }, + } + sortUpdates(updates, "DESC") + assert.Equal(t, int64(79), updates[0].Id) + assert.Equal(t, int64(78), updates[1].Id) + assert.Equal(t, int64(76), updates[2].Id) +}