Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
fix: delete sleep change local limit to 10 pages
Browse files Browse the repository at this point in the history
Signed-off-by: zychen5186 <[email protected]>
  • Loading branch information
zychen5186 committed Apr 30, 2024
1 parent 41a8836 commit b6b2c04
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
17 changes: 9 additions & 8 deletions pkg/bubbletea/bubbletea_pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -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
}
Expand Down Expand Up @@ -92,31 +92,31 @@ 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++
}
}
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--
}
}
fetchingBackward = false
}
m.paginator.SetTotalPages(countTotalPages())
m.paginator.SetTotalPages(getLocalLastPage())
return m, nil
case spinner.TickMsg:
m.spinner, cmd = m.spinner.Update(msg)
Expand Down Expand Up @@ -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...)
Expand Down
7 changes: 3 additions & 4 deletions pkg/bubbletea/bubbletea_pagination_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"strings"
"sync"
"time"

tea "github.com/charmbracelet/bubbletea"
"github.com/flyteorg/flytectl/pkg/filters"
Expand All @@ -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 (
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit b6b2c04

Please sign in to comment.