Skip to content

Commit f364019

Browse files
committed
slicetools.Chunks() -> slices.Chunk()
1 parent 25726b0 commit f364019

File tree

3 files changed

+12
-25
lines changed

3 files changed

+12
-25
lines changed

pkg/csplugin/broker.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222

2323
"github.com/crowdsecurity/go-cs-lib/csstring"
2424
"github.com/crowdsecurity/go-cs-lib/ptr"
25-
"github.com/crowdsecurity/go-cs-lib/slicetools"
2625

2726
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
2827
"github.com/crowdsecurity/crowdsec/pkg/models"
@@ -125,7 +124,7 @@ func (pb *PluginBroker) Run(pluginTomb *tomb.Tomb) {
125124
threshold = 1
126125
}
127126

128-
for _, chunk := range slicetools.Chunks(tmpAlerts, threshold) {
127+
for chunk := range slices.Chunk(tmpAlerts, threshold) {
129128
if err := pb.pushNotificationsToPlugin(ctx, pluginName, chunk); err != nil {
130129
log.WithField("plugin:", pluginName).Error(err)
131130
}

pkg/database/alerts.go

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
"slices"
78
"sort"
89
"strconv"
910
"strings"
@@ -14,7 +15,6 @@ import (
1415
log "github.com/sirupsen/logrus"
1516

1617
"github.com/crowdsecurity/go-cs-lib/cstime"
17-
"github.com/crowdsecurity/go-cs-lib/slicetools"
1818

1919
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
2020
"github.com/crowdsecurity/crowdsec/pkg/database/ent/alert"
@@ -170,9 +170,7 @@ func (c *Client) CreateOrUpdateAlert(ctx context.Context, machineID string, aler
170170

171171
decisions := []*ent.Decision{}
172172

173-
builderChunks := slicetools.Chunks(decisionBuilders, c.decisionBulkSize)
174-
175-
for _, builderChunk := range builderChunks {
173+
for builderChunk := range slices.Chunk(decisionBuilders, c.decisionBulkSize) {
176174
decisionsCreateRet, err := c.Ent.Decision.CreateBulk(builderChunk...).Save(ctx)
177175
if err != nil {
178176
return "", fmt.Errorf("creating alert decisions: %w", err)
@@ -183,9 +181,7 @@ func (c *Client) CreateOrUpdateAlert(ctx context.Context, machineID string, aler
183181

184182
// now that we bulk created missing decisions, let's update the alert
185183

186-
decisionChunks := slicetools.Chunks(decisions, c.decisionBulkSize)
187-
188-
for _, decisionChunk := range decisionChunks {
184+
for decisionChunk := range slices.Chunk(decisions, c.decisionBulkSize) {
189185
err = c.Ent.Alert.Update().Where(alert.UUID(alertItem.UUID)).AddDecisions(decisionChunk...).Exec(ctx)
190186
if err != nil {
191187
return "", fmt.Errorf("updating alert %s: %w", alertItem.UUID, err)
@@ -336,9 +332,7 @@ func (c *Client) UpdateCommunityBlocklist(ctx context.Context, alertItem *models
336332
valueList = append(valueList, *decisionItem.Value)
337333
}
338334

339-
deleteChunks := slicetools.Chunks(valueList, c.decisionBulkSize)
340-
341-
for _, deleteChunk := range deleteChunks {
335+
for deleteChunk := range slices.Chunk(valueList, c.decisionBulkSize) {
342336
// Deleting older decisions from capi
343337
deletedDecisions, err := txClient.Decision.Delete().
344338
Where(decision.And(
@@ -353,9 +347,7 @@ func (c *Client) UpdateCommunityBlocklist(ctx context.Context, alertItem *models
353347
deleted += deletedDecisions
354348
}
355349

356-
builderChunks := slicetools.Chunks(decisionBuilders, c.decisionBulkSize)
357-
358-
for _, builderChunk := range builderChunks {
350+
for builderChunk := range slices.Chunk(decisionBuilders, c.decisionBulkSize) {
359351
insertedDecisions, err := txClient.Decision.CreateBulk(builderChunk...).Save(ctx)
360352
if err != nil {
361353
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
555547
func buildDecisions(ctx context.Context, logger log.FieldLogger, client *Client, alertItem *models.Alert, stopAtTime time.Time) ([]*ent.Decision, int, error) {
556548
decisions := []*ent.Decision{}
557549

558-
decisionChunks := slicetools.Chunks(alertItem.Decisions, client.decisionBulkSize)
559-
for _, decisionChunk := range decisionChunks {
550+
for decisionChunk := range slices.Chunk(alertItem.Decisions, client.decisionBulkSize) {
560551
decisionRet, err := client.createDecisionChunk(ctx, *alertItem.Simulated, stopAtTime, decisionChunk)
561552
if err != nil {
562553
return nil, 0, fmt.Errorf("creating alert decisions: %w", err)
@@ -610,9 +601,8 @@ func saveAlerts(ctx context.Context, c *Client, alertBuilders []*ent.AlertCreate
610601
ret[i] = strconv.Itoa(a.ID)
611602

612603
d := alertDecisions[i]
613-
decisionsChunk := slicetools.Chunks(d, c.decisionBulkSize)
614604

615-
for _, d2 := range decisionsChunk {
605+
for d2 := range slices.Chunk(d, c.decisionBulkSize) {
616606
if err := retryOnBusy(func() error {
617607
_, err := c.Ent.Alert.Update().Where(alert.IDEQ(a.ID)).AddDecisions(d2...).Save(ctx)
618608
return err
@@ -728,10 +718,9 @@ func (c *Client) CreateAlert(ctx context.Context, machineID string, alertList []
728718

729719
c.Log.Debugf("writing %d items", len(alertList))
730720

731-
alertChunks := slicetools.Chunks(alertList, alertCreateBulkSize)
732721
alertIDs := []string{}
733722

734-
for _, alertChunk := range alertChunks {
723+
for alertChunk := range slices.Chunk(alertList, alertCreateBulkSize) {
735724
ids, err := c.createAlertChunk(ctx, machineID, owner, alertChunk)
736725
if err != nil {
737726
return nil, fmt.Errorf("machine '%s': %w", machineID, err)

pkg/database/decisions.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ package database
33
import (
44
"context"
55
"fmt"
6+
"slices"
67
"strconv"
78
"strings"
89
"time"
910

1011
"entgo.io/ent/dialect/sql"
1112
"github.com/pkg/errors"
1213

13-
"github.com/crowdsecurity/go-cs-lib/slicetools"
14-
1514
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
1615
"github.com/crowdsecurity/crowdsec/pkg/database/ent/decision"
1716
"github.com/crowdsecurity/crowdsec/pkg/database/ent/predicate"
@@ -415,7 +414,7 @@ func (c *Client) ExpireDecisions(ctx context.Context, decisions []*ent.Decision)
415414

416415
total := 0
417416

418-
for _, chunk := range slicetools.Chunks(decisions, decisionDeleteBulkSize) {
417+
for chunk := range slices.Chunk(decisions, decisionDeleteBulkSize) {
419418
rows, err := c.ExpireDecisions(ctx, chunk)
420419
if err != nil {
421420
return total, err
@@ -447,7 +446,7 @@ func (c *Client) DeleteDecisions(ctx context.Context, decisions []*ent.Decision)
447446

448447
tot := 0
449448

450-
for _, chunk := range slicetools.Chunks(decisions, decisionDeleteBulkSize) {
449+
for chunk := range slices.Chunk(decisions, decisionDeleteBulkSize) {
451450
rows, err := c.DeleteDecisions(ctx, chunk)
452451
if err != nil {
453452
return tot, err

0 commit comments

Comments
 (0)