Skip to content

Commit 48f03bc

Browse files
committed
wip
1 parent f65da88 commit 48f03bc

File tree

2 files changed

+95
-4
lines changed

2 files changed

+95
-4
lines changed

pkg/monitor/monitorapi/types.go

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,25 +324,72 @@ func (i Message) OldMessage() string {
324324
}
325325

326326
func (i Locator) OldLocator() string {
327-
keys := sets.NewString()
327+
keys := []string{}
328328
for k := range i.Keys {
329-
keys.Insert(string(k))
330-
}
329+
keys = append(keys, string(k))
330+
}
331+
// Some components rely on the ordering of keys in the locator for groupings in interval charts.
332+
// (i.e. namespace pod uid container, which groups container events with their pod.
333+
// Blindly going through the keys results in alphabetical ordering, container comes first, and then we've
334+
// got container events separated from their pod events on the intervals chart.
335+
// This will hopefully eventually go away but for now we need it.
336+
337+
// Ensure these keys appear in this order. Other keys can be mixed in and their order will be preserved at the end,
338+
// but we want these specific combos to come in this order so items appear together in interval charts
339+
// which sort on locator.
340+
// This function is courtesy of chatgpt, but unit tested.
341+
keysToEnsure := []string{"namespace", "pod", "uid", "container"}
342+
343+
// Create a map to store the index of each key in the keysToEnsure slice
344+
orderMap := make(map[string]int, len(keysToEnsure))
345+
for i, key := range keysToEnsure {
346+
orderMap[key] = i
347+
}
348+
349+
// Use a custom sorting function to reorder the keys
350+
customSort(keys, func(i, j int) bool {
351+
idxI, foundI := orderMap[keys[i]]
352+
idxJ, foundJ := orderMap[keys[j]]
353+
354+
// If either key is not in the keysToEnsure, keep their relative order
355+
if !foundI && !foundJ {
356+
return i < j
357+
} else if !foundI {
358+
return false
359+
} else if !foundJ {
360+
return true
361+
}
362+
363+
// Compare the indices of keysToEnsure
364+
return idxI < idxJ
365+
})
331366

332367
annotations := []string{}
333-
for _, k := range keys.List() {
368+
for _, k := range keys {
334369
v := i.Keys[LocatorKey(k)]
335370
if LocatorKey(k) == LocatorE2ETestKey {
336371
annotations = append(annotations, fmt.Sprintf("%v/%q", k, v))
337372
} else {
338373
annotations = append(annotations, fmt.Sprintf("%v/%v", k, v))
339374
}
340375
}
376+
341377
annotationString := strings.Join(annotations, " ")
342378

343379
return annotationString
344380
}
345381

382+
// customSort sorts the keys slice using a custom sorting function
383+
func customSort(keys []string, less func(i, j int) bool) {
384+
for i := 0; i < len(keys); i++ {
385+
for j := i + 1; j < len(keys); j++ {
386+
if less(j, i) {
387+
keys[i], keys[j] = keys[j], keys[i]
388+
}
389+
}
390+
}
391+
}
392+
346393
type IntervalFilter func(i Interval) bool
347394

348395
type IntervalFilters []IntervalFilter

pkg/monitor/monitorapi/types_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package monitorapi
33
import (
44
"testing"
55
"time"
6+
7+
"github.com/stretchr/testify/assert"
68
)
79

810
func TestIntervals_Duration(t *testing.T) {
@@ -106,3 +108,45 @@ func TestIntervals_Duration(t *testing.T) {
106108
})
107109
}
108110
}
111+
112+
// Not sure this test needs to live forever, but while working through the move
113+
//
114+
// to structured locators, it would be best if the legacy one kept coming out with
115+
// keys in the same order they were before, as the intervals chart sorts on these and
116+
// some are expected to be grouped together.
117+
func TestLocatorOldLocator(t *testing.T) {
118+
tests := []struct {
119+
name string
120+
locator Locator
121+
expected string // the legacy locator
122+
}{
123+
{
124+
name: "container locator",
125+
locator: NewLocator().ContainerFromNames("mynamespace", "mypod", "fakeuid", "mycontainer"),
126+
expected: "namespace/mynamespace pod/mypod uid/fakeuid container/mycontainer",
127+
},
128+
{
129+
name: "pod locator",
130+
locator: NewLocator().PodFromNames("mynamespace", "mypod", "fakeuid"),
131+
expected: "namespace/mynamespace pod/mypod uid/fakeuid",
132+
},
133+
{
134+
name: "container locator with keys mixed in", // not sure if this can happen but make sure what we expect occurs
135+
locator: Locator{Keys: map[LocatorKey]string{
136+
"a": "b",
137+
"container": "mycontainer",
138+
"foo": "bar",
139+
"namespace": "mynamespace",
140+
"pod": "mypod",
141+
"uid": "fakeuid",
142+
"zzz": "foobar",
143+
}},
144+
expected: "namespace/mynamespace pod/mypod uid/fakeuid container/mycontainer a/b foo/bar zzz/foobar",
145+
},
146+
}
147+
for _, tt := range tests {
148+
t.Run(tt.name, func(t *testing.T) {
149+
assert.Equal(t, tt.expected, tt.locator.OldLocator())
150+
})
151+
}
152+
}

0 commit comments

Comments
 (0)