From 1350ac722a6542b9c4b2f36f5e619e52ea9a596f Mon Sep 17 00:00:00 2001 From: TylerSchrag-NOAA <97621365+TylerSchrag-NOAA@users.noreply.github.com> Date: Thu, 25 Jan 2024 07:33:33 -0800 Subject: [PATCH] Cached FIM - Part 1b - Removal of Redshift & Other Optimizations / Fixes (#620) This PR marks another iterative step towards stabilizing the new cached FIM implementation. It includes several minor optimizations and fixes, with a couple major pivots to the infrastructure / workflow: **Major Changes:** - **Switch from HydroID, HUC8 & Branch indexing of HAND data to a new unique 'HAND_ID' integer.** - We may need to update this moving forward to match a coordinated effort on the FIM Dev team, but switching to a single unique integer ID speeds up the database operations significantly, as well as simplifying the scripting and join logic. - **Removal of Redshift Data Warehouse, Implementing in RDS now instead** - I initially chose Redshift for this feature after testing a prototype in early 2023 that was too large for RDS to run. That prototype was based on the initial [bad] plan that we would write a process that would preprocess all ~440 million HAND hydrotable geometries in advance (all steps of the synthetic rating curves), and would need the infrastructure to query that entire dataset efficiently enough for VPP pipelines. After testing our new lazy loading approach for several weeks (a much better idea, suggested by Corey), it has become apparent that most pipelines utilize a very small portion of the full hydrotables, and with the HAND_ID optimization, RDS is likely up to the task of handling these cache workflows just fine. While I wish I would have thought of these considerations earlier on and saved the work of trying out Redshift fully to begin with, I think this is ultimately a great pivot that dramatically simplifies and stabilizes this Cached FIM enhancement. The previous PRs of this series can serve as a reference for the team should they decide to utilize Redshift in the future... but it is worth noting that I still hadn't completely sorted out some issues that Redshift was having with some of the more complex FIM geometries that weren't included in my initial testing last year - that may end up being a full on deal breaker with Redshift (Aurora may be worth a try first if/when scaling the RDS instance isn't a good option any longer). **Other Noteworthy Edits:** - **Public FIM Clipping Optimization** - I changed the code to use a lookup on the derived.channels table to determine which reaches are in the public subset, instead of doing a spatial join with every pipeline (this was the thing causing MRF to fail during heavy mid-January weather). This is much more performant... but doesn't clip the FIM extent shapes to the exact border of the public domain, which is an enhancement that Corey added after specifically being asked by someone. I'll try to see if there is a way to do that in a more optimal way, but this will at least keep those pipelines from failing under load. **Deployment / DB Dump Considerations:** - I've added several version 2.1.5 DB dump files to the deployed folder in hydrovis-ti-deployment-us-east-1 (we could move them outside of this folder, if we want to test those during deployment, of course). - These will need to be added to the UAT S3 folders before deployment there. - I purposely avoided any changes to the ArcGIS mapx files... so hopefully the SD creation script on the EC2 won't cause issues for this. Hopefully. **Update:** I've added a bunch of misc. fixes and clean-up to this branch while it's been waiting deployment, and think I have resolved all issues with the regular operational pipelines (at least that I know of). I still need to fully implement AEP and CatFIM pipelines, but these should not hold up deployment to UAT, since they are only run as one-off products anyways (will need to be updated next for NWM 3.0 Recurrence Flow Updates and/or next FIM Version update). I'm planning to wrap that up in early February. --------- Co-authored-by: Nick Chadwick --- Core/EC2/RDSBastion/main.tf | 73 -------------- .../utils/setup_db_link_fdw_to_redshift.tftpl | 17 ---- .../scripts/viz/postgresql_setup.sh.tftpl | 7 -- .../scripts/viz/redshift_setup.sh.tftpl | 27 ------ Core/IAM/Roles/main.tf | 38 -------- Core/IAM/Roles/redshift.json.tftpl | 28 ------ Core/LAMBDA/layers/main.tf | 2 +- .../rnr_domain_generator/sql/timeslices.sql | 2 +- .../lambda_function.py | 18 ++-- Core/LAMBDA/viz_functions/main.tf | 25 +---- .../0_create_or_truncate_tables.sql | 47 +++++++++ ..._create_inundation_tables_if_not_exist.sql | 54 ----------- ..._create_inundation_tables_if_not_exist.sql | 70 -------------- ...l => 1_prepare_fim_config_flows_table.sql} | 9 +- .../1b_redshift_copy_inundation_flows.sql | 6 -- ...ion.sql => 2a_query_fim_cache-ras2fim.sql} | 26 ++--- .../2a_redshift_query_cached_fim_table.sql | 49 ---------- .../2b_query_fim_cache-hand.sql | 39 ++++++++ ...d_any_processed_hand_fim_back_to_cache.sql | 50 ++++++++++ .../3b_rds_cached_hand_insertion.sql | 38 -------- ... => 4_create_fim_config_publish_table.sql} | 26 +++-- .../5_dummy_rows_for_wpod_service_monitor.sql | 19 ++++ .../5a_redshift_cache_fim_from_rds.sql | 65 ------------- .../rfc_based_5day_max_inundation.sql | 25 +++-- .../rfc_based_5day_max_inundation.sql | 30 ++++-- .../viz_db_postprocess_sql/lambda_function.py | 74 +++++++++----- .../max_flows/ana_14day_max_flows.sql | 2 +- .../max_flows/ana_7day_max_flows.sql | 2 +- .../max_flows/mrf_gfs_10day_max_flows.sql | 1 - .../max_flows/mrf_gfs_10day_max_flows_ak.sql | 1 - .../max_flows/mrf_gfs_5day_max_flows.sql | 2 +- .../max_flows/mrf_gfs_5day_max_flows_ak.sql | 2 +- .../max_flows/mrf_gfs_mem2_5day_max_flows.sql | 2 +- .../max_flows/mrf_gfs_mem3_5day_max_flows.sql | 2 +- .../max_flows/mrf_gfs_mem4_5day_max_flows.sql | 2 +- .../max_flows/mrf_gfs_mem5_5day_max_flows.sql | 2 +- .../max_flows/mrf_gfs_mem6_5day_max_flows.sql | 2 +- .../max_flows/mrf_nbm_10day_max_flows.sql | 3 +- .../max_flows/mrf_nbm_10day_max_flows_ak.sql | 1 - .../max_flows/mrf_nbm_5day_max_flows.sql | 2 +- .../max_flows/mrf_nbm_5day_max_flows_ak.sql | 2 +- ...bm_10day_peak_flow_arrival_time_alaska.sql | 2 +- .../srf_48hr_peak_flow_arrival_time_prvi.sql | 2 +- .../viz_db_postprocess_sql/readme.md | 32 ------- .../building_footprints_fimpact.sql | 15 +-- .../building_footprints_fimpact.sql | 16 ++-- .../building_footprints_fimpact.sql | 16 ++-- .../14day_building_footprints_fimpact.sql | 15 +-- .../7day_building_footprints_fimpact.sql | 15 +-- .../10day_building_footprints_fimpact.sql | 17 ++-- .../3day_building_footprints_fimpact.sql | 16 ++-- .../5day_building_footprints_fimpact.sql | 18 ++-- .../5day_public_subset.sql | 5 +- .../10day_building_footprints_fimpact.sql | 16 ++-- .../3day_building_footprints_fimpact.sql | 15 +-- .../5day_building_footprints_fimpact.sql | 16 ++-- .../building_footprints_fimpact.sql | 15 +-- .../building_footprints_fimpact.sql | 15 +-- .../building_footprints_fimpact.sql | 16 ++-- .../building_footprints_fimpact.sql | 16 ++-- .../templates_sql/hand_features.sql | 6 +- .../analysis_assim/ana_inundation.yml | 2 +- .../ana_past_14day_max_inundation.yml | 8 +- .../ana_inundation_hi.yml | 2 +- .../ana_inundation_prvi.yml | 2 +- .../mrf_nbm_10day_max_inundation.yml | 6 +- ...rf_nbm_10day_peak_flow_arrival_time_ak.yml | 2 +- ...rf_gfs_5day_max_inundation_probability.yml | 96 ------------------- .../mrf_gfs_10day_max_inundation.yml | 6 +- .../rfc_based_5day_max_inundation.yml | 2 +- .../short_range/srf_18hr_max_inundation.yml | 2 +- .../srf_48hr_max_inundation_hi.yml | 2 +- .../srf_48hr_max_inundation_prvi.yml | 2 +- Core/Redshift/viz/main.tf | 74 -------------- Core/SecurityGroups/main.tf | 46 --------- .../viz_processing_pipeline.json.tftpl | 95 +++++++++++------- Core/main.tf | 27 ------ 77 files changed, 512 insertions(+), 1038 deletions(-) delete mode 100644 Core/EC2/RDSBastion/scripts/utils/setup_db_link_fdw_to_redshift.tftpl delete mode 100644 Core/EC2/RDSBastion/scripts/viz/redshift_setup.sh.tftpl delete mode 100644 Core/IAM/Roles/redshift.json.tftpl create mode 100644 Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/0_create_or_truncate_tables.sql delete mode 100644 Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/0a_redshift_create_inundation_tables_if_not_exist.sql delete mode 100644 Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/0b_rds_create_inundation_tables_if_not_exist.sql rename Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/{1a_rds_build_inundation_flows_table.sql => 1_prepare_fim_config_flows_table.sql} (64%) delete mode 100644 Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/1b_redshift_copy_inundation_flows.sql rename Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/{3a_rds_ras2fim_insertion.sql => 2a_query_fim_cache-ras2fim.sql} (57%) delete mode 100644 Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/2a_redshift_query_cached_fim_table.sql create mode 100644 Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/2b_query_fim_cache-hand.sql create mode 100644 Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/3_add_any_processed_hand_fim_back_to_cache.sql delete mode 100644 Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/3b_rds_cached_hand_insertion.sql rename Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/{4a_rds_create_fim_publish_table.sql => 4_create_fim_config_publish_table.sql} (54%) create mode 100644 Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/5_dummy_rows_for_wpod_service_monitor.sql delete mode 100644 Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/5a_redshift_cache_fim_from_rds.sql delete mode 100644 Core/LAMBDA/viz_functions/viz_db_postprocess_sql/readme.md delete mode 100644 Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/medium_range_ensemble/mrf_gfs_5day_max_inundation_probability.yml delete mode 100644 Core/Redshift/viz/main.tf diff --git a/Core/EC2/RDSBastion/main.tf b/Core/EC2/RDSBastion/main.tf index 7b355c17..2ba16ae6 100644 --- a/Core/EC2/RDSBastion/main.tf +++ b/Core/EC2/RDSBastion/main.tf @@ -64,26 +64,6 @@ variable "viz_db_name" { type = string } -variable "viz_redshift_master_secret_string" { - type = string -} - -variable "viz_redshift_address" { - type = string -} - -variable "viz_redshift_port" { - type = string -} - -variable "viz_redshift_name" { - type = string -} - -variable "viz_redshift_iam_role_arn" { - type = string -} - variable "egis_db_master_secret_string" { type = string } @@ -148,10 +128,6 @@ variable "viz_proc_dev_rw_secret_string" { type = string } -variable "viz_redshift_user_secret_string" { - type = string -} - variable "fim_version" { type = string } @@ -186,13 +162,6 @@ locals { db_username = jsondecode(var.viz_db_secret_string)["username"] db_password = jsondecode(var.viz_db_secret_string)["password"] } - viz_redshift = { - db_host = var.viz_redshift_address - db_port = var.viz_redshift_port - db_name = var.viz_redshift_name - db_username = jsondecode(var.viz_redshift_master_secret_string)["username"] - db_password = jsondecode(var.viz_redshift_master_secret_string)["password"] - } egis = { db_host = var.egis_db_address db_port = var.egis_db_port @@ -367,29 +336,6 @@ data "cloudinit_config" "startup" { }) } - part { - content_type = "text/x-shellscript" - filename = "2b_viz_redshift_setup.sh" - content = templatefile("${path.module}/scripts/viz/redshift_setup.sh.tftpl", { - viz_redshift_name = local.dbs["viz_redshift"]["db_name"] - viz_redshift_host = local.dbs["viz_redshift"]["db_host"] - viz_redshift_port = local.dbs["viz_redshift"]["db_port"] - viz_redshift_master_username = local.dbs["viz_redshift"]["db_username"] - viz_redshift_master_password = local.dbs["viz_redshift"]["db_password"] - viz_redshift_user_username = jsondecode(var.viz_redshift_user_secret_string)["username"] - viz_redshift_user_password = jsondecode(var.viz_redshift_user_secret_string)["password"] - viz_redshift_iam_role_arn = var.viz_redshift_iam_role_arn - viz_db_name = local.dbs["viz"]["db_name"] - viz_db_host = local.dbs["viz"]["db_host"] - viz_db_port = local.dbs["viz"]["db_port"] - viz_db_username = local.dbs["viz"]["db_username"] - viz_db_password = local.dbs["viz"]["db_password"] - viz_proc_admin_rw_secret_arn = var.viz_proc_admin_rw_secret_arn - viz_proc_admin_rw_username = jsondecode(var.viz_proc_admin_rw_secret_string)["username"] - viz_proc_admin_rw_password = jsondecode(var.viz_proc_admin_rw_secret_string)["password"] - }) - } - part { content_type = "text/x-shellscript" filename = "3_viz_restore_db_dumps.sh" @@ -447,25 +393,6 @@ data "cloudinit_config" "startup" { }) } - part { - content_type = "text/x-shellscript" - filename = "4c_viz_setup_fdw_to_redshift.sh" - content = templatefile("${path.module}/scripts/utils/setup_db_link_fdw_to_redshift.tftpl", { - db_name = local.dbs["viz"]["db_name"] - db_host = local.dbs["viz"]["db_host"] - db_port = local.dbs["viz"]["db_port"] - db_username = local.dbs["viz"]["db_username"] - db_password = local.dbs["viz"]["db_password"] - db_admin_mapping = jsondecode(var.viz_db_secret_string)["username"] - db_user_mapping = jsondecode(var.viz_proc_admin_rw_secret_string)["username"] - viz_redshift_name = local.dbs["viz_redshift"]["db_name"] - viz_redshift_host = local.dbs["viz_redshift"]["db_host"] - viz_redshift_port = local.dbs["viz_redshift"]["db_port"] - viz_redshift_user_username = jsondecode(var.viz_redshift_user_secret_string)["username"] - viz_redshift_user_password = jsondecode(var.viz_redshift_user_secret_string)["password"] - }) - } - part { content_type = "text/x-shellscript" filename = "5_egis_postgresql_setup.sh" diff --git a/Core/EC2/RDSBastion/scripts/utils/setup_db_link_fdw_to_redshift.tftpl b/Core/EC2/RDSBastion/scripts/utils/setup_db_link_fdw_to_redshift.tftpl deleted file mode 100644 index 18574bd1..00000000 --- a/Core/EC2/RDSBastion/scripts/utils/setup_db_link_fdw_to_redshift.tftpl +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash - -echo "" -echo "---- SETTING UP FOREIGN DATA WRAPPER TO REDSHIFT ----" - -# Setup foreign data server for db_link to Redshift - The syntax is a little diferent for this one, so I'm not abstracting it like the other foreign data wrapper connections at this time. -echo "Adding foreing data_server for db_link to Redshift..." -psql -h "${db_host}" -U "${db_username}" -p ${db_port} -d "${db_name}" \ - -tAc "CREATE SERVER external_vpp_redshift - FOREIGN DATA WRAPPER postgres_fdw - OPTIONS (host '${viz_redshift_host}', port '${viz_redshift_port}', dbname '${viz_redshift_name}', sslmode 'require'); - CREATE USER MAPPING FOR postgres - SERVER external_vpp_redshift - OPTIONS (user '${viz_redshift_user_username}', password '${viz_redshift_user_password}'); - CREATE USER MAPPING FOR ${db_user_mapping} - SERVER external_vpp_redshift - OPTIONS (user '${viz_redshift_user_username}', password '${viz_redshift_user_password}');" \ No newline at end of file diff --git a/Core/EC2/RDSBastion/scripts/viz/postgresql_setup.sh.tftpl b/Core/EC2/RDSBastion/scripts/viz/postgresql_setup.sh.tftpl index 098bf9dc..eace0437 100644 --- a/Core/EC2/RDSBastion/scripts/viz/postgresql_setup.sh.tftpl +++ b/Core/EC2/RDSBastion/scripts/viz/postgresql_setup.sh.tftpl @@ -21,13 +21,6 @@ rm "$${postgres_data_folder}/postgis_setup.sql" echo "Setting up aws_s3..." psql -h "${viz_db_host}" -U "${viz_db_username}" -p ${viz_db_port} -d "${viz_db_name}" -qtAc "CREATE EXTENSION IF NOT EXISTS aws_s3 CASCADE;" -echo "Setting up db_link..." -psql -h "${viz_db_host}" -U "${viz_db_username}" -p ${viz_db_port} -d "${viz_db_name}" -qtAc "CREATE EXTENSION IF NOT EXISTS dblink CASCADE;" - -# Set password encryption to md5 - we need this to play nice with Redshift foreign data connections -echo "Setting password encyption to md5" -psql -h "${viz_db_host}" -U "${viz_db_username}" -p ${viz_db_port} -d "${viz_db_name}" -qtAc "SET password_encryption = 'md5';" - # Adding users to Viz DB echo "Adding viz proc user..." psql -h "${viz_db_host}" -U "${viz_db_username}" -p ${viz_db_port} -d "${viz_db_name}" -qtAc "CREATE ROLE ${viz_proc_admin_rw_username};" diff --git a/Core/EC2/RDSBastion/scripts/viz/redshift_setup.sh.tftpl b/Core/EC2/RDSBastion/scripts/viz/redshift_setup.sh.tftpl deleted file mode 100644 index 94543648..00000000 --- a/Core/EC2/RDSBastion/scripts/viz/redshift_setup.sh.tftpl +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash - -export PGOPTIONS='-c client_min_messages=warning' - -postgres_data_folder="/home/ec2-user/postgres_data" - -echo "" -echo "---- SETTING UP VIZ REDSHIFT DB ----" - - -# Setting up Viz Redshift DB -export PGPASSWORD=${viz_redshift_master_password} - -# Adding users to Viz Redshift DB -echo "Adding viz redshift user..." -psql -h "${viz_redshift_host}" -U "${viz_redshift_master_username}" -p ${viz_redshift_port} -d "${viz_redshift_name}" -qtAc "CREATE USER ${viz_redshift_user_username} WITH PASSWORD '${viz_redshift_user_password}';" - -# Setup external schemas - linked to viz processing rds datbase - this could be abstracted as done with the viz processing foreign schemas, but I'm not doing that now since it is only one schema we need. -echo "Adding external schema link to viz ingest ..." -psql -h "${viz_redshift_host}" -U "${viz_redshift_master_username}" -p ${viz_redshift_port} -d "${viz_redshift_name}" \ - -tAc "DROP SCHEMA IF EXISTS external_viz_ingest; - CREATE EXTERNAL SCHEMA external_viz_ingest - FROM POSTGRES - DATABASE '${viz_db_name}' SCHEMA 'ingest' - URI '${viz_db_host}' PORT ${viz_db_port} - IAM_ROLE '${viz_redshift_iam_role_arn}' - SECRET_ARN '${viz_proc_admin_rw_secret_arn}';" \ No newline at end of file diff --git a/Core/IAM/Roles/main.tf b/Core/IAM/Roles/main.tf index 60fdbdc2..e2461412 100644 --- a/Core/IAM/Roles/main.tf +++ b/Core/IAM/Roles/main.tf @@ -26,10 +26,6 @@ variable "nws_shared_account_s3_bucket" { type = string } -variable "viz_proc_admin_rw_secret_arn" { - type = string -} - # Autoscaling Role resource "aws_iam_service_linked_role" "autoscaling" { aws_service_name = "autoscaling.amazonaws.com" @@ -203,36 +199,6 @@ resource "aws_iam_role_policy" "rds_s3_export" { }) } -# Redshift Role -resource "aws_iam_role" "redshift" { - name = "hv-vpp-${var.environment}-${var.region}-redshift" - - assume_role_policy = jsonencode({ - Version = "2012-10-17" - Statement = [ - { - Action = "sts:AssumeRole" - Effect = "Allow" - Sid = "" - Principal = { - Service = "redshift.amazonaws.com" - } - }, - ] - }) -} - -resource "aws_iam_role_policy" "redshift" { - name = "hv-vpp-${var.environment}-${var.region}-redshift" - role = aws_iam_role.redshift.id - policy = templatefile("${path.module}/redshift.json.tftpl", { - environment = var.environment - account_id = var.account_id - region = var.region - viz_proc_admin_rw_secret_arn = var.viz_proc_admin_rw_secret_arn - }) -} - # data-services Role resource "aws_iam_role" "data_services" { name = "hv-vpp-${var.environment}-${var.region}-data-services" @@ -449,10 +415,6 @@ output "role_rds_s3_export" { value = aws_iam_role.rds_s3_export } -output "role_redshift" { - value = aws_iam_role.redshift -} - output "profile_data_services" { value = aws_iam_instance_profile.data_services } diff --git a/Core/IAM/Roles/redshift.json.tftpl b/Core/IAM/Roles/redshift.json.tftpl deleted file mode 100644 index 0436fc4a..00000000 --- a/Core/IAM/Roles/redshift.json.tftpl +++ /dev/null @@ -1,28 +0,0 @@ -{ - "Version": "2012-10-17", - "Statement": [ - { - "Sid": "s3Access", - "Action": [ - "s3:PutObject", - "s3:Get*", - "s3:List*" - ], - "Effect": "Allow", - "Resource": [ - "arn:aws:s3:::hydrovis-${environment}-fim-${region}/*" - ] - }, - { - "Sid": "secretForExternalDataSchema", - "Action": [ - "secretsmanager:DescribeSecret", - "secretsmanager:GetSecretValue" - ], - "Effect": "Allow", - "Resource": [ - "${viz_proc_admin_rw_secret_arn}" - ] - } - ] -} \ No newline at end of file diff --git a/Core/LAMBDA/layers/main.tf b/Core/LAMBDA/layers/main.tf index a60e5e77..212b4c68 100644 --- a/Core/LAMBDA/layers/main.tf +++ b/Core/LAMBDA/layers/main.tf @@ -328,7 +328,7 @@ output "requests" { } output "yaml" { - value = resource.aws_lambda_layer_version.dask + value = resource.aws_lambda_layer_version.yaml } output "dask" { diff --git a/Core/LAMBDA/rnr_functions/rnr_domain_generator/sql/timeslices.sql b/Core/LAMBDA/rnr_functions/rnr_domain_generator/sql/timeslices.sql index 4830e730..84e78ffb 100644 --- a/Core/LAMBDA/rnr_functions/rnr_domain_generator/sql/timeslices.sql +++ b/Core/LAMBDA/rnr_functions/rnr_domain_generator/sql/timeslices.sql @@ -19,7 +19,7 @@ basic_output AS ( base.hydro_id, base.nws_station_id, base.time, - COALESCE(ana.maxflow_1hour_cms, fcst.flow_cms) as flow_cms + COALESCE(ana.discharge_cms, fcst.flow_cms) as flow_cms FROM timeslice_base base LEFT JOIN rnr.domain_forecasts fcst ON fcst.valid_time = base.time diff --git a/Core/LAMBDA/viz_functions/image_based/viz_hand_fim_processing/lambda_function.py b/Core/LAMBDA/viz_functions/image_based/viz_hand_fim_processing/lambda_function.py index 55f6c789..192c9cd7 100644 --- a/Core/LAMBDA/viz_functions/image_based/viz_hand_fim_processing/lambda_function.py +++ b/Core/LAMBDA/viz_functions/image_based/viz_hand_fim_processing/lambda_function.py @@ -94,9 +94,9 @@ def lambda_handler(event, context): # Upload zero_stage reaches for tracking / FIM cache print(f"Adding zero stage data to {db_table}_zero_stage")# Only process inundation configuration if available data - df_zero_stage_records['branch'] = int(branch) - df_zero_stage_records['huc8'] = int(huc8) - df_zero_stage_records.to_sql(f"{db_table}_zero_stage", con=process_db.engine, schema=db_schema, if_exists='append', index=True) + df_zero_stage_records = df_zero_stage_records.reset_index() + df_zero_stage_records.drop(columns=['hydro_id','feature_id'], inplace=True) + df_zero_stage_records.to_sql(f"{db_table}_zero_stage", con=process_db.engine, schema=db_schema, if_exists='append', index=False) # If no features with above zero stages are present, then just copy an unflood raster instead of processing nothing if stage_lookup.empty: @@ -107,16 +107,15 @@ def lambda_handler(event, context): df_inundation = create_inundation_output(huc8, branch, stage_lookup, reference_time, input_variable) # Split geometry into seperate table per new schema - df_inundation_geo = df_inundation[['hydro_id', 'feature_id', 'huc8', 'branch', 'rc_stage_ft', 'geom']] + df_inundation_geo = df_inundation[['hand_id', 'rc_stage_ft', 'geom']] df_inundation.drop(columns=['geom'], inplace=True) # If records exist in stage_lookup that don't exist in df_inundation, add those to the zero_stage table. - df_no_inundation = stage_lookup.merge(df_inundation.drop_duplicates(), on=['feature_id','hydro_id'],how='left',indicator=True) + df_no_inundation = stage_lookup.merge(df_inundation.drop_duplicates(), on=['hand_id'],how='left',indicator=True) df_no_inundation = df_no_inundation.loc[df_no_inundation['_merge'] == 'left_only'] if df_no_inundation.empty == False: - df_no_inundation.drop(df_no_inundation.columns.difference(['hydro_id','feature_id','huc8','branch','rc_discharge_cms','note']), axis=1, inplace=True) - df_no_inundation['branch'] = int(branch) - df_no_inundation['huc8'] = int(huc8) + print(f"Adding {len(df_no_inundation)} reaches with NaN inundation to zero_stage table") + df_no_inundation.drop(df_no_inundation.columns.difference(['hand_id','rc_discharge_cms','note']), axis=1, inplace=True) df_no_inundation['note'] = "Error - No inundation returned from hand processing." df_no_inundation.to_sql(f"{db_table}_zero_stage", con=process_db.engine, schema=db_schema, if_exists='append', index=False) # If no records exist for valid inundation, stop. @@ -125,6 +124,7 @@ def lambda_handler(event, context): print(f"Adding data to {db_fim_table}")# Only process inundation configuration if available data try: + df_inundation.drop(columns=['hydro_id', 'feature_id'], inplace=True) df_inundation.to_sql(db_table, con=process_db.engine, schema=db_schema, if_exists='append', index=False) df_inundation_geo.to_postgis(f"{db_table}_geo", con=process_db.engine, schema=db_schema, if_exists='append') except Exception as e: @@ -452,8 +452,6 @@ def process(window): df_final = df_final.rename(columns={"index": "hydro_id"}) df_final['fim_version'] = FIM_VERSION df_final['reference_time'] = reference_time - df_final['huc8'] = huc8 - df_final['branch'] = branch df_final['forecast_stage_ft'] = round(df_final['stage_m'] * 3.28084, 2) df_final['prc_method'] = 'HAND_Processing' diff --git a/Core/LAMBDA/viz_functions/main.tf b/Core/LAMBDA/viz_functions/main.tf index 37802ccf..aa761572 100644 --- a/Core/LAMBDA/viz_functions/main.tf +++ b/Core/LAMBDA/viz_functions/main.tf @@ -114,16 +114,6 @@ variable "egis_db_name" { type = string } -variable "viz_redshift_host" { - description = "Hostname of the viz data warehouse redshift cluster." - type = string -} - -variable "viz_redshift_db_name" { - description = "DB Name of the viz data warehouse redshift cluster." - type = string -} - variable "viz_db_user_secret_string" { description = "The secret string of the viz_processing data base user to write/read data as." type = string @@ -134,11 +124,6 @@ variable "egis_db_user_secret_string" { type = string } -variable "viz_redshift_user_secret_string" { - description = "The secret string of the viz_processing data base user to write/read data as." - type = string -} - variable "egis_portal_password" { description = "The password for the egis portal user to publish as." type = string @@ -412,7 +397,7 @@ resource "aws_s3_object" "python_preprocessing_zip_upload" { #### 3GB RAM Version #### ######################### resource "aws_lambda_function" "viz_python_preprocessing_3GB" { - function_name = "hv-vpp-${var.environment}-viz-python-preprocessing-3GB" + function_name = "hv-vpp-${var.environment}-viz-python-preprocessing" description = "Lambda function to create max streamflow files for NWM data" memory_size = 3072 ephemeral_storage { @@ -455,7 +440,7 @@ resource "aws_lambda_function" "viz_python_preprocessing_3GB" { ] tags = { - "Name" = "hv-vpp-${var.environment}-viz_python_preprocessing_3GB" + "Name" = "hv-vpp-${var.environment}-viz-python-preprocessing-3GB" } } @@ -506,7 +491,7 @@ resource "aws_lambda_function" "viz_python_preprocessing_10GB" { ] tags = { - "Name" = "hv-vpp-${var.environment}-viz_python_preprocessing_10GB" + "Name" = "hv-vpp-${var.environment}-viz-python-preprocessing-10GB" } } @@ -641,10 +626,6 @@ resource "aws_lambda_function" "viz_db_postprocess_sql" { VIZ_DB_HOST = var.viz_db_host VIZ_DB_USERNAME = jsondecode(var.viz_db_user_secret_string)["username"] VIZ_DB_PASSWORD = jsondecode(var.viz_db_user_secret_string)["password"] - REDSHIFT_DB_DATABASE = var.viz_redshift_db_name - REDSHIFT_DB_HOST = var.viz_redshift_host - REDSHIFT_DB_USERNAME = jsondecode(var.viz_redshift_user_secret_string)["username"] - REDSHIFT_DB_PASSWORD = jsondecode(var.viz_redshift_user_secret_string)["password"] } } s3_bucket = aws_s3_object.db_postprocess_sql_zip_upload.bucket diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/0_create_or_truncate_tables.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/0_create_or_truncate_tables.sql new file mode 100644 index 00000000..db387952 --- /dev/null +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/0_create_or_truncate_tables.sql @@ -0,0 +1,47 @@ +-- This creates the four tables on a RDS db needed for a cached fim pipeline run. +CREATE TABLE IF NOT EXISTS {db_fim_table}_flows +( + hand_id integer, + hydro_id integer, + feature_id integer, + huc8 integer, + branch bigint, + reference_time text, + discharge_cms double precision, + discharge_cfs double precision, + prc_status text +); + +CREATE TABLE IF NOT EXISTS {db_fim_table} +( + hand_id integer, + forecast_discharge_cfs double precision, + forecast_stage_ft double precision, + rc_discharge_cfs double precision, + rc_previous_discharge_cfs double precision, + rc_stage_ft integer, + rc_previous_stage_ft integer, + max_rc_stage_ft double precision, + max_rc_discharge_cfs double precision, + fim_version text, + reference_time text, + prc_method text +); + +CREATE TABLE IF NOT EXISTS {db_fim_table}_geo ( + hand_id integer, + rc_stage_ft integer, + geom geometry(geometry, 3857) +); + +CREATE TABLE IF NOT EXISTS {db_fim_table}_zero_stage ( + hand_id integer, + rc_discharge_cms double precision, + note text +); + +-- Truncate the tables so they are ready for the FIM Config run +TRUNCATE {db_fim_table}_flows; +TRUNCATE {db_fim_table}; +TRUNCATE {db_fim_table}_geo; +TRUNCATE {db_fim_table}_zero_stage; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/0a_redshift_create_inundation_tables_if_not_exist.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/0a_redshift_create_inundation_tables_if_not_exist.sql deleted file mode 100644 index 089bc63c..00000000 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/0a_redshift_create_inundation_tables_if_not_exist.sql +++ /dev/null @@ -1,54 +0,0 @@ --- This creates the four tables on a Redshift db needed for a cached fim pipeline run. --- These four tables exist on both RDS and Redshift, so any changes here will need to be synced with the RDS version as well - 0b_rds_create_inundation_tables_if_not_exist.sql -CREATE TABLE IF NOT EXISTS {rs_fim_table}_flows -( - feature_id integer, - hydro_id integer, - huc8 INTEGER, - branch bigint, - reference_time text, - discharge_cms double precision, - discharge_cfs double precision, - prc_status text, - PRIMARY KEY("hydro_id", "feature_id", "huc8", "branch") -); - -CREATE TABLE IF NOT EXISTS {rs_fim_table} ( - hydro_id integer, - feature_id integer, - huc8 integer, - branch bigint, - forecast_discharge_cfs double precision, - forecast_stage_ft double precision, - rc_discharge_cfs double precision, - rc_previous_discharge_cfs double precision, - rc_stage_ft double precision, - rc_previous_stage_ft double precision, - max_rc_discharge_cfs double precision, - max_rc_stage_ft double precision, - fim_version text, - reference_time text, - prc_method text, - PRIMARY KEY("hydro_id", "feature_id", "huc8", "branch") -) DISTSTYLE AUTO; - -CREATE TABLE IF NOT EXISTS {rs_fim_table}_geo ( - hydro_id integer, - feature_id integer, - huc8 INTEGER, - branch bigint, - rc_stage_ft integer, - geom_part integer, - geom geometry -) DISTSTYLE AUTO; - -CREATE TABLE IF NOT EXISTS {rs_fim_table}_zero_stage -( - feature_id integer, - hydro_id integer, - huc8 INTEGER, - branch bigint, - rc_discharge_cms double precision, - note text, - PRIMARY KEY("hydro_id", "feature_id", "huc8", "branch") -); diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/0b_rds_create_inundation_tables_if_not_exist.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/0b_rds_create_inundation_tables_if_not_exist.sql deleted file mode 100644 index 115c18ff..00000000 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/0b_rds_create_inundation_tables_if_not_exist.sql +++ /dev/null @@ -1,70 +0,0 @@ --- This creates the four tables on a RDS db needed for a cached fim pipeline run. --- These four tables exist on both RDS and Redshift, so any changes here will need to be synced with the Redshift version as well - 0a_redshift_create_inundation_tables_if_not_exist.sql -CREATE TABLE IF NOT EXISTS {db_fim_table}_flows -( - hydro_id integer, - feature_id integer, - huc8 integer, - branch bigint, - reference_time text, - discharge_cms double precision, - discharge_cfs double precision, - prc_status text -); - -CREATE TABLE IF NOT EXISTS {db_fim_table} -( - hydro_id integer, - feature_id integer, - huc8 integer, - branch bigint, - forecast_discharge_cfs double precision, - forecast_stage_ft double precision, - rc_discharge_cfs double precision, - rc_previous_discharge_cfs double precision, - rc_stage_ft integer, - rc_previous_stage_ft double precision, - max_rc_stage_ft double precision, - max_rc_discharge_cfs double precision, - fim_version text, - reference_time text, - prc_method text -); - -CREATE TABLE IF NOT EXISTS {db_fim_table}_geo ( - hydro_id integer, - feature_id integer, - huc8 integer, - branch bigint, - rc_stage_ft integer, - geom_part integer, - geom geometry(geometry, 3857) -); - -CREATE TABLE IF NOT EXISTS {db_fim_table}_zero_stage ( - hydro_id integer, - feature_id integer, - huc8 integer, - branch bigint, - rc_discharge_cms double precision, - note text -); - - -- Create a view that contains subdivided polygons in WKT text, for import into Redshift - CREATE OR REPLACE VIEW {db_fim_table}_geo_view AS - SELECT fim_subdivide.hydro_id, - fim_subdivide.feature_id, - fim_subdivide.huc8, - fim_subdivide.branch, - fim_subdivide.rc_stage_ft, - 0 AS geom_part, - st_astext(fim_subdivide.geom) AS geom_wkt - FROM ( SELECT fim.hydro_id, - fim.feature_id, - fim.huc8, - fim.branch, - fim.rc_stage_ft, - st_subdivide(fim_geo.geom) AS geom - FROM {db_fim_table} fim - JOIN {db_fim_table}_geo fim_geo ON fim.hydro_id = fim_geo.hydro_id AND fim.feature_id = fim_geo.feature_id AND fim.huc8 = fim_geo.huc8 AND fim.branch = fim_geo.branch AND fim.rc_stage_ft = fim_geo.rc_stage_ft - WHERE fim.prc_method = 'HAND_Processing'::text) fim_subdivide; diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/1a_rds_build_inundation_flows_table.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/1_prepare_fim_config_flows_table.sql similarity index 64% rename from Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/1a_rds_build_inundation_flows_table.sql rename to Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/1_prepare_fim_config_flows_table.sql index 3658f0a8..cdcf22ad 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/1a_rds_build_inundation_flows_table.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/1_prepare_fim_config_flows_table.sql @@ -1,10 +1,9 @@ --- This populates a standardized fim_flows table, filtered to high water threshold, on RDS. This is essentially the domain of a given fim run. +-- This populates a standardized fim_flows table, filtered to high water threshold. This is essentially the domain of a given fim run. -- the prc_status columns is updated throughout the fim run with a status reflecting how fim is calculated for each reach (from ras2fim cache, from hand cache, hand processing, etc.) --- This table is copied to Redshift in the next step (in order to query the cache there), but this table on RDS is the authoritative source as far as the prc_status column goes. -TRUNCATE {db_fim_table}_flows; -INSERT INTO {db_fim_table}_flows (feature_id, hydro_id, huc8, branch, reference_time, discharge_cms, discharge_cfs, prc_status) +INSERT INTO {db_fim_table}_flows (feature_id, hand_id, hydro_id, huc8, branch, reference_time, discharge_cms, discharge_cfs, prc_status) SELECT max_forecast.feature_id, + crosswalk.hand_id, crosswalk.hydro_id, crosswalk.huc8::integer, crosswalk.branch_id AS branch, @@ -13,7 +12,7 @@ SELECT max_forecast.discharge_cfs, 'Pending' AS prc_status FROM {max_flows_table} max_forecast -JOIN derived.recurrence_flows_conus rf ON rf.feature_id=max_forecast.feature_id +JOIN derived.recurrence_flows_{domain} rf ON rf.feature_id=max_forecast.feature_id JOIN derived.fim4_featureid_crosswalk AS crosswalk ON max_forecast.feature_id = crosswalk.feature_id WHERE max_forecast.discharge_cfs >= rf.high_water_threshold AND diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/1b_redshift_copy_inundation_flows.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/1b_redshift_copy_inundation_flows.sql deleted file mode 100644 index c4927f4d..00000000 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/1b_redshift_copy_inundation_flows.sql +++ /dev/null @@ -1,6 +0,0 @@ --- Copy the fim_flows table on RDS to Redshift - this allows querying the hand cache on redshift by joining to this table. -TRUNCATE {rs_fim_table}_flows; -INSERT INTO {rs_fim_table}_flows (feature_id, hydro_id, huc8, branch, reference_time, discharge_cms, discharge_cfs, prc_status) -SELECT - feature_id, hydro_id, huc8, branch, reference_time, discharge_cms, discharge_cfs, prc_status -FROM external_viz_{db_fim_table}_flows; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/3a_rds_ras2fim_insertion.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/2a_query_fim_cache-ras2fim.sql similarity index 57% rename from Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/3a_rds_ras2fim_insertion.sql rename to Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/2a_query_fim_cache-ras2fim.sql index 2bf25280..628b57a5 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/3a_rds_ras2fim_insertion.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/2a_query_fim_cache-ras2fim.sql @@ -1,19 +1,12 @@ -- This SQL queries the ras2fim cache on RDS, and inserts appropriate rows into the fim tables of the given run. -TRUNCATE {db_fim_table}; -TRUNCATE {db_fim_table}_geo; -TRUNCATE {db_fim_table}_zero_stage; - INSERT INTO {db_fim_table}( - hydro_id, feature_id, huc8, branch, forecast_discharge_cfs, + hand_id, forecast_discharge_cfs, rc_discharge_cfs, rc_previous_discharge_cfs, rc_stage_ft, rc_previous_stage_ft, max_rc_stage_ft, max_rc_discharge_cfs, fim_version, reference_time, prc_method ) SELECT - gc.feature_id as hydro_id, - gc.feature_id as feature_id, - fhc.huc8, - NULL as branch, + fs.hand_id, fs.discharge_cfs as forecast_discharge_cfs, gc.discharge_cfs as rc_discharge_cfs, gc.previous_discharge_cfs as rc_previous_discharge_cfs, @@ -26,20 +19,19 @@ SELECT 'Ras2FIM' AS prc_method FROM ras2fim.geocurves gc JOIN {db_fim_table}_flows fs ON fs.feature_id = gc.feature_id -JOIN derived.featureid_huc_crosswalk fhc ON fs.feature_id = fhc.feature_id JOIN ras2fim.max_geocurves mgc ON gc.feature_id = mgc.feature_id -JOIN {db_fim_table} fim ON gc.feature_id = fim.feature_id -WHERE gc.discharge_cfs >= fs.discharge_cfs AND gc.previous_discharge_cfs < fs.discharge_cfs - AND fim.feature_id IS NULL; +JOIN {db_fim_table} fim ON fs.hand_id = fim.hand_id +WHERE gc.discharge_cfs >= fs.discharge_cfs AND gc.previous_discharge_cfs < fs.discharge_cfs; -INSERT INTO {db_fim_table}_geo (hydro_id, feature_id, rc_stage_ft, geom_part, geom) -SELECT fim.hydro_id, fim.feature_id, fim.rc_stage_ft, row_number() OVER ()::integer AS geom_part, ST_Transform(gc.geom, 3857) as geom +INSERT INTO {db_fim_table}_geo (hand_id, rc_stage_ft, geom) +SELECT fim.hand_id, fim.rc_stage_ft, ST_Transform(gc.geom, 3857) as geom FROM {db_fim_table} AS fim -JOIN ras2fim.geocurves AS gc ON fim.feature_id = gc.feature_id AND fim.rc_stage_ft = gc.stage_ft; +JOIN {db_fim_table}_flows fs ON fim.hand_id = fs.hand_id +JOIN ras2fim.geocurves AS gc ON fs.feature_id = gc.feature_id AND fim.rc_stage_ft = gc.stage_ft; -- Update the flows table prc_status column to reflect the features that were inserted from Ras2FIM cache. UPDATE {db_fim_table}_flows AS flows SET prc_status = 'Inserted FROM Ras2FIM Cache' FROM {db_fim_table} AS fim -WHERE flows.feature_id = fim.feature_id AND flows.hydro_id = fim.hydro_id AND flows.huc8 = fim.huc8 AND flows.branch = fim.branch +WHERE flows.hand_id = fim.hand_id AND fim.prc_method = 'Ras2FIM' \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/2a_redshift_query_cached_fim_table.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/2a_redshift_query_cached_fim_table.sql deleted file mode 100644 index abeb6d91..00000000 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/2a_redshift_query_cached_fim_table.sql +++ /dev/null @@ -1,49 +0,0 @@ --- This is the query that pulls cached hand fim from the cache on Redshift. It does this by joining to the just-populated flows table, with WHERE clauses on discharge --- As of right now, feature_id, hydro_id, huc8, branch, and stage combine to represent a primary key in the hand hydrotables, so all of those fields are used in joins --- (I've asked the fim team to hash a single unique id for feature_id, hydro_id, huc8, branch combinations... which will simplify these queries, and hopefully help with performance. -TRUNCATE {rs_fim_table}; -TRUNCATE {rs_fim_table}_geo; -TRUNCATE {rs_fim_table}_zero_stage; -INSERT INTO {rs_fim_table}(hydro_id, feature_id, huc8, branch, forecast_discharge_cfs, rc_discharge_cfs, rc_previous_discharge_cfs, rc_stage_ft, rc_previous_stage_ft, - max_rc_stage_ft, max_rc_discharge_cfs, fim_version, reference_time, prc_method) -SELECT - fs.hydro_id as hydro_id, - fs.feature_id as feature_id, - fs.huc8, - fs.branch, - fs.discharge_cfs AS forecast_discharge_cfs, - cf.rc_discharge_cfs, - cf.rc_previous_discharge_cfs, - cf.rc_stage_ft, - cf.rc_previous_stage_ft, - cfm.max_rc_stage_ft, - cfm.max_rc_discharge_cfs, - cfm.fim_version, - to_char('1900-01-01 00:00:00'::timestamp without time zone, 'YYYY-MM-DD HH24:MI:SS UTC') AS reference_time, - 'Cached' AS prc_method -FROM {rs_fim_table}_flows AS fs -JOIN fim.hydrotable_cached_max AS cfm ON fs.feature_id = cfm.feature_id AND fs.hydro_id = cfm.hydro_id AND fs.huc8 = cfm.huc8 AND fs.branch = cfm.branch -JOIN fim.hydrotable_cached AS cf ON fs.feature_id = cf.feature_id AND fs.hydro_id = cf.hydro_id AND fs.huc8 = cf.huc8 AND fs.branch = cf.branch -WHERE (fs.discharge_cfs <= cf.rc_discharge_cfs AND fs.discharge_cfs > cf.rc_previous_discharge_cfs) OR (fs.discharge_cfs >= cfm.max_rc_discharge_cfs); - -INSERT INTO {rs_fim_table}_geo(hydro_id, feature_id, huc8, branch, rc_stage_ft, geom_part, geom) -SELECT fim.hydro_id, fim.feature_id, fim.huc8, fim.branch, fim.rc_stage_ft, row_number() OVER ()::integer AS geom_part, geom -FROM {rs_fim_table} AS fim -JOIN fim.hydrotable_cached_geo AS cfg ON fim.feature_id = cfg.feature_id AND fim.hydro_id = cfg.hydro_id AND fim.huc8 = cfg.huc8 AND fim.branch = cfg.branch AND fim.rc_stage_ft = cfg.rc_stage_ft; - -INSERT INTO {rs_fim_table}_zero_stage(hydro_id, feature_id, huc8, branch, rc_discharge_cms, note) -SELECT zero_stage.hydro_id, zero_stage.feature_id, zero_stage.huc8, zero_stage.branch, zero_stage.rc_discharge_cms, zero_stage.note -FROM fim.hydrotable_cached_zero_stage AS zero_stage -JOIN {rs_fim_table}_flows AS Status -ON status.feature_id = zero_stage.feature_id AND status.hydro_id = zero_stage.hydro_id AND status.huc8 = zero_stage.huc8 AND status.branch = zero_stage.branch -WHERE (status.discharge_cms <= zero_stage.rc_discharge_cms) OR zero_stage.rc_discharge_cms = 0; - -UPDATE {rs_fim_table}_flows AS status -SET prc_status = 'Cached' -FROM {rs_fim_table} AS fim -WHERE status.feature_id = fim.feature_id AND status.hydro_id = fim.hydro_id AND status.huc8 = fim.huc8 AND status.branch = fim.branch; - -UPDATE {rs_fim_table}_flows AS status -SET prc_status = 'Zero_Stage' -FROM {rs_fim_table}_zero_stage AS zero_stage -WHERE status.feature_id = zero_stage.feature_id AND status.hydro_id = zero_stage.hydro_id AND status.huc8 = zero_stage.huc8 AND status.branch = zero_stage.branch; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/2b_query_fim_cache-hand.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/2b_query_fim_cache-hand.sql new file mode 100644 index 00000000..e3b381d7 --- /dev/null +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/2b_query_fim_cache-hand.sql @@ -0,0 +1,39 @@ +-- Query the hand cache. +INSERT INTO {db_fim_table}(hand_id, forecast_discharge_cfs, rc_discharge_cfs, rc_previous_discharge_cfs, rc_stage_ft, rc_previous_stage_ft, + max_rc_stage_ft, max_rc_discharge_cfs, fim_version, reference_time, prc_method) +SELECT + fs.hand_id, + fs.discharge_cfs AS forecast_discharge_cfs, + cf.rc_discharge_cfs, + cf.rc_previous_discharge_cfs, + cf.rc_stage_ft, + cf.rc_previous_stage_ft, + cfm.max_rc_stage_ft, + cfm.max_rc_discharge_cfs, + cfm.fim_version, + to_char('1900-01-01 00:00:00'::timestamp without time zone, 'YYYY-MM-DD HH24:MI:SS UTC') AS reference_time, + 'Cached' AS prc_method +FROM {db_fim_table}_flows AS fs +JOIN fim_cache.hand_hydrotable_cached_max AS cfm ON fs.hand_id = cfm.hand_id +JOIN fim_cache.hand_hydrotable_cached AS cf ON fs.hand_id = cf.hand_id +WHERE fs.prc_status = 'Pending' AND ((fs.discharge_cfs <= cf.rc_discharge_cfs AND fs.discharge_cfs > cf.rc_previous_discharge_cfs) + OR ((fs.discharge_cfs >= cfm.max_rc_discharge_cfs) AND rc_stage_ft = 83)); + +INSERT INTO {db_fim_table}_geo(hand_id, rc_stage_ft, geom) +SELECT fim.hand_id, fim.rc_stage_ft, geom +FROM {db_fim_table} AS fim +JOIN fim_cache.hand_hydrotable_cached_geo AS cfg ON fim.hand_id = cfg.hand_id AND fim.rc_stage_ft = cfg.rc_stage_ft +WHERE fim.prc_method = 'Cached'; + +-- Update the flows table prc_status column to reflect the features that were inserted from cache. +UPDATE {db_fim_table}_flows AS flows +SET prc_status = 'Inserted FROM HAND Cache' +FROM {db_fim_table} AS fim +WHERE flows.hand_id = fim.hand_id + AND fim.prc_method = 'Cached'; + +-- Update the flows table prc_status column to reflect the features that were inserted from cache. +UPDATE {db_fim_table}_flows AS flows +SET prc_status = 'HAND Cache - Zero Stage' +FROM fim_cache.hand_hydrotable_cached_zero_stage AS zero_stage +WHERE flows.hand_id = zero_stage.hand_id AND flows.prc_status = 'Pending' AND ((flows.discharge_cms <= zero_stage.rc_discharge_cms) OR zero_stage.rc_discharge_cms = 0); \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/3_add_any_processed_hand_fim_back_to_cache.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/3_add_any_processed_hand_fim_back_to_cache.sql new file mode 100644 index 00000000..3ec143f2 --- /dev/null +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/3_add_any_processed_hand_fim_back_to_cache.sql @@ -0,0 +1,50 @@ +-- This template is designed to add freshly processed FIM polygons (which don't already exist in the cache) in the current FIM run back into to the cached hand tables. +-- To ensure that no duplicates are added to the cache (which could be possible if multiple fim configurations are running at the same time), this query joins to the target table and ensures that +-- the current hydrotable record doesn't alraedy exist in the cache. + +-- 1. Add unique hand_id records to the hydrotable_cached_max table +INSERT INTO fim_cache.hand_hydrotable_cached_max(hand_id, fim_version, max_rc_discharge_cfs, max_rc_stage_ft) +SELECT + fim.hand_id, + fim.fim_version, + fim.max_rc_discharge_cfs, + fim.max_rc_stage_ft +FROM {db_fim_table} AS fim +LEFT OUTER JOIN fim_cache.hand_hydrotable_cached_max AS hcm ON fim.hand_id = hcm.hand_id +WHERE fim.prc_method = 'HAND_Processing' AND +hcm.hand_id IS NULL +GROUP BY fim.hand_id, fim.fim_version, fim.max_rc_discharge_cfs, fim.max_rc_stage_ft; + +-- 2. Add records for each stage_ft step of the hydrotable to the hydrotable_cached table +INSERT INTO fim_cache.hand_hydrotable_cached (hand_id, rc_discharge_cfs, rc_previous_discharge_cfs, rc_stage_ft, rc_previous_stage_ft) +SELECT + fim.hand_id, + fim.rc_discharge_cfs, + fim.rc_previous_discharge_cfs, + fim.rc_stage_ft, + fim.rc_previous_stage_ft +FROM {db_fim_table} AS fim +LEFT OUTER JOIN fim_cache.hand_hydrotable_cached AS hc ON fim.hand_id = hc.hand_id AND fim.rc_stage_ft = hc.rc_stage_ft +WHERE fim.prc_method = 'HAND_Processing' AND +hc.rc_stage_ft IS NULL; + +-- 3. Add records for each geometry to hydrotable_cached_geo table +INSERT INTO fim_cache.hand_hydrotable_cached_geo (hand_id, rc_stage_ft, geom) +SELECT + fim_geo.hand_id, + fim_geo.rc_stage_ft, + fim_geo.geom +FROM {db_fim_table}_geo AS fim_geo +JOIN {db_fim_table} AS fim ON fim_geo.hand_id = fim.hand_id +LEFT OUTER JOIN fim_cache.hand_hydrotable_cached_geo AS hcg ON fim_geo.hand_id = hcg.hand_id AND fim_geo.rc_stage_ft = hcg.rc_stage_ft +WHERE fim.prc_method = 'HAND_Processing' AND hcg.rc_stage_ft IS NULL; + +-- 4. Add records for zero_stage features to zero stage table +INSERT INTO fim_cache.hand_hydrotable_cached_zero_stage (hand_id, rc_discharge_cms, note) +SELECT + fim.hand_id, + fim.rc_discharge_cms, + fim.note +FROM {db_fim_table}_zero_stage AS fim +LEFT OUTER JOIN fim_cache.hand_hydrotable_cached_zero_stage AS hczs ON fim.hand_id = hczs.hand_id +WHERE hczs.rc_discharge_cms IS NULL; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/3b_rds_cached_hand_insertion.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/3b_rds_cached_hand_insertion.sql deleted file mode 100644 index b62f0b5a..00000000 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/3b_rds_cached_hand_insertion.sql +++ /dev/null @@ -1,38 +0,0 @@ --- This SQL queries the just-updated hand cache table on RDS, and inserts appropriate rows into the fim tables of the given run. -INSERT INTO {db_fim_table}( - SELECT * FROM dblink('external_vpp_redshift', $REDSHIFT$ - SELECT hydro_id, feature_id, huc8, branch, forecast_discharge_cfs, forecast_stage_ft, rc_discharge_cfs, - rc_previous_discharge_cfs, rc_stage_ft, rc_previous_stage_ft, max_rc_stage_ft, max_rc_discharge_cfs, - fim_version, reference_time, prc_method - FROM {rs_fim_table}; - $REDSHIFT$) AS t1 (hydro_id integer, feature_id integer, huc8 integer, branch bigint, forecast_discharge_cfs double precision, forecast_stage_ft double precision, rc_discharge_cfs double precision, - rc_previous_discharge_cfs double precision, rc_stage_ft integer, rc_previous_stage_ft double precision, max_rc_stage_ft double precision, max_rc_discharge_cfs double precision, - fim_version text, reference_time text, prc_method text) -); - -INSERT INTO {db_fim_table}_geo( - SELECT * FROM dblink('external_vpp_redshift', $REDSHIFT$ - SELECT hydro_id, feature_id, huc8, branch, rc_stage_ft, geom_part, geom - FROM {rs_fim_table}_geo; - $REDSHIFT$) AS t1 (hydro_id integer, feature_id integer, huc8 integer, branch bigint, rc_stage_ft integer, geom_part integer, geom geometry) -); - -INSERT INTO {db_fim_table}_zero_stage( - SELECT * FROM dblink('external_vpp_redshift', $REDSHIFT$ - SELECT hydro_id , feature_id, huc8, branch, rc_discharge_cms, note - FROM {rs_fim_table}_zero_stage; - $REDSHIFT$) AS t1 (hydro_id integer, feature_id integer, huc8 integer, branch bigint, rc_discharge_cms double precision, note text) -); - --- Update the flows table prc_status column to reflect the features that were inserted from Redshift cache. -UPDATE {db_fim_table}_flows AS flows -SET prc_status = 'Inserted FROM HAND Cache' -FROM {db_fim_table} AS fim -WHERE flows.feature_id = fim.feature_id AND flows.hydro_id = fim.hydro_id AND flows.huc8 = fim.huc8 AND flows.branch = fim.branch - AND fim.prc_method = 'Cached' - --- Update the flows table prc_status column to reflect the features that were inserted from Redshift cache. -UPDATE {db_fim_table}_flows AS flows -SET prc_status = 'Inserted FROM HAND Cache - Zero Stage' -FROM {db_fim_table}_zero_stage AS fim -WHERE flows.feature_id = fim.feature_id AND flows.hydro_id = fim.hydro_id AND flows.huc8 = fim.huc8 AND flows.branch = fim.branch \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/4a_rds_create_fim_publish_table.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/4_create_fim_config_publish_table.sql similarity index 54% rename from Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/4a_rds_create_fim_publish_table.sql rename to Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/4_create_fim_config_publish_table.sql index a90c72f2..1db27613 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/4a_rds_create_fim_publish_table.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/4_create_fim_config_publish_table.sql @@ -2,29 +2,27 @@ DROP TABLE IF EXISTS {db_publish_table}; SELECT - inun.hydro_id, - inun.hydro_id::TEXT AS hydro_id_str, - inun.feature_id, - inun.feature_id::TEXT AS feature_id_str, - inun.huc8, - inun.branch, + flows.hydro_id, + flows.hydro_id::TEXT AS hydro_id_str, + flows.feature_id, + flows.feature_id::TEXT AS feature_id_str, + LPAD(flows.huc8::TEXT, 8, '0') AS huc8, + flows.branch::TEXT AS branch, channels.strm_order, channels.name, channels.state, inun.forecast_discharge_cfs as streamflow_cfs, inun.rc_discharge_cfs, - inun.forecast_stage_ft as fim_stage_ft, - inun.rc_stage_ft, + inun.rc_stage_ft as fim_stage_ft, inun.max_rc_stage_ft, inun.max_rc_discharge_cfs, inun.fim_version, to_char('1900-01-01 00:00:00'::timestamp without time zone, 'YYYY-MM-DD HH24:MI:SS UTC') AS reference_time, to_char('1900-01-01 00:00:00'::timestamp without time zone, 'YYYY-MM-DD HH24:MI:SS UTC') AS valid_time, to_char(now()::timestamp without time zone, 'YYYY-MM-DD HH24:MI:SS UTC') AS update_time, - ST_COLLECT(geo.geom) as geom + geo.geom as geom INTO {db_publish_table} -FROM {db_fim_table} as inun -JOIN {db_fim_table}_geo as geo ON inun.feature_id = geo.feature_id AND inun.hydro_id = geo.hydro_id AND inun.huc8 = geo.huc8 AND inun.branch = geo.branch -LEFT JOIN derived.channels_{domain} as channels ON channels.feature_id = inun.feature_id -GROUP BY inun.hydro_id, inun.feature_id, inun.huc8, inun.branch, channels.strm_order, channels.name, channels.state, inun.forecast_discharge_cfs, inun.forecast_stage_ft, - inun.rc_discharge_cfs,inun.rc_stage_ft,inun.max_rc_stage_ft,inun.max_rc_discharge_cfs,inun.fim_version; \ No newline at end of file +FROM {db_fim_table} as inun +JOIN {db_fim_table}_flows as flows ON inun.hand_id = flows.hand_id +JOIN {db_fim_table}_geo as geo ON inun.hand_id = geo.hand_id +LEFT JOIN derived.channels_{domain} as channels ON channels.feature_id = flows.feature_id; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/5_dummy_rows_for_wpod_service_monitor.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/5_dummy_rows_for_wpod_service_monitor.sql new file mode 100644 index 00000000..b1ccc05e --- /dev/null +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/5_dummy_rows_for_wpod_service_monitor.sql @@ -0,0 +1,19 @@ +-- Add an empty row so that service monitor will pick up a reference and update time in the event of no fim features +-- This is far from ideal +INSERT INTO {db_publish_table}( + hydro_id, hydro_id_str, geom, branch, feature_id, feature_id_str, streamflow_cfs, fim_stage_ft, max_rc_stage_ft, max_rc_discharge_cfs, fim_version, reference_time, huc8) + VALUES (-9999, '-9999', NULL, 'NA', -9999, '-9999', -9999, -9999, -9999, -9999, 'NA', to_char('1900-01-01 00:00:00'::timestamp without time zone, 'YYYY-MM-DD HH24:MI:SS UTC'), '-9999'); + +-- These needs to be added as well, so that the summary queries don't fail because of the dependent tables being empty - +-- There shouldn't be any harm to the fim_config, since this happens at the end... but far from ideal. +INSERT INTO {db_fim_table}_flows( + hydro_id, branch, feature_id, huc8, reference_time) + VALUES (-9999, -9999, -9999, -9999, to_char('1900-01-01 00:00:00'::timestamp without time zone, 'YYYY-MM-DD HH24:MI:SS UTC')); + +INSERT INTO {db_fim_table}( + hand_id, reference_time) + VALUES (-9999, to_char('1900-01-01 00:00:00'::timestamp without time zone, 'YYYY-MM-DD HH24:MI:SS UTC')); + +INSERT INTO {db_fim_table}_geo( + hand_id) + VALUES (-9999); \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/5a_redshift_cache_fim_from_rds.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/5a_redshift_cache_fim_from_rds.sql deleted file mode 100644 index a103c7cb..00000000 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_caching_templates/5a_redshift_cache_fim_from_rds.sql +++ /dev/null @@ -1,65 +0,0 @@ --- This template is designed to add freshly processed FIM polygons (which don't already exist in the cache) in the current FIM run back into to the cached hand tables on Redshift. --- To ensure that no duplicates are added to the cache (which could be possible if multiple fim configurations are running at the same time), this query joins to the target table and ensures that --- the current hydrotable record doesn't alraedy exist in the cache. This slows down the query significantly, and there is likely a potential optimization here... possibly using the UPSERT functionality of Redshift. --- As of right now, feature_id, hydro_id, huc8, branch, and stage combine to represent a primary key in the hand hydrotables, so all of those fields are used in joins --- (I've asked the fim team to hash a single unique id for feature_id, hydro_id, huc8, branch combinations... which will simplify these queries, and hopefully help with performance. - --- 1. Add unique feature_id/hydro_id records to the hydrotable_cached_max table -INSERT INTO fim.hydrotable_cached_max(hydro_id, feature_id, huc8, branch, fim_version, max_rc_discharge_cfs, max_rc_stage_ft) -SELECT - fim.hydro_id, - fim.feature_id, - fim.huc8, - fim.branch, - fim.fim_version, - fim.max_rc_discharge_cfs, - fim.max_rc_stage_ft -FROM {postgis_fim_table} AS fim -LEFT OUTER JOIN fim.hydrotable_cached_max AS hcm ON fim.hydro_id = hcm.hydro_id AND fim.feature_id = hcm.feature_id AND fim.huc8 = hcm.huc8 AND fim.branch = hcm.branch -WHERE fim.prc_method = 'HAND_Processing' AND -hcm.hydro_id IS NULL -GROUP BY fim.hydro_id, fim.feature_id, fim.huc8, fim.branch, fim.fim_version, fim.max_rc_discharge_cfs, fim.max_rc_stage_ft; - --- 2. Add records for each step of the hydrotable to the hydrotable_cached table -INSERT INTO fim.hydrotable_cached (hydro_id, feature_id, huc8, branch, rc_discharge_cfs, rc_previous_discharge_cfs, rc_stage_ft, rc_previous_stage_ft) -SELECT - fim.hydro_id, - fim.feature_id, - fim.huc8, - fim.branch, - fim.rc_discharge_cfs, - fim.rc_previous_discharge_cfs, - fim.rc_stage_ft, - fim.rc_previous_stage_ft -FROM {postgis_fim_table} AS fim -LEFT OUTER JOIN fim.hydrotable_cached AS hc ON fim.hydro_id = hc.hydro_id AND fim.rc_stage_ft = hc.rc_stage_ft AND fim.feature_id = hc.feature_id AND fim.huc8 = hc.huc8 AND fim.branch = hc.branch -WHERE fim.prc_method = 'HAND_Processing' AND -hc.rc_stage_ft IS NULL; - --- 3. Add records for each subdivided part of the geometry to hydrotable_cached_geo table -INSERT INTO fim.hydrotable_cached_geo (hydro_id, feature_id, huc8, branch, rc_stage_ft, geom_part, geom) -SELECT - fim.hydro_id, - fim.feature_id, - fim.huc8, - fim.branch, - fim.rc_stage_ft, - fim.geom_part, - ST_GeomFromText(geom_wkt) -FROM {postgis_fim_table}_geo_view AS fim -JOIN fim.hydrotable_cached_max AS hcm ON fim.hydro_id = hcm.hydro_id AND fim.feature_id = hcm.feature_id AND fim.huc8 = hcm.huc8 AND fim.branch = hcm.branch -LEFT OUTER JOIN fim.hydrotable_cached_geo AS hcg ON fim.hydro_id = hcg.hydro_id AND fim.rc_stage_ft = hcg.rc_stage_ft AND fim.feature_id = hcg.feature_id AND fim.huc8 = hcg.huc8 AND fim.branch = hcg.branch -WHERE hcg.rc_stage_ft IS NULL; - --- 4. Add records for zero_stage features to zero stage table -INSERT INTO fim.hydrotable_cached_zero_stage (hydro_id, feature_id, huc8, branch, rc_discharge_cms, note) -SELECT - fim.hydro_id, - fim.feature_id, - fim.huc8, - fim.branch, - fim.rc_discharge_cms, - fim.note -FROM {postgis_fim_table}_zero_stage AS fim -LEFT OUTER JOIN fim.hydrotable_cached_zero_stage AS hczs ON fim.hydro_id = hczs.hydro_id AND fim.feature_id = hczs.feature_id AND fim.huc8 = hczs.huc8 AND fim.branch = hczs.branch -WHERE hczs.rc_discharge_cms IS NULL; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_configs/rfc_based_5day_max_inundation.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_configs/rfc_based_5day_max_inundation.sql index e70c9984..a85c162f 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_configs/rfc_based_5day_max_inundation.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_configs/rfc_based_5day_max_inundation.sql @@ -1,12 +1,12 @@ DROP TABLE IF EXISTS publish.rfc_based_5day_max_inundation; SELECT - inun.hydro_id, - inun.hydro_id::TEXT AS hydro_id_str, - inun.feature_id, - inun.feature_id::TEXT AS feature_id_str, - inun.huc8, - inun.branch, + flows.hydro_id, + flows.hydro_id::TEXT AS hydro_id_str, + flows.feature_id, + flows.feature_id::TEXT AS feature_id_str, + flows.huc8, + flows.branch, channels.strm_order, channels.name, channels.state, @@ -21,11 +21,10 @@ SELECT rnr_flow.viz_status AS max_status, to_char('1900-01-01 00:00:00'::timestamp without time zone, 'YYYY-MM-DD HH24:MI:SS UTC') AS reference_time, to_char(now()::timestamp without time zone, 'YYYY-MM-DD HH24:MI:SS UTC') AS update_time, - ST_COLLECT(geo.geom) as geom + geo.geom INTO publish.rfc_based_5day_max_inundation -FROM ingest.rfc_based_5day_max_inundation as inun -JOIN ingest.rfc_based_5day_max_inundation_geo as geo ON inun.feature_id = geo.feature_id AND inun.hydro_id = geo.hydro_id AND inun.huc8 = geo.huc8 AND inun.branch = geo.branch -JOIN publish.rfc_based_5day_max_streamflow rnr_flow ON rnr_flow.feature_id = inun.feature_id -LEFT JOIN derived.channels_conus as channels ON channels.feature_id = inun.feature_id -GROUP BY inun.hydro_id, inun.feature_id, inun.huc8, inun.branch, channels.strm_order, channels.name, channels.state, inun.forecast_discharge_cfs, - inun.rc_discharge_cfs,inun.rc_stage_ft,inun.max_rc_stage_ft,inun.max_rc_discharge_cfs,inun.fim_version; \ No newline at end of file +FROM fim_ingest.rfc_based_5day_max_inundation as inun +JOIN fim_ingest.rfc_based_5day_max_inundation_geo as geo ON inun.hand_id = geo.hand_id +JOIN fim_ingest.rfc_based_5day_max_inundation_flows as flows ON inun.hand_id = flows.hand_id +JOIN publish.rfc_based_5day_max_streamflow rnr_flow ON rnr_flow.feature_id = flows.feature_id +LEFT JOIN derived.channels_conus as channels ON channels.feature_id = flows.feature_id; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_flows/rfc_based_5day_max_inundation.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_flows/rfc_based_5day_max_inundation.sql index 3ef9c399..c86b1b8a 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_flows/rfc_based_5day_max_inundation.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/fim_flows/rfc_based_5day_max_inundation.sql @@ -1,7 +1,25 @@ +-- This populates a standardized fim_flows table, filtered to high water threshold, on RDS. This is essentially the domain of a given fim run. +-- the prc_status columns is updated throughout the fim run with a status reflecting how fim is calculated for each reach (from ras2fim cache, from hand cache, hand processing, etc.) +TRUNCATE fim_ingest.rfc_based_5day_max_inundation_flows; +INSERT INTO fim_ingest.rfc_based_5day_max_inundation_flows (feature_id, hand_id, hydro_id, huc8, branch, reference_time, discharge_cms, discharge_cfs, prc_status) SELECT - rnr.feature_id, - streamflow_cms -FROM publish.rfc_based_5day_max_streamflow rnr -WHERE is_waterbody = 'no' - AND is_downstream_of_waterbody = 'no' - AND viz_status IN ('Action', 'Minor', 'Moderate', 'Major') \ No newline at end of file + max_forecast.feature_id, + crosswalk.hand_id, + crosswalk.hydro_id, + crosswalk.huc8::integer, + crosswalk.branch_id AS branch, + to_char('1900-01-01 00:00:00'::timestamp without time zone, 'YYYY-MM-DD HH24:MI:SS UTC') AS reference_time, + max_forecast.streamflow_cms AS discharge_cms, + max_forecast.streamflow AS discharge_cfs, + 'Pending' AS prc_status +FROM publish.rfc_based_5day_max_streamflow max_forecast +JOIN derived.recurrence_flows_conus rf ON rf.feature_id=max_forecast.feature_id +JOIN derived.fim4_featureid_crosswalk AS crosswalk ON max_forecast.feature_id = crosswalk.feature_id +WHERE + max_forecast.streamflow >= rf.high_water_threshold AND + rf.high_water_threshold > 0::double precision AND + crosswalk.huc8 IS NOT NULL AND + crosswalk.lake_id = -999 AND + max_forecast.is_waterbody = 'no' AND + max_forecast.is_downstream_of_waterbody = 'no' AND + viz_status IN ('Action', 'Minor', 'Moderate', 'Major'); \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/lambda_function.py b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/lambda_function.py index bf40edb8..4a88c025 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/lambda_function.py +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/lambda_function.py @@ -16,21 +16,30 @@ def lambda_handler(event, context): if event['args']['product']['configuration'] == "reference": return - # For FIM steps, setup some other variables based on the step function inputs and provide them to the sql_replace dictionary as args/params - if step == "hand_pre_processing" or step == "hand_post_processing" or step == "fim_config": - # Define the table names that will be used in the SQL templates - # TODO: Move this to initialize pipeline and Update this to work with past event functionality? - max_flows_table = event['args']['fim_config']['flows_table'] - db_fim_table = event['args']['fim_config']['target_table'] - rs_fim_table = db_fim_table.replace("ingest", "ingest_rs") - domain = event['args']['product']['domain'] - sql_replace.update({"{max_flows_table}":max_flows_table}) - sql_replace.update({"{db_fim_table}":db_fim_table}) - sql_replace.update({"{rs_fim_table}":rs_fim_table}) - sql_replace.update({"{domain}":domain}) - sql_replace.update({"{postgis_fim_table}":db_fim_table.replace("ingest", "external_viz_ingest")}) - sql_replace.update({"{db_publish_table}":db_fim_table.replace("ingest", "publish")}) - + # For FIM steps that require template use, setup some other variables based on the step function inputs and provide them to the sql_replace dictionary as args/params + # TODO: This is probably better to move to initialize pipeline and/or abstract these conditions / have the steps that need this specify it in the initilize_pipeline config files + if "hand_pre_processing" in step or step == "hand_post_processing" or step == "fim_config": + if event['args']['fim_config']['fim_type'] == 'hand': + + # Define the table names that will be used in the SQL templates + max_flows_table = event['args']['fim_config']['flows_table'] + db_fim_table = event['args']['fim_config']['target_table'] + domain = event['args']['product']['domain'] + db_publish_table = db_fim_table.replace("fim_ingest", "publish") + + # If these tables exist in the sql_replace dictionary, update accordingly (for past events) + if max_flows_table in sql_replace: + max_flows_table = sql_replace[max_flows_table] + if db_fim_table in sql_replace: + db_fim_table = sql_replace[db_fim_table] + if db_publish_table in sql_replace: + db_publish_table = sql_replace[db_publish_table] + + sql_replace.update({"{max_flows_table}": max_flows_table}) + sql_replace.update({"{db_fim_table}": db_fim_table}) + sql_replace.update({"{domain}": domain}) + sql_replace.update({"{db_publish_table}": db_publish_table}) + ############################################################ Conditional Logic ########################################################## # This section contains the conditional logic of database operations within our pipelline. At some point it may be nice to abstract this. @@ -44,7 +53,7 @@ def lambda_handler(event, context): if step == "max_flows": db_type = "viz" sql_file = event['args']['db_max_flow']['max_flows_sql_file'] - sql_files_to_run.append({"sql_file":sql_file, "db_type":db_type}) + sql_files_to_run.append({"sql_file":sql_file, "folder": folder, "db_type":db_type}) ###################### FIM Workflows ###################### # All of the pre and post processing steps of a fim workflow (everything but step 4) - see attached readme - are templated, and can be executed using the input parameters defined in the step function @@ -52,6 +61,20 @@ def lambda_handler(event, context): # Get the sql file instructions from the step function parameter dictionary, and add it to the list to run sql_templates_to_run = event['sql_templates_to_run'] sql_files_to_run.extend(sql_templates_to_run) + + elif step == "hand_pre_processing - prepare flows": + # If a sql file is present for this fim_config in the fim_flows folder, use it to generate flows table instead of the template. + fim_flows_file = os.path.join("fim_flows", event['args']['fim_config']['name'] + '.sql') + if os.path.exists(fim_flows_file): + sql_file = event['args']['fim_config']['name'] + sql_files_to_run.append({"sql_file":sql_file, "folder": "fim_flows", "db_type":"viz"}) + + # If a flows column is present in the fim_config, add that to the sql replace dictionary. + if 'flows_column' in event['args']['fim_config']: + sql_replace.update({"{flows_column}": event['args']['fim_config']['flows_column']}) + else: + sql_templates_to_run = event['sql_templates_to_run'] + sql_files_to_run.extend(sql_templates_to_run) # FIM Config Step 4 - This is where we actually create the publish inundation tables to send to the EGIS service, and in this case we look to # see if a product-specific sql file exists (for special cases like RnR, CatFIM, etc.), and if not, we use a template file. @@ -60,13 +83,12 @@ def lambda_handler(event, context): return db_type = "viz" sql_file = event['args']['fim_config']['postprocess']['sql_file'] - if os.path.exists(os.path.join(folder, sql_file)): #if there is product-specific fim_configs sql file, use it. - sql_files_to_run.append({"sql_file":sql_file, "db_type":db_type}) + if os.path.exists(os.path.join("fim_configs", sql_file + '.sql')): #if there is product-specific fim_configs sql file, use it. + sql_files_to_run.append({"sql_file":sql_file, "folder": "fim_configs", "db_type":db_type}) else: # if not, use the fim_publish_template folder = 'fim_caching_templates' - sql_file = '4a_rds_create_fim_publish_table' - sql_replace.update({"{domain}":domain}) - sql_files_to_run.append({"sql_file":sql_file, "db_type":db_type}) + sql_file = '4_create_fim_config_publish_table' + sql_files_to_run.append({"sql_file":sql_file, "folder": folder, "db_type":db_type, "check_dependencies": False}) ########################################################## @@ -75,18 +97,19 @@ def lambda_handler(event, context): db_type = "viz" folder = os.path.join(folder, event['args']['product']['configuration']) sql_file = event['args']['postprocess_sql']['sql_file'] - sql_files_to_run.append({"sql_file":sql_file, "db_type":db_type}) + sql_files_to_run.append({"sql_file":sql_file, "folder": folder, "db_type":db_type}) # Summary elif step == 'summaries': db_type = "viz" folder = os.path.join(folder, event['args']['product']['product']) - sql_file = event['args']['postprocess_summary']['sql_file'] - sql_files_to_run.append({"sql_file":sql_file, "db_type":db_type}) + sql_file = event['args']['postprocess_summary']['sql_file'] + sql_files_to_run.append({"sql_file":sql_file, "folder": folder, "db_type":db_type}) ############################################################ Run the SQL ########################################################## # Iterate through the sql commands defined in the logic above for sql_file_to_run in sql_files_to_run: sql_file = sql_file_to_run['sql_file'] + folder = sql_file_to_run['folder'] db_type = sql_file_to_run['db_type'] if 'check_dependencies' in sql_file_to_run: # This allows one to set a specific step to not check db dependences, which we currently want to avoid on Redshift and Hand Preprocessing steps (since tables are truncated prior) check_dependencies = sql_file_to_run['check_dependencies'] @@ -98,7 +121,8 @@ def lambda_handler(event, context): # Checks if all tables references in sql file exist and are updated (if applicable) # Raises a custom RequiredTableNotUpdated if not, which will be caught by viz_pipline # and invoke a retry - # TODO: I do not currently have this setup for Redshift, need to think that through. + # TODO: This doesn't work great with the new FIM_Caching templates, so I'm presently skipping it on several of the FIM steps.' + # I'll try to re-work this if there is time, but we need a way to ignore certain types of this error when appropriate, such as no fim records in HI, which happens often. database(db_type=db_type).check_required_tables_updated(sql_path, sql_replace, reference_time, raise_if_false=True) run_sql(sql_path, sql_replace, db_type=db_type) diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/ana_14day_max_flows.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/ana_14day_max_flows.sql index 38aa7aa1..cadb1cca 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/ana_14day_max_flows.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/ana_14day_max_flows.sql @@ -12,7 +12,7 @@ TRUNCATE TABLE cache.max_flows_ana_14day; INSERT INTO cache.max_flows_ana_14day(feature_id, reference_time, nwm_vers, discharge_cms, discharge_cfs) SELECT max_14day_forecast.feature_id, max_14day_forecast.reference_time, - max_14day_forecast.nwm_vers, + REPLACE(max_14day_forecast.nwm_vers, 'v', '')::double precision as nwm_vers, --TODO, not sure why this isn't happening on the ingest side like everything else. ROUND(max_14day_forecast.streamflow::numeric, 2) AS discharge_cms, ROUND((max_14day_forecast.streamflow * 35.315)::numeric, 2) AS discharge_cfs FROM ingest.nwm_channel_rt_ana_14day_max AS max_14day_forecast; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/ana_7day_max_flows.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/ana_7day_max_flows.sql index 6216990a..5dcd08a0 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/ana_7day_max_flows.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/ana_7day_max_flows.sql @@ -12,7 +12,7 @@ TRUNCATE TABLE cache.max_flows_ana_7day; INSERT INTO cache.max_flows_ana_7day(feature_id, reference_time, nwm_vers, discharge_cms, discharge_cfs) SELECT max_7day_forecast.feature_id, max_7day_forecast.reference_time, - max_7day_forecast.nwm_vers, + REPLACE(max_7day_forecast.nwm_vers, 'v', '')::double precision as nwm_vers, --TODO, not sure why this isn't happening on the ingest side like everything else. ROUND(max_7day_forecast.streamflow::numeric, 2) AS discharge_cms, ROUND((max_7day_forecast.streamflow * 35.315)::numeric, 2) AS discharge_cfs FROM ingest.nwm_channel_rt_ana_7day_max AS max_7day_forecast; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_10day_max_flows.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_10day_max_flows.sql index ef0169da..06ef829b 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_10day_max_flows.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_10day_max_flows.sql @@ -15,5 +15,4 @@ INSERT INTO cache.max_flows_mrf_gfs_10day(feature_id, reference_time, nwm_vers, round(max(forecasts.streamflow)::numeric, 2) AS discharge_cms, round(max(forecasts.streamflow * 35.315)::numeric, 2) AS discharge_cfs FROM ingest.nwm_channel_rt_mrf_gfs_mem1 forecasts - WHERE forecasts.forecast_hour > 120 GROUP BY forecasts.feature_id, forecasts.reference_time, forecasts.nwm_vers; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_10day_max_flows_ak.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_10day_max_flows_ak.sql index eb10a38c..3a2aaaaf 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_10day_max_flows_ak.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_10day_max_flows_ak.sql @@ -15,5 +15,4 @@ INSERT INTO cache.max_flows_mrf_gfs_10day_ak(feature_id, reference_time, nwm_ver round(max(forecasts.streamflow)::numeric, 2) AS discharge_cms, round(max(forecasts.streamflow * 35.315)::numeric, 2) AS discharge_cfs FROM ingest.nwm_channel_rt_mrf_gfs_ak_mem1 forecasts - WHERE forecasts.forecast_hour > 120 GROUP BY forecasts.feature_id, forecasts.reference_time, forecasts.nwm_vers; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_5day_max_flows.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_5day_max_flows.sql index 66ad335e..010f9ab6 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_5day_max_flows.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_5day_max_flows.sql @@ -15,5 +15,5 @@ INSERT INTO cache.max_flows_mrf_gfs_5day(feature_id, reference_time, nwm_vers, d round(max(forecasts.streamflow)::numeric, 2) AS discharge_cms, round(max(forecasts.streamflow * 35.315)::numeric, 2) AS discharge_cfs FROM ingest.nwm_channel_rt_mrf_gfs_mem1 forecasts - WHERE forecasts.forecast_hour > 72 AND forecasts.forecast_hour <= 120 + WHERE forecasts.forecast_hour <= 120 GROUP BY forecasts.feature_id, forecasts.reference_time, forecasts.nwm_vers; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_5day_max_flows_ak.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_5day_max_flows_ak.sql index 22dd54cc..3983c18f 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_5day_max_flows_ak.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_5day_max_flows_ak.sql @@ -15,5 +15,5 @@ INSERT INTO cache.max_flows_mrf_gfs_5day_ak(feature_id, reference_time, nwm_vers round(max(forecasts.streamflow)::numeric, 2) AS discharge_cms, round(max(forecasts.streamflow * 35.315)::numeric, 2) AS discharge_cfs FROM ingest.nwm_channel_rt_mrf_gfs_ak_mem1 forecasts - WHERE forecasts.forecast_hour > 72 AND forecasts.forecast_hour <= 120 + WHERE forecasts.forecast_hour <= 120 GROUP BY forecasts.feature_id, forecasts.reference_time, forecasts.nwm_vers; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_mem2_5day_max_flows.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_mem2_5day_max_flows.sql index f44589e4..a729e5bb 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_mem2_5day_max_flows.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_mem2_5day_max_flows.sql @@ -15,5 +15,5 @@ INSERT INTO cache.max_flows_mrf_gfs_mem2_5day(feature_id, reference_time, nwm_ve round(max(forecasts.streamflow)::numeric, 2) AS discharge_cms, round(max(forecasts.streamflow * 35.315)::numeric, 2) AS discharge_cfs FROM ingest.nwm_channel_rt_mrf_gfs_mem2 forecasts - WHERE forecasts.forecast_hour > 72 AND forecasts.forecast_hour <= 120 + WHERE forecasts.forecast_hour <= 120 GROUP BY forecasts.feature_id, forecasts.reference_time, forecasts.nwm_vers; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_mem3_5day_max_flows.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_mem3_5day_max_flows.sql index 1582051c..8e45292b 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_mem3_5day_max_flows.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_mem3_5day_max_flows.sql @@ -15,5 +15,5 @@ INSERT INTO cache.max_flows_mrf_mem3_gfs_5day(feature_id, reference_time, nwm_ve round(max(forecasts.streamflow)::numeric, 2) AS discharge_cms, round(max(forecasts.streamflow * 35.315)::numeric, 2) AS discharge_cfs FROM ingest.nwm_channel_rt_mrf_gfs_mem3 forecasts - WHERE forecasts.forecast_hour > 72 AND forecasts.forecast_hour <= 120 + WHERE forecasts.forecast_hour <= 120 GROUP BY forecasts.feature_id, forecasts.reference_time, forecasts.nwm_vers; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_mem4_5day_max_flows.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_mem4_5day_max_flows.sql index b4ffbeac..20bba3a1 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_mem4_5day_max_flows.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_mem4_5day_max_flows.sql @@ -15,5 +15,5 @@ INSERT INTO cache.max_flows_mrf_gfs_mem4_5day(feature_id, reference_time, nwm_ve round(max(forecasts.streamflow)::numeric, 2) AS discharge_cms, round(max(forecasts.streamflow * 35.315)::numeric, 2) AS discharge_cfs FROM ingest.nwm_channel_rt_mrf_gfs_mem4 forecasts - WHERE forecasts.forecast_hour > 72 AND forecasts.forecast_hour <= 120 + WHERE forecasts.forecast_hour <= 120 GROUP BY forecasts.feature_id, forecasts.reference_time, forecasts.nwm_vers; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_mem5_5day_max_flows.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_mem5_5day_max_flows.sql index cc008a12..e82122b4 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_mem5_5day_max_flows.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_mem5_5day_max_flows.sql @@ -15,5 +15,5 @@ INSERT INTO cache.max_flows_mrf_gfs_mem5_5day(feature_id, reference_time, nwm_ve round(max(forecasts.streamflow)::numeric, 2) AS discharge_cms, round(max(forecasts.streamflow * 35.315)::numeric, 2) AS discharge_cfs FROM ingest.nwm_channel_rt_mrf_gfs_mem5 forecasts - WHERE forecasts.forecast_hour > 72 AND forecasts.forecast_hour <= 120 + WHERE forecasts.forecast_hour <= 120 GROUP BY forecasts.feature_id, forecasts.reference_time, forecasts.nwm_vers; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_mem6_5day_max_flows.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_mem6_5day_max_flows.sql index 6ef8ea1a..0dedb130 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_mem6_5day_max_flows.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_gfs_mem6_5day_max_flows.sql @@ -15,5 +15,5 @@ INSERT INTO cache.max_flows_mrf_gfs_mem6_5day(feature_id, reference_time, nwm_ve round(max(forecasts.streamflow)::numeric, 2) AS discharge_cms, round(max(forecasts.streamflow * 35.315)::numeric, 2) AS discharge_cfs FROM ingest.nwm_channel_rt_mrf_gfs_mem6 forecasts - WHERE forecasts.forecast_hour > 72 AND forecasts.forecast_hour <= 120 + WHERE forecasts.forecast_hour <= 120 GROUP BY forecasts.feature_id, forecasts.reference_time, forecasts.nwm_vers; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_nbm_10day_max_flows.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_nbm_10day_max_flows.sql index 69c68e95..0c5506eb 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_nbm_10day_max_flows.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_nbm_10day_max_flows.sql @@ -8,12 +8,11 @@ CREATE TABLE IF NOT EXISTS cache.max_flows_mrf_nbm_10day ); TRUNCATE TABLE cache.max_flows_mrf_nbm_10day; -INSERT INTO cache.max_flows_mrf_nbm_10day_ak(feature_id, reference_time, nwm_vers, discharge_cms, discharge_cfs) +INSERT INTO cache.max_flows_mrf_nbm_10day(feature_id, reference_time, nwm_vers, discharge_cms, discharge_cfs) SELECT forecasts.feature_id, forecasts.reference_time, forecasts.nwm_vers, round(max(forecasts.streamflow)::numeric, 2) AS discharge_cms, round(max(forecasts.streamflow * 35.315)::numeric, 2) AS discharge_cfs FROM ingest.nwm_channel_rt_mrf_nbm forecasts - WHERE forecasts.forecast_hour > 120 GROUP BY forecasts.feature_id, forecasts.reference_time, forecasts.nwm_vers; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_nbm_10day_max_flows_ak.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_nbm_10day_max_flows_ak.sql index 2ad6ff59..5b9ef638 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_nbm_10day_max_flows_ak.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_nbm_10day_max_flows_ak.sql @@ -15,5 +15,4 @@ INSERT INTO cache.max_flows_mrf_nbm_10day_ak(feature_id, reference_time, nwm_ver round(max(forecasts.streamflow)::numeric, 2) AS discharge_cms, round(max(forecasts.streamflow * 35.315)::numeric, 2) AS discharge_cfs FROM ingest.nwm_channel_rt_mrf_nbm_ak forecasts - WHERE forecasts.forecast_hour > 120 GROUP BY forecasts.feature_id, forecasts.reference_time, forecasts.nwm_vers; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_nbm_5day_max_flows.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_nbm_5day_max_flows.sql index 03049435..113d0e8d 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_nbm_5day_max_flows.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_nbm_5day_max_flows.sql @@ -15,5 +15,5 @@ INSERT INTO cache.max_flows_mrf_nbm_5day(feature_id, reference_time, nwm_vers, d round(max(forecasts.streamflow)::numeric, 2) AS discharge_cms, round(max(forecasts.streamflow * 35.315)::numeric, 2) AS discharge_cfs FROM ingest.nwm_channel_rt_mrf_nbm forecasts - WHERE forecasts.forecast_hour > 72 AND forecasts.forecast_hour <= 120 + WHERE forecasts.forecast_hour <= 120 GROUP BY forecasts.feature_id, forecasts.reference_time, forecasts.nwm_vers; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_nbm_5day_max_flows_ak.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_nbm_5day_max_flows_ak.sql index 5975bc1c..64c2b135 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_nbm_5day_max_flows_ak.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/max_flows/mrf_nbm_5day_max_flows_ak.sql @@ -15,5 +15,5 @@ INSERT INTO cache.max_flows_mrf_nbm_5day_ak(feature_id, reference_time, nwm_vers round(max(forecasts.streamflow)::numeric, 2) AS discharge_cms, round(max(forecasts.streamflow * 35.315)::numeric, 2) AS discharge_cfs FROM ingest.nwm_channel_rt_mrf_nbm_ak forecasts - WHERE forecasts.forecast_hour > 72 AND forecasts.forecast_hour <= 120 + WHERE forecasts.forecast_hour <= 120 GROUP BY forecasts.feature_id, forecasts.reference_time, forecasts.nwm_vers; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/products/medium_range_blend_alaska/mrf_nbm_10day_peak_flow_arrival_time_alaska.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/products/medium_range_blend_alaska/mrf_nbm_10day_peak_flow_arrival_time_alaska.sql index e9c23c9e..87fae8fb 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/products/medium_range_blend_alaska/mrf_nbm_10day_peak_flow_arrival_time_alaska.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/products/medium_range_blend_alaska/mrf_nbm_10day_peak_flow_arrival_time_alaska.sql @@ -30,7 +30,7 @@ INTO publish.mrf_nbm_10day_peak_flow_arrival_time_alaska FROM ingest.nwm_channel_rt_mrf_nbm_ak AS forecasts -- Join in max flows on max streamflow to only get peak flows -JOIN cache.mrf_nbm_max_flows_ak_10day AS max_flows +JOIN cache.max_flows_mrf_nbm_10day_ak AS max_flows ON forecasts.feature_id = max_flows.feature_id AND round((forecasts.streamflow*35.315)::numeric, 2) = max_flows.discharge_cfs -- Join in channels data to get reach metadata and geometry diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/products/short_range_puertorico/srf_48hr_peak_flow_arrival_time_prvi.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/products/short_range_puertorico/srf_48hr_peak_flow_arrival_time_prvi.sql index 5bff9870..ca2b5cbd 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/products/short_range_puertorico/srf_48hr_peak_flow_arrival_time_prvi.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/products/short_range_puertorico/srf_48hr_peak_flow_arrival_time_prvi.sql @@ -35,7 +35,7 @@ JOIN derived.recurrence_flows_prvi as rf ON forecasts.feature_id = rf.feature_id JOIN publish.srf_48hr_high_water_arrival_time_prvi as arrival_time ON forecasts.feature_id = arrival_time.feature_id and forecasts.reference_time = arrival_time.reference_time WHERE round((forecasts.streamflow*35.315)::numeric, 2) >= rf.high_water_threshold -GROUP BY forecasts.feature_id, forecasts.reference_time, forecasts.nwm_vers, channels.name, channels.strm_order, channels.huc6, rf.high_water_threshold, arrival_time.below_bank_return_hour, arrival_time.below_bank_return_time, max_flows.discharge_cms, channels.geom; +GROUP BY forecasts.feature_id, forecasts.reference_time, forecasts.nwm_vers, channels.name, channels.strm_order, channels.huc6, rf.high_water_threshold, arrival_time.below_bank_return_hour, arrival_time.below_bank_return_time, max_flows.discharge_cms, max_flows.discharge_cfs, channels.geom; --Add an empty row so that service monitor will pick up a reference and update time in the event of no fim features INSERT INTO publish.srf_48hr_peak_flow_arrival_time_prvi( diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/readme.md b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/readme.md deleted file mode 100644 index 90641b10..00000000 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/readme.md +++ /dev/null @@ -1,32 +0,0 @@ -The postprocess_sql lambda function is a generalized lightweight lambda function used throughout VPP pipelines to execute SQL on a specified database (PostgreSQL RDS or Redshift) -It is typically used for operations like: - - Ingest prep: Truncate a table and delete indicies prior to ingesting new data. Create a table if it doesn't exist yet. - - Ingest finish: Rebuild indicies after ingest. - - Database transformations: Query and/or aggregate ingested data into new table for a map service - - Various steps of FIM Workflows (details below): With the implementation of FIM Caching to a Redshift Data Warehouse in HydroVIS version 2.2(?), this function can now be used to execute template SQL files as well. The template to use is defined by the input parameters in the step function, and generally does one of these things: - - Create a table if it doesn't exist - - Truncate a inundation table for a new pipeline run - - Copy data from RDS to Redshift or vice versa (via Foreign Data Wrappers and External Schemas) - - Data transformations / aggregations. - -############################################# FIM Workflow ############################################## -The following database operations are taken below to process FIM, largely via the fim_caching_templates sql files in this lambda function -0. Create four tables, if they don't already exist, on both RDS and Redshift. These tables replicate the schema of the HAND cache on Redshift, and are truncated and re-populated as part of each FIM run: - - ingest.{fim_config}_flows - this is a version of max_flows, with fim crosswalk columns added, as well as filtering for hight water threshold - - ingest.{fim_config} - this is the fim table, but without geometry - - ingest.{fim_config}_geo - this is the geometries for the fim table (one-to-many, since we're subdividing to keep geometries small for Redshift) - - ingest.{fim_config}_zero_stage - this table holds all of the fim features (hydro_table, feature_id, huc8, branch combinations) that have zero or NaN stage at the current discharge value - - ingest.{fim_config}_geo_view (RDS only) - this view subdivides the newly polygons in the inundation_geo table (because Redshift has a limit on the size of geometries) - - publish.{fim_config} (RDS only) - This is the finished publish table that gets copied to the EGIS service -1. Populate the FIM flows table on RDS (from max_flows with some joins), then copy it to Redshift -2. Query the HAND Cache on Redshift - a. Query the HAND cache on Redshift, joining to the just-populated flows table, to populate the inundation, inundation_geo, and inundation_zero_stage tables on Redshift -3. Populate the inundation tables on RDS - a. Prioritize Ras2FIM by querying the Ras2FIM cache on RDS first #TODO - b. Copy the FIM tables on Redshift (which were just populated from the HAND cache in 2a) into the inundation tables on RDS (skipping any records that were already added from Ras2FIM) - c. HAND processing for any FIM features remaining in the inundation flows table, that have not been added to the inundation table from Ras2FIM or the HAND cache (not done here, but administered by the fim_data_prep lambda function -4. Generate publish.inundation table on RDS, and copy it to the EGIS (done via the update_egis_data function) - a. We can use a template to do this generically for most inland inundation configurations (e.g. NWM) -5. Add any newly generated HAND features in this run into the Redshift HAND cache ( #TODO: it would be good to figure out how to do this in parallel outside of the fim_config map, so that this doesn't hold things up). - a. Insert records from the RDS inundation, inundation_geo, and inundation_zero_stage tables/view into the Redshift HAND cache tables, only taking records generated by HAND Processing, and which the primary key does not already exist (hydro_id, feature_id, huc8, branch, rc_stage_ft) - \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/ana_inundation/building_footprints_fimpact.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/ana_inundation/building_footprints_fimpact.sql index e32082b2..5f0d2d4c 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/ana_inundation/building_footprints_fimpact.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/ana_inundation/building_footprints_fimpact.sql @@ -14,16 +14,19 @@ SELECT buildings.source, buildings.val_method, fim.hydro_id, - fim.hydro_id_str::TEXT AS hydro_id_str, - fim.feature_id, - fim.feature_id_str::TEXT AS feature_id_str, - fim.streamflow_cfs, - fim.rc_stage_ft as fim_stage_ft, + flows.hydro_id, + flows.hydro_id::TEXT AS hydro_id_str, + flows.feature_id, + flows.feature_id::TEXT AS feature_id_str, + flows.discharge_cfs AS streamflow_cfs, + fim.rc_stage_ft AS fim_stage_ft, buildings.geom, ST_Centroid(buildings.geom) as geom_xy INTO publish.ana_inundation_building_footprints FROM external.building_footprints_fema as buildings -JOIN publish.ana_inundation fim ON ST_INTERSECTS(fim.geom, buildings.geom); +JOIN fim_ingest.ana_inundation_geo fim_geo ON ST_INTERSECTS(fim_geo.geom, buildings.geom) +JOIN fim_ingest.ana_inundation_flows flows ON fim_geo.hand_id = flows.hand_id +JOIN fim_ingest.ana_inundation fim ON fim_geo.hand_id = fim.hand_id; --------------- County Summary --------------- DROP TABLE IF EXISTS publish.ana_inundation_counties; diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/ana_inundation_hi/building_footprints_fimpact.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/ana_inundation_hi/building_footprints_fimpact.sql index 9c16efed..757963ab 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/ana_inundation_hi/building_footprints_fimpact.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/ana_inundation_hi/building_footprints_fimpact.sql @@ -13,17 +13,19 @@ SELECT buildings.prod_date, buildings.source, buildings.val_method, - fim.hydro_id, - fim.hydro_id_str::TEXT AS hydro_id_str, - fim.feature_id, - fim.feature_id_str::TEXT AS feature_id_str, - fim.streamflow_cfs, - fim.fim_stage_ft, + flows.hydro_id, + flows.hydro_id::TEXT AS hydro_id_str, + flows.feature_id, + flows.feature_id::TEXT AS feature_id_str, + flows.discharge_cfs AS streamflow_cfs, + fim.rc_stage_ft AS fim_stage_ft, buildings.geom, ST_Centroid(buildings.geom) as geom_xy INTO publish.ana_inundation_building_footprints_hi FROM external.building_footprints_fema as buildings -JOIN publish.ana_inundation_hi fim ON ST_INTERSECTS(fim.geom, buildings.geom); +JOIN fim_ingest.ana_inundation_hi_geo fim_geo ON ST_INTERSECTS(fim_geo.geom, buildings.geom) +JOIN fim_ingest.ana_inundation_hi_flows flows ON fim_geo.hand_id = flows.hand_id +JOIN fim_ingest.ana_inundation_hi fim ON fim_geo.hand_id = fim.hand_id; --------------- County Summary --------------- DROP TABLE IF EXISTS publish.ana_inundation_counties_hi; diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/ana_inundation_prvi/building_footprints_fimpact.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/ana_inundation_prvi/building_footprints_fimpact.sql index 0ce0a035..353892fc 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/ana_inundation_prvi/building_footprints_fimpact.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/ana_inundation_prvi/building_footprints_fimpact.sql @@ -13,17 +13,19 @@ SELECT buildings.prod_date, buildings.source, buildings.val_method, - fim.hydro_id, - fim.hydro_id_str::TEXT AS hydro_id_str, - fim.feature_id, - fim.feature_id_str::TEXT AS feature_id_str, - fim.streamflow_cfs, - fim.fim_stage_ft, + flows.hydro_id, + flows.hydro_id::TEXT AS hydro_id_str, + flows.feature_id, + flows.feature_id::TEXT AS feature_id_str, + flows.discharge_cfs AS streamflow_cfs, + fim.rc_stage_ft AS fim_stage_ft, buildings.geom, ST_Centroid(buildings.geom) as geom_xy INTO publish.ana_inundation_building_footprints_prvi FROM external.building_footprints_fema as buildings -JOIN publish.ana_inundation_prvi fim ON ST_INTERSECTS(fim.geom, buildings.geom); +JOIN fim_ingest.ana_inundation_prvi_geo fim_geo ON ST_INTERSECTS(fim_geo.geom, buildings.geom) +JOIN fim_ingest.ana_inundation_prvi_flows flows ON fim_geo.hand_id = flows.hand_id +JOIN fim_ingest.ana_inundation_prvi fim ON fim_geo.hand_id = fim.hand_id; --------------- County Summary --------------- DROP TABLE IF EXISTS publish.ana_inundation_counties_prvi; diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/ana_past_14day_max_inundation/14day_building_footprints_fimpact.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/ana_past_14day_max_inundation/14day_building_footprints_fimpact.sql index 5844b1c0..95a9acef 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/ana_past_14day_max_inundation/14day_building_footprints_fimpact.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/ana_past_14day_max_inundation/14day_building_footprints_fimpact.sql @@ -14,16 +14,19 @@ SELECT buildings.source, buildings.val_method, fim.hydro_id, - fim.hydro_id_str::TEXT AS hydro_id_str, - fim.feature_id, - fim.feature_id_str::TEXT AS feature_id_str, - fim.streamflow_cfs, - fim.fim_stage_ft, + flows.hydro_id, + flows.hydro_id::TEXT AS hydro_id_str, + flows.feature_id, + flows.feature_id::TEXT AS feature_id_str, + flows.discharge_cfs AS streamflow_cfs, + fim.rc_stage_ft AS fim_stage_ft, buildings.geom, ST_Centroid(buildings.geom) as geom_xy INTO publish.ana_past_14day_max_inundation_building_footprints FROM external.building_footprints_fema as buildings -JOIN publish.ana_past_14day_max_inundation fim ON ST_INTERSECTS(fim.geom, buildings.geom); +JOIN fim_ingest.ana_past_14day_max_inundation_geo fim_geo ON ST_INTERSECTS(fim_geo.geom, buildings.geom) +JOIN fim_ingest.ana_past_14day_max_inundation_flows flows ON fim_geo.hand_id = flows.hand_id +JOIN fim_ingest.ana_past_14day_max_inundation fim ON fim_geo.hand_id = fim.hand_id; --------------- County Summary --------------- DROP TABLE IF EXISTS publish.ana_past_14day_max_inundation_counties; diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/ana_past_14day_max_inundation/7day_building_footprints_fimpact.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/ana_past_14day_max_inundation/7day_building_footprints_fimpact.sql index 7f166a46..df15513a 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/ana_past_14day_max_inundation/7day_building_footprints_fimpact.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/ana_past_14day_max_inundation/7day_building_footprints_fimpact.sql @@ -14,16 +14,19 @@ SELECT buildings.source, buildings.val_method, fim.hydro_id, - fim.hydro_id_str::TEXT AS hydro_id_str, - fim.feature_id, - fim.feature_id_str::TEXT AS feature_id_str, - fim.streamflow_cfs, - fim.fim_stage_ft, + flows.hydro_id, + flows.hydro_id::TEXT AS hydro_id_str, + flows.feature_id, + flows.feature_id::TEXT AS feature_id_str, + flows.discharge_cfs AS streamflow_cfs, + fim.rc_stage_ft AS fim_stage_ft, buildings.geom, ST_Centroid(buildings.geom) as geom_xy INTO publish.ana_past_7day_max_inundation_building_footprints FROM external.building_footprints_fema as buildings -JOIN publish.ana_past_7day_max_inundation fim ON ST_INTERSECTS(fim.geom, buildings.geom); +JOIN fim_ingest.ana_past_7day_max_inundation_geo fim_geo ON ST_INTERSECTS(fim_geo.geom, buildings.geom) +JOIN fim_ingest.ana_past_7day_max_inundation_flows flows ON fim_geo.hand_id = flows.hand_id +JOIN fim_ingest.ana_past_7day_max_inundation fim ON fim_geo.hand_id = fim.hand_id; --------------- County Summary --------------- DROP TABLE IF EXISTS publish.ana_past_7day_max_inundation_counties; diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_gfs_10day_max_inundation/10day_building_footprints_fimpact.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_gfs_10day_max_inundation/10day_building_footprints_fimpact.sql index 19d839b6..48c6a98d 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_gfs_10day_max_inundation/10day_building_footprints_fimpact.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_gfs_10day_max_inundation/10day_building_footprints_fimpact.sql @@ -1,3 +1,4 @@ +--------------- Building Footprints --------------- -- We'll temporarily increase work_mem to 512MB, to help with performance on PostGIS spatial joins (default is 4MB) SET work_mem TO '512MB'; --------------- Building Footprints --------------- @@ -13,17 +14,19 @@ SELECT buildings.prod_date, buildings.source, buildings.val_method, - fim.hydro_id, - fim.hydro_id_str::TEXT AS hydro_id_str, - fim.feature_id, - fim.feature_id_str::TEXT AS feature_id_str, - fim.streamflow_cfs, - fim.fim_stage_ft, + flows.hydro_id, + flows.hydro_id::TEXT AS hydro_id_str, + flows.feature_id, + flows.feature_id::TEXT AS feature_id_str, + flows.discharge_cfs AS streamflow_cfs, + fim.rc_stage_ft AS fim_stage_ft, buildings.geom, ST_Centroid(buildings.geom) as geom_xy INTO publish.mrf_gfs_max_inundation_10day_building_footprints FROM external.building_footprints_fema as buildings -JOIN publish.mrf_gfs_max_inundation_10day fim ON ST_INTERSECTS(fim.geom, buildings.geom); +JOIN fim_ingest.mrf_gfs_max_inundation_10day_geo fim_geo ON ST_INTERSECTS(fim_geo.geom, buildings.geom) +JOIN fim_ingest.mrf_gfs_max_inundation_10day_flows flows ON fim_geo.hand_id = flows.hand_id +JOIN fim_ingest.mrf_gfs_max_inundation_10day fim ON fim_geo.hand_id = fim.hand_id; --------------- County Summary --------------- DROP TABLE IF EXISTS publish.mrf_gfs_max_inundation_10day_counties; diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_gfs_10day_max_inundation/3day_building_footprints_fimpact.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_gfs_10day_max_inundation/3day_building_footprints_fimpact.sql index 7a163bdf..9878ddc3 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_gfs_10day_max_inundation/3day_building_footprints_fimpact.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_gfs_10day_max_inundation/3day_building_footprints_fimpact.sql @@ -1,3 +1,4 @@ +--------------- Building Footprints --------------- -- We'll temporarily increase work_mem to 512MB, to help with performance on PostGIS spatial joins (default is 4MB) SET work_mem TO '512MB'; --------------- Building Footprints --------------- @@ -14,16 +15,19 @@ SELECT buildings.source, buildings.val_method, fim.hydro_id, - fim.hydro_id_str::TEXT AS hydro_id_str, - fim.feature_id, - fim.feature_id_str::TEXT AS feature_id_str, - fim.streamflow_cfs, - fim.fim_stage_ft, + flows.hydro_id, + flows.hydro_id::TEXT AS hydro_id_str, + flows.feature_id, + flows.feature_id::TEXT AS feature_id_str, + flows.discharge_cfs AS streamflow_cfs, + fim.rc_stage_ft AS fim_stage_ft, buildings.geom, ST_Centroid(buildings.geom) as geom_xy INTO publish.mrf_gfs_max_inundation_3day_building_footprints FROM external.building_footprints_fema as buildings -JOIN publish.mrf_gfs_max_inundation_3day fim ON ST_INTERSECTS(fim.geom, buildings.geom); +JOIN fim_ingest.mrf_gfs_max_inundation_3day_geo fim_geo ON ST_INTERSECTS(fim_geo.geom, buildings.geom) +JOIN fim_ingest.mrf_gfs_max_inundation_3day_flows flows ON fim_geo.hand_id = flows.hand_id +JOIN fim_ingest.mrf_gfs_max_inundation_3day fim ON fim_geo.hand_id = fim.hand_id; --------------- County Summary --------------- DROP TABLE IF EXISTS publish.mrf_gfs_max_inundation_3day_counties; diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_gfs_10day_max_inundation/5day_building_footprints_fimpact.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_gfs_10day_max_inundation/5day_building_footprints_fimpact.sql index dbbadca5..fa837af8 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_gfs_10day_max_inundation/5day_building_footprints_fimpact.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_gfs_10day_max_inundation/5day_building_footprints_fimpact.sql @@ -1,5 +1,3 @@ --- We'll temporarily increase work_mem to 512MB, to help with performance on PostGIS spatial joins (default is 4MB) -SET work_mem TO '512MB'; --------------- Building Footprints --------------- DROP TABLE IF EXISTS publish.mrf_gfs_max_inundation_5day_building_footprints; SELECT @@ -13,17 +11,19 @@ SELECT buildings.prod_date, buildings.source, buildings.val_method, - fim.hydro_id, - fim.hydro_id_str::TEXT AS hydro_id_str, - fim.feature_id, - fim.feature_id_str::TEXT AS feature_id_str, - fim.streamflow_cfs, - fim.fim_stage_ft, + flows.hydro_id, + flows.hydro_id::TEXT AS hydro_id_str, + flows.feature_id, + flows.feature_id::TEXT AS feature_id_str, + flows.discharge_cfs AS streamflow_cfs, + fim.rc_stage_ft AS fim_stage_ft, buildings.geom, ST_Centroid(buildings.geom) as geom_xy INTO publish.mrf_gfs_max_inundation_5day_building_footprints FROM external.building_footprints_fema as buildings -JOIN publish.mrf_gfs_max_inundation_5day fim ON ST_INTERSECTS(fim.geom, buildings.geom); +JOIN fim_ingest.mrf_gfs_max_inundation_5day_geo fim_geo ON ST_INTERSECTS(fim_geo.geom, buildings.geom) +JOIN fim_ingest.mrf_gfs_max_inundation_5day_flows flows ON fim_geo.hand_id = flows.hand_id +JOIN fim_ingest.mrf_gfs_max_inundation_5day fim ON fim_geo.hand_id = fim.hand_id; --------------- County Summary --------------- DROP TABLE IF EXISTS publish.mrf_gfs_max_inundation_5day_counties; diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_gfs_10day_max_inundation/5day_public_subset.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_gfs_10day_max_inundation/5day_public_subset.sql index 4f62e9b7..80f229de 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_gfs_10day_max_inundation/5day_public_subset.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_gfs_10day_max_inundation/5day_public_subset.sql @@ -2,10 +2,11 @@ DROP TABLE IF EXISTS publish.mrf_gfs_max_inundation_5day_public; SELECT inun.feature_id_str, - ST_Intersection(inun.geom, fim_domain.geom) as geom, + inun.geom, inun.streamflow_cfs, inun.reference_time, to_char(now()::timestamp without time zone, 'YYYY-MM-DD HH24:MI:SS UTC') AS update_time INTO publish.mrf_gfs_max_inundation_5day_public FROM publish.mrf_gfs_max_inundation_5day as inun -JOIN derived.public_fim_domain as fim_domain ON ST_Intersects(inun.geom, fim_domain.geom) \ No newline at end of file +JOIN derived.channels_conus AS channels ON inun.feature_id = channels.feature_id +WHERE public_fim_domain = True; \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_nbm_10day_max_inundation/10day_building_footprints_fimpact.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_nbm_10day_max_inundation/10day_building_footprints_fimpact.sql index 03ff06d5..65e4715f 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_nbm_10day_max_inundation/10day_building_footprints_fimpact.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_nbm_10day_max_inundation/10day_building_footprints_fimpact.sql @@ -13,17 +13,19 @@ SELECT buildings.prod_date, buildings.source, buildings.val_method, - fim.hydro_id, - fim.hydro_id_str::TEXT AS hydro_id_str, - fim.feature_id, - fim.feature_id_str::TEXT AS feature_id_str, - fim.streamflow_cfs, - fim.fim_stage_ft, + flows.hydro_id, + flows.hydro_id::TEXT AS hydro_id_str, + flows.feature_id, + flows.feature_id::TEXT AS feature_id_str, + flows.discharge_cfs AS streamflow_cfs, + fim.rc_stage_ft AS fim_stage_ft, buildings.geom, ST_Centroid(buildings.geom) as geom_xy INTO publish.mrf_nbm_max_inundation_10day_building_footprints FROM external.building_footprints_fema as buildings -JOIN publish.mrf_nbm_max_inundation_10day fim ON ST_INTERSECTS(fim.geom, buildings.geom); +JOIN fim_ingest.mrf_nbm_max_inundation_10day_geo fim_geo ON ST_INTERSECTS(fim_geo.geom, buildings.geom) +JOIN fim_ingest.mrf_nbm_max_inundation_10day_flows flows ON fim_geo.hand_id = flows.hand_id +JOIN fim_ingest.mrf_nbm_max_inundation_10day fim ON fim_geo.hand_id = fim.hand_id; --------------- County Summary --------------- DROP TABLE IF EXISTS publish.mrf_nbm_max_inundation_10day_counties; diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_nbm_10day_max_inundation/3day_building_footprints_fimpact.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_nbm_10day_max_inundation/3day_building_footprints_fimpact.sql index d7a51da0..89086023 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_nbm_10day_max_inundation/3day_building_footprints_fimpact.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_nbm_10day_max_inundation/3day_building_footprints_fimpact.sql @@ -14,16 +14,19 @@ SELECT buildings.source, buildings.val_method, fim.hydro_id, - fim.hydro_id_str::TEXT AS hydro_id_str, - fim.feature_id, - fim.feature_id_str::TEXT AS feature_id_str, - fim.streamflow_cfs, - fim.fim_stage_ft, + flows.hydro_id, + flows.hydro_id::TEXT AS hydro_id_str, + flows.feature_id, + flows.feature_id::TEXT AS feature_id_str, + flows.discharge_cfs AS streamflow_cfs, + fim.rc_stage_ft AS fim_stage_ft, buildings.geom, ST_Centroid(buildings.geom) as geom_xy INTO publish.mrf_nbm_max_inundation_3day_building_footprints FROM external.building_footprints_fema as buildings -JOIN publish.mrf_nbm_max_inundation_3day fim ON ST_INTERSECTS(fim.geom, buildings.geom); +JOIN fim_ingest.mrf_nbm_max_inundation_3day_geo fim_geo ON ST_INTERSECTS(fim_geo.geom, buildings.geom) +JOIN fim_ingest.mrf_nbm_max_inundation_3day_flows flows ON fim_geo.hand_id = flows.hand_id +JOIN fim_ingest.mrf_nbm_max_inundation_3day fim ON fim_geo.hand_id = fim.hand_id; --------------- County Summary --------------- DROP TABLE IF EXISTS publish.mrf_nbm_max_inundation_3day_counties; diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_nbm_10day_max_inundation/5day_building_footprints_fimpact.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_nbm_10day_max_inundation/5day_building_footprints_fimpact.sql index e3bb87c5..f36bc5e2 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_nbm_10day_max_inundation/5day_building_footprints_fimpact.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/mrf_nbm_10day_max_inundation/5day_building_footprints_fimpact.sql @@ -13,17 +13,19 @@ SELECT buildings.prod_date, buildings.source, buildings.val_method, - fim.hydro_id, - fim.hydro_id_str::TEXT AS hydro_id_str, - fim.feature_id, - fim.feature_id_str::TEXT AS feature_id_str, - fim.streamflow_cfs, - fim.fim_stage_ft, + flows.hydro_id, + flows.hydro_id::TEXT AS hydro_id_str, + flows.feature_id, + flows.feature_id::TEXT AS feature_id_str, + flows.discharge_cfs AS streamflow_cfs, + fim.rc_stage_ft AS fim_stage_ft, buildings.geom, ST_Centroid(buildings.geom) as geom_xy INTO publish.mrf_nbm_max_inundation_5day_building_footprints FROM external.building_footprints_fema as buildings -JOIN publish.mrf_nbm_max_inundation_5day fim ON ST_INTERSECTS(fim.geom, buildings.geom); +JOIN fim_ingest.mrf_nbm_max_inundation_5day_geo fim_geo ON ST_INTERSECTS(fim_geo.geom, buildings.geom) +JOIN fim_ingest.mrf_nbm_max_inundation_5day_flows flows ON fim_geo.hand_id = flows.hand_id +JOIN fim_ingest.mrf_nbm_max_inundation_5day fim ON fim_geo.hand_id = fim.hand_id; --------------- County Summary --------------- DROP TABLE IF EXISTS publish.mrf_nbm_max_inundation_5day_counties; diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/rfc_based_5day_max_inundation/building_footprints_fimpact.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/rfc_based_5day_max_inundation/building_footprints_fimpact.sql index 8f0784d6..57bd890e 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/rfc_based_5day_max_inundation/building_footprints_fimpact.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/rfc_based_5day_max_inundation/building_footprints_fimpact.sql @@ -14,16 +14,19 @@ SELECT buildings.source, buildings.val_method, fim.hydro_id, - fim.hydro_id_str::TEXT AS hydro_id_str, - fim.feature_id, - fim.feature_id_str::TEXT AS feature_id_str, - fim.streamflow_cfs, - fim.fim_stage_ft, + flows.hydro_id, + flows.hydro_id::TEXT AS hydro_id_str, + flows.feature_id, + flows.feature_id::TEXT AS feature_id_str, + flows.discharge_cfs AS streamflow_cfs, + fim.rc_stage_ft AS fim_stage_ft, buildings.geom, ST_Centroid(buildings.geom) as geom_xy INTO publish.rfc_based_5day_max_inundation_building_footprints FROM external.building_footprints_fema as buildings -JOIN publish.rfc_based_5day_max_inundation fim ON ST_INTERSECTS(fim.geom, buildings.geom); +JOIN fim_ingest.rfc_based_5day_max_inundation_geo fim_geo ON ST_INTERSECTS(fim_geo.geom, buildings.geom) +JOIN fim_ingest.rfc_based_5day_max_inundation_flows flows ON fim_geo.hand_id = flows.hand_id +JOIN fim_ingest.rfc_based_5day_max_inundation fim ON fim_geo.hand_id = fim.hand_id; --------------- County Summary --------------- DROP TABLE IF EXISTS publish.rfc_based_5day_max_inundation_counties; diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/srf_18hr_max_inundation/building_footprints_fimpact.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/srf_18hr_max_inundation/building_footprints_fimpact.sql index 9d5d1a18..ae3d06c1 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/srf_18hr_max_inundation/building_footprints_fimpact.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/srf_18hr_max_inundation/building_footprints_fimpact.sql @@ -14,16 +14,19 @@ SELECT buildings.source, buildings.val_method, fim.hydro_id, - fim.hydro_id_str::TEXT AS hydro_id_str, - fim.feature_id, - fim.feature_id_str::TEXT AS feature_id_str, - fim.streamflow_cfs, - fim.fim_stage_ft, + flows.hydro_id, + flows.hydro_id::TEXT AS hydro_id_str, + flows.feature_id, + flows.feature_id::TEXT AS feature_id_str, + flows.discharge_cfs AS streamflow_cfs, + fim.rc_stage_ft AS fim_stage_ft, buildings.geom, ST_Centroid(buildings.geom) as geom_xy INTO publish.srf_18hr_max_inundation_building_footprints FROM external.building_footprints_fema as buildings -JOIN publish.srf_18hr_max_inundation fim ON ST_INTERSECTS(fim.geom, buildings.geom); +JOIN fim_ingest.srf_18hr_max_inundation_geo fim_geo ON ST_INTERSECTS(fim_geo.geom, buildings.geom) +JOIN fim_ingest.srf_18hr_max_inundation_flows flows ON fim_geo.hand_id = flows.hand_id +JOIN fim_ingest.srf_18hr_max_inundation fim ON fim_geo.hand_id = fim.hand_id; --------------- County Summary --------------- DROP TABLE IF EXISTS publish.srf_18hr_max_inundation_counties; diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/srf_48hr_max_inundation_hi/building_footprints_fimpact.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/srf_48hr_max_inundation_hi/building_footprints_fimpact.sql index a60564b2..187ff004 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/srf_48hr_max_inundation_hi/building_footprints_fimpact.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/srf_48hr_max_inundation_hi/building_footprints_fimpact.sql @@ -13,17 +13,19 @@ SELECT buildings.prod_date, buildings.source, buildings.val_method, - fim.hydro_id, - fim.hydro_id_str::TEXT AS hydro_id_str, - fim.feature_id, - fim.feature_id_str::TEXT AS feature_id_str, - fim.streamflow_cfs, - fim.fim_stage_ft, + flows.hydro_id, + flows.hydro_id::TEXT AS hydro_id_str, + flows.feature_id, + flows.feature_id::TEXT AS feature_id_str, + flows.discharge_cfs AS streamflow_cfs, + fim.rc_stage_ft AS fim_stage_ft, buildings.geom, ST_Centroid(buildings.geom) as geom_xy INTO publish.srf_48hr_max_inundation_building_footprints_hi FROM external.building_footprints_fema as buildings -JOIN publish.srf_48hr_max_inundation_hi fim ON ST_INTERSECTS(fim.geom, buildings.geom); +JOIN fim_ingest.srf_48hr_max_inundation_hi_geo fim_geo ON ST_INTERSECTS(fim_geo.geom, buildings.geom) +JOIN fim_ingest.srf_48hr_max_inundation_hi_flows flows ON fim_geo.hand_id = flows.hand_id +JOIN fim_ingest.srf_48hr_max_inundation_hi fim ON fim_geo.hand_id = fim.hand_id; --------------- County Summary --------------- DROP TABLE IF EXISTS publish.srf_48hr_max_inundation_counties_hi; diff --git a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/srf_48hr_max_inundation_prvi/building_footprints_fimpact.sql b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/srf_48hr_max_inundation_prvi/building_footprints_fimpact.sql index e123657f..b968be69 100644 --- a/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/srf_48hr_max_inundation_prvi/building_footprints_fimpact.sql +++ b/Core/LAMBDA/viz_functions/viz_db_postprocess_sql/summaries/srf_48hr_max_inundation_prvi/building_footprints_fimpact.sql @@ -13,17 +13,19 @@ SELECT buildings.prod_date, buildings.source, buildings.val_method, - fim.hydro_id, - fim.hydro_id_str::TEXT AS hydro_id_str, - fim.feature_id, - fim.feature_id_str::TEXT AS feature_id_str, - fim.streamflow_cfs, - fim.fim_stage_ft, + flows.hydro_id, + flows.hydro_id::TEXT AS hydro_id_str, + flows.feature_id, + flows.feature_id::TEXT AS feature_id_str, + flows.discharge_cfs AS streamflow_cfs, + fim.rc_stage_ft AS fim_stage_ft, buildings.geom, ST_Centroid(buildings.geom) as geom_xy INTO publish.srf_48hr_max_inundation_building_footprints_prvi FROM external.building_footprints_fema as buildings -JOIN publish.srf_48hr_max_inundation_prvi fim ON ST_INTERSECTS(fim.geom, buildings.geom); +JOIN fim_ingest.srf_48hr_max_inundation_prvi_geo fim_geo ON ST_INTERSECTS(fim_geo.geom, buildings.geom) +JOIN fim_ingest.srf_48hr_max_inundation_prvi_flows flows ON fim_geo.hand_id = flows.hand_id +JOIN fim_ingest.srf_48hr_max_inundation_prvi fim ON fim_geo.hand_id = fim.hand_id; --------------- County Summary --------------- DROP TABLE IF EXISTS publish.srf_48hr_max_inundation_counties_prvi; diff --git a/Core/LAMBDA/viz_functions/viz_fim_data_prep/templates_sql/hand_features.sql b/Core/LAMBDA/viz_functions/viz_fim_data_prep/templates_sql/hand_features.sql index a4601b65..afbbd388 100644 --- a/Core/LAMBDA/viz_functions/viz_fim_data_prep/templates_sql/hand_features.sql +++ b/Core/LAMBDA/viz_functions/viz_fim_data_prep/templates_sql/hand_features.sql @@ -1,12 +1,10 @@ SELECT + fs.hand_id, fs.feature_id, CONCAT(LPAD(fs.huc8::text, 8, '0'), '-', fs.branch) as huc8_branch, LEFT(LPAD(fs.huc8::text, 8, '0'), 6) as huc, fs.hydro_id, fs.discharge_cms AS streamflow_cms --TODO: Update here and in lambda to discharge FROM {db_fim_table}_flows fs -LEFT JOIN {db_fim_table} fim ON fim.feature_id = fs.feature_id AND fim.hydro_id = fs.hydro_id AND fim.huc8 = fs.huc8 AND fim.branch = fs.branch -LEFT JOIN {db_fim_table}_zero_stage zs ON zs.feature_id = fs.feature_id AND zs.hydro_id = fs.hydro_id AND zs.huc8 = fs.huc8 AND zs.branch = fs.branch WHERE - fim.fim_version IS NULL AND - zs.rc_discharge_cms IS NULL \ No newline at end of file + fs.prc_status = 'Pending' \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/analysis_assim/ana_inundation.yml b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/analysis_assim/ana_inundation.yml index c224e8aa..d004370d 100644 --- a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/analysis_assim/ana_inundation.yml +++ b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/analysis_assim/ana_inundation.yml @@ -21,7 +21,7 @@ db_max_flows: fim_configs: - name: ana_inundation flows_table: cache.max_flows_ana - target_table: ingest.ana_inundation + target_table: fim_ingest.ana_inundation fim_type: hand postprocess: sql_file: ana_inundation diff --git a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/analysis_assim/ana_past_14day_max_inundation.yml b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/analysis_assim/ana_past_14day_max_inundation.yml index 203c9347..053d9ffa 100644 --- a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/analysis_assim/ana_past_14day_max_inundation.yml +++ b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/analysis_assim/ana_past_14day_max_inundation.yml @@ -11,7 +11,7 @@ python_preprocessing: file_step: 1H file_window: P7D product: max_values - lambda_ram: 3gb + lambda_ram: 3GB output_file: max_flows/analysis_assim/{{datetime:%Y%m%d}}/ana_7day_00_max_flows.nc target_table: ingest.nwm_channel_rt_ana_7day_max target_keys: (feature_id, streamflow) @@ -19,7 +19,7 @@ python_preprocessing: file_step: 1H file_window: P14D product: max_values - lambda_ram: 3gb + lambda_ram: 3GB output_file: max_flows/analysis_assim/{{datetime:%Y%m%d}}/ana_14day_00_max_flows.nc target_table: ingest.nwm_channel_rt_ana_14day_max target_keys: (feature_id, streamflow) @@ -39,14 +39,14 @@ db_max_flows: fim_configs: - name: ana_past_7day_max_inundation flows_table: cache.max_flows_ana_7day - target_table: ingest.ana_past_7day_max_inundation + target_table: fim_ingest.ana_past_7day_max_inundation fim_type: hand postprocess: sql_file: ana_past_7day_max_inundation target_table: publish.ana_past_7day_max_inundation - name: ana_past_14day_max_inundation flows_table: cache.max_flows_ana_14day - target_table: ingest.ana_past_14day_max_inundation + target_table: fim_ingest.ana_past_14day_max_inundation fim_type: hand postprocess: sql_file: ana_past_14day_max_inundation diff --git a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/analysis_assim_hawaii/ana_inundation_hi.yml b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/analysis_assim_hawaii/ana_inundation_hi.yml index 083de058..84a62112 100644 --- a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/analysis_assim_hawaii/ana_inundation_hi.yml +++ b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/analysis_assim_hawaii/ana_inundation_hi.yml @@ -21,7 +21,7 @@ db_max_flows: fim_configs: - name: ana_inundation_hi flows_table: cache.max_flows_ana_hi - target_table: ingest.ana_inundation_hi + target_table: fim_ingest.ana_inundation_hi fim_type: hand postprocess: sql_file: ana_inundation_hi diff --git a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/analysis_assim_puertorico/ana_inundation_prvi.yml b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/analysis_assim_puertorico/ana_inundation_prvi.yml index bf9957fa..dc6a268f 100644 --- a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/analysis_assim_puertorico/ana_inundation_prvi.yml +++ b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/analysis_assim_puertorico/ana_inundation_prvi.yml @@ -21,7 +21,7 @@ db_max_flows: fim_configs: - name: ana_inundation_prvi flows_table: cache.max_flows_ana_prvi - target_table: ingest.ana_inundation_prvi + target_table: fim_ingest.ana_inundation_prvi fim_type: hand postprocess: sql_file: ana_inundation_prvi diff --git a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/medium_range_blend/mrf_nbm_10day_max_inundation.yml b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/medium_range_blend/mrf_nbm_10day_max_inundation.yml index 93430b59..2daab934 100644 --- a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/medium_range_blend/mrf_nbm_10day_max_inundation.yml +++ b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/medium_range_blend/mrf_nbm_10day_max_inundation.yml @@ -32,21 +32,21 @@ db_max_flows: fim_configs: - name: mrf_nbm_max_inundation_3day flows_table: cache.max_flows_mrf_nbm_3day - target_table: ingest.mrf_nbm_max_inundation_3day + target_table: fim_ingest.mrf_nbm_max_inundation_3day fim_type: hand postprocess: sql_file: mrf_nbm_max_inundation_3day target_table: publish.mrf_nbm_max_inundation_3day - name: mrf_nbm_max_inundation_5day flows_table: cache.max_flows_mrf_nbm_5day - target_table: ingest.mrf_nbm_max_inundation_5day + target_table: fim_ingest.mrf_nbm_max_inundation_5day fim_type: hand postprocess: sql_file: mrf_nbm_max_inundation_5day target_table: publish.mrf_nbm_max_inundation_5day - name: mrf_nbm_max_inundation_10day flows_table: cache.max_flows_mrf_nbm_10day - target_table: ingest.mrf_nbm_max_inundation_10day + target_table: fim_ingest.mrf_nbm_max_inundation_10day fim_type: hand postprocess: sql_file: mrf_nbm_max_inundation_10day diff --git a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/medium_range_blend_alaska/mrf_nbm_10day_peak_flow_arrival_time_ak.yml b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/medium_range_blend_alaska/mrf_nbm_10day_peak_flow_arrival_time_ak.yml index 40b4459e..6ac0ff9f 100644 --- a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/medium_range_blend_alaska/mrf_nbm_10day_peak_flow_arrival_time_ak.yml +++ b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/medium_range_blend_alaska/mrf_nbm_10day_peak_flow_arrival_time_ak.yml @@ -13,7 +13,7 @@ ingest_files: db_max_flows: - name: mrf_nbm_10day_max_flows_ak - target_table: cache.mrf_nbm_10day_max_flows_ak + target_table: cache.max_flows_mrf_nbm_10day_ak target_keys: (feature_id, streamflow) method: database max_flows_sql_file: mrf_nbm_10day_max_flows_ak diff --git a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/medium_range_ensemble/mrf_gfs_5day_max_inundation_probability.yml b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/medium_range_ensemble/mrf_gfs_5day_max_inundation_probability.yml deleted file mode 100644 index 698626fa..00000000 --- a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/medium_range_ensemble/mrf_gfs_5day_max_inundation_probability.yml +++ /dev/null @@ -1,96 +0,0 @@ -product: mrf_gfs_5day_max_inundation_probability -configuration: medium_range_ensemble -product_type: "fim" -domain: conus -run: true - -ingest_files: - - file_format: common/data/model/com/nwm/{{variable:NWM_DATAFLOW_VERSION}}/nwm.{{datetime:%Y%m%d}}/medium_range_mem2/nwm.t{{datetime:%H}}z.medium_range.channel_rt_2.f{{range:3,121,3,%03d}}.conus.nc - file_step: None - file_window: None - target_table: ingest.nwm_channel_rt_mrf_gfs_mem2 - target_keys: (feature_id, streamflow) - - file_format: common/data/model/com/nwm/{{variable:NWM_DATAFLOW_VERSION}}/nwm.{{datetime:%Y%m%d}}/medium_range_mem3/nwm.t{{datetime:%H}}z.medium_range.channel_rt_3.f{{range:3,121,3,%03d}}.conus.nc - file_step: None - file_window: None - target_table: ingest.nwm_channel_rt_mrf_gfs_mem3 - target_keys: (feature_id, streamflow) - - file_format: common/data/model/com/nwm/{{variable:NWM_DATAFLOW_VERSION}}/nwm.{{datetime:%Y%m%d}}/medium_range_mem4/nwm.t{{datetime:%H}}z.medium_range.channel_rt_4.f{{range:3,121,3,%03d}}.conus.nc - file_step: None - file_window: None - target_table: ingest.nwm_channel_rt_mrf_gfs_mem4 - target_keys: (feature_id, streamflow) - - file_format: common/data/model/com/nwm/{{variable:NWM_DATAFLOW_VERSION}}/nwm.{{datetime:%Y%m%d}}/medium_range_mem5/nwm.t{{datetime:%H}}z.medium_range.channel_rt_5.f{{range:3,121,3,%03d}}.conus.nc - file_step: None - file_window: None - target_table: ingest.nwm_channel_rt_mrf_gfs_mem5 - target_keys: (feature_id, streamflow) - - file_format: common/data/model/com/nwm/{{variable:NWM_DATAFLOW_VERSION}}/nwm.{{datetime:%Y%m%d}}/medium_range_mem6/nwm.t{{datetime:%H}}z.medium_range.channel_rt_6.f{{range:3,121,3,%03d}}.conus.nc - file_step: None - file_window: None - target_table: ingest.nwm_channel_rt_mrf_gfs_mem6 - target_keys: (feature_id, streamflow) - -db_max_flows: - - name: mrf_gfs_mem2_5day_max_flows - target_table: cache.max_flows_mrf_gfs_mem2_5day - target_keys: (feature_id, streamflow) - method: database - max_flows_sql_file: mrf_gfs_mem2_5day_max_flows - - name: mrf_gfs_mem3_5day_max_flows - target_table: cache.max_flows_mrf_gfs_mem3_5day - target_keys: (feature_id, streamflow) - method: database - max_flows_sql_file: mrf_gfs_mem3_5day_max_flows - - name: mrf_gfs_mem4_5day_max_flows - target_table: cache.max_flows_mrf_gfs_mem4_5day - target_keys: (feature_id, streamflow) - method: database - max_flows_sql_file: mrf_gfs_mem4_5day_max_flows - - name: mrf_gfs_mem5_5day_max_flows - target_table: cache.max_flows_mrf_gfs_mem5_5day - target_keys: (feature_id, streamflow) - method: database - max_flows_sql_file: mrf_gfs_mem5_5day_max_flows - - name: mrf_gfs_mem6_5day_max_flows - target_table: cache.max_flows_mrf_gfs_mem6_5day - target_keys: (feature_id, streamflow) - method: database - max_flows_sql_file: mrf_gfs_mem6_5day_max_flows - -fim_configs: - - name: mrf_gfs_mem2_max_inundation_5day - flows_table: cache.max_flows_mrf_gfs_mem2_5day - target_table: ingest.mrf_gfs_mem2_max_inundation_5day - fim_type: hand - postprocess: - sql_file: mrf_gfs_mem2_max_inundation_5day - target_table: publish.mrf_gfs_mem2_max_inundation_5day - - name: mrf_gfs_mem3_max_inundation_5day - flows_table: cache.max_flows_mrf_gfs_mem3_5day - target_table: ingest.mrf_gfs_mem3_max_inundation_5day - fim_type: hand - postprocess: - sql_file: mrf_gfs_mem3_max_inundation_5day - target_table: publish.mrf_gfs_mem3_max_inundation_5day - - name: mrf_gfs_mem4_max_inundation_5day - flows_table: cache.max_flows_mrf_gfs_mem4_5day - target_table: ingest.mrf_gfs_mem4_max_inundation_5day - fim_type: hand - postprocess: - sql_file: mrf_gfs_mem4_max_inundation_5day - target_table: publish.mrf_gfs_mem4_max_inundation_5day - - name: mrf_gfs_mem5_max_inundation_5day - flows_table: cache.max_flows_mrf_gfs_mem5_5day - target_table: ingest.mrf_gfs_mem5_max_inundation_5day - fim_type: hand - postprocess: - sql_file: mrf_gfs_mem5_max_inundation_5day - target_table: publish.mrf_gfs_mem5_max_inundation_5day - - name: mrf_gfs_mem6_max_inundation_5day - flows_table: cache.max_flows_mrf_gfs_mem6_5day - target_table: ingest.mrf_gfs_mem6_max_inundation_5day - fim_type: hand - postprocess: - sql_file: mrf_gfs_mem6_max_inundation_5day - target_table: publish.mrf_gfs_mem6_max_inundation_5day \ No newline at end of file diff --git a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/medium_range_mem1/mrf_gfs_10day_max_inundation.yml b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/medium_range_mem1/mrf_gfs_10day_max_inundation.yml index 177e6b2b..88a54e16 100644 --- a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/medium_range_mem1/mrf_gfs_10day_max_inundation.yml +++ b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/medium_range_mem1/mrf_gfs_10day_max_inundation.yml @@ -31,21 +31,21 @@ db_max_flows: fim_configs: - name: mrf_gfs_max_inundation_3day flows_table: cache.max_flows_mrf_gfs_3day - target_table: ingest.mrf_gfs_max_inundation_3day + target_table: fim_ingest.mrf_gfs_max_inundation_3day fim_type: hand postprocess: sql_file: mrf_gfs_max_inundation_3day target_table: publish.mrf_gfs_max_inundation_3day - name: mrf_gfs_max_inundation_5day flows_table: cache.max_flows_mrf_gfs_5day - target_table: ingest.mrf_gfs_max_inundation_5day + target_table: fim_ingest.mrf_gfs_max_inundation_5day fim_type: hand postprocess: sql_file: mrf_gfs_max_inundation_5day target_table: publish.mrf_gfs_max_inundation_5day - name: mrf_gfs_max_inundation_10day flows_table: cache.max_flows_mrf_gfs_10day - target_table: ingest.mrf_gfs_max_inundation_10day + target_table: fim_ingest.mrf_gfs_max_inundation_10day fim_type: hand postprocess: sql_file: mrf_gfs_max_inundation_10day diff --git a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/replace_route/rfc_based_5day_max_inundation.yml b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/replace_route/rfc_based_5day_max_inundation.yml index 61a67596..f4f3ff28 100644 --- a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/replace_route/rfc_based_5day_max_inundation.yml +++ b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/replace_route/rfc_based_5day_max_inundation.yml @@ -7,7 +7,7 @@ run: true fim_configs: - name: rfc_based_5day_max_inundation flows_table: cache.max_flows_rnr - target_table: ingest.rfc_based_5day_max_inundation + target_table: fim_ingest.rfc_based_5day_max_inundation fim_type: hand postprocess: sql_file: rfc_based_5day_max_inundation diff --git a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/short_range/srf_18hr_max_inundation.yml b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/short_range/srf_18hr_max_inundation.yml index 9bb3fa9d..7a8ca33f 100644 --- a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/short_range/srf_18hr_max_inundation.yml +++ b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/short_range/srf_18hr_max_inundation.yml @@ -21,7 +21,7 @@ db_max_flows: fim_configs: - name: srf_18hr_max_inundation flows_table: cache.max_flows_srf - target_table: ingest.srf_18hr_max_inundation + target_table: fim_ingest.srf_18hr_max_inundation fim_type: hand postprocess: sql_file: srf_18hr_max_inundation diff --git a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/short_range_hawaii/srf_48hr_max_inundation_hi.yml b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/short_range_hawaii/srf_48hr_max_inundation_hi.yml index 49274390..b2fab7d1 100644 --- a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/short_range_hawaii/srf_48hr_max_inundation_hi.yml +++ b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/short_range_hawaii/srf_48hr_max_inundation_hi.yml @@ -21,7 +21,7 @@ db_max_flows: fim_configs: - name: srf_48hr_max_inundation_hi flows_table: cache.max_flows_srf_hi - target_table: ingest.srf_48hr_max_inundation_hi + target_table: fim_ingest.srf_48hr_max_inundation_hi fim_type: hand postprocess: sql_file: srf_48hr_max_inundation_hi diff --git a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/short_range_puertorico/srf_48hr_max_inundation_prvi.yml b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/short_range_puertorico/srf_48hr_max_inundation_prvi.yml index 77bbfeca..5756f9b5 100644 --- a/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/short_range_puertorico/srf_48hr_max_inundation_prvi.yml +++ b/Core/LAMBDA/viz_functions/viz_initialize_pipeline/product_configs/short_range_puertorico/srf_48hr_max_inundation_prvi.yml @@ -21,7 +21,7 @@ db_max_flows: fim_configs: - name: srf_48hr_max_inundation_prvi flows_table: cache.max_flows_srf_prvi - target_table: ingest.srf_48hr_max_inundation_prvi + target_table: fim_ingest.srf_48hr_max_inundation_prvi fim_type: hand postprocess: sql_file: srf_48hr_max_inundation_prvi diff --git a/Core/Redshift/viz/main.tf b/Core/Redshift/viz/main.tf deleted file mode 100644 index b018f7bc..00000000 --- a/Core/Redshift/viz/main.tf +++ /dev/null @@ -1,74 +0,0 @@ -variable "environment" { - type = string -} - -variable "db_viz_redshift_security_groups" { - type = list(any) -} - -variable "subnet-a" { - type = string -} - -variable "subnet-b" { - type = string -} - -variable "db_viz_redshift_master_secret_string" { - type = string -} - -variable "db_viz_redshift_user_secret_string" { - type = string -} - -variable "viz_redshift_db_name" { - type = string -} - -variable "role_viz_redshift_arn" { - type = string -} - -variable "private_route_53_zone" { - type = object({ - name = string - zone_id = string - }) -} - -resource "aws_redshift_subnet_group" "viz_redshift_subnet_group" { - name = "hv-vpp-${var.environment}-viz-data-warehouse-subnets" - subnet_ids = [var.subnet-a, var.subnet-b] - tags = { - Name = "Viz Redshift Data Warehouse Subnet Group" - } -} - -resource "aws_redshift_cluster" "viz_redshift_data_warehouse" { - cluster_identifier = "hv-vpp-${var.environment}-viz-data-warehouse" - database_name = var.viz_redshift_db_name - master_username = jsondecode(var.db_viz_redshift_master_secret_string)["username"] - master_password = jsondecode(var.db_viz_redshift_master_secret_string)["password"] - node_type = "dc2.large" - cluster_type = "single-node" - iam_roles = [var.role_viz_redshift_arn] - vpc_security_group_ids = var.db_viz_redshift_security_groups - cluster_subnet_group_name = aws_redshift_subnet_group.viz_redshift_subnet_group.name -} - -resource "aws_route53_record" "viz_redshift_data_warehouse" { - zone_id = var.private_route_53_zone.zone_id - name = "redshift-viz.${var.private_route_53_zone.name}" - type = "CNAME" - ttl = 300 - records = [aws_redshift_cluster.viz_redshift_data_warehouse.dns_name] -} - -output "dns_name" { - value = aws_route53_record.viz_redshift_data_warehouse.name -} - -output "port" { - value = aws_redshift_cluster.viz_redshift_data_warehouse.port -} \ No newline at end of file diff --git a/Core/SecurityGroups/main.tf b/Core/SecurityGroups/main.tf index 73e75640..0de392e1 100644 --- a/Core/SecurityGroups/main.tf +++ b/Core/SecurityGroups/main.tf @@ -125,48 +125,6 @@ resource "aws_security_group" "rds" { } } -resource "aws_security_group" "redshift" { - name = "hv-vpp-${var.environment}-redshift" - description = "Redshift access" - vpc_id = var.vpc_main_id - - egress = [ - { - cidr_blocks = [ - "0.0.0.0/0", - ] - description = "" - from_port = 0 - ipv6_cidr_blocks = [] - prefix_list_ids = [] - protocol = "-1" - security_groups = [] - self = false - to_port = 0 - }, - ] - - ingress = [ - { - cidr_blocks = [ - var.vpc_main_cidr_block, - ] - description = "" - from_port = 5439 - ipv6_cidr_blocks = [] - prefix_list_ids = [] - protocol = "tcp" - security_groups = [] - self = false - to_port = 5439 - } - ] - - tags = { - "Name" = "hv-vpp-${var.environment}-redshift" - } -} - resource "aws_security_group" "vpc_access" { name = "ssm-session-manager-sg" description = "allow access to VPC endpoints" @@ -377,10 +335,6 @@ output "rds" { value = aws_security_group.rds } -output "redshift" { - value = aws_security_group.redshift -} - output "vpc_access" { value = aws_security_group.vpc_access } diff --git a/Core/StepFunctions/viz_processing_pipeline.json.tftpl b/Core/StepFunctions/viz_processing_pipeline.json.tftpl index 04e689f0..646f3a31 100644 --- a/Core/StepFunctions/viz_processing_pipeline.json.tftpl +++ b/Core/StepFunctions/viz_processing_pipeline.json.tftpl @@ -824,7 +824,7 @@ "Comment": "Lambda Service Errors" } ], - "Next": "HUC Processing Map", + "Next": "HAND Processing", "ResultPath": "$.huc_processing_payload", "ResultSelector": { "hucs_to_process.$": "$.Payload.hucs_to_process", @@ -841,9 +841,9 @@ "Next": "Process Coastal (SCHISM) FIM" } ], - "Default": "FIM Cache - Save Flows to Redshift" + "Default": "FIM Cache - Prepare Flows Table" }, - "FIM Cache - Save Flows to Redshift": { + "FIM Cache - Prepare FIM Config Tables": { "Type": "Task", "Resource": "arn:aws:states:::lambda:invoke", "Parameters": { @@ -854,21 +854,10 @@ "folder": "fim_caching_templates", "sql_templates_to_run": [ { - "sql_file": "0b_rds_create_inundation_tables_if_not_exist", + "sql_file": "0_create_or_truncate_tables", + "folder": "fim_caching_templates", "db_type": "viz", "check_dependencies": false - }, - { - "sql_file": "0a_redshift_create_inundation_tables_if_not_exist", - "db_type": "redshift" - }, - { - "sql_file": "1a_rds_build_inundation_flows_table", - "db_type": "viz" - }, - { - "sql_file": "1b_redshift_copy_inundation_flows", - "db_type": "redshift" } ] } @@ -886,25 +875,26 @@ "BackoffRate": 2 } ], - "Next": "FIM Cache - Build FIM Table on Redshift", + "Next": "FIM Cache - Prepare Flows Table", "ResultPath": null }, - "FIM Cache - Build FIM Table on Redshift": { + "FIM Cache - Prepare Flows Table": { "Type": "Task", "Resource": "arn:aws:states:::lambda:invoke", "Parameters": { + "FunctionName": "${db_postprocess_sql_arn}", "Payload": { "args.$": "$", - "step": "hand_pre_processing", + "step": "hand_pre_processing - prepare flows", "folder": "fim_caching_templates", "sql_templates_to_run": [ { - "sql_file": "2a_redshift_query_cached_fim_table", - "db_type": "redshift" + "sql_file": "1_prepare_fim_config_flows_table", + "folder": "fim_caching_templates", + "db_type": "viz" } ] - }, - "FunctionName": "${db_postprocess_sql_arn}" + } }, "Retry": [ { @@ -917,12 +907,20 @@ "IntervalSeconds": 1, "MaxAttempts": 3, "BackoffRate": 2 + }, + { + "ErrorEquals": [ + "RequiredTableNotUpdated" + ], + "MaxAttempts": 3, + "IntervalSeconds": 30, + "BackoffRate": 1 } ], - "Next": "FIM Cache - Load Cached FIM Into Ingest Tables", + "Next": "FIM Cache - Load Cached FIM Into FIM Config Tables", "ResultPath": null }, - "FIM Cache - Load Cached FIM Into Ingest Tables": { + "FIM Cache - Load Cached FIM Into FIM Config Tables": { "Type": "Task", "Resource": "arn:aws:states:::lambda:invoke", "Parameters": { @@ -932,11 +930,14 @@ "folder": "fim_caching_templates", "sql_templates_to_run": [ { - "sql_file": "3a_rds_ras2fim_insertion", - "db_type": "viz" + "sql_file": "2a_query_fim_cache-ras2fim", + "folder": "fim_caching_templates", + "db_type": "viz", + "check_dependencies": false }, { - "sql_file": "3b_rds_cached_hand_insertion", + "sql_file": "2b_query_fim_cache-hand", + "folder": "fim_caching_templates", "db_type": "viz", "check_dependencies": false } @@ -982,7 +983,7 @@ "sql_rename_dict.$": "$.Output.sql_rename_dict" } }, - "HUC Processing Map": { + "HAND Processing": { "Type": "Map", "Iterator": { "StartAt": "FIM HUC Processing State Machine", @@ -1008,7 +1009,7 @@ } }, "ItemsPath": "$.huc_processing_payload.hucs_to_process", - "Next": "FIM Cache - Add Processed FIM to Cache", + "Next": "FIM Cache - Add Processed FIM Back to HAND Cache", "ItemSelector": { "huc_branches_to_process.$": "$$.Map.Item.Value", "data_bucket.$": "$.huc_processing_payload.data_bucket", @@ -1021,19 +1022,20 @@ "MaxConcurrency": 4, "ResultPath": null }, - "FIM Cache - Add Processed FIM to Cache": { + "FIM Cache - Add Processed FIM Back to HAND Cache": { "Type": "Task", "Resource": "arn:aws:states:::lambda:invoke", "Parameters": { - "FunctionName": "arn:aws:lambda:us-east-1:526904826677:function:hv-vpp-cached-fim-viz-db-postprocess-sql:$LATEST", + "FunctionName": "${db_postprocess_sql_arn}", "Payload": { "args.$": "$", "step": "hand_post_processing", "folder": "fim_caching_templates", "sql_templates_to_run": [ { - "sql_file": "5a_redshift_cache_fim_from_rds", - "db_type": "redshift", + "sql_file": "3_add_any_processed_hand_fim_back_to_cache", + "folder": "fim_caching_templates", + "db_type": "viz", "check_dependencies": false } ] @@ -1050,6 +1052,15 @@ "IntervalSeconds": 1, "MaxAttempts": 3, "BackoffRate": 2 + }, + { + "ErrorEquals": [ + "UniqueViolation" + ], + "BackoffRate": 1, + "MaxAttempts": 3, + "Comment": "Retry if a unique violation happens - this may need a custom exception.", + "IntervalSeconds": 60 } ], "Next": "Postprocess SQL - FIM Config", @@ -1063,7 +1074,21 @@ "Payload": { "args.$": "$", "step": "fim_config", - "folder": "fim_configs" + "folder": "fim_configs", + "sql_templates_to_run": [ + { + "sql_file": "4_create_fim_config_publish_table", + "folder": "fim_caching_templates", + "db_type": "viz", + "check_dependencies": false + }, + { + "sql_file": "5_dummy_rows_for_wpod_service_monitor", + "folder": "fim_caching_templates", + "db_type": "viz", + "check_dependencies": false + } + ] } }, "Retry": [ diff --git a/Core/main.tf b/Core/main.tf index 34ab193a..4a223317 100644 --- a/Core/main.tf +++ b/Core/main.tf @@ -58,7 +58,6 @@ module "iam-roles" { prod_account_id = local.env.prod_account_id region = local.env.region nws_shared_account_s3_bucket = local.env.nws_shared_account_s3_bucket - viz_proc_admin_rw_secret_arn = module.secrets-manager.secret_arns["viz-proc-admin-rw-user"] } # IAM Users @@ -111,8 +110,6 @@ module "secrets-manager" { "viz-processing-pg-rdssecret" = { "username" : "postgres" } "viz-proc-admin-rw-user" = { "username" : "viz_proc_admin_rw_user" } "viz-proc-dev-rw-user" = { "username" : "viz_proc_dev_rw_user" } - "viz-redshift-master" = { "username" : "viz_redshift_master" } - "viz-redshift-user" = { "username" : "viz_redshift_user" } "ingest-pg-rdssecret" = { "username" : "postgres" } "ingest-mqsecret" = { "username" : "rabbit_admin" } "rds-rfc-fcst" = { "username" : "rfc_fcst" } @@ -342,21 +339,6 @@ module "rds-viz" { private_route_53_zone = module.private-route53.zone } -module "redshift-viz" { - source = "./Redshift/viz" - - environment = local.env.environment - subnet-a = module.vpc.subnet_private_a.id - subnet-b = module.vpc.subnet_private_b.id - db_viz_redshift_master_secret_string = module.secrets-manager.secret_strings["viz-redshift-master"] - db_viz_redshift_user_secret_string = module.secrets-manager.secret_strings["viz-redshift-user"] - db_viz_redshift_security_groups = [module.security-groups.redshift.id] - viz_redshift_db_name = local.env.viz_redshift_db_name - role_viz_redshift_arn = module.iam-roles.role_redshift.arn - - private_route_53_zone = module.private-route53.zone -} - ###################### STAGE 4 ###################### (Set up Deployment Bucket Artifacts and EGIS Resources before deploying) # EGIS Route53 DNS @@ -416,12 +398,6 @@ module "rds-bastion" { viz_db_address = module.rds-viz.instance.address viz_db_port = module.rds-viz.instance.port viz_db_name = local.env.viz_db_name - viz_redshift_master_secret_string = module.secrets-manager.secret_strings["viz-redshift-master"] - viz_redshift_user_secret_string = module.secrets-manager.secret_strings["viz-redshift-user"] - viz_redshift_address = module.redshift-viz.dns_name - viz_redshift_port = module.redshift-viz.port - viz_redshift_name = local.env.viz_redshift_db_name - viz_redshift_iam_role_arn = module.iam-roles.role_redshift.arn egis_db_master_secret_string = module.secrets-manager.secret_strings["egis-master-pg-rds-secret"] egis_db_secret_string = module.secrets-manager.secret_strings["egis-pg-rds-secret"] egis_db_address = module.rds-egis.dns_name @@ -640,9 +616,6 @@ module "viz-lambda-functions" { egis_db_name = local.env.egis_db_name egis_db_user_secret_string = module.secrets-manager.secret_strings["egis-pg-rds-secret"] egis_portal_password = local.env.viz_ec2_hydrovis_egis_pass - viz_redshift_host = module.redshift-viz.dns_name - viz_redshift_db_name = local.env.viz_redshift_db_name - viz_redshift_user_secret_string = module.secrets-manager.secret_strings["viz-redshift-user"] dataservices_host = module.data-services.dns_name viz_pipeline_step_function_arn = module.step-functions.viz_pipeline_step_function.arn default_tags = local.env.tags