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

Method QueryAlertWithFilter

pkg/database/alerts.go:796–890  ·  view source on GitHub ↗
(ctx context.Context, filter map[string][]string)

Source from the content-addressed store, hash-verified

794}
795
796func (c *Client) QueryAlertWithFilter(ctx context.Context, filter map[string][]string) ([]*ent.Alert, error) {
797 sort := "DESC" // we sort by desc by default
798
799 if val, ok := filter["sort"]; ok {
800 if val[0] != "ASC" && val[0] != "DESC" {
801 c.Log.Errorf("invalid 'sort' parameter: %s", val)
802 } else {
803 sort = val[0]
804 }
805 }
806
807 limit := defaultLimit
808
809 if val, ok := filter["limit"]; ok {
810 limitConv, err := strconv.Atoi(val[0])
811 if err != nil {
812 return nil, fmt.Errorf("bad limit in parameters: %s: %w", val, QueryFail)
813 }
814
815 limit = limitConv
816 }
817
818 offset := 0
819 ret := make([]*ent.Alert, 0)
820
821 for {
822 alerts := c.Ent.Alert.Query()
823
824 alerts, err := applyAlertFilter(alerts, filter)
825 if err != nil {
826 return nil, err
827 }
828
829 // only if with_decisions is present and set to false, we exclude this
830 if val, ok := filter["with_decisions"]; ok && val[0] == "false" {
831 c.Log.Debugf("skipping decisions")
832 } else {
833 alerts = alerts.
834 WithDecisions()
835 }
836
837 alerts = alerts.
838 WithEvents().
839 WithMetas().
840 WithOwner()
841
842 if limit == 0 {
843 limit, err = alerts.Count(ctx)
844 if err != nil {
845 return nil, fmt.Errorf("unable to count nb alerts: %w", err)
846 }
847 }
848
849 if sort == "ASC" {
850 alerts = alerts.Order(ent.Asc(alert.FieldCreatedAt), ent.Asc(alert.FieldID))
851 } else {
852 alerts = alerts.Order(ent.Desc(alert.FieldCreatedAt), ent.Desc(alert.FieldID))
853 }

Callers 2

FlushAlertsMethod · 0.95
FindAlertsMethod · 0.80

Calls 13

AscFunction · 0.92
DescFunction · 0.92
applyAlertFilterFunction · 0.85
WithDecisionsMethod · 0.80
WithMetasMethod · 0.80
WithEventsMethod · 0.80
QueryMethod · 0.45
WithOwnerMethod · 0.45
CountMethod · 0.45
OrderMethod · 0.45
AllMethod · 0.45
OffsetMethod · 0.45

Tested by

no test coverage detected