Skip to content

Commit

Permalink
Catch error, change function name
Browse files Browse the repository at this point in the history
Signed-off-by: zychen5186 <[email protected]>
  • Loading branch information
zychen5186 committed May 3, 2024
1 parent c06530c commit fcea748
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
6 changes: 3 additions & 3 deletions flytectl/cmd/get/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ func ExecutionToProtoMessages(l []*admin.Execution) []proto.Message {
}

func getCallBack(ctx context.Context, cmdCtx cmdCore.CommandContext) bubbletea.DataCallback {
return func(filter filters.Filters) []proto.Message {
return func(filter filters.Filters) ([]proto.Message, error) {
executionList, err := cmdCtx.AdminFetcherExt().ListExecution(ctx, config.GetConfig().Project, config.GetConfig().Domain, filter)
if err != nil {
return nil
return nil, err
}
return ExecutionToProtoMessages(executionList.Executions)
return ExecutionToProtoMessages(executionList.Executions), nil
}
}

Expand Down
17 changes: 14 additions & 3 deletions flytectl/pkg/bubbletea/bubbletea_pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func newModel(initMsg []proto.Message) pageModel {
p := paginator.New()
p.PerPage = msgPerPage
p.Page = int(filter.Page) - 1
p.SetTotalPages(sumBatchLengths())
// Set the upper bound of the page number
p.SetTotalPages(getLastMsgIdx())

s := spinner.New()
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("56"))
Expand All @@ -54,6 +55,8 @@ func (m pageModel) Init() tea.Cmd {
func (m pageModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case error:
return m, tea.Quit
case tea.KeyMsg:
switch msg.String() {
case "q", "esc", "ctrl+c":
Expand Down Expand Up @@ -95,7 +98,8 @@ func (m pageModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
fetchingBackward = false
}
m.paginator.SetTotalPages(sumBatchLengths())
// Set the upper bound of the page number
m.paginator.SetTotalPages(getLastMsgIdx())
return m, nil
}

Expand Down Expand Up @@ -150,7 +154,10 @@ func Paginator(_listHeader []printer.Column, _callback DataCallback, _filter fil

var msg []proto.Message
for i := firstBatchIndex; i < lastBatchIndex+1; i++ {
newMessages := getMessageList(i)
newMessages, err := getMessageList(i)
if err != nil {
return err
}
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")
}
Expand All @@ -162,5 +169,9 @@ func Paginator(_listHeader []printer.Column, _callback DataCallback, _filter fil
log.Fatal(err)
}

if errMsg != nil {
return errMsg
}

return nil
}
22 changes: 16 additions & 6 deletions flytectl/pkg/bubbletea/bubbletea_pagination_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/golang/protobuf/proto"
)

type DataCallback func(filter filters.Filters) []proto.Message
type DataCallback func(filter filters.Filters) ([]proto.Message, error)

type printTableProto struct{ proto.Message }

Expand Down Expand Up @@ -52,6 +52,8 @@ var (
batchLen = make(map[int]int)
// Avoid fetching back and forward at the same time
mutex sync.Mutex
// Used to catch error happened while running paginator
errMsg error = nil
)

func (p printTableProto) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -106,38 +108,46 @@ func getTable(m *pageModel) (string, error) {
return buf.String(), nil
}

func getMessageList(batchIndex int) []proto.Message {
func getMessageList(batchIndex int) ([]proto.Message, error) {
mutex.Lock()
spin = true
defer func() {
spin = false
mutex.Unlock()
}()

msg := callback(filters.Filters{
msg, err := callback(filters.Filters{
Limit: msgPerBatch,
Page: int32(batchIndex + 1),
SortBy: filter.SortBy,
Asc: filter.Asc,
})
if err != nil {
return nil, err
}

batchLen[batchIndex] = len(msg)

return msg
return msg, nil
}

func fetchDataCmd(batchIndex int, fetchDirection direction) tea.Cmd {
return func() tea.Msg {
newItems, err := getMessageList(batchIndex)
if err != nil {
errMsg = err
return err
}
msg := newDataMsg{
newItems: getMessageList(batchIndex),
newItems: newItems,
batchIndex: batchIndex,
fetchDirection: fetchDirection,
}
return msg
}
}

func sumBatchLengths() int {
func getLastMsgIdx() int {
sum := 0
for i := 0; i < lastBatchIndex+1; i++ {
length, ok := batchLen[i]
Expand Down

0 comments on commit fcea748

Please sign in to comment.