Skip to content

Commit 871df57

Browse files
committed
dirty workaround
1 parent 0a701fc commit 871df57

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"
@@ -170,7 +171,7 @@ func (c *Client) CreateOrUpdateAlert(ctx context.Context, machineID string, aler
170171

171172
decisions := []*ent.Decision{}
172173

173-
for builderChunk := range slices.Chunk(decisionBuilders, c.decisionBulkSize) {
174+
for builderChunk := range slices.Chunk(decisionBuilders, max(1, cmp.Or(c.decisionBulkSize, len(decisionBuilders)))) {
174175
decisionsCreateRet, err := c.Ent.Decision.CreateBulk(builderChunk...).Save(ctx)
175176
if err != nil {
176177
return "", fmt.Errorf("creating alert decisions: %w", err)
@@ -181,7 +182,7 @@ func (c *Client) CreateOrUpdateAlert(ctx context.Context, machineID string, aler
181182

182183
// now that we bulk created missing decisions, let's update the alert
183184

184-
for decisionChunk := range slices.Chunk(decisions, c.decisionBulkSize) {
185+
for decisionChunk := range slices.Chunk(decisions, max(1, cmp.Or(c.decisionBulkSize, len(decisions)))) {
185186
err = c.Ent.Alert.Update().Where(alert.UUID(alertItem.UUID)).AddDecisions(decisionChunk...).Exec(ctx)
186187
if err != nil {
187188
return "", fmt.Errorf("updating alert %s: %w", alertItem.UUID, err)
@@ -332,7 +333,7 @@ func (c *Client) UpdateCommunityBlocklist(ctx context.Context, alertItem *models
332333
valueList = append(valueList, *decisionItem.Value)
333334
}
334335

335-
for deleteChunk := range slices.Chunk(valueList, c.decisionBulkSize) {
336+
for deleteChunk := range slices.Chunk(valueList, max(1, cmp.Or(c.decisionBulkSize, len(valueList)))) {
336337
// Deleting older decisions from capi
337338
deletedDecisions, err := txClient.Decision.Delete().
338339
Where(decision.And(
@@ -347,7 +348,7 @@ func (c *Client) UpdateCommunityBlocklist(ctx context.Context, alertItem *models
347348
deleted += deletedDecisions
348349
}
349350

350-
for builderChunk := range slices.Chunk(decisionBuilders, c.decisionBulkSize) {
351+
for builderChunk := range slices.Chunk(decisionBuilders, max(1, cmp.Or(c.decisionBulkSize, len(decisionBuilders)))) {
351352
insertedDecisions, err := txClient.Decision.CreateBulk(builderChunk...).Save(ctx)
352353
if err != nil {
353354
return 0, 0, 0, rollbackOnError(txClient, err, "bulk creating decisions")
@@ -547,7 +548,7 @@ func buildMetaCreates(ctx context.Context, logger log.FieldLogger, client *ent.C
547548
func buildDecisions(ctx context.Context, logger log.FieldLogger, client *Client, alertItem *models.Alert, stopAtTime time.Time) ([]*ent.Decision, int, error) {
548549
decisions := []*ent.Decision{}
549550

550-
for decisionChunk := range slices.Chunk(alertItem.Decisions, client.decisionBulkSize) {
551+
for decisionChunk := range slices.Chunk(alertItem.Decisions, max(1, cmp.Or(client.decisionBulkSize, len(alertItem.Decisions)))) {
551552
decisionRet, err := client.createDecisionChunk(ctx, *alertItem.Simulated, stopAtTime, decisionChunk)
552553
if err != nil {
553554
return nil, 0, fmt.Errorf("creating alert decisions: %w", err)
@@ -602,7 +603,7 @@ func saveAlerts(ctx context.Context, c *Client, alertBuilders []*ent.AlertCreate
602603

603604
d := alertDecisions[i]
604605

605-
for d2 := range slices.Chunk(d, c.decisionBulkSize) {
606+
for d2 := range slices.Chunk(d, max(1, cmp.Or(c.decisionBulkSize, len(d)))) {
606607
if err := retryOnBusy(func() error {
607608
_, err := c.Ent.Alert.Update().Where(alert.IDEQ(a.ID)).AddDecisions(d2...).Save(ctx)
608609
return err
@@ -720,7 +721,7 @@ func (c *Client) CreateAlert(ctx context.Context, machineID string, alertList []
720721

721722
alertIDs := []string{}
722723

723-
for alertChunk := range slices.Chunk(alertList, alertCreateBulkSize) {
724+
for alertChunk := range slices.Chunk(alertList, max(1, cmp.Or(alertCreateBulkSize, len(alertList)))) {
724725
ids, err := c.createAlertChunk(ctx, machineID, owner, alertChunk)
725726
if err != nil {
726727
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"
@@ -414,7 +415,7 @@ func (c *Client) ExpireDecisions(ctx context.Context, decisions []*ent.Decision)
414415

415416
total := 0
416417

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

447448
tot := 0
448449

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

0 commit comments

Comments
 (0)