From b6b2c04ed6f9b58a7d8f411398711304fcb62302 Mon Sep 17 00:00:00 2001 From: zychen5186 Date: Mon, 29 Apr 2024 22:31:18 -0700 Subject: [PATCH] fix: delete sleep change local limit to 10 pages Signed-off-by: zychen5186 --- pkg/bubbletea/bubbletea_pagination.go | 17 +++++++++-------- pkg/bubbletea/bubbletea_pagination_util.go | 7 +++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/pkg/bubbletea/bubbletea_pagination.go b/pkg/bubbletea/bubbletea_pagination.go index fb6688dc..e393e40c 100644 --- a/pkg/bubbletea/bubbletea_pagination.go +++ b/pkg/bubbletea/bubbletea_pagination.go @@ -34,7 +34,7 @@ func newModel(initMsg []proto.Message) pageModel { p := paginator.New() p.PerPage = msgPerPage p.Page = int(filter.Page) - 1 - p.SetTotalPages(countTotalPages()) + p.SetTotalPages(getLocalLastPage()) s := spinner.New() s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("56")) @@ -58,7 +58,7 @@ func (m pageModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case tea.KeyMsg: switch { case key.Matches(msg, m.paginator.KeyMap.PrevPage): - // If current page will be out of the range of the first batch, don't update + // If previous page will be out of the range of the first batch, don't update if m.paginator.Page == firstBatchIndex*pagePerBatch { return m, cmd } @@ -92,11 +92,11 @@ func (m pageModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } case newDataMsg: if msg.fetchDirection == forward { - // If current page is not in the range of the last batch, don't update + // Update if current page is in the range of the last batch + // e.g. user left last batch while still fetching, then don't update if m.paginator.Page/pagePerBatch >= lastBatchIndex { *m.items = append(*m.items, msg.newItems...) lastBatchIndex++ - // If the number of batches exceeds the limit, remove the first batch if lastBatchIndex-firstBatchIndex >= localBatchLimit { *m.items = (*m.items)[batchLen[firstBatchIndex]:] firstBatchIndex++ @@ -104,11 +104,11 @@ func (m pageModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } fetchingForward = false } else { - // If current page is not in the range of the first batch, don't update + // Update if current page is in the range of the first batch + // e.g. user left first batch while still fetching, then don't update if m.paginator.Page/pagePerBatch <= firstBatchIndex { *m.items = append(msg.newItems, *m.items...) firstBatchIndex-- - // If the number of batches exceeds the limit, remove the last batch if lastBatchIndex-firstBatchIndex >= localBatchLimit { *m.items = (*m.items)[:len(*m.items)-batchLen[lastBatchIndex]] lastBatchIndex-- @@ -116,7 +116,7 @@ func (m pageModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } fetchingBackward = false } - m.paginator.SetTotalPages(countTotalPages()) + m.paginator.SetTotalPages(getLocalLastPage()) return m, nil case spinner.TickMsg: m.spinner, cmd = m.spinner.Update(msg) @@ -153,7 +153,8 @@ func Paginator(_listHeader []printer.Column, _callback DataCallback, _filter fil var msg []proto.Message for i := firstBatchIndex; i < lastBatchIndex+1; i++ { newMessages := getMessageList(i) - if len(newMessages) == 0 || (int(filter.Page))%pagePerBatch > int(math.Ceil(float64(len(newMessages))/msgPerPage)) { + fmt.Println("newMessages", len(newMessages)) + if int(filter.Page)-(firstBatchIndex*pagePerBatch) > int(math.Ceil(float64(len(newMessages))/msgPerPage)) { return fmt.Errorf("the specified page has no data, please enter a valid page number") } msg = append(msg, newMessages...) diff --git a/pkg/bubbletea/bubbletea_pagination_util.go b/pkg/bubbletea/bubbletea_pagination_util.go index a5bcc78f..38715234 100644 --- a/pkg/bubbletea/bubbletea_pagination_util.go +++ b/pkg/bubbletea/bubbletea_pagination_util.go @@ -6,7 +6,6 @@ import ( "fmt" "strings" "sync" - "time" tea "github.com/charmbracelet/bubbletea" "github.com/flyteorg/flytectl/pkg/filters" @@ -25,7 +24,7 @@ const ( msgPerPage = 10 pagePerBatch = msgPerBatch / msgPerPage prefetchThreshold = pagePerBatch - 1 - localBatchLimit = 2 // Please set localBatchLimit at least 2 + localBatchLimit = 10 // Please set localBatchLimit at least 2 ) var ( @@ -100,7 +99,7 @@ func getMessageList(batchIndex int) []proto.Message { spin = false mutex.Unlock() }() - time.Sleep(2 * time.Second) + msg := callback(filters.Filters{ Limit: msgPerBatch, Page: int32(batchIndex + 1), @@ -137,7 +136,7 @@ func fetchDataCmd(batchIndex int, fetchDirection direction) tea.Cmd { } } -func countTotalPages() int { +func getLocalLastPage() int { sum := 0 for i := 0; i < lastBatchIndex+1; i++ { length, ok := batchLen[i]