@@ -163,33 +163,24 @@ func (c *Client) CreateOrUpdateAlert(ctx context.Context, machineID string, aler
163163 SetScope (* decisionItem .Scope ).
164164 SetOrigin (* decisionItem .Origin ).
165165 SetSimulated (* alertItem .Simulated ).
166- SetUUID (decisionItem .UUID )
166+ SetUUID (decisionItem .UUID ).
167+ SetOwnerID (foundAlert .ID )
167168
168169 decisionBuilders = append (decisionBuilders , decisionBuilder )
169170 }
170171
171- decisions := []* ent.Decision {}
172-
173- builderChunks := slicetools .Chunks (decisionBuilders , c .decisionBulkSize )
174-
175- for _ , builderChunk := range builderChunks {
176- decisionsCreateRet , err := c .Ent .Decision .CreateBulk (builderChunk ... ).Save (ctx )
177- if err != nil {
178- return "" , fmt .Errorf ("creating alert decisions: %w" , err )
179- }
180-
181- decisions = append (decisions , decisionsCreateRet ... )
182- }
183-
184- // now that we bulk created missing decisions, let's update the alert
185-
186- decisionChunks := slicetools .Chunks (decisions , c .decisionBulkSize )
172+ // create missing decisions in batches
187173
188- for _ , decisionChunk := range decisionChunks {
189- err = c .Ent .Alert .Update ().Where (alert .UUID (alertItem .UUID )).AddDecisions (decisionChunk ... ).Exec (ctx )
174+ decisions := make ([]* ent.Decision , 0 , len (decisionBuilders ))
175+ if err := slicetools .Batch (ctx , decisionBuilders , c .decisionBulkSize , func (ctx context.Context , b []* ent.DecisionCreate ) error {
176+ ret , err := c .Ent .Decision .CreateBulk (b ... ).Save (ctx )
190177 if err != nil {
191- return "" , fmt .Errorf ("updating alert %s : %w" , alertItem . UUID , err )
178+ return fmt .Errorf ("creating alert decisions : %w" , err )
192179 }
180+ decisions = append (decisions , ret ... )
181+ return nil
182+ }); err != nil {
183+ return "" , err
193184 }
194185
195186 return "" , nil
@@ -333,32 +324,35 @@ func (c *Client) UpdateCommunityBlocklist(ctx context.Context, alertItem *models
333324 valueList = append (valueList , * decisionItem .Value )
334325 }
335326
336- deleteChunks := slicetools . Chunks ( valueList , c . decisionBulkSize )
327+ // Delete older decisions from capi
337328
338- for _ , deleteChunk := range deleteChunks {
339- // Deleting older decisions from capi
329+ if err := slicetools .Batch (ctx , valueList , c .decisionBulkSize , func (ctx context.Context , vals []string ) error {
340330 deletedDecisions , err := txClient .Decision .Delete ().
341331 Where (decision .And (
342332 decision .OriginEQ (decOrigin ),
343333 decision .Not (decision .HasOwnerWith (alert .IDEQ (alertRef .ID ))),
344- decision .ValueIn (deleteChunk ... ),
345- )).Exec (ctx )
334+ decision .ValueIn (vals ... ),
335+ )).Exec (ctx )
346336 if err != nil {
347- return 0 , 0 , 0 , rollbackOnError ( txClient , err , "deleting older community blocklist decisions" )
337+ return err
348338 }
349-
350339 deleted += deletedDecisions
340+ return nil
341+ }); err != nil {
342+ return 0 , 0 , 0 , rollbackOnError (txClient , err , "deleting older community blocklist decisions" )
351343 }
352344
353- builderChunks := slicetools . Chunks ( decisionBuilders , c . decisionBulkSize )
345+ // Insert new decisions
354346
355- for _ , builderChunk := range builderChunks {
356- insertedDecisions , err := txClient .Decision .CreateBulk (builderChunk ... ).Save (ctx )
347+ if err := slicetools . Batch ( ctx , decisionBuilders , c . decisionBulkSize , func ( ctx context. Context , b [] * ent. DecisionCreate ) error {
348+ insertedDecisions , err := txClient .Decision .CreateBulk (b ... ).Save (ctx )
357349 if err != nil {
358- return 0 , 0 , 0 , rollbackOnError ( txClient , err , "bulk creating decisions" )
350+ return err
359351 }
360-
361352 inserted += len (insertedDecisions )
353+ return nil
354+ }); err != nil {
355+ return 0 , 0 , 0 , rollbackOnError (txClient , err , "bulk creating decisions" )
362356 }
363357
364358 log .Debugf ("deleted %d decisions for %s vs %s" , deleted , decOrigin , * alertItem .Decisions [0 ].Origin )
@@ -371,7 +365,7 @@ func (c *Client) UpdateCommunityBlocklist(ctx context.Context, alertItem *models
371365 return alertRef .ID , inserted , deleted , nil
372366}
373367
374- func (c * Client ) createDecisionChunk (ctx context.Context , simulated bool , stopAtTime time.Time , decisions []* models.Decision ) ([]* ent.Decision , error ) {
368+ func (c * Client ) createDecisionBatch (ctx context.Context , simulated bool , stopAtTime time.Time , decisions []* models.Decision ) ([]* ent.Decision , error ) {
375369 decisionCreate := []* ent.DecisionCreate {}
376370
377371 for _ , decisionItem := range decisions {
@@ -548,15 +542,15 @@ func buildMetaCreates(ctx context.Context, logger log.FieldLogger, client *ent.C
548542
549543func buildDecisions (ctx context.Context , logger log.FieldLogger , client * Client , alertItem * models.Alert , stopAtTime time.Time ) ([]* ent.Decision , int , error ) {
550544 decisions := []* ent.Decision {}
551-
552- decisionChunks := slicetools .Chunks (alertItem .Decisions , client .decisionBulkSize )
553- for _ , decisionChunk := range decisionChunks {
554- decisionRet , err := client .createDecisionChunk (ctx , * alertItem .Simulated , stopAtTime , decisionChunk )
545+ if err := slicetools .Batch (ctx , alertItem .Decisions , client .decisionBulkSize , func (ctx context.Context , part []* models.Decision ) error {
546+ ret , err := client .createDecisionBatch (ctx , * alertItem .Simulated , stopAtTime , part )
555547 if err != nil {
556- return nil , 0 , fmt .Errorf ("creating alert decisions: %w" , err )
548+ return fmt .Errorf ("creating alert decisions: %w" , err )
557549 }
558-
559- decisions = append (decisions , decisionRet ... )
550+ decisions = append (decisions , ret ... )
551+ return nil
552+ }); err != nil {
553+ return nil , 0 , err
560554 }
561555
562556 discarded := len (alertItem .Decisions ) - len (decisions )
@@ -617,15 +611,13 @@ func saveAlerts(ctx context.Context, c *Client, batch []alertCreatePlan) ([]stri
617611 continue
618612 }
619613
620- decisionsChunk := slicetools .Chunks (d , c .decisionBulkSize )
621-
622- for _ , d2 := range decisionsChunk {
623- if err := retryOnBusy (func () error {
614+ if err := slicetools .Batch (ctx , d , c .decisionBulkSize , func (ctx context.Context , d2 []* ent.Decision ) error {
615+ return retryOnBusy (func () error {
624616 _ , err := c .Ent .Alert .Update ().Where (alert .IDEQ (a .ID )).AddDecisions (d2 ... ).Save (ctx )
625617 return err
626- }); err != nil {
627- return nil , fmt . Errorf ( "attach decisions to alert %d: %w" , a . ID , err )
628- }
618+ })
619+ }); err != nil {
620+ return nil , fmt . Errorf ( "attach decisions to alert %d: %w" , a . ID , err )
629621 }
630622 }
631623
@@ -637,7 +629,7 @@ type alertCreatePlan struct {
637629 decisions []* ent.Decision
638630}
639631
640- func (c * Client ) createAlertChunk (ctx context.Context , machineID string , owner * ent.Machine , alerts []* models.Alert ) ([]string , error ) {
632+ func (c * Client ) createAlertBatch (ctx context.Context , machineID string , owner * ent.Machine , alerts []* models.Alert ) ([]string , error ) {
641633 batch := make ([]alertCreatePlan , 0 , len (alerts ))
642634
643635 for _ , alertItem := range alerts {
@@ -737,16 +729,16 @@ func (c *Client) CreateAlert(ctx context.Context, machineID string, alertList []
737729
738730 c .Log .Debugf ("writing %d items" , len (alertList ))
739731
740- alertChunks := slicetools .Chunks (alertList , alertCreateBulkSize )
741732 alertIDs := []string {}
742-
743- for _ , alertChunk := range alertChunks {
744- ids , err := c .createAlertChunk (ctx , machineID , owner , alertChunk )
733+ if err := slicetools .Batch (ctx , alertList , alertCreateBulkSize , func (ctx context.Context , part []* models.Alert ) error {
734+ ids , err := c .createAlertBatch (ctx , machineID , owner , part )
745735 if err != nil {
746- return nil , fmt .Errorf ("machine '%s' : %w" , machineID , err )
736+ return fmt .Errorf ("machine %q : %w" , machineID , err )
747737 }
748-
749738 alertIDs = append (alertIDs , ids ... )
739+ return nil
740+ }); err != nil {
741+ return nil , err
750742 }
751743
752744 if owner != nil {
0 commit comments