Skip to content

Commit

Permalink
fix: prevent crash when no committed offests are found
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-grgt committed Feb 17, 2025
1 parent d224694 commit 24af2e2
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 12 deletions.
6 changes: 5 additions & 1 deletion kadmin/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ func (m MockKadmin) ListConfigs(topic string) tea.Msg {
func (m MockKadmin) SetSra(sra sradmin.SrAdmin) {
}

func NewMockKadmin() Instantiator {
func NewMockKadminInstantiator() Instantiator {
return func(cd ConnectionDetails) (Kadmin, error) {
return &MockKadmin{}, nil
}
}

func NewMockKadmin() Kadmin {
return &MockKadmin{}
}
4 changes: 2 additions & 2 deletions ktea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestKtea(t *testing.T) {
t.Run("No clusters configured", func(t *testing.T) {
t.Run("Shows create cluster page", func(t *testing.T) {
model := NewModel(kadmin.NewMockKadmin(), config.NewInMemoryConfigIO(&config.Config{}))
model := NewModel(kadmin.NewMockKadminInstantiator(), config.NewInMemoryConfigIO(&config.Config{}))
model.Update(config.LoadedMsg{
Config: &config.Config{},
})
Expand Down Expand Up @@ -51,7 +51,7 @@ func TestKtea(t *testing.T) {
},
},
)
model := NewModel(kadmin.NewMockKadmin(), io)
model := NewModel(kadmin.NewMockKadminInstantiator(), io)
model.Update(config.LoadedMsg{Config: config.New(io)})
//model.Update(config.LoadedMsg{
// Config: &config.Config{
Expand Down
10 changes: 10 additions & 0 deletions styles/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ type StatusBarStyle struct {
cluster lipgloss.Style
}

func CenterText(width int, height int) lipgloss.Style {
return lipgloss.NewStyle().
Width(width - 2).
Height(height).
AlignVertical(lipgloss.Center).
AlignHorizontal(lipgloss.Center).
Bold(true).
Foreground(lipgloss.Color(ColorPink))
}

func CmdBarWithWidth(width int) lipgloss.Style {
return CmdBar.Width(width)
}
Expand Down
10 changes: 8 additions & 2 deletions ui/pages/cgroups_topics_page/cgroups_parts_offsets_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ type Model struct {

func (m *Model) View(ktx *kontext.ProgramKtx, renderer *ui.Renderer) string {

if len(m.topicByPartOffset) == 0 {
return styles.
CenterText(ktx.WindowWidth, ktx.AvailableHeight).
Render("👀 No Committed Offsets Found")
}

halfWidth := int(float64(ktx.WindowWidth / 2))

cmdBarView := m.cmdBar.View(ktx, renderer)
Expand Down Expand Up @@ -120,8 +126,8 @@ func (m *Model) Update(msg tea.Msg) tea.Cmd {
}

func (m *Model) recreateOffsetRows() {
// if topics aren't listed yet
if m.topicsRows == nil {
// if topics aren't listed yet or there are no topics
if m.topicsRows == nil || len(m.topicsRows) == 0 {
return
}
selectedTopic := m.selectedRow()
Expand Down
62 changes: 62 additions & 0 deletions ui/pages/cgroups_topics_page/cgroups_parts_offsets_page_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cgroups_topics_page

import (
"github.com/stretchr/testify/assert"
"ktea/kadmin"
"ktea/ui"
"testing"
)

func TestCgroupPartsOffsetsPage(t *testing.T) {

t.Run("List consumer groups", func(t *testing.T) {
model, _ := New(kadmin.NewMockKadmin(), "test-group")

model.Update(kadmin.OffsetListedMsg{
Offsets: []kadmin.TopicPartitionOffset{
{
Topic: "topic-1",
Partition: 0,
Offset: 10,
},
{
Topic: "topic-1",
Partition: 1,
Offset: 11,
},
{
Topic: "topic-2",
Partition: 0,
Offset: 20,
},
{
Topic: "topic-2",
Partition: 1,
Offset: 21,
},
},
})

view := model.View(ui.NewTestKontext(), ui.TestRenderer)

assert.Contains(t, view, "topic-1")
assert.Contains(t, view, "topic-2")
assert.Contains(t, view, "10")
assert.Contains(t, view, "11")
assert.NotContains(t, view, "20")
assert.NotContains(t, view, "21")
})

t.Run("Render empty page when no offsets found", func(t *testing.T) {
model, _ := New(kadmin.NewMockKadmin(), "test-group")

model.Update(kadmin.OffsetListedMsg{
Offsets: nil,
})

view := model.View(ui.NewTestKontext(), ui.TestRenderer)

assert.Contains(t, view, "👀 No Committed Offsets Found")
})

}
8 changes: 1 addition & 7 deletions ui/pages/consumption_page/consumption_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ func (m *Model) View(ktx *kontext.ProgramKtx, renderer *ui.Renderer) string {
views = append(views, m.cmdBar.View(ktx, renderer))

if m.noRecordsAvailable {
views = append(views, lipgloss.NewStyle().
Width(ktx.WindowWidth-2).
Height(ktx.AvailableHeight).
AlignVertical(lipgloss.Center).
AlignHorizontal(lipgloss.Center).
Bold(true).
Foreground(lipgloss.Color(styles.ColorPink)).
views = append(views, styles.CenterText(ktx.WindowWidth, ktx.AvailableHeight).
Render("👀 Empty topic"))
} else if len(m.rows) > 0 {
m.table.SetColumns([]table.Column{
Expand Down

0 comments on commit 24af2e2

Please sign in to comment.