Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dynamic column sizing to wsk activation list command #427

Merged
merged 3 commits into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ dependencies {
build(['name':'golang.org/x/sys/unix', 'version':'7f918dd405547ecb864d14a8ecbbfe205b5f930f', 'transitive':false])
build(['name':'gopkg.in/yaml.v2', 'version':'eb3733d160e74a9c7e442f435eb3bea458e1d19f', 'transitive':false])
build(['name':'github.com/ghodss/yaml', 'version':'0ca9ea5df5451ffdf184b4428c902747c2c11cd7', 'transitive':false])
build(['name':'github.com/apache/incubator-openwhisk-client-go/whisk','version':'4286a8212a74c40d8950ee76681a67e12c9bf1a0','transitive':false])
build(['name':'github.com/apache/incubator-openwhisk-client-go/whisk','version':'47ad3426a4e3632fd17d859303f4074ae7b959ff','transitive':false])
build(['name':'github.com/apache/incubator-openwhisk-wskdeploy','version':'7d79fd74ca1045658196e5004f8820b67570734c','transitive':false])
// END - Imported from Godeps
test name:'github.com/stretchr/testify', version:'b91bfb9ebec76498946beb6af7c0230c7cc7ba6c', transitive:false //, tag: 'v1.2.0'
Expand Down
51 changes: 50 additions & 1 deletion commands/activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"os"
"os/signal"
"strconv"
"syscall"
"time"

Expand Down Expand Up @@ -53,6 +54,7 @@ var activationListCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
var err error
var qualifiedName = new(QualifiedName)
var orderedFilteredRow []whisk.ActivationFilteredRow

if whiskErr := CheckArgs(args, 0, 1, "Activation list",
wski18n.T("An optional namespace is the only valid argument.")); whiskErr != nil {
Expand Down Expand Up @@ -93,7 +95,18 @@ var activationListCmd = &cobra.Command{
if options.Docs == true {
printFullActivationList(activations)
} else {
printList(activations, false) // Default sorting for Activations are by creation time, hence sortByName is always false
maxKindSize := max(len("Kind"), getLargestKindSize(activations))
maxStatusSize := max(len("Status"), getLargestStatusSize(activations))

// Header string should show "Datetime", "Activation ID", "Kind", "Start", "Duration", "Status", "Entity", with Kind and Status being
// dynamically sized. The last column Entity will be sized correctly when printed, so no need to calculate size here
headerFmt := "%-19s %-32s %-" + strconv.Itoa(maxKindSize) + "s %-6s%-10s %-" + strconv.Itoa(maxStatusSize) + "s %-6s\n"
rowFmt := "%d-%02d-%02d %02d:%02d:%02d %-32s %-" + strconv.Itoa(maxKindSize) + "s %-5s %-10v %-" + strconv.Itoa(maxStatusSize) + "s %-"

for i := 0; i < len(activations); i++ {
orderedFilteredRow = append(orderedFilteredRow, whisk.ActivationFilteredRow{Row: activations[i], HeaderFmt: headerFmt, RowFmt: rowFmt})
}
printList(orderedFilteredRow, false) // Default sorting for Activations are by creation time, hence sortByName is always false
}

return nil
Expand Down Expand Up @@ -397,6 +410,42 @@ var activationPollCmd = &cobra.Command{
},
}

// Find the size needed for the Kind column when listing activations
func getLargestKindSize(activations []whisk.Activation) int {
var maxLen = 0
var curLen int
var kind interface{}

for i := 0; i < len(activations); i++ {
kind = activations[i].Annotations.GetValue("kind")
if kind == nil {
kind = "unknown"
}
curLen = len(kind.(string))
if curLen > maxLen {
maxLen = curLen
}
}
return maxLen
}

// Find the size needed for the Status column when listing activations
func getLargestStatusSize(activations []whisk.Activation) int {
// The first array in the StatusCodes variable is "success"
var maxLen = len(whisk.StatusCodes[0])
var curLen int

for i := 0; i < len(activations); i++ {
if activations[i].StatusCode > 0 && activations[i].StatusCode < len(whisk.StatusCodes) {
curLen = len(whisk.StatusCodes[activations[i].StatusCode])
if curLen > maxLen {
maxLen = curLen
}
}
}
return maxLen
}

func init() {
activationListCmd.Flags().IntVarP(&Flags.common.skip, "skip", "s", 0, wski18n.T("exclude the first `SKIP` number of activations from the result"))
activationListCmd.Flags().IntVarP(&Flags.common.limit, "limit", "l", DEFAULT_ACTIVATION_LIMIT, wski18n.T("only return `LIMIT` number of activations from the collection with a maximum LIMIT of {{.max}} activations",
Expand Down
4 changes: 3 additions & 1 deletion commands/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func printList(collection interface{}, sortByName bool) {
for i := range collection {
commandToSort = append(commandToSort, collection[i])
}
case []whisk.Activation:
case []whisk.ActivationFilteredRow:
for i := range collection {
commandToSort = append(commandToSort, collection[i])
}
Expand Down Expand Up @@ -266,6 +266,8 @@ func makeDefaultHeader(collection interface{}) string {
defaultHeader = fmt.Sprintf("%-30s %7s %20s %s", "Action", "Verb", "API Name", "URL")
} else if defaultHeader == "apifilteredlists" {
defaultHeader = ""
} else if defaultHeader == "activationfilteredrows" {
defaultHeader = ""
}
return defaultHeader
}
Expand Down
6 changes: 3 additions & 3 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"ignore": "test",
"package": [
{
"checksumSHA1": "6D6U+hfBxkxhDZWSHTrn8uhGod8=",
"checksumSHA1": "W0Cr3GbXN1qhrbg6BVwt2VH9qSQ=",
"path": "github.com/apache/incubator-openwhisk-client-go/whisk",
"revision": "4286a8212a74c40d8950ee76681a67e12c9bf1a0",
"revisionTime": "2019-03-04T14:44:55Z"
"revision": "47ad3426a4e3632fd17d859303f4074ae7b959ff",
"revisionTime": "2019-04-04T18:35:19Z"
},
{
"checksumSHA1": "4NY5lFykxXaoN+JNMxo179L79sU=",
Expand Down