From 03d8a41e086b83c7bf7ecb32351ad88814dcf55b Mon Sep 17 00:00:00 2001 From: andrewp-CWF <146005147+andrewp-CWF@users.noreply.github.com> Date: Tue, 13 Aug 2024 15:38:42 -0400 Subject: [PATCH] Ranking update (#562) * Added permission assignment to auto_rank.py * Update auto_rank.py with truncate instead of drop * Created auto_rank.sql This file includes a function to create the ranked_barrier table for a given wcrp in the given schema * merge, fix typo --------- Co-authored-by: Simon Norris --- db/v0.5.2/migrate.sh | 11 +++- db/v0.5.2/sql/auto_rank.sql | 51 ++++++++++++++++++ jobs/auto_rank.py | 103 +++++++++++++++++------------------- 3 files changed, 108 insertions(+), 57 deletions(-) create mode 100644 db/v0.5.2/sql/auto_rank.sql diff --git a/db/v0.5.2/migrate.sh b/db/v0.5.2/migrate.sh index 043a2b1d..fad2511b 100755 --- a/db/v0.5.2/migrate.sh +++ b/db/v0.5.2/migrate.sh @@ -1,10 +1,17 @@ #!/bin/bash set -euxo pipefail - +# update crossings_vw psql $DATABASE_URL -f sql/modelled_crossing_office_review_date.sql -psql $DATABASE_URL -c "update bcfishpass.db_version set tag = '${PWD##*/}'" +# FWCP echo "On systems supporting FPTWG reporting, restore views by running:" echo "psql $DATABASE_URL -f ../v0.5.0/sql/views/fptwg_freshwater_fish_habitat_accessibility_model.sql" echo "psql $DATABASE_URL -f ../v0.5.0/sql/reports/fptwg_assmt_wsd_summary_vw.sql" + +# CWF +echo "On systems supporting CWF WCRP reporting, add auto_rank.sql" +echo "psql $DATABASE_URL -f sql/auto_rank.sql" + +# note version +psql $DATABASE_URL -c "update bcfishpass.db_version set tag = '${PWD##*/}'" \ No newline at end of file diff --git a/db/v0.5.2/sql/auto_rank.sql b/db/v0.5.2/sql/auto_rank.sql new file mode 100644 index 00000000..cea1e793 --- /dev/null +++ b/db/v0.5.2/sql/auto_rank.sql @@ -0,0 +1,51 @@ +-- create table for ranked list + +CREATE FUNCTION bcfishpass.create_rank_table(schema_name text, wcrp text) + RETURNS VOID + LANGUAGE plpgsql AS +$func$ +BEGIN + + EXECUTE format(' + CREATE TABLE IF NOT EXISTS %I.%I + ( + aggregated_crossings_id text, + crossing_source text, + crossing_feature_type text, + pscis_status text, + crossing_type_code text, + barrier_status text, + pscis_road_name text, + pscis_assessment_comment text, + pscis_assessment_date date, + utm_zone integer, + utm_easting integer, + utm_northing integer, + blue_line_key integer, + watershed_group_code integer, + gnis_stream_name text, + barriers_anthropogenic_dnstr text, + barriers_anthropogenic_habitat_wcrp_upstr text, + barriers_anthropogenic_habitat_wcrp_upstr_count integer, + all_spawning_km double precision, + all_spawning_belowupstrbarriers_km double precision, + all_rearing_km double precision, + all_rearing_belowupstrbarriers_km double precision, + all_spawningrearing_km double precision, + all_spawningrearing_belowupstrbarriers_km double precision, + set_id numeric, + total_hab_gain_set numeric, + num_barriers_set integer, + avg_gain_per_barrier numeric, + dnstr_set_ids character varying[], + rank_avg_gain_per_barrier numeric, + rank_avg_gain_tiered numeric, + tier_combined character varying, + geom geometry + )', + schema_name, + 'ranked_barriers_' || wcrp + ); + +END +$func$ \ No newline at end of file diff --git a/jobs/auto_rank.py b/jobs/auto_rank.py index d0b8b360..7c2cea16 100644 --- a/jobs/auto_rank.py +++ b/jobs/auto_rank.py @@ -509,62 +509,55 @@ def runQuery(condition, wcrp, wcrp_schema, conn): q_wcrp_rank_table = f""" -- Create a table to join crossings_wcrp_vw and wcrp_ranked_barriers fields in wcrp schema - DROP TABLE IF EXISTS wcrp_{wcrp_schema}.ranked_barriers_{wcrp}; + TRUNCATE TABLE wcrp_{wcrp_schema}.ranked_barriers_{wcrp}; - CREATE TABLE wcrp_{wcrp_schema}.ranked_barriers_{wcrp} as - ( - select - cv.aggregated_crossings_id - ,cv.crossing_source - ,cv.crossing_feature_type - ,cv.pscis_status - ,cv.crossing_type_code - ,cv.crossing_subtype_code - ,cv.barrier_status - ,cv.pscis_road_name - ,cv.pscis_stream_name - ,cv.pscis_assessment_comment - ,cv.pscis_assessment_date - ,cv.utm_zone - ,cv.utm_easting - ,cv.utm_northing - ,cv.blue_line_key - ,cv.watershed_group_code - ,cv.gnis_stream_name - ,cv.barriers_anthropogenic_dnstr - ,cv.barriers_anthropogenic_dnstr_count - ,cv.barriers_anthropogenic_habitat_wcrp_upstr - ,cv.barriers_anthropogenic_habitat_wcrp_upstr_count - ,cv.all_spawning_km - ,cv.all_spawning_belowupstrbarriers_km - ,cv.all_rearing_km - ,cv.all_rearing_belowupstrbarriers_km - ,cv.all_spawningrearing_km - ,cv.all_spawningrearing_belowupstrbarriers_km - ,r.set_id - ,r.total_hab_gain_set - ,r.num_barriers_set - ,r.avg_gain_per_barrier - ,r.dnstr_set_ids - ,r.rank_avg_gain_per_barrier - ,r.rank_avg_gain_tiered - ,r.rank_combined - ,r.tier_combined - ,cv.geom - from bcfishpass.wcrp_ranked_barriers r - join bcfishpass.crossings_wcrp_vw cv - on r.aggregated_crossings_id = cv.aggregated_crossings_id - join bcfishpass.crossings c - on c.aggregated_crossings_id = cv.aggregated_crossings_id - where {condition} - order by rank_combined - ); - - ALTER TABLE wcrp_{wcrp_schema}.ranked_barriers_{wcrp} GRANT SELECT TO cwf_user; - ALTER TABLE wcrp_{wcrp_schema}.ranked_barriers_{wcrp} GRANT ALL TO cwf_analyst; - ALTER TABLE wcrp_{wcrp_schema}.ranked_barriers_{wcrp} GRANT SELECT TO bcfishpass_user; - ALTER TABLE wcrp_{wcrp_schema}.ranked_barriers_{wcrp} GRANT ALL TO cwf_analyst; - """ + INSERT INTO wcrp_{wcrp_schema}.ranked_barriers_{wcrp} + select + cv.aggregated_crossings_id + ,cv.crossing_source + ,cv.crossing_feature_type + ,cv.pscis_status + ,cv.crossing_type_code + ,cv.crossing_subtype_code + ,cv.barrier_status + ,cv.pscis_road_name + ,cv.pscis_stream_name + ,cv.pscis_assessment_comment + ,cv.pscis_assessment_date + ,cv.utm_zone + ,cv.utm_easting + ,cv.utm_northing + ,cv.blue_line_key + ,cv.watershed_group_code + ,cv.gnis_stream_name + ,cv.barriers_anthropogenic_dnstr + ,cv.barriers_anthropogenic_dnstr_count + ,cv.barriers_anthropogenic_habitat_wcrp_upstr + ,cv.barriers_anthropogenic_habitat_wcrp_upstr_count + ,cv.all_spawning_km + ,cv.all_spawning_belowupstrbarriers_km + ,cv.all_rearing_km + ,cv.all_rearing_belowupstrbarriers_km + ,cv.all_spawningrearing_km + ,cv.all_spawningrearing_belowupstrbarriers_km + ,r.set_id + ,r.total_hab_gain_set + ,r.num_barriers_set + ,r.avg_gain_per_barrier + ,r.dnstr_set_ids + ,r.rank_avg_gain_per_barrier + ,r.rank_avg_gain_tiered + ,r.rank_combined + ,r.tier_combined + ,cv.geom + from bcfishpass.wcrp_ranked_barriers r + join bcfishpass.crossings_wcrp_vw cv + on r.aggregated_crossings_id = cv.aggregated_crossings_id + join bcfishpass.crossings c + on c.aggregated_crossings_id = cv.aggregated_crossings_id + where {condition} + order by rank_combined; + """ cursor.execute(q_wcrp_rank_table) conn.commit()