Skip to content

Commit

Permalink
feat: add feedback and telemetry tables
Browse files Browse the repository at this point in the history
  • Loading branch information
njogz committed Nov 14, 2024
1 parent bf60640 commit b5ca26b
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
32 changes: 32 additions & 0 deletions models/users/feedback.sql
Original file line number Diff line number Diff line change
@@ -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 %}
51 changes: 51 additions & 0 deletions models/users/telemetry.sql
Original file line number Diff line number Diff line change
@@ -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 %}
33 changes: 33 additions & 0 deletions models/users/telemetry_devices.sql
Original file line number Diff line number Diff line change
@@ -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 %}

0 comments on commit b5ca26b

Please sign in to comment.