@@ -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,30 +294,24 @@ 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 ) {
238-
239307 locator , err := deserializeActionLocator (
240308 bytes .NewReader (locatorBytes ),
241309 )
242310 if err != nil {
243311 return nil , err
244312 }
245-
246313 return getAction (actionsBucket , locator )
247314 }
248-
249315 actions , lastIndex , totalCount , err = paginateActions (
250316 query , actionsIndexBucket .Cursor (), readAction ,
251317 filterFn ,
@@ -255,14 +321,20 @@ func (db *BoltDB) ListActions(filterFn ListActionsFilterFn,
255321 if err != nil {
256322 return nil , 0 , 0 , err
257323 }
258-
259324 return actions , lastIndex , totalCount , nil
260325}
261326
262- // ListSessionActions returns a list of the given session's Actions that pass
327+ // listActionsFilterFn defines a function that can be used to determine if an
328+ // action should be included in a set of results or not. The reversed parameter
329+ // indicates if the actions are being traversed in reverse order or not.
330+ // The first return boolean indicates if the action should be included or not
331+ // and the second one indicates if the iteration should be stopped or not.
332+ type listActionsFilterFn func (a * Action , reversed bool ) (bool , bool )
333+
334+ // listSessionActions returns a list of the given session's Actions that pass
263335// the filterFn requirements.
264- func (db * BoltDB ) ListSessionActions (sessionID session.ID ,
265- filterFn ListActionsFilterFn , query * ListActionsQuery ) ([]* Action ,
336+ func (db * BoltDB ) listSessionActions (sessionID session.ID ,
337+ filterFn listActionsFilterFn , query * ListActionsQuery ) ([]* Action ,
266338 uint64 , uint64 , error ) {
267339
268340 var (
@@ -303,12 +375,12 @@ func (db *BoltDB) ListSessionActions(sessionID session.ID,
303375 return actions , lastIndex , totalCount , nil
304376}
305377
306- // ListGroupActions returns a list of the given session group's Actions that
378+ // listGroupActions returns a list of the given session group's Actions that
307379// pass the filterFn requirements.
308380//
309381// TODO: update to allow for pagination.
310- func (db * BoltDB ) ListGroupActions (ctx context.Context , groupID session.ID ,
311- filterFn ListActionsFilterFn ) ([]* Action , error ) {
382+ func (db * BoltDB ) listGroupActions (ctx context.Context , groupID session.ID ,
383+ filterFn listActionsFilterFn ) ([]* Action , error ) {
312384
313385 if filterFn == nil {
314386 filterFn = func (a * Action , reversed bool ) (bool , bool ) {
0 commit comments