Skip to content

Commit

Permalink
Remove no longer used params on parse_query endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed May 15, 2024
1 parent 9af5c61 commit 0da9c0d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 248 deletions.
46 changes: 8 additions & 38 deletions web/contact/parse_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"context"
"net/http"

"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/contactql"
"github.com/nyaruka/mailroom/core/models"
"github.com/nyaruka/mailroom/core/search"
"github.com/nyaruka/mailroom/runtime"
"github.com/nyaruka/mailroom/web"
"github.com/pkg/errors"
Expand All @@ -21,22 +19,18 @@ func init() {
//
// {
// "org_id": 1,
// "query": "age > 10",
// "group_id": 234
// "query": "AGE > 10"
// }
type parseRequest struct {
OrgID models.OrgID `json:"org_id" validate:"required"`
Query string `json:"query" validate:"required"`
ParseOnly bool `json:"parse_only"`
GroupID models.GroupID `json:"group_id"`
GroupUUID assets.GroupUUID `json:"group_uuid"` // deprecated
OrgID models.OrgID `json:"org_id" validate:"required"`
Query string `json:"query" validate:"required"`
ParseOnly bool `json:"parse_only"`
}

// Response for a parse query request
//
// {
// "query": "age > 10",
// "elastic_query": { .. },
// "query": "fields.age > 10",
// "metadata": {
// "fields": [
// {"key": "age", "name": "Age"}
Expand All @@ -45,9 +39,8 @@ type parseRequest struct {
// }
// }
type parseResponse struct {
Query string `json:"query"`
ElasticQuery any `json:"elastic_query"`
Metadata *contactql.Inspection `json:"metadata,omitempty"`
Query string `json:"query"`
Metadata *contactql.Inspection `json:"metadata,omitempty"`
}

// handles a query parsing request
Expand All @@ -57,13 +50,6 @@ func handleParseQuery(ctx context.Context, rt *runtime.Runtime, r *parseRequest)
return nil, 0, errors.Wrapf(err, "unable to load org assets")
}

var group *models.Group
if r.GroupID != 0 {
group = oa.GroupByID(r.GroupID)
} else if r.GroupUUID != "" {
group = oa.GroupByUUID(r.GroupUUID)
}

env := oa.Env()
var resolver contactql.Resolver
if !r.ParseOnly {
Expand All @@ -83,21 +69,5 @@ func handleParseQuery(ctx context.Context, rt *runtime.Runtime, r *parseRequest)
normalized := parsed.String()
metadata := contactql.Inspect(parsed)

var elasticSource any
if !r.ParseOnly {
eq := search.BuildElasticQuery(oa, group, models.NilContactStatus, nil, parsed)
elasticSource, err = eq.Source()
if err != nil {
return nil, 0, errors.Wrap(err, "error getting elastic source")
}
}

// build our response
response := &parseResponse{
Query: normalized,
ElasticQuery: elasticSource,
Metadata: metadata,
}

return response, http.StatusOK, nil
return &parseResponse{Query: normalized, Metadata: metadata}, http.StatusOK, nil
}
210 changes: 0 additions & 210 deletions web/contact/testdata/parse_query.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
"status": 200,
"response": {
"query": "birthday = \"tomorrow\" AND tel = 12345",
"elastic_query": null,
"metadata": {
"attributes": [],
"schemes": [
Expand Down Expand Up @@ -83,194 +82,6 @@
"status": 200,
"response": {
"query": "age > 10",
"elastic_query": {
"bool": {
"must": [
{
"term": {
"org_id": 1
}
},
{
"term": {
"is_active": true
}
},
{
"nested": {
"path": "fields",
"query": {
"bool": {
"must": [
{
"term": {
"fields.field": "903f51da-2717-47c7-a0d3-f2f32877013d"
}
},
{
"range": {
"fields.number": {
"from": 10,
"include_lower": false,
"include_upper": true,
"to": null
}
}
}
]
}
}
}
}
]
}
},
"metadata": {
"attributes": [],
"schemes": [],
"fields": [
{
"key": "age",
"name": "Age"
}
],
"groups": [],
"allow_as_group": true
}
}
},
{
"label": "valid query with group by ID",
"method": "POST",
"path": "/mr/contact/parse_query",
"body": {
"org_id": 1,
"query": "age > 10",
"group_id": 10000
},
"status": 200,
"response": {
"query": "age > 10",
"elastic_query": {
"bool": {
"must": [
{
"term": {
"org_id": 1
}
},
{
"term": {
"is_active": true
}
},
{
"term": {
"group_ids": 10000
}
},
{
"nested": {
"path": "fields",
"query": {
"bool": {
"must": [
{
"term": {
"fields.field": "903f51da-2717-47c7-a0d3-f2f32877013d"
}
},
{
"range": {
"fields.number": {
"from": 10,
"include_lower": false,
"include_upper": true,
"to": null
}
}
}
]
}
}
}
}
]
}
},
"metadata": {
"attributes": [],
"schemes": [],
"fields": [
{
"key": "age",
"name": "Age"
}
],
"groups": [],
"allow_as_group": true
}
}
},
{
"label": "valid query with group by UUID",
"method": "POST",
"path": "/mr/contact/parse_query",
"body": {
"org_id": 1,
"query": "age > 10",
"group_uuid": "c153e265-f7c9-4539-9dbc-9b358714b638"
},
"status": 200,
"response": {
"query": "age > 10",
"elastic_query": {
"bool": {
"must": [
{
"term": {
"org_id": 1
}
},
{
"term": {
"is_active": true
}
},
{
"term": {
"group_ids": 10000
}
},
{
"nested": {
"path": "fields",
"query": {
"bool": {
"must": [
{
"term": {
"fields.field": "903f51da-2717-47c7-a0d3-f2f32877013d"
}
},
{
"range": {
"fields.number": {
"from": 10,
"include_lower": false,
"include_upper": true,
"to": null
}
}
}
]
}
}
}
}
]
}
},
"metadata": {
"attributes": [],
"schemes": [],
Expand All @@ -296,27 +107,6 @@
"status": 200,
"response": {
"query": "group = \"Testers\"",
"elastic_query": {
"bool": {
"must": [
{
"term": {
"org_id": 1
}
},
{
"term": {
"is_active": true
}
},
{
"term": {
"group_ids": 10001
}
}
]
}
},
"metadata": {
"attributes": [
"group"
Expand Down

0 comments on commit 0da9c0d

Please sign in to comment.