| 720 | } |
| 721 | |
| 722 | func (c *Client) CreateAlert(ctx context.Context, machineID string, alertList []*models.Alert) ([]string, error) { |
| 723 | var ( |
| 724 | owner *ent.Machine |
| 725 | err error |
| 726 | ) |
| 727 | |
| 728 | if machineID != "" { |
| 729 | owner, err = c.QueryMachineByID(ctx, machineID) |
| 730 | if err != nil { |
| 731 | if !errors.Is(err, UserNotExists) { |
| 732 | return nil, fmt.Errorf("machine '%s': %w", machineID, err) |
| 733 | } |
| 734 | |
| 735 | c.Log.Debugf("creating alert: machine %s doesn't exist", machineID) |
| 736 | |
| 737 | owner = nil |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | c.Log.Debugf("writing %d items", len(alertList)) |
| 742 | |
| 743 | alertIDs := []string{} |
| 744 | if err := slicetools.Batch(ctx, alertList, alertCreateBulkSize, func(ctx context.Context, part []*models.Alert) error { |
| 745 | ids, err := c.createAlertBatch(ctx, machineID, owner, part) |
| 746 | if err != nil { |
| 747 | return fmt.Errorf("machine %q: %w", machineID, err) |
| 748 | } |
| 749 | alertIDs = append(alertIDs, ids...) |
| 750 | return nil |
| 751 | }); err != nil { |
| 752 | return nil, err |
| 753 | } |
| 754 | |
| 755 | if owner != nil { |
| 756 | err = owner.Update().SetLastPush(time.Now().UTC()).Exec(ctx) |
| 757 | if err != nil { |
| 758 | return nil, fmt.Errorf("machine '%s': %w", machineID, err) |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | return alertIDs, nil |
| 763 | } |
| 764 | |
| 765 | func (c *Client) AlertsCountPerScenario(ctx context.Context, filter map[string][]string) (map[string]int, error) { |
| 766 | var res []struct { |