diff --git a/.gitignore b/.gitignore index 6567ff4a..72587234 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ dbt_packages/ logs/ */.env */dbt_packages/ +dbt-env/* diff --git a/models/brac/impact/PNC/get_dashboard_data_pnc_impact.sql b/models/brac/impact/PNC/get_dashboard_data_pnc_impact.sql deleted file mode 100644 index 97ceddcc..00000000 --- a/models/brac/impact/PNC/get_dashboard_data_pnc_impact.sql +++ /dev/null @@ -1,68 +0,0 @@ -{{ config( - materialized='table', - description='Dashboard data for PNC impact in BRAC Uganda' -) }} - -WITH period_CTE AS ( - SELECT generate_series( - date_trunc(param_interval_unit, now() - (param_num_units || ' ' || param_interval_unit)::interval), - CASE - WHEN param_include_current THEN now() - ELSE now() - ('1 ' || param_interval_unit)::interval - END, - ('1 ' || param_interval_unit)::interval - )::date AS start -) - -SELECT - CASE - WHEN param_facility_group_by = 'clinic' OR param_facility_group_by = 'health_center' OR param_facility_group_by = 'district_hospital' - THEN place_period.district_hospital_uuid - ELSE 'All' - END AS district_hospital_uuid, - CASE - WHEN param_facility_group_by = 'clinic' OR param_facility_group_by = 'health_center' OR param_facility_group_by = 'district_hospital' - THEN place_period.district_hospital_name - ELSE 'All' - END AS district_hospital_name, - CASE - WHEN param_facility_group_by = 'clinic' OR param_facility_group_by = 'health_center' - THEN place_period.health_center_uuid - ELSE 'All' - END AS health_center_uuid, - CASE - WHEN param_facility_group_by = 'clinic' OR param_facility_group_by = 'health_center' - THEN place_period.health_center_name - ELSE 'All' - END AS health_center_name, - 'All' AS clinic_uuid, - 'All' AS clinic_name, - place_period.period_start AS period_start, - date_part('epoch', place_period.period_start)::numeric AS period_start_epoch, - CASE - WHEN param_facility_group_by = 'health_center' - THEN place_period.health_center_uuid - WHEN param_facility_group_by = 'district_hospital' - THEN place_period.district_hospital_uuid - ELSE 'All' - END AS facility_join_field, - /* Other fields... (add the remaining fields here) */ -FROM - ( - -- Subqueries and joins... (add the subqueries and joins here) - ) AS place_period -GROUP BY - district_hospital_uuid, - district_hospital_name, - health_center_uuid, - health_center_name, - clinic_uuid, - clinic_name, - period_start, - period_start_epoch, - facility_join_field -ORDER BY - district_hospital_name, - health_center_name, - clinic_name, - period_start; diff --git a/models/brac/impact/PNC/impact_pncview_expected_enrollments.sql b/models/brac/impact/PNC/impact_pncview_expected_enrollments.sql index 9fcd3d2d..d4030973 100644 --- a/models/brac/impact/PNC/impact_pncview_expected_enrollments.sql +++ b/models/brac/impact/PNC/impact_pncview_expected_enrollments.sql @@ -33,4 +33,4 @@ WITH pnc_expected_enrollments AS ( -- Model: pncview_expected_enrollments SELECT * -FROM pnc_expected_enrollments; +FROM pnc_expected_enrollments diff --git a/models/brac/impact/hhvisit/get_dashboard_data_hh_impact.sql b/models/brac/impact/hhvisit/get_dashboard_data_hh_impact.sql deleted file mode 100644 index 103f56b5..00000000 --- a/models/brac/impact/hhvisit/get_dashboard_data_hh_impact.sql +++ /dev/null @@ -1,148 +0,0 @@ -WITH hh_data AS ( -WITH main_data AS ( - WITH period_CTE AS ( - SELECT generate_series( - CASE - WHEN param_include_current - THEN date_trunc(param_interval_unit, now() - (param_num_units || ' ' || param_interval_unit)::INTERVAL) - ELSE - date_trunc(param_interval_unit, min(reported))::DATE - END, - CASE - WHEN param_include_current - THEN now() - ELSE now() - ('1 ' || param_interval_unit)::INTERVAL - END, - ('1 ' || param_interval_unit)::INTERVAL - )::DATE AS start - ) - - SELECT - CASE - WHEN param_facility_group_by IN ('clinic', 'health_center', 'district_hospital') - THEN place_period.district_hospital_uuid - ELSE 'All' - END AS _district_hospital_uuid, - CASE - WHEN param_facility_group_by IN ('clinic', 'health_center', 'district_hospital') - THEN place_period.district_hospital_name - ELSE 'All' - END AS _district_hospital_name, - CASE - WHEN param_facility_group_by IN ('clinic', 'health_center') - THEN place_period.health_center_uuid - ELSE 'All' - END AS _health_center_uuid, - CASE - WHEN param_facility_group_by IN ('clinic', 'health_center') - THEN place_period.health_center_name - ELSE 'All' - END AS _health_center_name, - 'All'::TEXT AS _clinic_uuid, - 'All'::TEXT AS _clinic_name, - place_period.period_start AS _period_start, - date_part('epoch', place_period.period_start)::NUMERIC AS _period_start_epoch, - CASE - WHEN param_facility_group_by = 'health_center' - THEN place_period.health_center_uuid - WHEN param_facility_group_by = 'district_hospital' - THEN place_period.district_hospital_uuid - ELSE 'All' - END AS _facility_join_field, - COALESCE(sum(hhcount.hh_registered), 0) AS hh_registered, - COALESCE(sum(hh_visit), 0) AS hh_visit - FROM - ( - SELECT - district_hospital.uuid AS district_hospital_uuid, - district_hospital.name AS district_hospital_name, - health_center.uuid AS health_center_uuid, - health_center.name AS health_center_name, - period_CTE.start AS period_start - FROM - period_CTE, - contactview_metadata AS health_center - INNER JOIN contactview_metadata AS district_hospital ON - (health_center.parent_uuid = district_hospital.uuid) - WHERE - district_hospital.type = 'district_hospital' - AND district_hospital.name NOT IN ('HQ', 'HW OVC') - ) AS place_period - LEFT JOIN - ( - SELECT - reported_by_parent, - date_trunc(param_interval_unit, reported)::DATE AS reported_month, - count(DISTINCT household_id) AS hh_visit - FROM - hhview_visits - GROUP BY - reported_by_parent, - reported_month - ) AS hh_visit ON (place_period.period_start = hh_visit.reported_month AND place_period.health_center_uuid = hh_visit.reported_by_parent) - LEFT JOIN ( - SELECT - parent_uuid, - date_trunc(param_interval_unit, reported) AS reported_month, - count(DISTINCT uuid) AS hh_registered - FROM - contactview_metadata - WHERE - TYPE = 'clinic' - GROUP BY - reported_month, - parent_uuid - ) AS hhcount ON (place_period.period_start = hhcount.reported_month AND place_period.health_center_uuid = hhcount.parent_uuid) - GROUP BY - _district_hospital_uuid, - _district_hospital_name, - _health_center_uuid, - _health_center_name, - _clinic_uuid, - _clinic_name, - _period_start, - _facility_join_field - ORDER BY - _district_hospital_name, - _health_center_name, - _clinic_name, - _period_start -) - -SELECT - _district_hospital_uuid, - _district_hospital_name, - _health_center_uuid, - _health_center_name, - _clinic_uuid, - _clinic_name, - _period_start, - _period_start_epoch, - _facility_join_field, - hh_registered, - -- Cumulative sum over all months - hh_visit, - sum(hh_registered) OVER (PARTITION BY _facility_join_field ORDER BY _period_start) AS total_hh_registered -FROM - main_data -) - -SELECT -_district_hospital_uuid, -_district_hospital_name, -_health_center_uuid, -_health_center_name, -_clinic_uuid, -_clinic_name, -_period_start, -_period_start_epoch, -_facility_join_field, -hh_registered, -total_hh_registered, -hh_visit, -safe_divide(hh_visit, total_hh_registered, 2) AS percent_hh_visit -FROM -hh_data -WHERE --- Filter the required data only. -_period_start >= now() - ((1 + param_num_units::INT) || ' ' || param_interval_unit)::INTERVAL; diff --git a/models/brac/indexes.sql b/models/brac/indexes.sql deleted file mode 100644 index b0753f67..00000000 --- a/models/brac/indexes.sql +++ /dev/null @@ -1,9 +0,0 @@ -{{ config(schema='v1', materialized = 'raw_sql') }} - -CREATE INDEX IF NOT EXISTS couchdb_doc_id ON {{ env_var('ROOT_POSTGRES_SCHEMA') }}.{{ env_var('POSTGRES_TABLE') }} ((doc ->> '_id'::text) text_ops); -CREATE INDEX IF NOT EXISTS couchdb_doc_type ON {{ env_var('ROOT_POSTGRES_SCHEMA') }}.{{ env_var('POSTGRES_TABLE') }}((doc ->> 'type'::text) text_ops); -CREATE INDEX IF NOT EXISTS couchdb_doc_form ON {{ env_var('ROOT_POSTGRES_SCHEMA') }}.{{ env_var('POSTGRES_TABLE') }}((doc ->> 'form'::text) text_ops) WHERE (doc ->> 'type'::text) = 'data_record'::text; -CREATE INDEX IF NOT EXISTS couchdb_doc_form_patient_id ON {{ env_var('ROOT_POSTGRES_SCHEMA') }}.{{ env_var('POSTGRES_TABLE') }}((doc #>> '{fields,patient_id}'::text[]) text_ops) WHERE (doc ->> 'type'::text) = 'data_record'::text; -CREATE INDEX IF NOT EXISTS couchdb_doc_form_place_id ON {{ env_var('ROOT_POSTGRES_SCHEMA') }}.{{ env_var('POSTGRES_TABLE') }}((doc #>> '{fields,place_id}'::text[]) text_ops) WHERE (doc ->> 'type'::text) = 'data_record'::text; -CREATE INDEX IF NOT EXISTS couchdb_doc_form_source_id ON {{ env_var('ROOT_POSTGRES_SCHEMA') }}.{{ env_var('POSTGRES_TABLE') }}((doc #>> '{fields,inputs,source_id}'::text[]) text_ops) WHERE (doc ->> 'type'::text) = 'data_record'::text; -CREATE INDEX IF NOT EXISTS couchdb_doc_assessment_patient_age ON {{ env_var('ROOT_POSTGRES_SCHEMA') }}.{{ env_var('POSTGRES_TABLE') }}((nullif(doc #>> '{fields,patient_age_in_years}', '')::int)) WHERE (doc ->> 'form'::text) = 'assessment'::text AND (doc #>> '{fields,patient_age_in_years}') != ''; \ No newline at end of file diff --git a/models/brac/useviews/useview_assessment_follow_up.sql b/models/brac/useviews/useview_assessment_follow_up.sql index 7defd3ff..f5947139 100644 --- a/models/brac/useviews/useview_assessment_follow_up.sql +++ b/models/brac/useviews/useview_assessment_follow_up.sql @@ -14,7 +14,7 @@ }} SELECT -{{ dbt_utils.surrogate_key(['form_source_id', 'reported', 'uuid']) }} AS useview_assessment_follow_up_source_date_uuid, +{{ dbt_utils.generate_surrogate_key(['form_source_id', 'reported', 'uuid']) }} AS useview_assessment_follow_up_source_date_uuid, * FROM( diff --git a/models/user_tables/chws.sql b/models/user_tables/chws.sql deleted file mode 100644 index 3f8658e7..00000000 --- a/models/user_tables/chws.sql +++ /dev/null @@ -1,21 +0,0 @@ -{{ - config( - materialized = 'table', - indexes=[ - {'columns': ['"@timestamp"'], 'type': 'brin'}, - {'columns': ['"patient_id"'], 'type': 'hash'}, - ] - ) -}} - -SELECT - doc->>'name' AS name, - doc->>'date_of_birth' AS date_of_birth, - doc->>'phone' AS phone, - doc->>'sex' AS sex, - doc->>'reported_date' AS reported_date, - doc->>'patient_id' AS patient_id, - * -FROM {{ ref('couchdb') }} -WHERE - doc->>'type' = 'person'