@@ -29,9 +29,9 @@ type DecisionsByScenario struct {
29
29
30
30
func BuildDecisionRequestWithFilter (query * ent.DecisionQuery , filter map [string ][]string ) (* ent.DecisionQuery , error ) {
31
31
var (
32
- err error
32
+ err error
33
33
start_ip , start_sfx , end_ip , end_sfx int64
34
- ip_sz int
34
+ ip_sz int
35
35
)
36
36
37
37
contains := true
@@ -100,18 +100,21 @@ func BuildDecisionRequestWithFilter(query *ent.DecisionQuery, filter map[string]
100
100
if err != nil {
101
101
return nil , errors .Wrapf (InvalidFilter , "invalid limit value : %s" , err )
102
102
}
103
+
103
104
query = query .Limit (limit )
104
105
case "offset" :
105
106
offset , err := strconv .Atoi (value [0 ])
106
107
if err != nil {
107
108
return nil , errors .Wrapf (InvalidFilter , "invalid offset value : %s" , err )
108
109
}
110
+
109
111
query = query .Offset (offset )
110
112
case "id_gt" :
111
113
id , err := strconv .Atoi (value [0 ])
112
114
if err != nil {
113
115
return nil , errors .Wrapf (InvalidFilter , "invalid id_gt value : %s" , err )
114
116
}
117
+
115
118
query = query .Where (decision .IDGT (id ))
116
119
}
117
120
}
@@ -201,7 +204,7 @@ func (c *Client) QueryDecisionCountByScenario(ctx context.Context) ([]*Decisions
201
204
202
205
func (c * Client ) QueryDecisionWithFilter (ctx context.Context , filter map [string ][]string ) ([]* ent.Decision , error ) {
203
206
var (
204
- err error
207
+ err error
205
208
data []* ent.Decision
206
209
)
207
210
@@ -322,70 +325,12 @@ func (c *Client) QueryNewDecisionsSinceWithFilters(ctx context.Context, since *t
322
325
return data , nil
323
326
}
324
327
325
- func (c * Client ) DeleteDecisionsWithFilter (ctx context.Context , filter map [string ][]string ) (string , []* ent.Decision , error ) {
326
- var (
327
- err error
328
- start_ip , start_sfx , end_ip , end_sfx int64
329
- ip_sz int
330
- )
331
-
332
- contains := true
333
- /*if contains is true, return bans that *contains* the given value (value is the inner)
334
- else, return bans that are *contained* by the given value (value is the outer) */
335
-
336
- decisions := c .Ent .Decision .Query ()
337
-
338
- for param , value := range filter {
339
- switch param {
340
- case "contains" :
341
- contains , err = strconv .ParseBool (value [0 ])
342
- if err != nil {
343
- return "0" , nil , errors .Wrapf (InvalidFilter , "invalid contains value : %s" , err )
344
- }
345
- case "scope" :
346
- decisions = decisions .Where (decision .ScopeEQ (value [0 ]))
347
- case "value" :
348
- decisions = decisions .Where (decision .ValueEQ (value [0 ]))
349
- case "type" :
350
- decisions = decisions .Where (decision .TypeEQ (value [0 ]))
351
- case "ip" , "range" :
352
- ip_sz , start_ip , start_sfx , end_ip , end_sfx , err = types .Addr2Ints (value [0 ])
353
- if err != nil {
354
- return "0" , nil , errors .Wrapf (InvalidIPOrRange , "unable to convert '%s' to int: %s" , value [0 ], err )
355
- }
356
- case "scenario" :
357
- decisions = decisions .Where (decision .ScenarioEQ (value [0 ]))
358
- default :
359
- return "0" , nil , errors .Wrap (InvalidFilter , fmt .Sprintf ("'%s' doesn't exist" , param ))
360
- }
361
- }
362
-
363
- decisions , err = decisionIPFilter (decisions , contains , ip_sz , start_ip , start_sfx , end_ip , end_sfx )
364
- if err != nil {
365
- return "0" , nil , err
366
- }
367
-
368
- toDelete , err := decisions .All (ctx )
369
- if err != nil {
370
- c .Log .Warningf ("DeleteDecisionsWithFilter : %s" , err )
371
- return "0" , nil , errors .Wrap (DeleteFail , "decisions with provided filter" )
372
- }
373
-
374
- count , err := c .DeleteDecisions (ctx , toDelete )
375
- if err != nil {
376
- c .Log .Warningf ("While deleting decisions : %s" , err )
377
- return "0" , nil , errors .Wrap (DeleteFail , "decisions with provided filter" )
378
- }
379
-
380
- return strconv .Itoa (count ), toDelete , nil
381
- }
382
-
383
328
// ExpireDecisionsWithFilter updates the expiration time to now() for the decisions matching the filter, and returns the updated items
384
- func (c * Client ) ExpireDecisionsWithFilter (ctx context.Context , filter map [string ][]string ) (string , []* ent.Decision , error ) {
329
+ func (c * Client ) ExpireDecisionsWithFilter (ctx context.Context , filter map [string ][]string ) (int , []* ent.Decision , error ) {
385
330
var (
386
- err error
331
+ err error
387
332
start_ip , start_sfx , end_ip , end_sfx int64
388
- ip_sz int
333
+ ip_sz int
389
334
)
390
335
391
336
contains := true
@@ -398,7 +343,7 @@ func (c *Client) ExpireDecisionsWithFilter(ctx context.Context, filter map[strin
398
343
case "contains" :
399
344
contains , err = strconv .ParseBool (value [0 ])
400
345
if err != nil {
401
- return "0" , nil , errors .Wrapf (InvalidFilter , "invalid contains value : %s" , err )
346
+ return 0 , nil , errors .Wrapf (InvalidFilter , "invalid contains value : %s" , err )
402
347
}
403
348
case "scopes" :
404
349
decisions = decisions .Where (decision .ScopeEQ (value [0 ]))
@@ -413,32 +358,32 @@ func (c *Client) ExpireDecisionsWithFilter(ctx context.Context, filter map[strin
413
358
case "ip" , "range" :
414
359
ip_sz , start_ip , start_sfx , end_ip , end_sfx , err = types .Addr2Ints (value [0 ])
415
360
if err != nil {
416
- return "0" , nil , errors .Wrapf (InvalidIPOrRange , "unable to convert '%s' to int: %s" , value [0 ], err )
361
+ return 0 , nil , errors .Wrapf (InvalidIPOrRange , "unable to convert '%s' to int: %s" , value [0 ], err )
417
362
}
418
363
case "scenario" :
419
364
decisions = decisions .Where (decision .ScenarioEQ (value [0 ]))
420
365
default :
421
- return "0" , nil , errors .Wrapf (InvalidFilter , "'%s' doesn't exist" , param )
366
+ return 0 , nil , errors .Wrapf (InvalidFilter , "'%s' doesn't exist" , param )
422
367
}
423
368
}
424
369
425
370
decisions , err = decisionIPFilter (decisions , contains , ip_sz , start_ip , start_sfx , end_ip , end_sfx )
426
371
if err != nil {
427
- return "0" , nil , err
372
+ return 0 , nil , err
428
373
}
429
374
430
375
DecisionsToDelete , err := decisions .All (ctx )
431
376
if err != nil {
432
377
c .Log .Warningf ("ExpireDecisionsWithFilter : %s" , err )
433
- return "0" , nil , errors .Wrap (DeleteFail , "expire decisions with provided filter" )
378
+ return 0 , nil , errors .Wrap (DeleteFail , "expire decisions with provided filter" )
434
379
}
435
380
436
381
count , err := c .ExpireDecisions (ctx , DecisionsToDelete )
437
382
if err != nil {
438
- return "0" , nil , errors .Wrapf (DeleteFail , "expire decisions with provided filter : %s" , err )
383
+ return 0 , nil , errors .Wrapf (DeleteFail , "expire decisions with provided filter : %s" , err )
439
384
}
440
385
441
- return strconv . Itoa ( count ) , DecisionsToDelete , err
386
+ return count , DecisionsToDelete , err
442
387
}
443
388
444
389
func decisionIDs (decisions []* ent.Decision ) []int {
@@ -564,13 +509,7 @@ func (c *Client) CountDecisionsByValue(ctx context.Context, value string, since
564
509
}
565
510
566
511
func (c * Client ) GetActiveDecisionsTimeLeftByValue (ctx context.Context , decisionValue string ) (time.Duration , error ) {
567
- var (
568
- err error
569
- start_ip , start_sfx , end_ip , end_sfx int64
570
- ip_sz int
571
- )
572
-
573
- ip_sz , start_ip , start_sfx , end_ip , end_sfx , err = types .Addr2Ints (decisionValue )
512
+ ip_sz , start_ip , start_sfx , end_ip , end_sfx , err := types .Addr2Ints (decisionValue )
574
513
if err != nil {
575
514
return 0 , fmt .Errorf ("unable to convert '%s' to int: %w" , decisionValue , err )
576
515
}
0 commit comments