Skip to content

Commit 4f3e9a3

Browse files
committed
Update API layout for get command
1 parent 7f2ed68 commit 4f3e9a3

File tree

5 files changed

+239
-78
lines changed

5 files changed

+239
-78
lines changed

cli/cmd/get.go

Lines changed: 73 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/cortexlabs/cortex/pkg/lib/sets/strset"
3232
"github.com/cortexlabs/cortex/pkg/lib/slices"
3333
s "github.com/cortexlabs/cortex/pkg/lib/strings"
34+
"github.com/cortexlabs/cortex/pkg/lib/table"
3435
libtime "github.com/cortexlabs/cortex/pkg/lib/time"
3536
"github.com/cortexlabs/cortex/pkg/lib/urls"
3637
"github.com/cortexlabs/cortex/pkg/operator/api/context"
@@ -221,119 +222,113 @@ func pythonPackagesStr(dataStatuses map[string]*resource.DataStatus, ctx *contex
221222
return ""
222223
}
223224

224-
strings := make(map[string]string)
225-
for name, pythonPackage := range ctx.PythonPackages {
226-
strings[name] = dataResourceRow(name, pythonPackage, dataStatuses)
225+
resources := make([]context.Resource, 0, len(ctx.PythonPackages))
226+
for _, pythonPackage := range ctx.PythonPackages {
227+
resources = append(resources, pythonPackage)
227228
}
228229

229230
title := "\n"
230231
if showTitle {
231232
title = titleStr("Python Packages")
232233
}
233-
return title + dataResourcesHeader() + strMapToStr(strings)
234+
return title + dataResourceTable(resources, dataStatuses) + "\n"
234235
}
235236

236237
func rawColumnsStr(dataStatuses map[string]*resource.DataStatus, ctx *context.Context, showTitle bool) string {
237238
if len(ctx.RawColumns) == 0 {
238239
return ""
239240
}
240241

241-
strings := make(map[string]string)
242-
for name, rawColumn := range ctx.RawColumns {
243-
strings[name] = dataResourceRow(name, rawColumn, dataStatuses)
242+
resources := make([]context.Resource, 0, len(ctx.RawColumns))
243+
for _, rawColumn := range ctx.RawColumns {
244+
resources = append(resources, rawColumn)
244245
}
245246

246247
title := "\n"
247248
if showTitle {
248249
title = titleStr("Raw Columns")
249250
}
250-
return title + dataResourcesHeader() + strMapToStr(strings)
251+
return title + dataResourceTable(resources, dataStatuses) + "\n"
251252
}
252253

253254
func aggregatesStr(dataStatuses map[string]*resource.DataStatus, ctx *context.Context, showTitle bool) string {
254255
if len(ctx.Aggregates) == 0 {
255256
return ""
256257
}
257258

258-
strings := make(map[string]string)
259-
for name, aggregate := range ctx.Aggregates {
260-
strings[name] = dataResourceRow(name, aggregate, dataStatuses)
259+
resources := make([]context.Resource, 0, len(ctx.Aggregates))
260+
for _, aggregate := range ctx.Aggregates {
261+
resources = append(resources, aggregate)
261262
}
262263

263264
title := "\n"
264265
if showTitle {
265266
title = titleStr("Aggregates")
266267
}
267-
return title + dataResourcesHeader() + strMapToStr(strings)
268+
return title + dataResourceTable(resources, dataStatuses) + "\n"
268269
}
269270

270271
func transformedColumnsStr(dataStatuses map[string]*resource.DataStatus, ctx *context.Context, showTitle bool) string {
271272
if len(ctx.TransformedColumns) == 0 {
272273
return ""
273274
}
274275

275-
strings := make(map[string]string)
276-
for name, transformedColumn := range ctx.TransformedColumns {
277-
strings[name] = dataResourceRow(name, transformedColumn, dataStatuses)
276+
resources := make([]context.Resource, 0, len(ctx.TransformedColumns))
277+
for _, transformedColumn := range ctx.TransformedColumns {
278+
resources = append(resources, transformedColumn)
278279
}
279280

280281
title := "\n"
281282
if showTitle {
282283
title = titleStr("Transformed Columns")
283284
}
284-
return title + dataResourcesHeader() + strMapToStr(strings)
285+
return title + dataResourceTable(resources, dataStatuses) + "\n"
285286
}
286287

287288
func trainingDataStr(dataStatuses map[string]*resource.DataStatus, ctx *context.Context, showTitle bool) string {
288289
if len(ctx.Models) == 0 {
289290
return ""
290291
}
291292

292-
strings := make(map[string]string)
293+
resources := make([]context.Resource, 0, len(ctx.Models))
293294
for _, model := range ctx.Models {
294-
name := model.Dataset.Name
295-
strings[name] = dataResourceRow(name, model.Dataset, dataStatuses)
295+
resources = append(resources, model.Dataset)
296296
}
297297

298298
title := "\n"
299299
if showTitle {
300300
title = titleStr("Training Datasets")
301301
}
302-
return title + dataResourcesHeader() + strMapToStr(strings)
302+
return title + dataResourceTable(resources, dataStatuses) + "\n"
303303
}
304304

305305
func modelsStr(dataStatuses map[string]*resource.DataStatus, ctx *context.Context, showTitle bool) string {
306306
if len(ctx.Models) == 0 {
307307
return ""
308308
}
309309

310-
strings := make(map[string]string)
311-
for name, model := range ctx.Models {
312-
strings[name] = dataResourceRow(name, model, dataStatuses)
310+
resources := make([]context.Resource, 0, len(ctx.Models))
311+
for _, model := range ctx.Models {
312+
resources = append(resources, model)
313313
}
314314

315315
title := "\n"
316316
if showTitle {
317317
title = titleStr("Models")
318318
}
319-
return title + dataResourcesHeader() + strMapToStr(strings)
319+
return title + dataResourceTable(resources, dataStatuses) + "\n"
320320
}
321321

322322
func apisStr(apiGroupStatuses map[string]*resource.APIGroupStatus, showTitle bool) string {
323323
if len(apiGroupStatuses) == 0 {
324324
return ""
325325
}
326326

327-
strings := make(map[string]string)
328-
for name, apiGroupStatus := range apiGroupStatuses {
329-
strings[name] = apiResourceRow(apiGroupStatus)
330-
}
331-
332327
title := "\n"
333328
if showTitle {
334329
title = titleStr("APIs")
335330
}
336-
return title + apisHeader() + strMapToStr(strings)
331+
return title + apiResourceTable(apiGroupStatuses)
337332
}
338333

339334
func describePythonPackage(name string, resourcesRes *schema.GetResourcesResponse) (string, error) {
@@ -428,11 +423,6 @@ func describeAPI(name string, resourcesRes *schema.GetResourcesResponse) (string
428423
ctx := resourcesRes.Context
429424
api := ctx.APIs[name]
430425

431-
var ctxAPIStatus *resource.APIStatus
432-
if api != nil {
433-
ctxAPIStatus = resourcesRes.APIStatuses[api.ID]
434-
}
435-
436426
var anyAPIStatus *resource.APIStatus
437427
for _, apiStatus := range resourcesRes.APIStatuses {
438428
if apiStatus.APIName == name {
@@ -441,17 +431,6 @@ func describeAPI(name string, resourcesRes *schema.GetResourcesResponse) (string
441431
}
442432
}
443433

444-
var requestedReplicas int32
445-
if ctxAPIStatus != nil {
446-
requestedReplicas = api.Compute.InitReplicas
447-
if ctxAPIStatus.K8sRequested > 0 {
448-
requestedReplicas = ctxAPIStatus.K8sRequested
449-
}
450-
if requestedReplicas < api.Compute.MinReplicas {
451-
requestedReplicas = api.Compute.MinReplicas
452-
}
453-
}
454-
455434
refreshedAt := groupStatus.Start
456435
if groupStatus.ActiveStatus != nil && groupStatus.ActiveStatus.Start != nil {
457436
refreshedAt = groupStatus.ActiveStatus.Start
@@ -472,7 +451,7 @@ func describeAPI(name string, resourcesRes *schema.GetResourcesResponse) (string
472451
out += fmt.Sprintf("Available replicas: %s\n", s.Int32(groupStatus.Available()))
473452
out += fmt.Sprintf(" - Current model: %s%s\n", s.Int32(groupStatus.UpToDate()), staleComputeStr)
474453
out += fmt.Sprintf(" - Previous model: %s\n", s.Int32(groupStatus.ReadyStaleModel))
475-
out += fmt.Sprintf("Requested replicas: %s\n", s.Int32(requestedReplicas))
454+
out += fmt.Sprintf("Requested replicas: %s\n", s.Int32(groupStatus.Requested))
476455
out += fmt.Sprintf("Failed replicas: %s\n", s.Int32(groupStatus.FailedUpdated))
477456
out += "\n"
478457
out += fmt.Sprintf("Created at: %s\n", libtime.LocalTimestamp(groupStatus.Start))
@@ -537,40 +516,60 @@ func strMapToStr(strings map[string]string) string {
537516
return out
538517
}
539518

540-
func dataResourceRow(name string, resource context.Resource, dataStatuses map[string]*resource.DataStatus) string {
541-
dataStatus := dataStatuses[resource.GetID()]
542-
return resourceRow(name, dataStatus.Message(), dataStatus.End)
543-
}
544-
545-
func apiResourceRow(groupStatus *resource.APIGroupStatus) string {
546-
var updatedAt *time.Time
547-
if groupStatus.ActiveStatus != nil {
548-
updatedAt = groupStatus.ActiveStatus.Start
519+
func dataResourceTable(resources []context.Resource, dataStatuses map[string]*resource.DataStatus) string {
520+
rows := make([][]interface{}, len(resources))
521+
for rowNum, res := range resources {
522+
dataStatus := dataStatuses[res.GetID()]
523+
rows[rowNum] = []interface{}{
524+
res.GetName(),
525+
dataStatus.Message(),
526+
libtime.Since(dataStatus.End),
527+
}
549528
}
550-
return resourceRow(groupStatus.APIName, groupStatus.Message(), updatedAt)
551-
}
552529

553-
func resourceRow(name string, status string, startTime *time.Time) string {
554-
if len(name) > 33 {
555-
name = name[0:30] + "..."
530+
t := table.Table{
531+
Headers: []table.Header{
532+
{Title: "NAME", MinWidth: 32, MaxWidth: 32},
533+
{Title: "STATUS", MaxWidth: 21, MinWidth: 21},
534+
{Title: "AGE"},
535+
},
536+
Rows: rows,
556537
}
557-
if len(status) > 23 {
558-
status = status[0:20] + "..."
559-
}
560-
timeSince := libtime.Since(startTime)
561-
return stringifyRow(name, status, timeSince)
562-
}
563538

564-
func dataResourcesHeader() string {
565-
return stringifyRow("NAME", "STATUS", "AGE") + "\n"
539+
return table.MustFormat(t)
566540
}
567541

568-
func apisHeader() string {
569-
return stringifyRow("NAME", "STATUS", "LAST UPDATE") + "\n"
570-
}
542+
func apiResourceTable(apiGroupStatuses map[string]*resource.APIGroupStatus) string {
543+
rows := make([][]interface{}, 0, len(apiGroupStatuses))
544+
for name, groupStatus := range apiGroupStatuses {
545+
var updatedAt *time.Time
546+
if groupStatus.ActiveStatus != nil {
547+
updatedAt = groupStatus.ActiveStatus.Start
548+
}
549+
550+
rows = append(rows, []interface{}{
551+
name,
552+
groupStatus.Available(),
553+
groupStatus.UpToDate(),
554+
groupStatus.Requested,
555+
groupStatus.FailedUpdated,
556+
libtime.Since(updatedAt),
557+
})
558+
}
559+
560+
t := table.Table{
561+
Headers: []table.Header{
562+
{Title: "NAME"},
563+
{Title: "AVAILABLE"},
564+
{Title: "UP-TO-DATE"},
565+
{Title: "REQUESTED"},
566+
{Title: "FAILED"},
567+
{Title: "LAST UPDATE"},
568+
},
569+
Rows: rows,
570+
}
571571

572-
func stringifyRow(name string, status string, timeSince string) string {
573-
return fmt.Sprintf("%-35s%-24s%s", name, status, timeSince)
572+
return table.MustFormat(t)
574573
}
575574

576575
func titleStr(title string) string {

pkg/lib/strings/stringify.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,10 @@ func ObjFlat(val interface{}) string {
317317
return strIndent(val, "", "", "", `"`)
318318
}
319319

320+
func ObjFlatNoQuotes(val interface{}) string {
321+
return strIndent(val, "", "", "", "")
322+
}
323+
320324
func UserStr(val interface{}) string {
321325
return strIndent(val, "", "", "", `"`)
322326
}

0 commit comments

Comments
 (0)