Skip to content

Commit

Permalink
Add query inspection metadata to preview_start endpoint response
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Apr 21, 2022
1 parent 1d00902 commit e52be7a
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 10 deletions.
27 changes: 21 additions & 6 deletions web/flow/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/nyaruka/gocommon/urns"
"github.com/nyaruka/goflow/assets"
"github.com/nyaruka/goflow/contactql"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/utils"
"github.com/nyaruka/mailroom/core/models"
Expand Down Expand Up @@ -40,7 +41,13 @@ func init() {
// {
// "query": "(group = "No Age" OR group = "No Name" OR uuid = "e5bb9e6f-7703-4ba1-afba-0b12791de38b" OR tel = "+1234567890") AND history != \"Registration\"",
// "count": 567,
// "sample": [12, 34, 56, 67, 78]
// "sample": [12, 34, 56, 67, 78],
// "metadata": {
// "fields": [
// {"key": "age", "name": "Age"}
// ],
// "allow_as_group": true
// }
// }
//
type previewStartRequest struct {
Expand All @@ -55,9 +62,10 @@ type previewStartRequest struct {
}

type previewStartResponse struct {
Query string `json:"query"`
Count int `json:"count"`
Sample []models.ContactID `json:"sample"`
Query string `json:"query"`
Count int `json:"count"`
Sample []models.ContactID `json:"sample"`
Metadata *contactql.Inspection `json:"metadata,omitempty"`
}

func handlePreviewStart(ctx context.Context, rt *runtime.Runtime, r *http.Request) (interface{}, int, error) {
Expand Down Expand Up @@ -86,13 +94,20 @@ func handlePreviewStart(ctx context.Context, rt *runtime.Runtime, r *http.Reques

query := search.BuildStartQuery(oa.Env(), flow, groups, request.ContactUUIDs, request.URNs, request.Query, request.Exclusions)
if query == "" {
return &previewStartResponse{Query: "", Count: 0, Sample: []models.ContactID{}}, http.StatusOK, nil
return &previewStartResponse{Sample: []models.ContactID{}}, http.StatusOK, nil
}

parsedQuery, sampleIDs, count, err := search.GetContactIDsForQueryPage(ctx, rt.ES, oa, nil, nil, query, "", 0, request.SampleSize)
if err != nil {
return nil, http.StatusInternalServerError, errors.Wrapf(err, "error querying preview")
}

return &previewStartResponse{Query: parsedQuery.String(), Count: int(count), Sample: sampleIDs}, http.StatusOK, nil
inspection := contactql.Inspect(parsedQuery)

return &previewStartResponse{
Query: parsedQuery.String(),
Count: int(count),
Sample: sampleIDs,
Metadata: inspection,
}, http.StatusOK, nil
}
85 changes: 81 additions & 4 deletions web/flow/testdata/preview_start.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,29 @@
"count": 1,
"sample": [
10000
]
],
"metadata": {
"attributes": [
"group",
"uuid"
],
"fields": [],
"groups": [
{
"name": "Doctors",
"uuid": "c153e265-f7c9-4539-9dbc-9b358714b638"
},
{
"name": "Testers",
"uuid": "5e9d8fab-5e7e-4f51-b533-261af5dea70d"
}
],
"schemes": [
"facebook",
"tel"
],
"allow_as_group": false
}
}
},
{
Expand All @@ -84,7 +106,19 @@
"count": 1,
"sample": [
10001
]
],
"metadata": {
"attributes": [],
"fields": [
{
"key": "gender",
"name": "Gender"
}
],
"groups": [],
"schemes": [],
"allow_as_group": true
}
}
},
{
Expand Down Expand Up @@ -121,7 +155,33 @@
"count": 1,
"sample": [
10002
]
],
"metadata": {
"attributes": [
"flow",
"group",
"history",
"last_seen_on",
"status",
"uuid"
],
"fields": [],
"groups": [
{
"name": "Doctors",
"uuid": "c153e265-f7c9-4539-9dbc-9b358714b638"
},
{
"name": "Testers",
"uuid": "5e9d8fab-5e7e-4f51-b533-261af5dea70d"
}
],
"schemes": [
"facebook",
"tel"
],
"allow_as_group": false
}
}
},
{
Expand All @@ -146,7 +206,24 @@
"count": 1,
"sample": [
10003
]
],
"metadata": {
"attributes": [
"flow",
"history",
"last_seen_on",
"status"
],
"fields": [
{
"key": "gender",
"name": "Gender"
}
],
"groups": [],
"schemes": [],
"allow_as_group": false
}
}
}
]

0 comments on commit e52be7a

Please sign in to comment.