@@ -14,6 +14,7 @@ import (
14
14
log "github.com/sirupsen/logrus"
15
15
16
16
"github.com/crowdsecurity/go-cs-lib/cstime"
17
+ "github.com/crowdsecurity/go-cs-lib/slicetools"
17
18
18
19
"github.com/crowdsecurity/crowdsec/pkg/csnet"
19
20
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
@@ -168,7 +169,7 @@ func (c *Client) CreateOrUpdateAlert(ctx context.Context, machineID string, aler
168
169
// create missing decisions in batches
169
170
170
171
decisions := make ([]* ent.Decision , 0 , len (decisionBuilders ))
171
- if err := Batch (ctx , decisionBuilders , c .decisionBulkSize , func (ctx context.Context , b []* ent.DecisionCreate ) error {
172
+ if err := slicetools . Batch (ctx , decisionBuilders , c .decisionBulkSize , func (ctx context.Context , b []* ent.DecisionCreate ) error {
172
173
ret , err := c .Ent .Decision .CreateBulk (b ... ).Save (ctx )
173
174
if err != nil {
174
175
return fmt .Errorf ("creating alert decisions: %w" , err )
@@ -181,7 +182,7 @@ func (c *Client) CreateOrUpdateAlert(ctx context.Context, machineID string, aler
181
182
182
183
// attach decisions to alert in batches
183
184
184
- if err := Batch (ctx , decisions , c .decisionBulkSize , func (ctx context.Context , d []* ent.Decision ) error {
185
+ if err := slicetools . Batch (ctx , decisions , c .decisionBulkSize , func (ctx context.Context , d []* ent.Decision ) error {
185
186
if err := c .Ent .Alert .Update ().Where (alert .UUID (alertItem .UUID )).AddDecisions (d ... ).Exec (ctx ); err != nil {
186
187
return fmt .Errorf ("updating alert %s: %w" , alertItem .UUID , err )
187
188
}
@@ -333,7 +334,7 @@ func (c *Client) UpdateCommunityBlocklist(ctx context.Context, alertItem *models
333
334
334
335
// Delete older decisions from capi
335
336
336
- if err := Batch (ctx , valueList , c .decisionBulkSize , func (ctx context.Context , vals []string ) error {
337
+ if err := slicetools . Batch (ctx , valueList , c .decisionBulkSize , func (ctx context.Context , vals []string ) error {
337
338
deletedDecisions , err := txClient .Decision .Delete ().
338
339
Where (decision .And (
339
340
decision .OriginEQ (decOrigin ),
@@ -351,7 +352,7 @@ func (c *Client) UpdateCommunityBlocklist(ctx context.Context, alertItem *models
351
352
352
353
// Insert new decisions
353
354
354
- if err := Batch (ctx , decisionBuilders , c .decisionBulkSize , func (ctx context.Context , b []* ent.DecisionCreate ) error {
355
+ if err := slicetools . Batch (ctx , decisionBuilders , c .decisionBulkSize , func (ctx context.Context , b []* ent.DecisionCreate ) error {
355
356
insertedDecisions , err := txClient .Decision .CreateBulk (b ... ).Save (ctx )
356
357
if err != nil {
357
358
return err
@@ -549,7 +550,7 @@ func buildMetaCreates(ctx context.Context, logger log.FieldLogger, client *ent.C
549
550
550
551
func buildDecisions (ctx context.Context , logger log.FieldLogger , client * Client , alertItem * models.Alert , stopAtTime time.Time ) ([]* ent.Decision , int , error ) {
551
552
decisions := []* ent.Decision {}
552
- if err := Batch (ctx , alertItem .Decisions , client .decisionBulkSize , func (ctx context.Context , part []* models.Decision ) error {
553
+ if err := slicetools . Batch (ctx , alertItem .Decisions , client .decisionBulkSize , func (ctx context.Context , part []* models.Decision ) error {
553
554
ret , err := client .createDecisionBatch (ctx , * alertItem .Simulated , stopAtTime , part )
554
555
if err != nil {
555
556
return fmt .Errorf ("creating alert decisions: %w" , err )
@@ -625,7 +626,7 @@ func saveAlerts(ctx context.Context, c *Client, batch []alertCreatePlan) ([]stri
625
626
continue
626
627
}
627
628
628
- if err := Batch (ctx , d , c .decisionBulkSize , func (ctx context.Context , d2 []* ent.Decision ) error {
629
+ if err := slicetools . Batch (ctx , d , c .decisionBulkSize , func (ctx context.Context , d2 []* ent.Decision ) error {
629
630
return retryOnBusy (func () error {
630
631
_ , err := c .Ent .Alert .Update ().Where (alert .IDEQ (a .ID )).AddDecisions (d2 ... ).Save (ctx )
631
632
return err
@@ -744,7 +745,7 @@ func (c *Client) CreateAlert(ctx context.Context, machineID string, alertList []
744
745
c .Log .Debugf ("writing %d items" , len (alertList ))
745
746
746
747
alertIDs := []string {}
747
- if err := Batch (ctx , alertList , alertCreateBulkSize , func (ctx context.Context , part []* models.Alert ) error {
748
+ if err := slicetools . Batch (ctx , alertList , alertCreateBulkSize , func (ctx context.Context , part []* models.Alert ) error {
748
749
ids , err := c .createAlertBatch (ctx , machineID , owner , part )
749
750
if err != nil {
750
751
return fmt .Errorf ("machine %q: %w" , machineID , err )
0 commit comments