From 65ca22d960fdce6978df9390f96d55c24a8bc07a Mon Sep 17 00:00:00 2001 From: Tom Wier Date: Fri, 16 Aug 2024 14:47:54 +0300 Subject: [PATCH] testing against default project --- macros/cht_form_model.sql | 77 ++++++++++++++++++++++++++ models/contacts/hierarchy.sql | 23 ++++++++ models/forms/pregnancy_danger_sign.sql | 8 +++ 3 files changed, 108 insertions(+) create mode 100644 macros/cht_form_model.sql create mode 100644 models/contacts/hierarchy.sql create mode 100644 models/forms/pregnancy_danger_sign.sql diff --git a/macros/cht_form_model.sql b/macros/cht_form_model.sql new file mode 100644 index 00000000..08d7c6b6 --- /dev/null +++ b/macros/cht_form_model.sql @@ -0,0 +1,77 @@ +-- a macro defining the resusable columns for all form models +{% macro data_record_columns() %} + data_record.uuid as uuid, + data_record.saved_timestamp, + data_record.contact_uuid as reported_by, + data_record.parent_uuid as reported_by_parent, + data_record.reported +{% endmacro %} + +-- a macro defining the resusable indexes for columns above +{% macro data_record_indexes() %} + {{ return([ + {'columns': ['uuid'], 'type': 'hash'}, + {'columns': ['saved_timestamp']}, + {'columns': ['reported_by']}, + {'columns': ['reported_by_parent']}, + {'columns': ['reported']} + ])}} +{% endmacro %} + +-- the from, join and where condition common to form models +-- selects the form from data record by name +-- joins back to the source table to get the document +-- and adds an incremental condition +{% macro data_record_join(form_name) %} + FROM {{ ref('data_record') }} data_record + INNER JOIN {{ env_var('POSTGRES_SCHEMA') }}.{{ env_var('POSTGRES_TABLE') }} couchdb ON couchdb._id = data_record.uuid + WHERE + data_record.form = '{{ form_name }}' + {% if is_incremental() %} + AND data_record.saved_timestamp >= {{ max_existing_timestamp('saved_timestamp') }} + {% endif %} +{% endmacro %} + +-- this macro creates a simple form model +-- form_name: the name of the form to be selected +-- form_columns; form speciifc columns +-- form_indexes; any indexes for the form specific columns +{% macro cht_form_model(form_name, form_columns, form_indexes=[]) %} + {{ cht_form_multi([{'form_name': form_name, 'form_columns': form_columns}], form_indexes) }} +{% endmacro %} + +-- this macro creates a model from a list of cht_forms +-- forms: [{ 'form_name': the name of the form to be selected, +-- 'form_columns': form speciifc columns }] +-- form_indexes; any indexes for the form specific columns +-- each of the forms will be UNIONED together +-- and should have the same custom columns +{% macro cht_form_multi(forms, form_indexes=[]) %} + -- combine any form specifc indexes with the general + {%- set all_indexes = data_record_indexes() + form_indexes -%} + + -- config common to all form models + {{ + config( + materialized='incremental', + unique_key='uuid', + on_schema_change='append_new_columns', + indexes=all_indexes + ) + }} + + -- the actual select; a combination of + -- form specific fields + -- the common data record fields + -- and the common data record from, join and where clause + {% for form in forms %} + SELECT + {{ data_record_columns() }}, + {{ form.form_columns }} + {{ data_record_join(form.form_name) }} + -- if there is more than one form, union them together + {% if not loop.last %} + UNION + {% endif %} + {% endfor %} +{% endmacro %} diff --git a/models/contacts/hierarchy.sql b/models/contacts/hierarchy.sql new file mode 100644 index 00000000..6290e349 --- /dev/null +++ b/models/contacts/hierarchy.sql @@ -0,0 +1,23 @@ +{{ + config( + materialized = 'materialized_view', + indexes=[ + {'columns': ['chw_uuid']}, + {'columns': ['clinic_uuid']}, + {'columns': ['health_center_uuid']}, + {'columns': ['district_hospital_uuid']}, + ] + ) +}} + +SELECT + chw.uuid as chw_uuid, + clinic.uuid as clinic_uuid, + health_center.uuid as health_center_uuid, + district_hospital.uuid as district_hospital_uuid +FROM + {{ref('contact')}} chw + INNER JOIN {{ref('contact')}} clinic ON chw.parent_uuid = clinic.uuid + LEFT JOIN {{ref('contact')}} health_center ON clinic.parent_uuid = health_center.uuid + LEFT JOIN {{ref('contact')}} district_hospital ON health_center.parent_uuid = district_hospital.uuid +WHERE chw.contact_type = 'person' AND clinic.contact_type = 'clinic'; diff --git a/models/forms/pregnancy_danger_sign.sql b/models/forms/pregnancy_danger_sign.sql new file mode 100644 index 00000000..08546151 --- /dev/null +++ b/models/forms/pregnancy_danger_sign.sql @@ -0,0 +1,8 @@ +{%- set form_indexes = [ + {'columns': ['test']}] +-%} +{% set form_columns %} + NULL as test +{% endset %} +{{ cht_form_model('pregnancy_danger_sign', form_columns, form_indexes) }} +