Skip to content

Commit

Permalink
Fix sort events
Browse files Browse the repository at this point in the history
  • Loading branch information
jlandowner committed May 23, 2024
1 parent c8aa5dd commit 1363baa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
27 changes: 5 additions & 22 deletions pkg/apiconv/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
31 changes: 31 additions & 0 deletions pkg/kosmo/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package kosmo
import (
"context"
"fmt"
"sort"
"time"

eventsv1 "k8s.io/api/events/v1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -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
}
}

0 comments on commit 1363baa

Please sign in to comment.