Skip to content

Commit

Permalink
Merge pull request rapidpro#468 from nyaruka/parse_only
Browse files Browse the repository at this point in the history
🔍 Parse search queries without asset checking
  • Loading branch information
rowanseymour authored Jul 30, 2021
2 parents a9293b9 + 4071ce0 commit 2b9bd2a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 49 deletions.
5 changes: 1 addition & 4 deletions core/models/contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -917,10 +917,7 @@ func URNForID(ctx context.Context, db Queryer, org *OrgAssets, urnID URNID) (urn
// CalculateDynamicGroups recalculates all the dynamic groups for the passed in contact, recalculating
// campaigns as necessary based on those group changes.
func CalculateDynamicGroups(ctx context.Context, db Queryer, org *OrgAssets, contact *flows.Contact) error {
added, removed, errs := contact.ReevaluateQueryBasedGroups(org.Env())
if len(errs) > 0 {
return errors.Wrapf(errs[0], "error calculating dynamic groups")
}
added, removed := contact.ReevaluateQueryBasedGroups(org.Env())

campaigns := make(map[CampaignID]*Campaign)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/mattn/go-sqlite3 v1.10.0 // indirect
github.com/nyaruka/ezconf v0.2.1
github.com/nyaruka/gocommon v1.13.0
github.com/nyaruka/goflow v0.128.0
github.com/nyaruka/goflow v0.130.1
github.com/nyaruka/librato v1.0.0
github.com/nyaruka/logrus_sentry v0.8.2-0.20190129182604-c2962b80ba7d
github.com/nyaruka/null v1.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ github.com/nyaruka/ezconf v0.2.1 h1:TDXWoqjqYya1uhou1mAJZg7rgFYL98EB0Tb3+BWtUh0=
github.com/nyaruka/ezconf v0.2.1/go.mod h1:ey182kYkw2MIi4XiWe1FR/mzI33WCmTWuceDYYxgnQw=
github.com/nyaruka/gocommon v1.13.0 h1:WPL//ekajA30KinYRr6IrdP1igNZpcUAfABleHCuxPQ=
github.com/nyaruka/gocommon v1.13.0/go.mod h1:Jn7UIE8zwIr4JaviDf4PZrrQlN8r6QGVhOuaF/JoKus=
github.com/nyaruka/goflow v0.128.0 h1:BQ6JhtJp6TCcMb5ZwEfyz8nvAwmpGgJgjToWYM7L6xk=
github.com/nyaruka/goflow v0.128.0/go.mod h1:Xp1p21TyYiMM/fVQNWQRok/fZ1ZeNoeQGUd//LvYxq4=
github.com/nyaruka/goflow v0.130.1 h1:ai84idJkjgn2XJdv735/4qz72L0AuOYtEX8+GpL4xs0=
github.com/nyaruka/goflow v0.130.1/go.mod h1:Xp1p21TyYiMM/fVQNWQRok/fZ1ZeNoeQGUd//LvYxq4=
github.com/nyaruka/librato v1.0.0 h1:Vznj9WCeC1yZXbBYyYp40KnbmXLbEkjKmHesV/v2SR0=
github.com/nyaruka/librato v1.0.0/go.mod h1:pkRNLFhFurOz0QqBz6/DuTFhHHxAubWxs4Jx+J7yUgg=
github.com/nyaruka/logrus_sentry v0.8.2-0.20190129182604-c2962b80ba7d h1:hyp9u36KIwbTCo2JAJ+TuJcJBc+UZzEig7RI/S5Dvkc=
Expand Down
42 changes: 16 additions & 26 deletions web/contact/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func handleSearch(ctx context.Context, rt *runtime.Runtime, r *http.Request) (in
type parseRequest struct {
OrgID models.OrgID `json:"org_id" validate:"required"`
Query string `json:"query" validate:"required"`
ParseOnly bool `json:"parse_only"`
GroupUUID assets.GroupUUID `json:"group_uuid"`
}

Expand All @@ -155,10 +156,6 @@ type parseResponse struct {
Query string `json:"query"`
ElasticQuery interface{} `json:"elastic_query"`
Metadata *contactql.Inspection `json:"metadata,omitempty"`

// deprecated
Fields []string `json:"fields"`
AllowAsGroup bool `json:"allow_as_group"`
}

// handles a query parsing request
Expand All @@ -175,8 +172,12 @@ func handleParseQuery(ctx context.Context, rt *runtime.Runtime, r *http.Request)
}

env := oa.Env()
parsed, err := contactql.ParseQuery(env, request.Query, oa.SessionAssets())
var resolver contactql.Resolver
if !request.ParseOnly {
resolver = oa.SessionAssets()
}

parsed, err := contactql.ParseQuery(env, request.Query, resolver)
if err != nil {
isQueryError, qerr := contactql.IsQueryError(err)
if isQueryError {
Expand All @@ -186,34 +187,23 @@ func handleParseQuery(ctx context.Context, rt *runtime.Runtime, r *http.Request)
}

// normalize and inspect the query
normalized := ""
var metadata *contactql.Inspection
allowAsGroup := false
fields := make([]string, 0)

if parsed != nil {
normalized = parsed.String()
metadata = contactql.Inspect(parsed)
fields = append(fields, metadata.Attributes...)
for _, f := range metadata.Fields {
fields = append(fields, f.Key)
normalized := parsed.String()
metadata := contactql.Inspect(parsed)

var elasticSource interface{}
if !request.ParseOnly {
eq := models.BuildElasticQuery(oa, request.GroupUUID, models.NilContactStatus, nil, parsed)
elasticSource, err = eq.Source()
if err != nil {
return nil, http.StatusInternalServerError, errors.Wrap(err, "error getting elastic source")
}
allowAsGroup = metadata.AllowAsGroup
}

eq := models.BuildElasticQuery(oa, request.GroupUUID, models.NilContactStatus, nil, parsed)
eqj, err := eq.Source()
if err != nil {
return nil, http.StatusInternalServerError, err
}

// build our response
response := &parseResponse{
Query: normalized,
ElasticQuery: eqj,
ElasticQuery: elasticSource,
Metadata: metadata,
Fields: fields,
AllowAsGroup: allowAsGroup,
}

return response, http.StatusOK, nil
Expand Down
2 changes: 1 addition & 1 deletion web/contact/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func TestSearch(t *testing.T) {
}
}

func TestParse(t *testing.T) {
func TestParseQuery(t *testing.T) {
testsuite.Reset()

web.RunWebTests(t, "testdata/parse_query.json", nil)
Expand Down
47 changes: 32 additions & 15 deletions web/contact/testdata/parse_query.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,35 @@
}
}
},
{
"label": "query with invalid property but parse_only = true",
"method": "POST",
"path": "/mr/contact/parse_query",
"body": {
"org_id": 1,
"query": "birthday = tomorrow AND tel = 12345",
"parse_only": true
},
"status": 200,
"response": {
"query": "birthday = \"tomorrow\" AND tel = 12345",
"elastic_query": null,
"metadata": {
"attributes": [],
"schemes": [
"tel"
],
"fields": [
{
"key": "birthday",
"name": ""
}
],
"groups": [],
"allow_as_group": true
}
}
},
{
"label": "valid query without group",
"method": "POST",
Expand Down Expand Up @@ -107,11 +136,7 @@
],
"groups": [],
"allow_as_group": true
},
"fields": [
"age"
],
"allow_as_group": true
}
}
},
{
Expand Down Expand Up @@ -184,11 +209,7 @@
],
"groups": [],
"allow_as_group": true
},
"fields": [
"age"
],
"allow_as_group": true
}
}
},
{
Expand Down Expand Up @@ -236,11 +257,7 @@
}
],
"allow_as_group": false
},
"fields": [
"group"
],
"allow_as_group": false
}
}
}
]

0 comments on commit 2b9bd2a

Please sign in to comment.