MCPcopy
hub / github.com/crowdsecurity/crowdsec / CreateAlert

Method CreateAlert

pkg/database/alerts.go:722–763  ·  view source on GitHub ↗
(ctx context.Context, machineID string, alertList []*models.Alert)

Source from the content-addressed store, hash-verified

720}
721
722func (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
765func (c *Client) AlertsCountPerScenario(ctx context.Context, filter map[string][]string) (map[string]int, error) {
766 var res []struct {

Callers 2

CreateOrUpdateAlertMethod · 0.95

Calls 5

QueryMachineByIDMethod · 0.95
createAlertBatchMethod · 0.95
ExecMethod · 0.45
SetLastPushMethod · 0.45
UpdateMethod · 0.45

Tested by 1