Skip to content

Commit

Permalink
Add filters in completion api (#2224)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramanan-ravi authored Jun 27, 2024
1 parent e8d9c4e commit a3a3725
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions deepfence_server/reporters/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
"github.com/deepfence/ThreatMapper/deepfence_utils/telemetry"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"github.com/rs/zerolog/log"
"github.com/samber/mo"
)

type CompletionNodeFieldReq struct {
Completion string `json:"completion" required:"true"`
FieldName string `json:"field_name" required:"true"`
Window model.FetchWindow `json:"window" required:"true"`
ScanID string `json:"scan_id" required:"false"`
Completion string `json:"completion" required:"true"`
FieldName string `json:"field_name" required:"true"`
Window model.FetchWindow `json:"window" required:"true"`
ScanID string `json:"scan_id" required:"false"` //TODO: deprecate in favor of Filters
Filters *reporters.FieldsFilters `json:"filters" required:"false"`
}

type CompletionNodeFieldRes struct {
Expand Down Expand Up @@ -48,18 +50,25 @@ func FieldValueCompletion[T reporters.Cypherable](ctx context.Context, req Compl

query := ""

filterClauses := mo.None[reporters.FieldsFilters]()
if req.Filters != nil {
filterClauses = mo.Some(*req.Filters)
}

if req.ScanID != "" {
if dummy.NodeType() == "CloudCompliance" {
query = `
MATCH (n{node_id: $scan_id}) -[:DETECTED]-> (r:` + dummy.NodeType() + `)
WHERE r.` + req.FieldName + ` =~ '^` + req.Completion + `.*'
` + reporters.ParseFieldFilters2CypherWhereConditions(`n`, filterClauses, false) + `
RETURN DISTINCT r.` + req.FieldName + `
ORDER BY r.` + req.FieldName +
req.Window.FetchWindow2CypherQuery()
} else {
query = `
MATCH (n{node_id: $scan_id}) -[:DETECTED]-> (m) -[:IS]-> (r:` + dummy.NodeType() + `)
WHERE r.` + req.FieldName + ` =~ '^` + req.Completion + `.*'
` + reporters.ParseFieldFilters2CypherWhereConditions(`n`, filterClauses, false) + `
RETURN DISTINCT r.` + req.FieldName + `
ORDER BY r.` + req.FieldName +
req.Window.FetchWindow2CypherQuery()
Expand All @@ -68,6 +77,7 @@ func FieldValueCompletion[T reporters.Cypherable](ctx context.Context, req Compl
query = `
MATCH (n:` + dummy.NodeType() + `)
WHERE n.` + req.FieldName + ` =~ '^` + req.Completion + `.*'
` + reporters.ParseFieldFilters2CypherWhereConditions(`n`, filterClauses, false) + `
RETURN DISTINCT n.` + req.FieldName + `
ORDER BY n.` + req.FieldName +
req.Window.FetchWindow2CypherQuery()
Expand Down

0 comments on commit a3a3725

Please sign in to comment.