Skip to content
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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
VERSION ?= $(shell git describe --tags --always --dirty)

build:
go install -ldflags "-s -w -trimpath -X main.version=$(VERSION)" ./cmd/...
go install -trimpath -ldflags "-s -w -X main.version=$(VERSION)" ./cmd/...

lint:
golangci-lint run ./...
Expand Down
43 changes: 43 additions & 0 deletions internal/ui/tui/table_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"slices"
"strconv"
"strings"

"github.com/charmbracelet/bubbles/table"

Expand Down Expand Up @@ -117,15 +118,31 @@ func getTableRows(dataset *models.Dataset, category domain.Category, context *do

// computeStats calculates stats for the given category and rows.
func computeStats(category domain.Category, rows []table.Row) tableStats {
if len(rows) == 0 {
return nil
}

stats := computeNumericStats(category, rows)
if category == domain.DedicatedAICluster {
stats = appendDedicatedAIClusterStats(rows, stats)
}

return stats
}

// computeNumericStats sums numeric columns defined for the category.
func computeNumericStats(category domain.Category, rows []table.Row) tableStats {
cols, ok := statsColumns[category]
if !ok || len(rows) == 0 {
return nil
}

headers := getHeaders(category)
idx := make(map[string]int)
for i, h := range headers {
idx[h.text] = i
}

totals := make(tableStats)
for _, col := range cols {
columnIdx, ok := idx[col]
Expand All @@ -141,9 +158,35 @@ func computeStats(category domain.Category, rows []table.Row) tableStats {
}
totals[col] = sum
}

return totals
}

func appendDedicatedAIClusterStats(rows []table.Row, stats tableStats) tableStats {
headers := getHeaders(domain.DedicatedAICluster)
statusIdx := -1
for i, h := range headers {
if h.text == "Status" {
statusIdx = i
break
}
}

var active, failed int
for _, r := range rows {
switch strings.ToLower(strings.TrimSpace(r[statusIdx])) {
case "active", "ready":
active++
case "fail", "failed":
failed++
}
}

stats["Active"] = active
stats["Failed"] = failed
return stats
}

/*
filterRows filters a slice of items using the provided filter and row function.
It returns a slice of table.Row for items that match the filter.
Expand Down
23 changes: 23 additions & 0 deletions internal/ui/tui/table_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/jingle2008/toolkit/internal/domain"
"github.com/jingle2008/toolkit/internal/ui/tui/common"
"github.com/jingle2008/toolkit/pkg/models"
)

Expand Down Expand Up @@ -119,6 +120,28 @@ func Test_getTableRows_empty_dataset(t *testing.T) {
_, _ = getTableRows(nil, domain.Tenant, nil, "", "", true, false)
}

func TestGetTableRows_DedicatedAIClusterStats(t *testing.T) {
t.Parallel()
dataset := &models.Dataset{
DedicatedAIClusterMap: map[string][]models.DedicatedAICluster{
"tenantA": {
{Name: "dac-active1", Status: "Active", Size: 3},
{Name: "dac-active2", Status: "Active", Size: 2},
{Name: "dac-ready", Status: "ready", Size: 3},
{Name: "dac-failed", Status: "FAILED", Size: 1},
{Name: "dac-other", Status: "Provisioning", Size: 2},
},
},
}

rows, stats := getTableRows(dataset, domain.DedicatedAICluster, nil, "", "", true, false)
require.Len(t, rows, 5)
require.NotNil(t, stats)
assert.Equal(t, 11, stats[common.SizeCol])
assert.Equal(t, 3, stats["Active"])
assert.Equal(t, 1, stats["Failed"])
}

func TestGetItemKeyAndString(t *testing.T) {
t.Parallel()
// Table-driven: category, row, expected string
Expand Down
Loading