diff --git a/cmd/get/execution.go b/cmd/get/execution.go index da597c51..30dfc9d6 100644 --- a/cmd/get/execution.go +++ b/cmd/get/execution.go @@ -154,17 +154,15 @@ func getExecutionFunc(ctx context.Context, args []string, cmdCtx cmdCore.Command return adminPrinter.Print(config.GetConfig().MustOutputFormat(), executionColumns, ExecutionToProtoMessages(executions)...) } + if config.GetConfig().Interactive { + err := bubbletea.Paginator(executionColumns, getCallBack(ctx, cmdCtx), execution.DefaultConfig.Filter) + return err + } executionList, err := cmdCtx.AdminFetcherExt().ListExecution(ctx, config.GetConfig().Project, config.GetConfig().Domain, execution.DefaultConfig.Filter) if err != nil { return err } logger.Infof(ctx, "Retrieved %v executions", len(executionList.Executions)) - - if config.GetConfig().Interactive { - bubbletea.Paginator(executionColumns, getCallBack(ctx, cmdCtx)) - return nil - } - return adminPrinter.Print(config.GetConfig().MustOutputFormat(), executionColumns, ExecutionToProtoMessages(executionList.Executions)...) } diff --git a/pkg/bubbletea/bubbletea_pagination.go b/pkg/bubbletea/bubbletea_pagination.go index da0c84b9..3f049a3f 100644 --- a/pkg/bubbletea/bubbletea_pagination.go +++ b/pkg/bubbletea/bubbletea_pagination.go @@ -3,12 +3,14 @@ package bubbletea import ( "fmt" "log" + "math" "strings" "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/paginator" "github.com/charmbracelet/bubbles/spinner" "github.com/charmbracelet/lipgloss" + "github.com/flyteorg/flytectl/pkg/filters" "github.com/flyteorg/flytectl/pkg/printer" "github.com/golang/protobuf/proto" @@ -31,7 +33,8 @@ type pageModel struct { func newModel(initMsg []proto.Message) pageModel { p := paginator.New() p.PerPage = msgPerPage - p.SetTotalPages(len(initMsg)) + p.Page = int(filter.Page) - 1 + p.SetTotalPages(countTotalPages()) s := spinner.New() s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("56")) @@ -129,13 +132,20 @@ func (m pageModel) View() string { return b.String() } -func Paginator(_listHeader []printer.Column, _callback DataCallback) { +func Paginator(_listHeader []printer.Column, _callback DataCallback, _filter filters.Filters) error { listHeader = _listHeader callback = _callback + filter = _filter + filter.Page = int32(_max(int(filter.Page), 1)) + firstBatchIndex = (int(filter.Page) - 1) / pagePerBatch + lastBatchIndex = firstBatchIndex 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)) { + return fmt.Errorf("the specified page has no data, please enter a valid page number") + } msg = append(msg, newMessages...) } @@ -143,4 +153,6 @@ func Paginator(_listHeader []printer.Column, _callback DataCallback) { if _, err := p.Run(); err != nil { log.Fatal(err) } + + return nil } diff --git a/pkg/bubbletea/bubbletea_pagination_util.go b/pkg/bubbletea/bubbletea_pagination_util.go index 0b8710d0..0c149a0d 100644 --- a/pkg/bubbletea/bubbletea_pagination_util.go +++ b/pkg/bubbletea/bubbletea_pagination_util.go @@ -28,14 +28,14 @@ const ( ) var ( - // Record the index of the first and last batch that is in cache - firstBatchIndex = 0 - lastBatchIndex = 0 - batchLen = make(map[int]int) // Callback function used to fetch data from the module that called bubbletea pagination. - callback DataCallback - // The header of the table + callback DataCallback listHeader []printer.Column + filter filters.Filters + // Record the index of the first and last batch that is in cache + firstBatchIndex int + lastBatchIndex int + batchLen = make(map[int]int) // Avoid fetching back and forward at the same time mutex sync.Mutex ) @@ -50,6 +50,13 @@ func (p printTableProto) MarshalJSON() ([]byte, error) { return buf.Bytes(), nil } +func _max(a, b int) int { + if a > b { + return a + } + return b +} + func _min(a, b int) int { if a < b { return a @@ -96,8 +103,8 @@ func getMessageList(batchIndex int) []proto.Message { msg := callback(filters.Filters{ Limit: msgPerBatch, Page: int32(batchIndex + 1), - SortBy: "created_at", - Asc: false, + SortBy: filter.SortBy, + Asc: filter.Asc, }) batchLen[batchIndex] = len(msg) @@ -130,7 +137,12 @@ func fetchDataCmd(batchIndex int, fetchDirection int) tea.Cmd { func countTotalPages() int { sum := 0 for i := 0; i < lastBatchIndex+1; i++ { - sum += batchLen[i] + length, ok := batchLen[i] + if ok { + sum += length + } else { + sum += msgPerBatch + } } return sum }