From df8fc301b66758c8b49d6960cfc11d09c01209ed Mon Sep 17 00:00:00 2001 From: Xavier Schildwachter Date: Tue, 27 Feb 2024 16:35:19 -0800 Subject: [PATCH 1/3] Add proc --- .../procs/R2.13.0__add_list_downloaders.sql | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 synapse_data_warehouse/synapse/procs/R2.13.0__add_list_downloaders.sql diff --git a/synapse_data_warehouse/synapse/procs/R2.13.0__add_list_downloaders.sql b/synapse_data_warehouse/synapse/procs/R2.13.0__add_list_downloaders.sql new file mode 100644 index 00000000..48036725 --- /dev/null +++ b/synapse_data_warehouse/synapse/procs/R2.13.0__add_list_downloaders.sql @@ -0,0 +1,42 @@ +USE SCHEMA {{database_name}}.synapse; + +CREATE OR REPLACE PROCEDURE list_downloaders(start_record_date string, entity_list string) +RETURNS TABLE (user_id integer, c integer, min_t timestamp, max_t timestamp) +LANGUAGE SQL +AS +declare + rs resultset; + query_str1 varchar default ( + 'with recursive filetree (parent_id, id, node_type) as ( + select null as parent_id, id, node_type + from node_latest + where id in (' || :entity_list || ') + union all + select fc.parent_id, fc.id, fc.node_type + from node_latest fc + join filetree ft on ft.id = fc.parent_id + ),'); + query_str2 varchar default + 'download (user_id, timestamp, entity_id) as ( + select fd.user_id, fd.timestamp, fd.association_object_id as entity_id + from filedownload fd + join filetree ft on ft.id = fd.association_object_id + where fd.association_object_type = \'FileEntity\' + and ft.node_type = \'file\' + and fd.record_date >= \'||:start_record_date||\' + ),'; + query_str3 varchar default + 'download_summary (user_id, c, min_t, max_t) as ( + select user_id, count(*) as c, min(timestamp) as min_t, max(timestamp) as max_t + from download + group by user_id + )'; + query_str4 varchar default + 'select * + from download_summary + order by min_t, user_id'; + query_str varchar default (:query_str1 || :query_str2 || :query_str3 || :query_str4); +begin + rs := (EXECUTE IMMEDIATE :query_str); + return table(rs); +end; \ No newline at end of file From e5db6477bfff89b7875a898423fd3c5820c6671b Mon Sep 17 00:00:00 2001 From: Xavier Schildwachter Date: Tue, 27 Feb 2024 17:27:53 -0800 Subject: [PATCH 2/3] Fixes --- .../synapse/procs/R2.13.0__add_list_downloaders.sql | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/synapse_data_warehouse/synapse/procs/R2.13.0__add_list_downloaders.sql b/synapse_data_warehouse/synapse/procs/R2.13.0__add_list_downloaders.sql index 48036725..3f068a0f 100644 --- a/synapse_data_warehouse/synapse/procs/R2.13.0__add_list_downloaders.sql +++ b/synapse_data_warehouse/synapse/procs/R2.13.0__add_list_downloaders.sql @@ -1,7 +1,5 @@ -USE SCHEMA {{database_name}}.synapse; - CREATE OR REPLACE PROCEDURE list_downloaders(start_record_date string, entity_list string) -RETURNS TABLE (user_id integer, c integer, min_t timestamp, max_t timestamp) +RETURNS TABLE (user_id integer, user_name varchar, email varchar, synapse_profile varchar, num_downloads integer, earliest_download_time timestamp, latest_download_time timestamp) LANGUAGE SQL AS declare @@ -23,7 +21,7 @@ declare join filetree ft on ft.id = fd.association_object_id where fd.association_object_type = \'FileEntity\' and ft.node_type = \'file\' - and fd.record_date >= \'||:start_record_date||\' + and fd.record_date >= ''' ||:start_record_date|| ''' ),'; query_str3 varchar default 'download_summary (user_id, c, min_t, max_t) as ( @@ -32,9 +30,10 @@ declare group by user_id )'; query_str4 varchar default - 'select * - from download_summary - order by min_t, user_id'; + 'select fds.user_id, up.user_name, up.email, concat(\'https://www.synapse.org/#!Profile:\', fds.user_id) as synapse_profile, fds.c as num_downloads, fds.min_t as earliest_download_time, fds.max_t as latest_download_time + from download_summary fds + join userprofile_latest up on up.id=fds.user_id + order by fds.min_t, fds.user_id'; query_str varchar default (:query_str1 || :query_str2 || :query_str3 || :query_str4); begin rs := (EXECUTE IMMEDIATE :query_str); From 2345fb63cbf80ff87c4f3645dbc64c23b66b112b Mon Sep 17 00:00:00 2001 From: Xavier Schildwachter Date: Wed, 28 Feb 2024 08:14:25 -0800 Subject: [PATCH 3/3] Consistent format --- .../synapse/procs/R2.13.0__add_list_downloaders.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse_data_warehouse/synapse/procs/R2.13.0__add_list_downloaders.sql b/synapse_data_warehouse/synapse/procs/R2.13.0__add_list_downloaders.sql index 3f068a0f..2cc9d793 100644 --- a/synapse_data_warehouse/synapse/procs/R2.13.0__add_list_downloaders.sql +++ b/synapse_data_warehouse/synapse/procs/R2.13.0__add_list_downloaders.sql @@ -19,8 +19,8 @@ declare select fd.user_id, fd.timestamp, fd.association_object_id as entity_id from filedownload fd join filetree ft on ft.id = fd.association_object_id - where fd.association_object_type = \'FileEntity\' - and ft.node_type = \'file\' + where fd.association_object_type = ''FileEntity'' + and ft.node_type = ''file'' and fd.record_date >= ''' ||:start_record_date|| ''' ),'; query_str3 varchar default