Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
cmd: order scale status list by time recent->oldest.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasell committed Nov 14, 2019
1 parent 5f4485b commit 29e7011
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions cmd/scale/status/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)))
}
}

Expand All @@ -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 {
Expand Down

0 comments on commit 29e7011

Please sign in to comment.