From 29e7011cc8896af9b2e14bcb8ae815598e576e9f Mon Sep 17 00:00:00 2001 From: James Rasell Date: Thu, 14 Nov 2019 15:59:55 +0100 Subject: [PATCH] cmd: order scale status list by time recent->oldest. --- cmd/scale/status/command.go | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/cmd/scale/status/command.go b/cmd/scale/status/command.go index 71c37f4..5642223 100644 --- a/cmd/scale/status/command.go +++ b/cmd/scale/status/command.go @@ -3,8 +3,10 @@ package status import ( "fmt" "os" + "sort" "strings" + "github.com/gofrs/uuid" "github.com/jrasell/sherpa/cmd/helper" "github.com/jrasell/sherpa/pkg/api" clientCfg "github.com/jrasell/sherpa/pkg/config/client" @@ -63,11 +65,14 @@ func runList(c *api.Client, latest bool) int { os.Exit(sysexits.Software) } + orderedIDs := orderStatusIDs(resp) + out := []string{listOutputHeader} - for id, jobEvents := range resp { - for jg, event := range jobEvents { - out = append(out, fmt.Sprintf("%v|%s|%s|%v", id, jg, event.Status, helper.UnixNanoToHumanUTC(event.Time))) + for i := range orderedIDs { + for jg, event := range resp[orderedIDs[i]] { + out = append(out, fmt.Sprintf("%v|%s|%s|%v", + orderedIDs[i], jg, event.Status, helper.UnixNanoToHumanUTC(event.Time))) } } @@ -77,6 +82,26 @@ func runList(c *api.Client, latest bool) int { return sysexits.OK } +func orderStatusIDs(input map[uuid.UUID]map[string]*api.ScalingEvent) []uuid.UUID { + inter := make(map[int64]uuid.UUID) + timeList := []int64{} + resp := []uuid.UUID{} + + for id, jobEvents := range input { + for _, event := range jobEvents { + inter[event.Time] = id + timeList = append(timeList, event.Time) + continue + } + } + + sort.Slice(timeList, func(i, j int) bool { return timeList[i] > timeList[j] }) + for i := range timeList { + resp = append(resp, inter[timeList[i]]) + } + return resp +} + func runInfo(c *api.Client, id string) int { resp, err := c.Scale().Info(id) if err != nil {