@@ -198,19 +198,91 @@ func (db *BoltDB) SetActionState(al *ActionLocator, state ActionState,
198198 })
199199}
200200
201- // ListActionsFilterFn defines a function that can be used to determine if an
202- // action should be included in a set of results or not. The reversed parameter
203- // indicates if the actions are being traversed in reverse order or not.
204- // The first return boolean indicates if the action should be included or not
205- // and the second one indicates if the iteration should be stopped or not.
206- type ListActionsFilterFn func (a * Action , reversed bool ) (bool , bool )
207-
208201// ListActions returns a list of Actions that pass the filterFn requirements.
209202// The indexOffset and maxNum params can be used to control the number of
210203// actions returned. The return values are the list of actions, the last index
211204// and the total count (iff query.CountTotal is set).
212- func (db * BoltDB ) ListActions (filterFn ListActionsFilterFn ,
213- query * ListActionsQuery ) ([]* Action , uint64 , uint64 , error ) {
205+ // ListActions returns a list of Actions. The query IndexOffset and MaxNum
206+ // params can be used to control the number of actions returned.
207+ // ListActionOptions may be used to filter on specific Action values. The return
208+ // values are the list of actions, the last index and the total count (iff
209+ // query.CountTotal is set).
210+ func (db * BoltDB ) ListActions (ctx context.Context , query * ListActionsQuery ,
211+ options ... ListActionOption ) ([]* Action , uint64 , uint64 , error ) {
212+
213+ opts := newListActionOptions ()
214+ for _ , o := range options {
215+ o (opts )
216+ }
217+
218+ filterFn := func (a * Action , reversed bool ) (bool , bool ) {
219+ timeStamp := a .AttemptedAt
220+ if ! opts .endTime .IsZero () {
221+ // If actions are being considered in order and the
222+ // timestamp of this action exceeds the given end
223+ // timestamp, then there is no need to continue
224+ // traversing.
225+ if ! reversed && timeStamp .After (opts .endTime ) {
226+ return false , false
227+ }
228+
229+ // If the actions are in reverse order and the timestamp
230+ // comes after the end timestamp, then the actions is
231+ // not included but the search can continue.
232+ if reversed && timeStamp .After (opts .endTime ) {
233+ return false , true
234+ }
235+ }
236+
237+ if ! opts .startTime .IsZero () {
238+ // If actions are being considered in order and the
239+ // timestamp of this action comes before the given start
240+ // timestamp, then the action is not included but the
241+ // search can continue.
242+ if ! reversed && timeStamp .Before (opts .startTime ) {
243+ return false , true
244+ }
245+
246+ // If the actions are in reverse order and the timestamp
247+ // comes before the start timestamp, then there is no
248+ // need to continue traversing.
249+ if reversed && timeStamp .Before (opts .startTime ) {
250+ return false , false
251+ }
252+ }
253+
254+ if opts .featureName != "" && a .FeatureName != opts .featureName {
255+ return false , true
256+ }
257+
258+ if opts .actorName != "" && a .ActorName != opts .actorName {
259+ return false , true
260+ }
261+
262+ if opts .methodName != "" && a .RPCMethod != opts .methodName {
263+ return false , true
264+ }
265+
266+ if opts .state != ActionStateUnknown && a .State != opts .state {
267+ return false , true
268+ }
269+
270+ return true , true
271+ }
272+
273+ if opts .sessionID != session .EmptyID {
274+ return db .listSessionActions (
275+ opts .sessionID , filterFn , query ,
276+ )
277+ }
278+ if opts .groupID != session .EmptyID {
279+ actions , err := db .listGroupActions (ctx , opts .groupID , filterFn )
280+ if err != nil {
281+ return nil , 0 , 0 , err
282+ }
283+
284+ return actions , 0 , uint64 (len (actions )), nil
285+ }
214286
215287 var (
216288 actions []* Action
@@ -222,17 +294,14 @@ func (db *BoltDB) ListActions(filterFn ListActionsFilterFn,
222294 if err != nil {
223295 return err
224296 }
225-
226297 actionsBucket := mainActionsBucket .Bucket (actionsKey )
227298 if actionsBucket == nil {
228299 return ErrNoSuchKeyFound
229300 }
230-
231301 actionsIndexBucket := mainActionsBucket .Bucket (actionsIndex )
232302 if actionsIndexBucket == nil {
233303 return ErrNoSuchKeyFound
234304 }
235-
236305 readAction := func (index , locatorBytes []byte ) (* Action ,
237306 error ) {
238307
@@ -242,10 +311,8 @@ func (db *BoltDB) ListActions(filterFn ListActionsFilterFn,
242311 if err != nil {
243312 return nil , err
244313 }
245-
246314 return getAction (actionsBucket , locator )
247315 }
248-
249316 actions , lastIndex , totalCount , err = paginateActions (
250317 query , actionsIndexBucket .Cursor (), readAction ,
251318 filterFn ,
@@ -255,14 +322,20 @@ func (db *BoltDB) ListActions(filterFn ListActionsFilterFn,
255322 if err != nil {
256323 return nil , 0 , 0 , err
257324 }
258-
259325 return actions , lastIndex , totalCount , nil
260326}
261327
262- // ListSessionActions returns a list of the given session's Actions that pass
328+ // listActionsFilterFn defines a function that can be used to determine if an
329+ // action should be included in a set of results or not. The reversed parameter
330+ // indicates if the actions are being traversed in reverse order or not.
331+ // The first return boolean indicates if the action should be included or not
332+ // and the second one indicates if the iteration should be stopped or not.
333+ type listActionsFilterFn func (a * Action , reversed bool ) (bool , bool )
334+
335+ // listSessionActions returns a list of the given session's Actions that pass
263336// the filterFn requirements.
264- func (db * BoltDB ) ListSessionActions (sessionID session.ID ,
265- filterFn ListActionsFilterFn , query * ListActionsQuery ) ([]* Action ,
337+ func (db * BoltDB ) listSessionActions (sessionID session.ID ,
338+ filterFn listActionsFilterFn , query * ListActionsQuery ) ([]* Action ,
266339 uint64 , uint64 , error ) {
267340
268341 var (
@@ -303,12 +376,12 @@ func (db *BoltDB) ListSessionActions(sessionID session.ID,
303376 return actions , lastIndex , totalCount , nil
304377}
305378
306- // ListGroupActions returns a list of the given session group's Actions that
379+ // listGroupActions returns a list of the given session group's Actions that
307380// pass the filterFn requirements.
308381//
309382// TODO: update to allow for pagination.
310- func (db * BoltDB ) ListGroupActions (ctx context.Context , groupID session.ID ,
311- filterFn ListActionsFilterFn ) ([]* Action , error ) {
383+ func (db * BoltDB ) listGroupActions (ctx context.Context , groupID session.ID ,
384+ filterFn listActionsFilterFn ) ([]* Action , error ) {
312385
313386 if filterFn == nil {
314387 filterFn = func (a * Action , reversed bool ) (bool , bool ) {
0 commit comments