@@ -3,6 +3,7 @@ package cli
33import (
44 "fmt"
55 "io"
6+ "strconv"
67
78 "github.com/flashcatcloud/go-flashduty"
89 "github.com/spf13/cobra"
@@ -98,12 +99,24 @@ type listPage struct {
9899 Limit int // effective page size: requested, else the schema default; 0 when unknown
99100 Total int // total matching rows; negative when the API returned none
100101 More moreState // has_next_page / next cursor, when the API returned one
102+ // TotalCapped marks an endpoint that stops counting at serverCountCap; its
103+ // callers must also set More, the only "rows remain" signal past the cap.
104+ TotalCapped bool
101105}
102106
103107// totalKnown reports whether Total is a usable server total. A total below the
104108// shown count is an absent field, not a real total.
105109func (p listPage ) totalKnown () bool { return p .Total >= 0 && p .Total >= p .Count }
106110
111+ // serverCountCap is where the incident, alert and alert-event list endpoints
112+ // stop counting: past it they report this value as the total while rows keep
113+ // coming (the alert and alert-event API reference says "capped at 1000").
114+ const serverCountCap = 1000
115+
116+ // totalIsFloor reports whether Total is a capped count, which says only that at
117+ // least that many rows match.
118+ func (p listPage ) totalIsFloor () bool { return p .TotalCapped && p .Total >= serverCountCap }
119+
107120// offset returns how many rows precede this page, when that is knowable: always
108121// for the first page, and for later pages only with a known page size and base.
109122func (p listPage ) offset () (int , bool ) {
@@ -143,6 +156,8 @@ func results(n int) string {
143156// footer and the structured-mode stderr note ("" when no warning applies):
144157//
145158// a. total known: "showing N of T", plus a next-page hint when rows remain;
159+ // a capped total reads "at least T", and has_next_page alone decides
160+ // whether rows remain;
146161// b. else has_next_page/cursor known: true warns, false is complete;
147162// c. else a full page (N == limit) cannot tell: warn that more may exist;
148163// d. else only the plain count.
@@ -179,13 +194,18 @@ func (p listPage) statement(cmd *cobra.Command) (footer, note string) {
179194 }
180195 switch {
181196 case p .totalKnown ():
182- footer = fmt .Sprintf ("Showing %s%s." , results (p .Count ), paren (fmt .Sprintf ("total %d" , p .Total )))
183- if off , ok := p .offset (); ok && off + p .Count < p .Total {
184- body := fmt .Sprintf ("showing %d of %d total results%s" , p .Count , p .Total , paren ("" ))
185- if hint != "" {
186- body += ": " + hint
187- }
188- note = "note: " + body
197+ total := strconv .Itoa (p .Total )
198+ if p .totalIsFloor () {
199+ total = "at least " + total
200+ }
201+ footer = fmt .Sprintf ("Showing %s%s." , results (p .Count ), paren ("total " + total ))
202+ off , offKnown := p .offset ()
203+ remain := offKnown && off + p .Count < p .Total
204+ if p .totalIsFloor () {
205+ remain = p .More == moreYes
206+ }
207+ if remain {
208+ _ , note = warn (fmt .Sprintf ("%d of %s total results%s" , p .Count , total , paren ("" )), "" )
189209 }
190210 return footer , note
191211 case p .More == moreYes :
@@ -257,6 +277,14 @@ func (ctx *RunContext) PrintList(items any, cols []output.Column, count, page, l
257277 return ctx .PrintPage (items , cols , listPage {Count : count , Page : page , Base : pageOne , Limit : limit , Total : total })
258278}
259279
280+ // PrintCappedList is PrintList for an endpoint whose total stops at
281+ // serverCountCap; hasNext is the response's has_next_page.
282+ func (ctx * RunContext ) PrintCappedList (items any , cols []output.Column , count , page , limit , total int , hasNext bool ) error {
283+ return ctx .PrintPage (items , cols , listPage {
284+ Count : count , Page : page , Base : pageOne , Limit : limit , Total : total , More : moreOf (hasNext ), TotalCapped : true ,
285+ })
286+ }
287+
260288// WriteResult prints a success message as plain text or JSON.
261289func (ctx * RunContext ) WriteResult (message string ) {
262290 writeResult (ctx .Writer , message )
0 commit comments