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

Commit

Permalink
Reuse get filter, user can select to start from a specific page
Browse files Browse the repository at this point in the history
Signed-off-by: zychen5186 <[email protected]>
  • Loading branch information
zychen5186 committed Apr 28, 2024
1 parent 3d44a80 commit 3adf5e7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
10 changes: 4 additions & 6 deletions cmd/get/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)...)
}
16 changes: 14 additions & 2 deletions pkg/bubbletea/bubbletea_pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"))
Expand Down Expand Up @@ -129,18 +132,27 @@ 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...)
}

p := tea.NewProgram(newModel(msg))
if _, err := p.Run(); err != nil {
log.Fatal(err)
}

return nil
}
30 changes: 21 additions & 9 deletions pkg/bubbletea/bubbletea_pagination_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit 3adf5e7

Please sign in to comment.