Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track history of flow ids on contacts #39

Merged
merged 8 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions contacts/index_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@
"flow": {
"type": "keyword"
},
"flow_id": {
"type": "integer"
},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems preferable to index a list of integers rather than UUIDs... and if we have to know about flow ids, why not switch flow over to being id based as well

"flow_history": {
"type": "integer"
},
"tickets": {
"type": "integer"
},
Expand Down
9 changes: 7 additions & 2 deletions contacts/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,16 @@ SELECT org_id, id, modified_on, is_active, row_to_json(t) FROM (
) AS groups,
(
SELECT f.uuid FROM flows_flow f WHERE f.id = contacts_contact.current_flow_id
) AS flow
) AS flow,
current_flow_id AS flow_id,
(
SELECT array_to_json(array_agg(DISTINCT fr.flow_id))
FROM flows_flowrun fr WHERE fr.contact_id = contacts_contact.id
) AS flow_history
FROM contacts_contact
WHERE modified_on >= $1
ORDER BY modified_on ASC
LIMIT 500000
LIMIT 100000
) t;
`

Expand Down
6 changes: 6 additions & 0 deletions indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ func TestIndexing(t *testing.T) {
assertQuery(t, client, physicalName, elastic.NewMatchQuery("flow", "6d3cf1eb-546e-4fb8-a5ca-69187648fbf6"), []int64{2, 3})
assertQuery(t, client, physicalName, elastic.NewMatchQuery("flow", "4eea8ff1-4fe2-4ce5-92a4-0870a499973a"), []int64{4})

assertQuery(t, client, physicalName, elastic.NewMatchQuery("flow_id", 1), []int64{2, 3})
assertQuery(t, client, physicalName, elastic.NewMatchQuery("flow_id", 2), []int64{4})

assertQuery(t, client, physicalName, elastic.NewMatchQuery("flow_history", 1), []int64{1, 2})
assertQuery(t, client, physicalName, elastic.NewMatchQuery("flow_history", 2), []int64{1})

// created_on range query
assertQuery(t, client, physicalName, elastic.NewRangeQuery("created_on").Gt("2017-01-01"), []int64{1, 6, 8})

Expand Down
15 changes: 15 additions & 0 deletions testdb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ CREATE TABLE contacts_contactgroup_contacts (
contact_id integer NOT NULL REFERENCES contacts_contact(id)
);


DROP TABLE IF EXISTS flows_flowrun CASCADE;
CREATE TABLE flows_flowrun (
id SERIAL PRIMARY KEY,
uuid character varying(36) NOT NULL,
flow_id integer REFERENCES flows_flow(id),
contact_id integer REFERENCES contacts_contact(id)
);

INSERT INTO flows_flow(id, uuid, name) VALUES
(1, '6d3cf1eb-546e-4fb8-a5ca-69187648fbf6', 'Favorites'),
(2, '4eea8ff1-4fe2-4ce5-92a4-0870a499973a', 'Catch All');
Expand Down Expand Up @@ -152,3 +161,9 @@ INSERT INTO contacts_contactgroup_contacts(id, contact_id, contactgroup_id) VALU
(1, 1, 1),
(2, 1, 4),
(3, 2, 4);

INSERT INTO flows_flowrun(id, uuid, flow_id, contact_id) VALUES
(1, '8b30ee61-e19d-427e-bb9f-4b8cd2c31d0c', 1, 1),
(2, '94639979-155e-444d-95e9-a39dad64dbd5', 1, 1),
(3, '74d918df-0e31-4547-98a9-5d765450e2ac', 2, 1),
(4, '14fdf8fc-6e02-4759-b9be-cacc5991cd14', 1, 2);