@@ -324,25 +324,72 @@ func (i Message) OldMessage() string {
324324}
325325
326326func (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+
346393type IntervalFilter func (i Interval ) bool
347394
348395type IntervalFilters []IntervalFilter
0 commit comments