From 631fa026a2a52ebbee81a1001e3305ff10b5bd3f Mon Sep 17 00:00:00 2001 From: Gerrit Date: Tue, 30 Jul 2024 16:18:01 +0200 Subject: [PATCH 1/3] Improved partition capacity. --- cmd/partition_test.go | 56 +++++++++++------------ cmd/tableprinters/partition.go | 82 ++++++++++++++++++++++------------ go.mod | 2 +- go.sum | 4 +- 4 files changed, 84 insertions(+), 60 deletions(-) diff --git a/cmd/partition_test.go b/cmd/partition_test.go index 43b2d31..389954b 100644 --- a/cmd/partition_test.go +++ b/cmd/partition_test.go @@ -270,16 +270,16 @@ func Test_PartitionCapacityCmd(t *testing.T) { Name: "partition-1", Servers: []*models.V1ServerCapacity{ { - Allocated: pointer.Pointer(int32(1)), - Faulty: pointer.Pointer(int32(2)), + Allocated: 1, + Faulty: 2, Faultymachines: []string{"abc"}, - Free: pointer.Pointer(int32(3)), - Other: pointer.Pointer(int32(4)), + Free: 3, + Other: 4, Othermachines: []string{"def"}, Size: pointer.Pointer("size-1"), - Total: pointer.Pointer(int32(5)), - Reservations: pointer.Pointer(int32(3)), - Usedreservations: pointer.Pointer(int32(1)), + Total: 5, + Reservations: 3, + Usedreservations: 1, }, }, }, @@ -294,16 +294,16 @@ func Test_PartitionCapacityCmd(t *testing.T) { Name: "partition-1", Servers: []*models.V1ServerCapacity{ { - Allocated: pointer.Pointer(int32(1)), - Faulty: pointer.Pointer(int32(2)), + Allocated: 1, + Faulty: 2, Faultymachines: []string{"abc"}, - Free: pointer.Pointer(int32(3)), - Other: pointer.Pointer(int32(4)), + Free: 3, + Other: 4, Othermachines: []string{"def"}, Size: pointer.Pointer("size-1"), - Total: pointer.Pointer(int32(5)), - Reservations: pointer.Pointer(int32(3)), - Usedreservations: pointer.Pointer(int32(1)), + Total: 5, + Reservations: 3, + Usedreservations: 1, }, }, }, @@ -349,16 +349,16 @@ Total 5 3 1 1/3 4 2 Name: "partition-1", Servers: []*models.V1ServerCapacity{ { - Allocated: pointer.Pointer(int32(1)), - Faulty: pointer.Pointer(int32(2)), + Allocated: 1, + Faulty: 2, Faultymachines: []string{"abc"}, - Free: pointer.Pointer(int32(3)), - Other: pointer.Pointer(int32(4)), + Free: 3, + Other: 4, Othermachines: []string{"def"}, Size: pointer.Pointer("size-1"), - Total: pointer.Pointer(int32(5)), - Reservations: pointer.Pointer(int32(3)), - Usedreservations: pointer.Pointer(int32(1)), + Total: 5, + Reservations: 3, + Usedreservations: 1, }, }, }, @@ -373,16 +373,16 @@ Total 5 3 1 1/3 4 2 Name: "partition-1", Servers: []*models.V1ServerCapacity{ { - Allocated: pointer.Pointer(int32(1)), - Faulty: pointer.Pointer(int32(2)), + Allocated: 1, + Faulty: 2, Faultymachines: []string{"abc"}, - Free: pointer.Pointer(int32(3)), - Other: pointer.Pointer(int32(4)), + Free: 3, + Other: 4, Othermachines: []string{"def"}, Size: pointer.Pointer("size-1"), - Total: pointer.Pointer(int32(5)), - Reservations: pointer.Pointer(int32(3)), - Usedreservations: pointer.Pointer(int32(1)), + Total: 5, + Reservations: 3, + Usedreservations: 1, }, }, }, diff --git a/cmd/tableprinters/partition.go b/cmd/tableprinters/partition.go index 14fd6cf..8adde9e 100644 --- a/cmd/tableprinters/partition.go +++ b/cmd/tableprinters/partition.go @@ -37,18 +37,25 @@ func (t *TablePrinter) PartitionTable(data []*models.V1PartitionResponse, wide b func (t *TablePrinter) PartitionCapacityTable(data []*models.V1PartitionCapacity, wide bool) ([]string, [][]string, error) { var ( - header = []string{"Partition", "Size", "Total", "Free", "Allocated", "Reserved", "Other", "Faulty"} + header = []string{"Partition", "Size", "Allocated", "Free", "Unavailable", "Reservations", "|", "Total", "|", "Faulty"} rows [][]string - totalCount int32 - freeCount int32 allocatedCount int32 faultyCount int32 + freeCount int32 otherCount int32 + phonedHomeCount int32 reservationCount int32 + totalCount int32 + unavailableCount int32 usedReservationCount int32 + waitingCount int32 ) + if wide { + header = append(header, "Phoned Home", "Waiting", "Other") + } + for _, pc := range data { pc := pc @@ -56,45 +63,62 @@ func (t *TablePrinter) PartitionCapacityTable(data []*models.V1PartitionCapacity id := pointer.SafeDeref(c.Size) var ( - allocated = fmt.Sprintf("%d", pointer.SafeDeref(c.Allocated)) - total = fmt.Sprintf("%d", pointer.SafeDeref(c.Total)) - free = fmt.Sprintf("%d", pointer.SafeDeref(c.Free)) - faulty = fmt.Sprintf("%d", pointer.SafeDeref(c.Faulty)) - other = fmt.Sprintf("%d", pointer.SafeDeref(c.Other)) - reservations = fmt.Sprintf("%d/%d", pointer.SafeDeref(c.Usedreservations), pointer.SafeDeref(c.Reservations)) + allocated = fmt.Sprintf("%d", c.Allocated) + faulty = fmt.Sprintf("%d", c.Faulty) + free = fmt.Sprintf("%d", c.Free) + other = fmt.Sprintf("%d", c.Other) + phonedHome = fmt.Sprintf("%d", c.PhonedHome) + reservations = "0" + total = fmt.Sprintf("%d", c.Total) + unavailable = fmt.Sprintf("%d", c.Unavailable) + waiting = fmt.Sprintf("%d", c.Waiting) ) - if wide { - if len(c.Faultymachines) > 0 { - faulty = strings.Join(c.Faultymachines, "\n") - } - if len(c.Othermachines) > 0 { - other = strings.Join(c.Othermachines, "\n") - } + if c.Reservations > 0 { + reservations = fmt.Sprintf("%d (%d/%d used)", c.Reservations-c.Usedreservations, c.Usedreservations, c.Reservations) } - totalCount += pointer.SafeDeref(c.Total) - freeCount += pointer.SafeDeref(c.Free) - allocatedCount += pointer.SafeDeref(c.Allocated) - otherCount += pointer.SafeDeref(c.Other) - faultyCount += pointer.SafeDeref(c.Faulty) - reservationCount += pointer.SafeDeref(c.Reservations) - usedReservationCount += pointer.SafeDeref(c.Usedreservations) + allocatedCount += c.Allocated + faultyCount += c.Faulty + freeCount += c.Free + otherCount += c.Other + phonedHomeCount += c.PhonedHome + reservationCount += c.Reservations + totalCount += c.Total + unavailableCount += c.Unavailable + usedReservationCount += c.Usedreservations + waitingCount += c.Waiting + + row := []string{*pc.ID, id, allocated, free, unavailable, reservations, "|", total, "|", faulty} + if wide { + row = append(row, phonedHome, waiting, other) + } - rows = append(rows, []string{*pc.ID, id, total, free, allocated, reservations, other, faulty}) + rows = append(rows, row) } } footerRow := ([]string{ - "Total", + "Σ", "", - fmt.Sprintf("%d", totalCount), - fmt.Sprintf("%d", freeCount), fmt.Sprintf("%d", allocatedCount), - fmt.Sprintf("%d/%d", usedReservationCount, reservationCount), - fmt.Sprintf("%d", otherCount), + fmt.Sprintf("%d", freeCount), + fmt.Sprintf("%d", unavailableCount), + fmt.Sprintf("%d", reservationCount-usedReservationCount), + "|", + fmt.Sprintf("%d", totalCount), + "|", fmt.Sprintf("%d", faultyCount), }) + + if wide { + footerRow = append(footerRow, []string{ + fmt.Sprintf("%d", phonedHomeCount), + fmt.Sprintf("%d", waitingCount), + fmt.Sprintf("%d", otherCount), + }...) + } + rows = append(rows, footerRow) return header, rows, nil diff --git a/go.mod b/go.mod index 977b2ee..87e74c7 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/go-openapi/strfmt v0.23.0 github.com/google/go-cmp v0.6.0 github.com/google/uuid v1.6.0 - github.com/metal-stack/metal-go v0.32.2 + github.com/metal-stack/metal-go v0.32.4-0.20240730135831-fe6be460a83e github.com/metal-stack/metal-lib v0.17.1 github.com/metal-stack/updater v1.2.2 github.com/metal-stack/v v1.0.3 diff --git a/go.sum b/go.sum index 3719e85..dedfbd3 100644 --- a/go.sum +++ b/go.sum @@ -235,8 +235,8 @@ github.com/mdlayher/sdnotify v1.0.0 h1:Ma9XeLVN/l0qpyx1tNeMSeTjCPH6NtuD6/N9XdTlQ github.com/mdlayher/sdnotify v1.0.0/go.mod h1:HQUmpM4XgYkhDLtd+Uad8ZFK1T9D5+pNxnXQjCeJlGE= github.com/mdlayher/socket v0.5.1 h1:VZaqt6RkGkt2OE9l3GcC6nZkqD3xKeQLyfleW/uBcos= github.com/mdlayher/socket v0.5.1/go.mod h1:TjPLHI1UgwEv5J1B5q0zTZq12A/6H7nKmtTanQE37IQ= -github.com/metal-stack/metal-go v0.32.2 h1:vD1LtGVAeLx9vrPrguPBchXYsp7/oZ5MfTnfUO/yMz0= -github.com/metal-stack/metal-go v0.32.2/go.mod h1:3MJTYCS4YJz8D8oteTKhjpaAKNMMjMKYDrIy9awHGtQ= +github.com/metal-stack/metal-go v0.32.4-0.20240730135831-fe6be460a83e h1:pecYgPnB1uZjemB2YaR4ZVNrK/p5n6aGuvFpNzPuDhA= +github.com/metal-stack/metal-go v0.32.4-0.20240730135831-fe6be460a83e/go.mod h1:3MJTYCS4YJz8D8oteTKhjpaAKNMMjMKYDrIy9awHGtQ= github.com/metal-stack/metal-lib v0.17.1 h1:JLa4wJ62dgxtY9UOLF+QDk10/i/W5vhzrv8RsundDUY= github.com/metal-stack/metal-lib v0.17.1/go.mod h1:nyNGI4DZFOcWbSoq2Y6V3SHpFxuXBIqYBZHTb6cy//s= github.com/metal-stack/security v0.8.0 h1:tVaSDB9m5clwYrnLyaXfPy7mQlJTnmeoHscG+RUy/xo= From 6a5eee8f69181683e8ba87b3bc5598c5be8543eb Mon Sep 17 00:00:00 2001 From: Gerrit Date: Fri, 13 Sep 2024 13:10:26 +0200 Subject: [PATCH 2/3] Fix tests. --- cmd/common_test.go | 3 +++ cmd/partition_test.go | 40 +++++++++++++++++----------------- cmd/printers.go | 1 + cmd/tableprinters/partition.go | 13 +++++++++++ cmd/tableprinters/printer.go | 5 +++++ 5 files changed, 42 insertions(+), 20 deletions(-) diff --git a/cmd/common_test.go b/cmd/common_test.go index e9f6537..eda3799 100644 --- a/cmd/common_test.go +++ b/cmd/common_test.go @@ -6,6 +6,7 @@ import ( "fmt" "log/slog" "os" + "strconv" "strings" "testing" "time" @@ -65,6 +66,8 @@ func (c *test[R]) testCmd(t *testing.T) { require.NotEmpty(t, c.name, "test name must not be empty") require.NotEmpty(t, c.cmd, "cmd must not be empty") + t.Setenv(strings.ToUpper(binaryName)+"_FORCE_COLOR", strconv.FormatBool(false)) + if c.wantErr != nil { _, _, config := c.newMockConfig(t) diff --git a/cmd/partition_test.go b/cmd/partition_test.go index 389954b..577dafd 100644 --- a/cmd/partition_test.go +++ b/cmd/partition_test.go @@ -309,24 +309,24 @@ func Test_PartitionCapacityCmd(t *testing.T) { }, }, wantTable: pointer.Pointer(` -PARTITION SIZE TOTAL FREE ALLOCATED RESERVED OTHER FAULTY -1 size-1 5 3 1 1/3 4 2 -Total 5 3 1 1/3 4 2 +PARTITION SIZE ALLOCATED FREE UNAVAILABLE RESERVATIONS | TOTAL | FAULTY +1 size-1 1 3 0 2 (1/3 used) | 5 | 2 +Total 1 3 0 2 | 5 | 2 `), wantWideTable: pointer.Pointer(` -PARTITION SIZE TOTAL FREE ALLOCATED RESERVED OTHER FAULTY -1 size-1 5 3 1 1/3 def abc -Total 5 3 1 1/3 4 2 +PARTITION SIZE ALLOCATED FREE UNAVAILABLE RESERVATIONS | TOTAL | FAULTY PHONED HOME WAITING OTHER +1 size-1 1 3 0 2 (1/3 used) | 5 | 2 0 0 4 +Total 1 3 0 2 | 5 | 2 0 0 4 `), template: pointer.Pointer("{{ .id }} {{ .name }}"), wantTemplate: pointer.Pointer(` 1 partition-1 `), wantMarkdown: pointer.Pointer(` -| PARTITION | SIZE | TOTAL | FREE | ALLOCATED | RESERVED | OTHER | FAULTY | -|-----------|--------|-------|------|-----------|----------|-------|--------| -| 1 | size-1 | 5 | 3 | 1 | 1/3 | 4 | 2 | -| Total | | 5 | 3 | 1 | 1/3 | 4 | 2 | +| PARTITION | SIZE | ALLOCATED | FREE | UNAVAILABLE | RESERVATIONS | TOTAL | FAULTY | +|-----------|--------|-----------|------|-------------|--------------|-------|--------| +| 1 | size-1 | 1 | 3 | 0 | 2 (1/3 used) | 5 | 2 | +| Total | | 1 | 3 | 0 | 2 | 5 | 2 | `), }, { @@ -388,24 +388,24 @@ Total 5 3 1 1/3 4 2 }, }, wantTable: pointer.Pointer(` -PARTITION SIZE TOTAL FREE ALLOCATED RESERVED OTHER FAULTY -1 size-1 5 3 1 1/3 4 2 -Total 5 3 1 1/3 4 2 +PARTITION SIZE ALLOCATED FREE UNAVAILABLE RESERVATIONS | TOTAL | FAULTY +1 size-1 1 3 0 2 (1/3 used) | 5 | 2 +Total 1 3 0 2 | 5 | 2 `), wantWideTable: pointer.Pointer(` -PARTITION SIZE TOTAL FREE ALLOCATED RESERVED OTHER FAULTY -1 size-1 5 3 1 1/3 def abc -Total 5 3 1 1/3 4 2 +PARTITION SIZE ALLOCATED FREE UNAVAILABLE RESERVATIONS | TOTAL | FAULTY PHONED HOME WAITING OTHER +1 size-1 1 3 0 2 (1/3 used) | 5 | 2 0 0 4 +Total 1 3 0 2 | 5 | 2 0 0 4 `), template: pointer.Pointer("{{ .id }} {{ .name }}"), wantTemplate: pointer.Pointer(` 1 partition-1 `), wantMarkdown: pointer.Pointer(` -| PARTITION | SIZE | TOTAL | FREE | ALLOCATED | RESERVED | OTHER | FAULTY | -|-----------|--------|-------|------|-----------|----------|-------|--------| -| 1 | size-1 | 5 | 3 | 1 | 1/3 | 4 | 2 | -| Total | | 5 | 3 | 1 | 1/3 | 4 | 2 | +| PARTITION | SIZE | ALLOCATED | FREE | UNAVAILABLE | RESERVATIONS | TOTAL | FAULTY | +|-----------|--------|-----------|------|-------------|--------------|-------|--------| +| 1 | size-1 | 1 | 3 | 0 | 2 (1/3 used) | 5 | 2 | +| Total | | 1 | 3 | 0 | 2 | 5 | 2 | `), }, } diff --git a/cmd/printers.go b/cmd/printers.go index 7bfe5d3..d2679ce 100644 --- a/cmd/printers.go +++ b/cmd/printers.go @@ -28,6 +28,7 @@ func newPrinterFromCLI(out io.Writer) printers.Printer { NoHeaders: viper.GetBool("no-headers"), }).WithOut(out) + tp.SetMarkdown(format == "markdown") tp.SetPrinter(tablePrinter) tp.SetLastEventErrorThreshold(viper.GetDuration("last-event-error-threshold")) diff --git a/cmd/tableprinters/partition.go b/cmd/tableprinters/partition.go index b4d8c93..bef3b11 100644 --- a/cmd/tableprinters/partition.go +++ b/cmd/tableprinters/partition.go @@ -2,6 +2,7 @@ package tableprinters import ( "fmt" + "slices" "sort" "strings" @@ -119,6 +120,18 @@ func (t *TablePrinter) PartitionCapacityTable(data []*models.V1PartitionCapacity }...) } + if t.markdown { + // for markdown we already have enough dividers, remove them + removeDivider := func(e string) bool { + return e == "|" + } + header = slices.DeleteFunc(header, removeDivider) + footerRow = slices.DeleteFunc(footerRow, removeDivider) + for i, row := range rows { + rows[i] = slices.DeleteFunc(row, removeDivider) + } + } + rows = append(rows, footerRow) return header, rows, nil diff --git a/cmd/tableprinters/printer.go b/cmd/tableprinters/printer.go index 10500af..401b482 100644 --- a/cmd/tableprinters/printer.go +++ b/cmd/tableprinters/printer.go @@ -13,6 +13,7 @@ import ( type TablePrinter struct { t *printers.TablePrinter lastEventErrorThreshold time.Duration + markdown bool } func New() *TablePrinter { @@ -27,6 +28,10 @@ func (t *TablePrinter) SetLastEventErrorThreshold(threshold time.Duration) { t.lastEventErrorThreshold = threshold } +func (t *TablePrinter) SetMarkdown(markdown bool) { + t.markdown = markdown +} + func (t *TablePrinter) ToHeaderAndRows(data any, wide bool) ([]string, [][]string, error) { switch d := data.(type) { case []*models.V1AuditResponse: From 02beaf9ad0c7dcbb6b6bad16df222f22d2213e72 Mon Sep 17 00:00:00 2001 From: Gerrit Date: Fri, 13 Sep 2024 13:14:05 +0200 Subject: [PATCH 3/3] Fix linter. --- cmd/tableprinters/size.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/tableprinters/size.go b/cmd/tableprinters/size.go index 6ad7e31..d0973ac 100644 --- a/cmd/tableprinters/size.go +++ b/cmd/tableprinters/size.go @@ -30,9 +30,9 @@ func (t *TablePrinter) SizeTable(data []*models.V1SizeResponse, wide bool) ([]st case models.V1SizeConstraintTypeCores: cpu = fmt.Sprintf("%d - %d", c.Min, c.Max) case models.V1SizeConstraintTypeMemory: - memory = fmt.Sprintf("%s - %s", humanize.Bytes(uint64(c.Min)), humanize.Bytes(uint64(c.Max))) + memory = fmt.Sprintf("%s - %s", humanize.Bytes(uint64(c.Min)), humanize.Bytes(uint64(c.Max))) //nolint:gosec case models.V1SizeConstraintTypeStorage: - storage = fmt.Sprintf("%s - %s", humanize.Bytes(uint64(c.Min)), humanize.Bytes(uint64(c.Max))) + storage = fmt.Sprintf("%s - %s", humanize.Bytes(uint64(c.Min)), humanize.Bytes(uint64(c.Max))) //nolint:gosec case models.V1SizeConstraintTypeGpu: gpu = fmt.Sprintf("%s: %d - %d", c.Identifier, c.Min, c.Max) }