From 78e2907b046f94c54a4051520169fcf3c7301d13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pol=20Mons=C3=B3=20Purt=C3=AD?= Date: Tue, 24 Oct 2023 11:59:37 +0200 Subject: [PATCH] Fix/dset changes (et/somenergia-jardiner!19) * add support for signal_sensor_id, signal_device_id and signal_device_type * set malformed uuids to null --- .../dset/raw_dset_responses__api_response.sql | 60 ++++++++++++++----- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/dbt_jardiner/models/jardiner/raw/dset/raw_dset_responses__api_response.sql b/dbt_jardiner/models/jardiner/raw/dset/raw_dset_responses__api_response.sql index ec7f4718..d02b49ce 100644 --- a/dbt_jardiner/models/jardiner/raw/dset/raw_dset_responses__api_response.sql +++ b/dbt_jardiner/models/jardiner/raw/dset/raw_dset_responses__api_response.sql @@ -9,21 +9,49 @@ with unnest_groups as ( SELECT * FROM unnest_groups WHERE group_name != 'SOMENERGIA' +), +unnested_dset_response as ( + SELECT + group_name, + (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_id')::text as signal_id, + (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_tz')::text as signal_tz, + (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_code')::text as signal_code, + (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_type')::text as signal_type, + (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_unit')::text as signal_unit, + (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_last_ts')::timestamp at time zone + (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_tz')::text as signal_last_ts, + (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_frequency')::text as signal_frequency, + (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_is_virtual')::text as signal_is_virtual, + (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_last_value')::numeric as signal_last_value, + (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_description')::text as signal_description, + (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_device_id')::text as signal_device_uuid, + (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_device_type')::text as signal_device_type, + (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_sensor_id')::text as signal_uuid, + (jsonb_array_elements(jsonb_array_elements((response)::jsonb#>'{signals}')#>'{data}')->>'ts')::timestamp at time zone + (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_tz')::text as ts, + (jsonb_array_elements(jsonb_array_elements((response)::jsonb#>'{signals}')#>'{data}')->>'value')::numeric as signal_value + FROM filter_dummy_group ) -SELECT +select + ts, group_name, - (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_id')::text as signal_id, - (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_tz')::text as signal_tz, - (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_code')::text as signal_code, - (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_type')::text as signal_type, - (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_unit')::text as signal_unit, - (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_last_ts')::timestamp at time zone - (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_tz')::text as signal_last_ts, - (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_frequency')::text as signal_frequency, - (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_is_virtual')::text as signal_is_virtual, - (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_last_value')::numeric as signal_last_value, - left((jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_description'), 36)::uuid as signal_uuid, - (jsonb_array_elements(jsonb_array_elements((response)::jsonb#>'{signals}')#>'{data}')->>'ts')::timestamp at time zone - (jsonb_array_elements((response)::jsonb#>'{signals}')->>'signal_tz')::text as ts, - (jsonb_array_elements(jsonb_array_elements((response)::jsonb#>'{signals}')#>'{data}')->>'value')::numeric as signal_value -FROM filter_dummy_group \ No newline at end of file + signal_value, + case when signal_uuid ~ E'^[[:xdigit:]]{8}-([[:xdigit:]]{4}-){3}[[:xdigit:]]{12}$' then + signal_uuid::uuid + else NULL + end + as signal_uuid, + signal_id, + signal_tz, + signal_code, + signal_type, + signal_unit, + signal_last_ts, + signal_frequency, + signal_is_virtual, + signal_last_value, + signal_uuid as signal_uuid_debug, + signal_device_type, + signal_device_uuid +from unnested_dset_response +order by ts desc