Skip to content

Commit

Permalink
Ranking update (#562)
Browse files Browse the repository at this point in the history
* 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 <snorris@hillcrestgeo.ca>
  • Loading branch information
andrewp-CWF and smnorris committed Aug 13, 2024
1 parent f304abd commit 03d8a41
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 57 deletions.
11 changes: 9 additions & 2 deletions db/v0.5.2/migrate.sh
Original file line number Diff line number Diff line change
@@ -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##*/}'"
51 changes: 51 additions & 0 deletions db/v0.5.2/sql/auto_rank.sql
Original file line number Diff line number Diff line change
@@ -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$
103 changes: 48 additions & 55 deletions jobs/auto_rank.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 03d8a41

Please sign in to comment.