From 1363baae6025679ebee109a585ea1e01b1b2ad78 Mon Sep 17 00:00:00 2001 From: jlandowner Date: Thu, 23 May 2024 20:23:32 +0900 Subject: [PATCH] Fix sort events --- pkg/apiconv/user.go | 27 +++++---------------------- pkg/kosmo/event.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/pkg/apiconv/user.go b/pkg/apiconv/user.go index 69788f81..bc50eab1 100644 --- a/pkg/apiconv/user.go +++ b/pkg/apiconv/user.go @@ -10,6 +10,7 @@ import ( eventsv1 "k8s.io/api/events/v1" cosmov1alpha1 "github.com/cosmo-workspace/cosmo/api/v1alpha1" + "github.com/cosmo-workspace/cosmo/pkg/kosmo" dashv1alpha1 "github.com/cosmo-workspace/cosmo/proto/gen/dashboard/v1alpha1" ) @@ -153,29 +154,9 @@ func D2S_UserAddons(addons []*dashv1alpha1.UserAddon) []string { func K2D_Events(events []eventsv1.Event) []*dashv1alpha1.Event { es := make([]*dashv1alpha1.Event, len(events)) for i, v := range events { - var eventTime *timestamppb.Timestamp - if v.EventTime.Year() != 1 { - eventTime = timestamppb.New(v.EventTime.Time) - } else { - eventTime = timestamppb.New(v.DeprecatedLastTimestamp.Time) - } - - var count int32 - if v.Series != nil { - count = v.Series.Count - } else { - count = v.DeprecatedCount - } - - var lastTime *timestamppb.Timestamp - if v.Series != nil { - lastTime = timestamppb.New(v.Series.LastObservedTime.Time) - } else { - lastTime = timestamppb.New(v.DeprecatedLastTimestamp.Time) - } e := &dashv1alpha1.Event{ - EventTime: eventTime, + EventTime: timestamppb.New(kosmo.EventTime(v)), Reason: v.Reason, Note: v.Note, Type: v.Type, @@ -187,10 +168,12 @@ func K2D_Events(events []eventsv1.Event) []*dashv1alpha1.Event { }, ReportingController: v.ReportingController, } + + count := kosmo.EventCount(v) if count > 1 { e.Series = &dashv1alpha1.EventSeries{ Count: count, - LastObservedTime: lastTime, + LastObservedTime: timestamppb.New(kosmo.EventLastTime(v)), } } es[i] = e diff --git a/pkg/kosmo/event.go b/pkg/kosmo/event.go index 0224916f..7712d8ac 100644 --- a/pkg/kosmo/event.go +++ b/pkg/kosmo/event.go @@ -3,6 +3,8 @@ package kosmo import ( "context" "fmt" + "sort" + "time" eventsv1 "k8s.io/api/events/v1" apierrs "k8s.io/apimachinery/pkg/api/errors" @@ -47,6 +49,35 @@ func (c *Client) ListEventsForUser(ctx context.Context, user string) ([]eventsv1 } } + sort.SliceStable(userEvents, func(i, j int) bool { + return EventTime(userEvents[i]). + Before(EventTime(userEvents[j])) + }) + return userEvents, nil } + +func EventTime(v eventsv1.Event) time.Time { + if v.EventTime.Year() != 1 { + return v.EventTime.Time + } else { + return v.DeprecatedLastTimestamp.Time + } +} + +func EventCount(v eventsv1.Event) int32 { + if v.Series != nil { + return v.Series.Count + } else { + return v.DeprecatedCount + } +} + +func EventLastTime(v eventsv1.Event) time.Time { + if v.Series != nil { + return v.Series.LastObservedTime.Time + } else { + return v.DeprecatedLastTimestamp.Time + } +}