From b5ca26b7995703cd63bcdd42eca8f37794a80579 Mon Sep 17 00:00:00 2001 From: Njuguna Ndung'u Date: Thu, 14 Nov 2024 11:11:38 +0300 Subject: [PATCH] feat: add feedback and telemetry tables --- models/users/feedback.sql | 32 +++++++++++++++++++ models/users/telemetry.sql | 51 ++++++++++++++++++++++++++++++ models/users/telemetry_devices.sql | 33 +++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 models/users/feedback.sql create mode 100644 models/users/telemetry.sql create mode 100644 models/users/telemetry_devices.sql diff --git a/models/users/feedback.sql b/models/users/feedback.sql new file mode 100644 index 00000000..c371a629 --- /dev/null +++ b/models/users/feedback.sql @@ -0,0 +1,32 @@ +{{ + config( + materialized = 'incremental', + unique_key='uuid', + on_schema_change='append_new_columns', + indexes=[ + {'columns': ['uuid'], 'type': 'hash'}, + {'columns': ['saved_timestamp']}, + {'columns': ['period_start']}, + ] + ) +}} + +SELECT + document_metadata.uuid as uuid, + document_metadata.saved_timestamp, + doc#>>'{meta,source}' AS SOURCE, + doc#>>'{meta,url}' AS url, + doc#>>'{meta,user,name}' AS user_name, + doc#>>'{meta,time}' AS period_start, + COALESCE(doc#>>'{info,cause}',doc->>'info') AS cause, + doc#>>'{info,message}' AS message +FROM {{ ref('document_metadata') }} document_metadata +INNER JOIN + {{ source('couchdb', env_var('POSTGRES_TABLE')) }} source_table + ON source_table._id = document_metadata.uuid +WHERE + document_metadata.doc_type = 'feedback' + AND document_metadata._deleted = false +{% if is_incremental() %} + AND document_metadata.saved_timestamp >= {{ max_existing_timestamp('saved_timestamp') }} +{% endif %} diff --git a/models/users/telemetry.sql b/models/users/telemetry.sql new file mode 100644 index 00000000..d0a58686 --- /dev/null +++ b/models/users/telemetry.sql @@ -0,0 +1,51 @@ +{{ + config( + materialized = 'incremental', + unique_key='telemetry_id', + on_schema_change='append_new_columns', + indexes=[ + {'columns': ['feedback_id'], 'type': 'hash'}, + {'columns': ['saved_timestamp']}, + ] + ) +}} + +SELECT + document_metadata.uuid as telemetry_id, + document_metadata.saved_timestamp, + CONCAT_WS( --> Date concatenation from JSON fields, eg. 2021-5-17 + '-', + doc#>>'{metadata,year}', --> year + CASE --> month of the year + WHEN + string_to_array(substring(doc#>>'{metadata,versions,app}' FROM '(\d+.\d+.\d+)'),'.')::int[] < '{3,8,0}'::int[] + THEN + (doc#>>'{metadata,month}')::int+1 --> Legacy, months zero-indexed (0 - 11) + ELSE + (doc#>>'{metadata,month}')::int --> Month is between 1 - 12 + END, + CASE --> day of the month, else 1 + WHEN + (doc#>>'{metadata,day}') IS NOT NULL + THEN + doc#>>'{metadata,day}' + ELSE + '1' + END + )::date AS period_start, + doc#>>'{metadata,user}' AS user_name, + doc#>>'{metadata,versions,app}' AS app_version, + doc#>>'{metrics,boot_time,min}' AS boot_time_min, + doc#>>'{metrics,boot_time,max}' AS boot_time_max, + doc#>>'{metrics,boot_time,count}' AS boot_time_count, + doc#>>'{dbInfo,doc_count}' AS doc_count_on_local_db +FROM {{ ref('document_metadata') }} document_metadata +INNER JOIN + {{ source('couchdb', env_var('POSTGRES_TABLE')) }} source_table + ON source_table._id = document_metadata.uuid +WHERE + document_metadata.doc_type = 'telemetry' + AND document_metadata._deleted = false +{% if is_incremental() %} + AND document_metadata.saved_timestamp >= {{ max_existing_timestamp('saved_timestamp') }} +{% endif %} diff --git a/models/users/telemetry_devices.sql b/models/users/telemetry_devices.sql new file mode 100644 index 00000000..113ea6ba --- /dev/null +++ b/models/users/telemetry_devices.sql @@ -0,0 +1,33 @@ +{{ + config( + materialized = 'incremental', + unique_key='uuid', + on_schema_change='append_new_columns', + indexes=[ + {'columns': ['uuid'], 'type': 'hash'}, + {'columns': ['saved_timestamp']}, + ] + ) +}} + +SELECT + telemetry.uuid, + telemetry.saved_timestamp, + telemetry.period_start, + doc #>> '{device,deviceInfo,hardware,manufacturer}' AS device_manufacturer, + doc #>> '{device,deviceInfo,hardware,model}' AS device_model, + doc #>> '{dbInfo,doc_count}' AS doc_count, + doc #>> '{device,userAgent}' AS user_agent, + doc #>> '{device,deviceInfo,app,version}' AS cht_android_version, + doc #>> '{device,deviceInfo,software,androidVersion}' AS android_version, + doc #>> '{device,deviceInfo,storage,free}' AS storage_free, + doc #>> '{device,deviceInfo,storage,total}' AS storage_total, + doc #>> '{device,deviceInfo,network,upSpeed}' AS network_up_speed, + doc #>> '{device,deviceInfo,network,downSpeed}' AS network_down_speed +FROM {{ ref('telemetry') }} telemetry +INNER JOIN + {{ source('couchdb', env_var('POSTGRES_TABLE')) }} source_table + ON source_table._id = telemetry.uuid +{% if is_incremental() %} + WHERE telemetry.saved_timestamp >= {{ max_existing_timestamp('saved_timestamp') }} +{% endif %}