Skip to content

Commit

Permalink
Support using path_nodes and path_times when set
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Dec 4, 2024
1 parent 1e15071 commit f0cb122
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
4 changes: 2 additions & 2 deletions archives/archives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ func TestCreateRunArchive(t *testing.T) {

// should have two record
assert.Equal(t, 2, task.RecordCount)
assert.Equal(t, int64(458), task.Size)
assert.Equal(t, "7220a13c19f5b6065e7d4c419c114635", task.Hash)
assert.Equal(t, int64(457), task.Size)
assert.Equal(t, "c283214529adb0666655f3a6b03a4992", task.Hash)
assertArchiveFile(t, task, "runs1.jsonl")

DeleteArchiveFile(task)
Expand Down
19 changes: 15 additions & 4 deletions archives/runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,21 @@ FROM (
row_to_json(flow_struct) AS flow,
row_to_json(contact_struct) AS contact,
fr.responded,
(SELECT coalesce(jsonb_agg(path_data), '[]'::jsonb) from (
SELECT path_row ->> 'node_uuid' AS node, (path_row ->> 'arrived_on')::timestamptz as time
FROM jsonb_array_elements(fr.path::jsonb) AS path_row LIMIT 500) as path_data
) as path,
(SELECT CASE
WHEN (fr.path_nodes IS NOT NULL AND fr.path_times IS NOT NULL)
THEN (
SELECT coalesce(jsonb_agg(path_data), '[]'::jsonb)
FROM (
SELECT node, time
FROM unnest(fr.path_nodes::text[] , fr.path_times::timestamptz[]) x(node, time) LIMIT 500)
as path_data)
ELSE (
SELECT coalesce(jsonb_agg(path_data), '[]'::jsonb)
FROM (
SELECT path_row ->> 'node_uuid' AS node, (path_row ->> 'arrived_on')::timestamptz as time
FROM jsonb_array_elements(fr.path::jsonb) AS path_row LIMIT 500)
as path_data)
END as path),
(SELECT coalesce(jsonb_object_agg(values_data.key, values_data.value), '{}'::jsonb) from (
SELECT key, jsonb_build_object('name', value -> 'name', 'value', value -> 'value', 'input', value -> 'input', 'time', (value -> 'created_on')::text::timestamptz, 'category', value -> 'category', 'node', value -> 'node_uuid') as value
FROM jsonb_each(fr.results::jsonb)) AS values_data
Expand Down
2 changes: 1 addition & 1 deletion archives/testdata/runs1.jsonl
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{"id":1,"uuid":"4ced1260-9cfe-4b7f-81dd-b637108f15b9","flow":{"uuid":"6639286a-9120-45d4-aa39-03ae3942a4a6","name":"Flow 1"},"contact":{"uuid":"3e814add-e614-41f7-8b5d-a07f670a698f","name":"Ajodinabiff Dane"},"responded":true,"path":[],"values":{},"created_on":"2017-08-12T19:11:59.890662+00:00","modified_on":"2017-08-12T19:11:59.890662+00:00","exited_on":"2017-08-12T19:11:59.890662+00:00","exit_type":"completed"}
{"id":2,"uuid":"7d68469c-0494-498a-bdf3-bac68321fd6d","flow":{"uuid":"6639286a-9120-45d4-aa39-03ae3942a4a6","name":"Flow 1"},"contact":{"uuid":"3e814add-e614-41f7-8b5d-a07f670a698f","name":"Ajodinabiff Dane"},"responded":true,"path":[{"node": "10896d63-8df7-4022-88dd-a9d93edf355b", "time": "2017-08-12T13:07:24.049815+00:00"}],"values":{"agree": {"name": "Do you agree?", "node": "a0434c54-3e26-4eb0-bafc-46cdeaf435ac", "time": "2017-05-03T12:25:21.714339+00:00", "input": "A", "value": "A", "category": "Strongly agree"}},"created_on":"2017-08-12T19:11:59.890662+00:00","modified_on":"2017-08-12T19:11:59.890662+00:00","exited_on":"2017-08-12T19:11:59.890662+00:00","exit_type":"completed"}
{"id":2,"uuid":"7d68469c-0494-498a-bdf3-bac68321fd6d","flow":{"uuid":"6639286a-9120-45d4-aa39-03ae3942a4a6","name":"Flow 1"},"contact":{"uuid":"3e814add-e614-41f7-8b5d-a07f670a698f","name":"Ajodinabiff Dane"},"responded":true,"path":[{"node": "d1a55403-83a3-42f1-b24c-6446bb18e6a6", "time": "2017-08-12T13:07:25.049815+00:00"}],"values":{"agree": {"name": "Do you agree?", "node": "a0434c54-3e26-4eb0-bafc-46cdeaf435ac", "time": "2017-05-03T12:25:21.714339+00:00", "input": "A", "value": "A", "category": "Strongly agree"}},"created_on":"2017-08-12T19:11:59.890662+00:00","modified_on":"2017-08-12T19:11:59.890662+00:00","exited_on":"2017-08-12T19:11:59.890662+00:00","exit_type":"completed"}
2 changes: 1 addition & 1 deletion testdb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ INSERT INTO flows_flowrun(id, uuid, org_id, responded, contact_id, flow_id, resu
(2, '7d68469c-0494-498a-bdf3-bac68321fd6d', 2, TRUE, 6, 1,
'{"agree": {"category": "Strongly agree", "node_uuid": "a0434c54-3e26-4eb0-bafc-46cdeaf435ac", "name": "Do you agree?", "value": "A", "created_on": "2017-05-03T12:25:21.714339+00:00", "input": "A"}}',
'[{"uuid": "c3d0b417-db75-417c-8050-33776ec8f620", "node_uuid": "10896d63-8df7-4022-88dd-a9d93edf355b", "arrived_on": "2017-08-12T15:07:24.049815+02:00", "exit_uuid": "2f890507-2ad2-4bd1-92fc-0ca031155fca"}]',
'{"10896d63-8df7-4022-88dd-a9d93edf355b"}', '{"2017-08-12T15:07:24.049815+02:00"}', '2017-08-12 21:11:59.890662+02:00','2017-08-12 21:11:59.890662+02:00','2017-08-12 21:11:59.890662+02:00', 'C', NULL),
'{"d1a55403-83a3-42f1-b24c-6446bb18e6a6"}', '{"2017-08-12T15:07:25.049815+02:00"}', '2017-08-12 21:11:59.890662+02:00','2017-08-12 21:11:59.890662+02:00','2017-08-12 21:11:59.890662+02:00', 'C', NULL),
(3, 'de782b35-a398-46ed-8550-34c66053841b', 3, TRUE, 7, 2,
'{"agree": {"category": "Strongly agree", "node_uuid": "084c8cf1-715d-4d0a-b38d-a616ed74e638", "name": "Agree", "value": "A", "created_on": "2017-05-03T12:25:21.714339+00:00", "input": "A"}, "confirm_agree": {"category": "Confirmed Strongly agree", "node_uuid": "a0434c54-3e26-4eb0-bafc-46cdeaf435ab", "name": "Do you agree?", "value": "A", "created_on": "2017-05-03T12:25:21.714339+00:00", "input": "A"}}',
'[{"uuid": "600ac5b4-4895-4161-ad97-6e2f1bb48bcb", "node_uuid": "accbc6e2-b0df-46cd-9a76-bff0fdf4d753", "arrived_on": "2017-08-12T15:07:24.049815+02:00", "exit_uuid": "8249e2dc-c893-4200-b6d2-398d07a459bc"}]',
Expand Down

0 comments on commit f0cb122

Please sign in to comment.