From 0d47e108d79b38863deb646aaf538af958d8d633 Mon Sep 17 00:00:00 2001 From: George Robinson Date: Mon, 29 Apr 2024 15:47:43 +0100 Subject: [PATCH] Add routeID to GroupMarker interface Signed-off-by: George Robinson --- types/types.go | 20 +++++++++++++------- types/types_test.go | 17 +++++++++++------ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/types/types.go b/types/types.go index dfc119acda..648f0a7e01 100644 --- a/types/types.go +++ b/types/types.go @@ -100,16 +100,22 @@ type AlertMarker interface { Inhibited(model.Fingerprint) ([]string, bool) } +// GroupMarker helps to mark groups as active or muted. +// All methods are goroutine-safe. +// +// TODO(grobinson): routeID is used in Muted and SetMuted because groupKey +// is not unique (see #3817). Once groupKey uniqueness is fixed routeID can +// be removed from the GroupMarker interface. type GroupMarker interface { // Muted returns true if the group is muted, otherwise false. If the group // is muted then it also returns the names of the time intervals that muted // it. - Muted(groupKey string) ([]string, bool) + Muted(routeID, groupKey string) ([]string, bool) // SetMuted marks the group as muted, and sets the names of the time // intervals that mute it. If the list of names is nil or the empty slice // then the muted marker is removed. - SetMuted(groupKey string, timeIntervalNames []string) + SetMuted(routeID, groupKey string, timeIntervalNames []string) } // NewMarker returns an instance of a AlertMarker implementation. @@ -130,10 +136,10 @@ type MemMarker struct { } // Muted implements GroupMarker. -func (m *MemMarker) Muted(groupKey string) ([]string, bool) { +func (m *MemMarker) Muted(routeID, groupKey string) ([]string, bool) { m.mtx.Lock() defer m.mtx.Unlock() - status, ok := m.groups[groupKey] + status, ok := m.groups[routeID+groupKey] if !ok { return nil, false } @@ -141,13 +147,13 @@ func (m *MemMarker) Muted(groupKey string) ([]string, bool) { } // SetMuted implements GroupMarker. -func (m *MemMarker) SetMuted(groupKey string, timeIntervalNames []string) { +func (m *MemMarker) SetMuted(routeID, groupKey string, timeIntervalNames []string) { m.mtx.Lock() defer m.mtx.Unlock() - status, ok := m.groups[groupKey] + status, ok := m.groups[routeID+groupKey] if !ok { status = &groupStatus{} - m.groups[groupKey] = status + m.groups[routeID+groupKey] = status } status.mutedBy = timeIntervalNames } diff --git a/types/types_test.go b/types/types_test.go index 8d009e140f..45bc59e2f0 100644 --- a/types/types_test.go +++ b/types/types_test.go @@ -34,24 +34,29 @@ func TestMemMarker_Muted(t *testing.T) { marker := NewMarker(r) // No groups should be muted. - timeIntervalNames, isMuted := marker.Muted("group1") + timeIntervalNames, isMuted := marker.Muted("route1", "group1") require.False(t, isMuted) require.Empty(t, timeIntervalNames) // Mark the group as muted because it's the weekend. - marker.SetMuted("group1", []string{"weekends"}) - timeIntervalNames, isMuted = marker.Muted("group1") + marker.SetMuted("route1", "group1", []string{"weekends"}) + timeIntervalNames, isMuted = marker.Muted("route1", "group1") require.True(t, isMuted) require.Equal(t, []string{"weekends"}, timeIntervalNames) // Other groups should not be marked as muted. - timeIntervalNames, isMuted = marker.Muted("group2") + timeIntervalNames, isMuted = marker.Muted("route1", "group2") + require.False(t, isMuted) + require.Empty(t, timeIntervalNames) + + // Other routes should not be marked as muted either. + timeIntervalNames, isMuted = marker.Muted("route2", "group1") require.False(t, isMuted) require.Empty(t, timeIntervalNames) // The group is no longer muted. - marker.SetMuted("group1", nil) - timeIntervalNames, isMuted = marker.Muted("group1") + marker.SetMuted("route1", "group1", nil) + timeIntervalNames, isMuted = marker.Muted("route1", "group1") require.False(t, isMuted) require.Empty(t, timeIntervalNames) }