Skip to content

Commit 9cc0ca5

Browse files
committed
dirty workaround
1 parent 3c73be8 commit 9cc0ca5

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

pkg/csplugin/broker.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package csplugin
22

33
import (
4+
"cmp"
45
"context"
56
"errors"
67
"fmt"
@@ -124,7 +125,7 @@ func (pb *PluginBroker) Run(pluginTomb *tomb.Tomb) {
124125
threshold = 1
125126
}
126127

127-
for chunk := range slices.Chunk(tmpAlerts, threshold) {
128+
for chunk := range slices.Chunk(tmpAlerts, max(1, cmp.Or(threshold, len(tmpAlerts)))) {
128129
if err := pb.pushNotificationsToPlugin(ctx, pluginName, chunk); err != nil {
129130
log.WithField("plugin:", pluginName).Error(err)
130131
}

pkg/database/alerts.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package database
22

33
import (
4+
"cmp"
45
"context"
56
"encoding/json"
67
"fmt"
@@ -166,7 +167,7 @@ func (c *Client) CreateOrUpdateAlert(ctx context.Context, machineID string, aler
166167

167168
decisions := []*ent.Decision{}
168169

169-
for builderChunk := range slices.Chunk(decisionBuilders, c.decisionBulkSize) {
170+
for builderChunk := range slices.Chunk(decisionBuilders, max(1, cmp.Or(c.decisionBulkSize, len(decisionBuilders)))) {
170171
decisionsCreateRet, err := c.Ent.Decision.CreateBulk(builderChunk...).Save(ctx)
171172
if err != nil {
172173
return "", fmt.Errorf("creating alert decisions: %w", err)
@@ -177,7 +178,7 @@ func (c *Client) CreateOrUpdateAlert(ctx context.Context, machineID string, aler
177178

178179
// now that we bulk created missing decisions, let's update the alert
179180

180-
for decisionChunk := range slices.Chunk(decisions, c.decisionBulkSize) {
181+
for decisionChunk := range slices.Chunk(decisions, max(1, cmp.Or(c.decisionBulkSize, len(decisions)))) {
181182
err = c.Ent.Alert.Update().Where(alert.UUID(alertItem.UUID)).AddDecisions(decisionChunk...).Exec(ctx)
182183
if err != nil {
183184
return "", fmt.Errorf("updating alert %s: %w", alertItem.UUID, err)
@@ -325,7 +326,7 @@ func (c *Client) UpdateCommunityBlocklist(ctx context.Context, alertItem *models
325326
valueList = append(valueList, *decisionItem.Value)
326327
}
327328

328-
for deleteChunk := range slices.Chunk(valueList, c.decisionBulkSize) {
329+
for deleteChunk := range slices.Chunk(valueList, max(1, cmp.Or(c.decisionBulkSize, len(valueList)))) {
329330
// Deleting older decisions from capi
330331
deletedDecisions, err := txClient.Decision.Delete().
331332
Where(decision.And(
@@ -340,7 +341,7 @@ func (c *Client) UpdateCommunityBlocklist(ctx context.Context, alertItem *models
340341
deleted += deletedDecisions
341342
}
342343

343-
for builderChunk := range slices.Chunk(decisionBuilders, c.decisionBulkSize) {
344+
for builderChunk := range slices.Chunk(decisionBuilders, max(1, cmp.Or(c.decisionBulkSize, len(decisionBuilders)))) {
344345
insertedDecisions, err := txClient.Decision.CreateBulk(builderChunk...).Save(ctx)
345346
if err != nil {
346347
return 0, 0, 0, rollbackOnError(txClient, err, "bulk creating decisions")
@@ -537,7 +538,7 @@ func buildMetaCreates(ctx context.Context, logger log.FieldLogger, client *ent.C
537538
func buildDecisions(ctx context.Context, logger log.FieldLogger, client *Client, alertItem *models.Alert, stopAtTime time.Time) ([]*ent.Decision, int, error) {
538539
decisions := []*ent.Decision{}
539540

540-
for decisionChunk := range slices.Chunk(alertItem.Decisions, client.decisionBulkSize) {
541+
for decisionChunk := range slices.Chunk(alertItem.Decisions, max(1, cmp.Or(client.decisionBulkSize, len(alertItem.Decisions)))) {
541542
decisionRet, err := client.createDecisionChunk(ctx, *alertItem.Simulated, stopAtTime, decisionChunk)
542543
if err != nil {
543544
return nil, 0, fmt.Errorf("creating alert decisions: %w", err)
@@ -593,7 +594,7 @@ func saveAlerts(ctx context.Context, c *Client, alertBuilders []*ent.AlertCreate
593594

594595
d := alertDecisions[i]
595596

596-
for d2 := range slices.Chunk(d, c.decisionBulkSize) {
597+
for d2 := range slices.Chunk(d, max(1, cmp.Or(c.decisionBulkSize, len(d)))) {
597598
if err := retryOnBusy(func() error {
598599
_, err := c.Ent.Alert.Update().Where(alert.IDEQ(a.ID)).AddDecisions(d2...).Save(ctx)
599600
return err
@@ -712,7 +713,7 @@ func (c *Client) CreateAlert(ctx context.Context, machineID string, alertList []
712713

713714
alertIDs := []string{}
714715

715-
for alertChunk := range slices.Chunk(alertList, alertCreateBulkSize) {
716+
for alertChunk := range slices.Chunk(alertList, max(1, cmp.Or(alertCreateBulkSize, len(alertList)))) {
716717
ids, err := c.createAlertChunk(ctx, machineID, owner, alertChunk)
717718
if err != nil {
718719
return nil, fmt.Errorf("machine '%s': %w", machineID, err)

pkg/database/decisions.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package database
22

33
import (
4+
"cmp"
45
"context"
56
"fmt"
67
"slices"
@@ -413,7 +414,7 @@ func (c *Client) ExpireDecisions(ctx context.Context, decisions []*ent.Decision)
413414

414415
total := 0
415416

416-
for chunk := range slices.Chunk(decisions, decisionDeleteBulkSize) {
417+
for chunk := range slices.Chunk(decisions, max(1, cmp.Or(decisionDeleteBulkSize, len(decisions)))) {
417418
rows, err := c.ExpireDecisions(ctx, chunk)
418419
if err != nil {
419420
return total, err
@@ -445,7 +446,7 @@ func (c *Client) DeleteDecisions(ctx context.Context, decisions []*ent.Decision)
445446

446447
tot := 0
447448

448-
for chunk := range slices.Chunk(decisions, decisionDeleteBulkSize) {
449+
for chunk := range slices.Chunk(decisions, max(1, cmp.Or(decisionDeleteBulkSize, len(decisions)))) {
449450
rows, err := c.DeleteDecisions(ctx, chunk)
450451
if err != nil {
451452
return tot, err

0 commit comments

Comments
 (0)