From 04d8945b8628246efb95d642d50b5911e69c427d Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Tue, 22 Mar 2022 14:58:33 -0500 Subject: [PATCH 1/3] Track history of flow ids on contacts --- contacts/index_settings.json | 3 +++ contacts/query.go | 8 ++++++-- indexer_test.go | 3 +++ testdb.sql | 15 +++++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/contacts/index_settings.json b/contacts/index_settings.json index 75e8728..66db7aa 100644 --- a/contacts/index_settings.json +++ b/contacts/index_settings.json @@ -153,6 +153,9 @@ "flow": { "type": "keyword" }, + "flow_history": { + "type": "integer" + }, "tickets": { "type": "integer" }, diff --git a/contacts/query.go b/contacts/query.go index d909a02..50dee78 100644 --- a/contacts/query.go +++ b/contacts/query.go @@ -61,11 +61,15 @@ 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, + ( + 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; ` diff --git a/indexer_test.go b/indexer_test.go index 1a5af69..8957363 100644 --- a/indexer_test.go +++ b/indexer_test.go @@ -104,6 +104,9 @@ 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_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}) diff --git a/testdb.sql b/testdb.sql index 845d627..46ec484 100644 --- a/testdb.sql +++ b/testdb.sql @@ -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'); @@ -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); From 28427467a54924eebae007b2729d3b2428a3d72b Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Tue, 22 Mar 2022 15:51:02 -0500 Subject: [PATCH 2/3] Add flow_id to index as well since we need to switch flows to use ids anyway --- contacts/index_settings.json | 3 +++ contacts/query.go | 1 + indexer_test.go | 3 +++ 3 files changed, 7 insertions(+) diff --git a/contacts/index_settings.json b/contacts/index_settings.json index 66db7aa..5061272 100644 --- a/contacts/index_settings.json +++ b/contacts/index_settings.json @@ -153,6 +153,9 @@ "flow": { "type": "keyword" }, + "flow_id": { + "type": "integer" + }, "flow_history": { "type": "integer" }, diff --git a/contacts/query.go b/contacts/query.go index 50dee78..6f18c61 100644 --- a/contacts/query.go +++ b/contacts/query.go @@ -62,6 +62,7 @@ SELECT org_id, id, modified_on, is_active, row_to_json(t) FROM ( ( SELECT f.uuid FROM flows_flow f WHERE f.id = contacts_contact.current_flow_id ) 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 diff --git a/indexer_test.go b/indexer_test.go index 8957363..cfb0c72 100644 --- a/indexer_test.go +++ b/indexer_test.go @@ -104,6 +104,9 @@ 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}) From bace1d5dda447753f4d448d806967284852053c9 Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Thu, 31 Mar 2022 11:30:25 -0500 Subject: [PATCH 3/3] Tweak new field name, add tests --- indexers/contacts.go | 5 ++--- indexers/contacts.settings.json | 2 +- indexers/contacts_test.go | 2 ++ testdb.sql | 4 +++- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/indexers/contacts.go b/indexers/contacts.go index 75259f3..c6f2d08 100644 --- a/indexers/contacts.go +++ b/indexers/contacts.go @@ -150,9 +150,8 @@ SELECT org_id, id, modified_on, is_active, row_to_json(t) FROM ( ) 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 + SELECT array_to_json(array_agg(DISTINCT fr.flow_id)) FROM flows_flowrun fr WHERE fr.contact_id = contacts_contact.id + ) AS flow_history_ids FROM contacts_contact WHERE modified_on >= $1 ORDER BY modified_on ASC diff --git a/indexers/contacts.settings.json b/indexers/contacts.settings.json index 5061272..7a76a84 100644 --- a/indexers/contacts.settings.json +++ b/indexers/contacts.settings.json @@ -156,7 +156,7 @@ "flow_id": { "type": "integer" }, - "flow_history": { + "flow_history_ids": { "type": "integer" }, "tickets": { diff --git a/indexers/contacts_test.go b/indexers/contacts_test.go index 848e417..071b9af 100644 --- a/indexers/contacts_test.go +++ b/indexers/contacts_test.go @@ -32,6 +32,8 @@ var contactQueryTests = []struct { {elastic.NewMatchQuery("flow", "4eea8ff1-4fe2-4ce5-92a4-0870a499973a"), []int64{4}}, {elastic.NewMatchQuery("flow_id", 1), []int64{2, 3}}, {elastic.NewMatchQuery("flow_id", 2), []int64{4}}, + {elastic.NewMatchQuery("flow_history_ids", 1), []int64{1, 2, 3}}, + {elastic.NewMatchQuery("flow_history_ids", 2), []int64{1, 2}}, {elastic.NewRangeQuery("created_on").Gt("2017-01-01"), []int64{1, 6, 8}}, // created_on range {elastic.NewRangeQuery("last_seen_on").Lt("2019-01-01"), []int64{3, 4}}, // last_seen_on range {elastic.NewExistsQuery("last_seen_on"), []int64{1, 2, 3, 4, 5, 6}}, // last_seen_on is set diff --git a/testdb.sql b/testdb.sql index 46ec484..8e72d88 100644 --- a/testdb.sql +++ b/testdb.sql @@ -166,4 +166,6 @@ 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); +(4, '14fdf8fc-6e02-4759-b9be-cacc5991cd14', 1, 2), +(5, '5171b4d8-e9bb-46c1-901e-53e13f3afe5d', 2, 2), +(6, '4cc84e32-910f-41d6-865d-63fe25db4cde', 1, 3);