Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spi migrate users ups into test spi #1364

Merged
merged 35 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
018158b
initial commit for study areas. File creation and first attempt at th…
LouisThedroux Aug 28, 2024
cb1fbec
finished ?? sampling methods, only need to replace PLACEHOLDER method…
AMEIJER1 Aug 28, 2024
a358281
trying to fix indentations on when statements
AMEIJER1 Aug 28, 2024
c3e323e
currently very broken and returning way too many rows. Not sure how t…
LouisThedroux Aug 28, 2024
bfb3a05
study areas troubleshooting
AMEIJER1 Aug 28, 2024
0f22137
removed a migrations file i had created in this branch by accident as…
AMEIJER1 Aug 29, 2024
6f8d1a1
sudy area script, still need to loop or something to fix timeout error
AMEIJER1 Aug 29, 2024
b9e2b34
new branch for transforming DC visits into sampling periods
AMEIJER1 Aug 29, 2024
a281809
untested materials
AMEIJER1 Aug 29, 2024
420b58a
changes to sampling period
AMEIJER1 Sep 3, 2024
2087224
ready for review
AMEIJER1 Sep 3, 2024
0d44484
ready for review, but actually this time
AMEIJER1 Sep 3, 2024
e03cdb2
please review possibilty of creating new temp table to use for spi wl…
AMEIJER1 Sep 4, 2024
8319a5e
changing progress ID portion to reflect status according to dates
AMEIJER1 Sep 9, 2024
378d9f5
added some logic that pulls some email addresses from spi_secured_per…
LouisThedroux Sep 9, 2024
8076d92
;
LouisThedroux Sep 9, 2024
a712e18
added additional sql for comparison
AMEIJER1 Sep 10, 2024
df009f1
changed email address to email address when we can link one via first…
LouisThedroux Sep 10, 2024
b897ef1
study area updates
AMEIJER1 Sep 10, 2024
d37a619
updates to survey
AMEIJER1 Sep 10, 2024
7ac3d6d
added join and condition. If no match on first and last names, system…
LouisThedroux Sep 10, 2024
96bab09
reverting back because I made mistakes
LouisThedroux Sep 10, 2024
a4d0c19
updates to check if user already exists based off of name and setting…
LouisThedroux Sep 10, 2024
ad7020c
a commit of defeat and sadness
AMEIJER1 Sep 11, 2024
cd06549
add comment
AMEIJER1 Sep 11, 2024
7c4b629
Merge branch 'spi-migrate-survey_ups' into spi-migrate-users_ups
AMEIJER1 Sep 11, 2024
d035827
merge in latest
AMEIJER1 Sep 11, 2024
cb4d13b
merge latest
AMEIJER1 Sep 11, 2024
05ec038
Merge branch 'spi-migrate-spi-study-areas' into spi-migrate-users_ups
AMEIJER1 Sep 11, 2024
0036e92
Merge branch 'spi-migrate-samplingvisits' into spi-migrate-users_ups
AMEIJER1 Sep 11, 2024
8a5d843
merge latest
AMEIJER1 Sep 11, 2024
7cb8796
pr fixes
AMEIJER1 Sep 12, 2024
af55c5d
pr edits, again
AMEIJER1 Sep 12, 2024
9f0f41e
make fix
LouisThedroux Sep 12, 2024
06450af
merge latest
LouisThedroux Sep 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,767 changes: 817 additions & 1,950 deletions spi/package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions spi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ async function main() {
// STEP 6. Transforms SPI Design Components into SIMS Sampling Sites
await transformSampleSites(connection);
AMEIJER1 marked this conversation as resolved.
Show resolved Hide resolved

//STEP 7. Transforms SPI Survey Areas into SIMS Survey Locations
await transformSampleSites(connection);

// Commit the transactions
connection.commit();

Expand Down
59 changes: 50 additions & 9 deletions spi/src/transformations/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,65 @@ export const transformProjects = async (connection: IDBConnection): Promise<void
SELECT
p.spi_project_id,
pp.person_id,
b.project_id
b.project_id,
CONCAT(pp.first_given_name, ' ', pp.surname) AS full_name,
spp.email_address
FROM
public.spi_projects p
INNER JOIN
biohub.project b ON p.spi_project_id = b.spi_project_id
INNER JOIN
spi_persons pp ON pp.spi_project_id = p.spi_project_id
INNER JOIN biohub.project b
ON p.spi_project_id = b.spi_project_id
INNER JOIN spi_persons pp
ON pp.spi_project_id = p.spi_project_id
LEFT JOIN public.spi_secure_persons spp
ON spp.first_name = pp.first_given_name
AND spp.last_name = pp.surname
)
INSERT INTO
biohub.project_participation (project_id, system_user_id, project_role_id, create_user)
SELECT DISTINCT
w_mapping.project_id,
(SELECT biohub_user_id FROM public.migrate_spi_user_deduplication WHERE w_mapping.person_id = ANY (spi_person_ids)),
(SELECT project_role_id FROM biohub.project_role WHERE name = 'Collaborator'),
(SELECT system_user_id FROM biohub.system_user WHERE user_identifier = 'spi')

COALESCE (
(SELECT biohub_user_id
FROM public.migrate_spi_user_deduplication
WHERE w_mapping.person_id = ANY (spi_person_ids)
),
(SELECT system_user_id
FROM biohub.system_user
WHERE user_identifier = 'spi')
) AS system_user_id,

CASE
WHEN p.coordinator IS NOT NULL AND w_mapping.full_name LIKE '%' || TRIM(BOTH ', ' FROM p.coordinator) || '%' AND spp.email_address LIKE '%@gov.bc.ca%' THEN
mauberti-bc marked this conversation as resolved.
Show resolved Hide resolved
(SELECT project_role_id FROM biohub.project_role WHERE name = 'Coordinator')
WHEN NOT EXISTS (
SELECT 1
FROM biohub.project_participation
WHERE project_id = w_mapping.project_id
AND project_role_id = (SELECT project_role_id FROM biohub.project_role WHERE name = 'Coordinator')
) THEN (SELECT project_role_id FROM biohub.project_role WHERE name = 'Coordinator'), (SELECT system_user_id FROM biohub.system_user WHERE user_identifier = 'spi')
mauberti-bc marked this conversation as resolved.
Show resolved Hide resolved
----- double check that when the not exists is doing what we want it to do (assign spi as coordinator when coordinator is undefined)
WHEN spp.email_address LIKE '%@gov.bc.ca%'
AND NOT EXISTS (
SELECT 1
FROM biohub.project_participation
WHERE project_id = w_mapping.project_id
AND project_role_id = (SELECT project_role_id FROM biohub.project_role WHERE name = 'Coordinator')
) THEN
(SELECT project_role_id FROM biohub.project_role WHERE name = 'Collaborator')
ELSE
(SELECT project_role_id FROM biohub.project_role WHERE name = 'Observer')
END AS project_role_id,

(SELECT system_user_id FROM biohub.system_user WHERE user_identifier = 'spi') AS create_user

FROM
w_mapping
ON CONFLICT DO NOTHING;
JOIN public.spi_projects p
ON p.spi_project_id = w_mapping.spi_project_id
JOIN public.spi_secure_persons spp
ON spp.first_name = pp.first_given_name
AND spp.last_name = pp.surname;
`;

await connection.sql(sql);
Expand Down
939 changes: 854 additions & 85 deletions spi/src/transformations/sampling_methods.ts

Large diffs are not rendered by default.

70 changes: 70 additions & 0 deletions spi/src/transformations/sampling_period.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import SQL from 'sql-template-strings';
import { IDBConnection } from '../db';

export const transformSampleVisits = async (connection: IDBConnection): Promise<void> => {
const transformSampleVisitsSql = SQL`
set search_path = biohub,public;

------ transforming design component visits into sampling periods ---
INSERT INTO biohub.survey_sample_period (create_date, survey_sample_method_id, start_date, end_date, start_time, end_time)
SELECT
sdcv.when_created,
ssm.survey_sample_method_id,
DATE(sdcv.visit_date) AS start_date,
DATE(COALESCE(sdcv.visit_end_date, sdcv.visit_date)) AS end_date,
CASE
WHEN sdcv.start_time = '0000' THEN NULL
ELSE TO_TIMESTAMP(LPAD(sdcv.start_time, 4, '0'), 'HH24MI')::time
END AS start_time,
CASE
WHEN sdcv.end_time = '0000' THEN NULL
ELSE TO_TIMESTAMP(LPAD(sdcv.end_time, 4, '0'), 'HH24MI')::time
END AS end_time
FROM
public.spi_design_component_visits sdcv
JOIN
public.migrate_spi_sample_design_component msd
ON sdcv.design_component_id = msd.design_component_id
JOIN
biohub.survey_sample_method ssm
ON msd.survey_sample_site_id = ssm.survey_sample_site_id
GROUP BY
sdcv.design_component_visit_id,
sdcv.when_created,
sdcv.start_time,
sdcv.end_time,
ssm.survey_sample_method_id;

-- possibility to create another temporary table that houses the design component visit id and the survey sample period id
-- ensures that we can properly attribute spi_wildlife_observations to the appropriate sampling period to get dates

-- setting up the table --
CREATE TABLE IF NOT EXISTS public.migrate_spi_sample_period_visit (
design_component_visit_id INT,
survey_sample_period_id INT,
PRIMARY KEY (design_component_visit_id, survey_sample_period_id));

-- inserting component visit id and survey sample period id into the table --
INSERT INTO public.migrate_spi_sample_period_visit (design_component_visit_id, survey_sample_period_id)
SELECT
sdcv.design_component_visit_id,
ssp.survey_sample_period_id
FROM
public.spi_design_component_visits sdcv
JOIN
public.migrate_spi_sample_design_component msd
ON sdcv.design_component_id = msd.design_component_id
JOIN
biohub.survey_sample_method ssm
ON msd.survey_sample_site_id = ssm.survey_sample_site_id
JOIN
biohub.survey_sample_period ssp
ON ssp.survey_sample_method_id = ssm.survey_sample_method_id
GROUP BY
sdcv.design_component_visit_id, ssp.survey_sample_period_id;
`;

await connection.sql(transformSampleVisitsSql);

console.log('Successfully transformed design component visits');
};
58 changes: 58 additions & 0 deletions spi/src/transformations/study-area.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import SQL from 'sql-template-strings';
import { IDBConnection } from '../db';

export const transformStudyAreas = async (connection: IDBConnection): Promise<void> => {
console.log('Transforming Study Area');

const sql = SQL`
-------------------------------------------------------------------------------------------------
-- Transforms SPI Study Areas into SIMS Survey Locations

WITH w_survey_points AS (
SELECT
bs.survey_id,
sa.study_area_name,
sa.study_area_description,
igd.geo,
ST_ConvexHull(ST_Collect(ST_SetSRID(ST_MakePoint(wso.longitude, wso.latitude), 4326))) AS convex_hull,
COUNT(wso.wlo_id) AS so_count
FROM biohub.survey bs
JOIN public.spi_survey_study_areas ssa
ON bs.spi_survey_id = ssa.survey_id
JOIN public.spi_study_areas sa
ON ssa.study_area_id = sa.study_area_id
JOIN public.spi_document_references sdr
ON sa.study_area_id = sdr.study_area_id
LEFT JOIN public.imported_geo_data igd
ON sdr.document_reference_id = igd.spi_document_reference
LEFT JOIN public.spi_wildlife_observations wso
ON bs.spi_survey_id = wso.survey_id
WHERE sa.study_area_id IS NOT NULL
AND wso.longitude IS NOT NULL
AND wso.latitude IS NOT NULL
GROUP BY bs.survey_id, sa.study_area_name, sa.study_area_description, igd.geo
),
buffered_geographies AS (
SELECT
sp.survey_id,
sp.study_area_name,
sp.study_area_description,
sp.convex_hull,
sp.geo,
sp.so_count,
(0.2 * sqrt(ST_Area(sp.convex_hull)) + 0.05 * pow((ST_Area(sp.convex_hull) / sp.so_count), 0.75)) AS buffer_value
AMEIJER1 marked this conversation as resolved.
Show resolved Hide resolved
FROM w_survey_points sp
)
INSERT INTO biohub.survey_location (survey_id, name, description, geography)
SELECT
bg.survey_id,
bg.study_area_name,
bg.study_area_description,
COALESCE(sp.geo, ST_Buffer(bg.convex_hull, bg.buffer_value)) AS geography
FROM buffered_geographies bg;
`;

await connection.sql(sql);

console.log('Successfully transformed study areas');
};
7 changes: 6 additions & 1 deletion spi/src/transformations/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ export const transformSurveys = async (connection: IDBConnection): Promise<void>
WHEN ss.end_date < ss.start_date THEN ss.start_date
ELSE ss.end_date
END AS end_date,
(SELECT survey_progress_id FROM biohub.survey_progress WHERE name = 'In progress'),
CASE
WHEN ss.end_date < now() THEN (SELECT survey_progress_id FROM biohub.survey_progress WHERE name = 'Completed')
WHEN ss.start_date > now() THEN (SELECT survey_progress_id FROM biohub.survey_progress WHERE name = 'Planning')
WHEN now() BETWEEN ss.start_date AND ss.end_date THEN (SELECT survey_progress_id FROM biohub.survey_progress WHERE name = 'In progress')
ELSE (SELECT survey_progress_id FROM biohub.survey_progress WHERE name = 'In progress')
END AS progress_id,
'Start: ' || ss.start_date || '. End: ' || ss.end_date
FROM
public.spi_surveys ss
Expand Down
65 changes: 40 additions & 25 deletions spi/src/transformations/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,54 @@ export const transformUsers = async (connection: IDBConnection): Promise<void> =
-------------------------------------------------------------------------------------------------
-- Turn deduplicated users into SIMS users
-------------------------------------------------------------------------------------------------
WITH w_existing_users AS (
SELECT
system_user_id,
family_name,
given_name
FROM biohub.system_user
)
INSERT INTO biohub.system_user (
user_identity_source_id,
user_identifier,
record_effective_date,
display_name,
given_name,
family_name,
notes,
email
user_identity_source_id,
user_identifier,
record_effective_date,
display_name,
given_name,
family_name,
notes,
email
)
SELECT
(SELECT user_identity_source_id FROM biohub.user_identity_source WHERE name = 'UNVERIFIED'),
'spi-' || id,
now(),
display_name,
given_name,
family_name,
'Migrated from SPI as user' || id,
'default'
(SELECT user_identity_source_id FROM biohub.user_identity_source WHERE name = 'UNVERIFIED'),
'spi-' || md.id,
now(),
md.display_name,
md.given_name,
md.family_name,
'Migrated from SPI as user ' || md.id,
COALESCE(spp.email_address, 'default') AS email
FROM
migrate_spi_user_deduplication;
migrate_spi_user_deduplication md
LEFT JOIN
public.spi_secure_persons spp ON spp.first_name = md.given_name AND spp.last_name = md.family_name
LEFT JOIN
w_existing_users eu ON eu.given_name = md.given_name AND eu.family_name = md.family_name
WHERE eu.system_user_id IS NULL;
AMEIJER1 marked this conversation as resolved.
Show resolved Hide resolved

-------------------------------------------------------------------------------------------------
-- Update deduplicated users table with the system_user_id
-------------------------------------------------------------------------------------------------
UPDATE
migrate_spi_user_deduplication AS m
SET
biohub_user_id = su.system_user_id
FROM
biohub.system_user AS su
WHERE
su.user_identifier = 'spi-' || m.id;
UPDATE migrate_spi_user_deduplication AS m
SET biohub_user_id = su.system_user_id
FROM biohub.system_user AS su
WHERE su.user_identifier = 'spi-' || m.id;

-- For existing users
UPDATE migrate_spi_user_deduplication AS m
SET biohub_user_id = eu.system_user_id
FROM w_existing_users eu
WHERE eu.given_name = m.given_name
AND eu.family_name = m.family_name;
`;

await connection.sql(sql);
Expand Down