fix create alert bulk for decisions insertion (#1107)

* fix create alert bulk for decisions insertion
This commit is contained in:
AlteredCoder 2021-12-16 18:26:19 +01:00 committed by GitHub
parent ec53fbfdab
commit d913ac160e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -296,7 +296,6 @@ func chunkDecisions(decisions []*ent.Decision, chunkSize int) [][]*ent.Decision
}
func (c *Client) CreateAlertBulk(machineId string, alertList []*models.Alert) ([]string, error) {
ret := []string{}
bulkSize := 20
@ -504,9 +503,9 @@ func (c *Client) CreateAlertBulk(machineId string, alertList []*models.Alert) ([
if err != nil {
return []string{}, errors.Wrapf(BulkError, "bulk creating alert : %s", err)
}
for _, a := range alerts {
for alertIndex, a := range alerts {
ret = append(ret, strconv.Itoa(a.ID))
for _, d := range alertDecisions {
d := alertDecisions[alertIndex]
decisionsChunk := chunkDecisions(d, bulkSize)
for _, d2 := range decisionsChunk {
_, err := c.Ent.Alert.Update().Where(alert.IDEQ(a.ID)).AddDecisions(d2...).Save(c.CTX)
@ -515,7 +514,6 @@ func (c *Client) CreateAlertBulk(machineId string, alertList []*models.Alert) ([
}
}
}
}
if len(alertList)-i <= bulkSize {
bulk = make([]*ent.AlertCreate, 0, (len(alertList) - i))
alertDecisions = make([][]*ent.Decision, 0, (len(alertList) - i))
@ -531,9 +529,9 @@ func (c *Client) CreateAlertBulk(machineId string, alertList []*models.Alert) ([
return []string{}, errors.Wrapf(BulkError, "leftovers creating alert : %s", err)
}
for _, a := range alerts {
for alertIndex, a := range alerts {
ret = append(ret, strconv.Itoa(a.ID))
for _, d := range alertDecisions {
d := alertDecisions[alertIndex]
decisionsChunk := chunkDecisions(d, bulkSize)
for _, d2 := range decisionsChunk {
_, err := c.Ent.Alert.Update().Where(alert.IDEQ(a.ID)).AddDecisions(d2...).Save(c.CTX)
@ -542,7 +540,6 @@ func (c *Client) CreateAlertBulk(machineId string, alertList []*models.Alert) ([
}
}
}
}
return ret, nil
}