4
4
"context"
5
5
"encoding/json"
6
6
"fmt"
7
+ "slices"
7
8
"sort"
8
9
"strconv"
9
10
"strings"
@@ -14,7 +15,6 @@ import (
14
15
log "github.com/sirupsen/logrus"
15
16
16
17
"github.com/crowdsecurity/go-cs-lib/cstime"
17
- "github.com/crowdsecurity/go-cs-lib/slicetools"
18
18
19
19
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
20
20
"github.com/crowdsecurity/crowdsec/pkg/database/ent/alert"
@@ -170,9 +170,7 @@ func (c *Client) CreateOrUpdateAlert(ctx context.Context, machineID string, aler
170
170
171
171
decisions := []* ent.Decision {}
172
172
173
- builderChunks := slicetools .Chunks (decisionBuilders , c .decisionBulkSize )
174
-
175
- for _ , builderChunk := range builderChunks {
173
+ for builderChunk := range slices .Chunk (decisionBuilders , c .decisionBulkSize ) {
176
174
decisionsCreateRet , err := c .Ent .Decision .CreateBulk (builderChunk ... ).Save (ctx )
177
175
if err != nil {
178
176
return "" , fmt .Errorf ("creating alert decisions: %w" , err )
@@ -183,9 +181,7 @@ func (c *Client) CreateOrUpdateAlert(ctx context.Context, machineID string, aler
183
181
184
182
// now that we bulk created missing decisions, let's update the alert
185
183
186
- decisionChunks := slicetools .Chunks (decisions , c .decisionBulkSize )
187
-
188
- for _ , decisionChunk := range decisionChunks {
184
+ for decisionChunk := range slices .Chunk (decisions , c .decisionBulkSize ) {
189
185
err = c .Ent .Alert .Update ().Where (alert .UUID (alertItem .UUID )).AddDecisions (decisionChunk ... ).Exec (ctx )
190
186
if err != nil {
191
187
return "" , fmt .Errorf ("updating alert %s: %w" , alertItem .UUID , err )
@@ -336,9 +332,7 @@ func (c *Client) UpdateCommunityBlocklist(ctx context.Context, alertItem *models
336
332
valueList = append (valueList , * decisionItem .Value )
337
333
}
338
334
339
- deleteChunks := slicetools .Chunks (valueList , c .decisionBulkSize )
340
-
341
- for _ , deleteChunk := range deleteChunks {
335
+ for deleteChunk := range slices .Chunk (valueList , c .decisionBulkSize ) {
342
336
// Deleting older decisions from capi
343
337
deletedDecisions , err := txClient .Decision .Delete ().
344
338
Where (decision .And (
@@ -353,9 +347,7 @@ func (c *Client) UpdateCommunityBlocklist(ctx context.Context, alertItem *models
353
347
deleted += deletedDecisions
354
348
}
355
349
356
- builderChunks := slicetools .Chunks (decisionBuilders , c .decisionBulkSize )
357
-
358
- for _ , builderChunk := range builderChunks {
350
+ for builderChunk := range slices .Chunk (decisionBuilders , c .decisionBulkSize ) {
359
351
insertedDecisions , err := txClient .Decision .CreateBulk (builderChunk ... ).Save (ctx )
360
352
if err != nil {
361
353
return 0 , 0 , 0 , rollbackOnError (txClient , err , "bulk creating decisions" )
@@ -555,8 +547,7 @@ func buildMetaCreates(ctx context.Context, logger log.FieldLogger, client *ent.C
555
547
func buildDecisions (ctx context.Context , logger log.FieldLogger , client * Client , alertItem * models.Alert , stopAtTime time.Time ) ([]* ent.Decision , int , error ) {
556
548
decisions := []* ent.Decision {}
557
549
558
- decisionChunks := slicetools .Chunks (alertItem .Decisions , client .decisionBulkSize )
559
- for _ , decisionChunk := range decisionChunks {
550
+ for decisionChunk := range slices .Chunk (alertItem .Decisions , client .decisionBulkSize ) {
560
551
decisionRet , err := client .createDecisionChunk (ctx , * alertItem .Simulated , stopAtTime , decisionChunk )
561
552
if err != nil {
562
553
return nil , 0 , fmt .Errorf ("creating alert decisions: %w" , err )
@@ -610,9 +601,8 @@ func saveAlerts(ctx context.Context, c *Client, alertBuilders []*ent.AlertCreate
610
601
ret [i ] = strconv .Itoa (a .ID )
611
602
612
603
d := alertDecisions [i ]
613
- decisionsChunk := slicetools .Chunks (d , c .decisionBulkSize )
614
604
615
- for _ , d2 := range decisionsChunk {
605
+ for d2 := range slices . Chunk ( d , c . decisionBulkSize ) {
616
606
if err := retryOnBusy (func () error {
617
607
_ , err := c .Ent .Alert .Update ().Where (alert .IDEQ (a .ID )).AddDecisions (d2 ... ).Save (ctx )
618
608
return err
@@ -728,10 +718,9 @@ func (c *Client) CreateAlert(ctx context.Context, machineID string, alertList []
728
718
729
719
c .Log .Debugf ("writing %d items" , len (alertList ))
730
720
731
- alertChunks := slicetools .Chunks (alertList , alertCreateBulkSize )
732
721
alertIDs := []string {}
733
722
734
- for _ , alertChunk := range alertChunks {
723
+ for alertChunk := range slices . Chunk ( alertList , alertCreateBulkSize ) {
735
724
ids , err := c .createAlertChunk (ctx , machineID , owner , alertChunk )
736
725
if err != nil {
737
726
return nil , fmt .Errorf ("machine '%s': %w" , machineID , err )
0 commit comments