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

23.11 fb cleanup vet assignment #1052

Open
wants to merge 18 commits into
base: release23.11-SNAPSHOT
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
64 changes: 26 additions & 38 deletions onprc_ehr/resources/queries/study/demographicsAssignedVet.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,34 @@ Returns one or more assigned vets per animal ID
Future enhancements
* Add ProjectType to select statement and hide in XML
* Add Protocol_PI to study.vet_assignmentFilter and to onprc_ehr.vet_assignment
* Add species to study.vet_assignmentFilter and to onprc_ehr.vet_assignment
* Add species to onprc_ehr.vet_assignment
* Add validation to onprc_ehr.vet_assignment to prevent creating a rule that has
* no match in the filter
*/

SELECT DISTINCT VAF1.Id
, VAF1.AssignedVet
, VAF1.AssignmentType
, VAF1.Room
, VAF1.Area
, VAF1.Calculated_Status
, VAF1.MatchedRule
FROM vetAssignment_filter AS VAF1
INNER JOIN (
SELECT VAF2.Id
, MIN(VAF2.matchedRule) AS minRule
FROM vetAssignment_filter AS VAF2
GROUP BY VAF2.id
) AS VAF3
ON VAF1.id = VAF3.id
AND VAF1.matchedRule = VAF3.minRule
-- Step 1: Find the minimum matchedRule for each ID
WITH MinMatchedRules AS (
SELECT
VAF2.Id,
MIN(VAF2.matchedRule) AS MinRule
FROM
vetAssignment_filter AS VAF2
GROUP BY
VAF2.Id
)


-- SELECT VAF1.Id
-- , VAF1.AssignedVet
-- , VAF1.AssignmentType
-- /* , VAF1.AssignmentType AS codeAssignmentType*/
-- , VAF1.Room
-- , VAF1.Area
-- , VAF1.Project
-- , VAF1.Protocol
-- , VAF1.ProtocolPI
-- , VAF1.Calculated_Status
-- , VAF1.MatchedRule
-- , VAF1.history
-- FROM vetAssignment_filter AS VAF1
-- INNER JOIN (
-- SELECT VAF2.Id
-- , MIN(VAF2.matchedRule) AS minRule
-- FROM vetAssignment_filter AS VAF2
-- GROUP BY VAF2.id
-- ) AS VAF3 ON VAF1.id = VAF3.id
-- AND VAF1.matchedRule = VAF3.minRule
-- Step 2: Join the original table with the filtered minimum matched rules
SELECT DISTINCT
VAF1.Id,
VAF1.AssignedVet,
VAF1.AssignmentType,
VAF1.Room,
VAF1.Area,
VAF1.Calculated_Status,
VAF1.MatchedRule
FROM
vetAssignment_filter AS VAF1
INNER JOIN
MinMatchedRules AS MMR
ON VAF1.Id = MMR.Id
AND VAF1.matchedRule = MMR.MinRule
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<customView name="NHPs Without an Assigned Vet" xmlns="http://labkey.org/data/xml/queryCustomView">
<columns>
<column name="Id"/>
<column name="AssignedVet"/>
<column name="AssignmentType"/>
<column name="area"/>
<column name="room"/>
<column name="project"/>
<column name="protocol"/>
<column name="protocolPI"/>
<column name="Id/demographics/history"/>
</columns>
<filters>
<filter column="AssignedVet" operator="contains" value="Unassigned"/>
</filters>
<sorts>
<sort column="Id" descending="true"/>
</sorts>
</customView>
92 changes: 19 additions & 73 deletions onprc_ehr/resources/queries/study/vetAssignment_demographics.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,84 +9,30 @@ Notes:
result in open cases * assignments for that animal
* Left joins are needed for CMUcases and assignedProject as those tables
might not have records for an animal ID. Left joins are used for all
other joins to be resiliant against missing data.
* enddate.TimeCoalesced is never in the future

Future modifications:
* Limit open cases to those that are not inactive after confirming w/ Heather
other joins to be resilient against missing data.
*/

SELECT demographics.id
, CMUcases.assignedVet.displayName AS caseVet
, CMUcases.date AS caseDate
, housing.room
, housing.room.area
, assignedProject.project AS project
, assignedProject.Protocol AS protocol
, assignedProject.PI AS protocolPI
, assignedProject.projectType AS assignmentType
, demographics.calculated_status
, demographics.gender
, demographics.species
, demographics.history
SELECT demographics.id,
CMUcases.assignedVet.displayName AS caseVet,
CMUcases.date AS caseDate,
housing.room,
housing.room.area,
assignedProject.project AS project,
assignedProject.Protocol AS protocol,
assignedProject.PI AS protocolPI,
assignedProject.projectType AS assignmentType,
demographics.calculated_status,
demographics.gender,
demographics.species,
demographics.history
FROM Site.{ substitutePath moduleProperty('EHR', 'EHRStudyContainer') }.study.animal AS nhp
LEFT JOIN Site.{ substitutePath moduleProperty('EHR', 'EHRStudyContainer') }.study.ClinicalCases_Open AS CMUcases
ON CMUcases.id = nhp.id
LEFT JOIN Site.{ substitutePath moduleProperty('EHR', 'EHRStudyContainer') }.study.housing AS housing
ON CMUcases.id = nhp.id
LEFT JOIN Site.{ substitutePath moduleProperty('EHR', 'EHRStudyContainer') }.study.housing AS housing
ON housing.id = nhp.id
LEFT JOIN Site.{ substitutePath moduleProperty('EHR', 'EHRStudyContainer') }.study.demographics AS demographics
LEFT JOIN Site.{ substitutePath moduleProperty('EHR', 'EHRStudyContainer') }.study.demographics AS demographics
ON demographics.id = nhp.id
LEFT JOIN Site.{ substitutePath moduleProperty('EHR', 'EHRStudyContainer') }.study.vetAssignment_projects AS assignedProject
LEFT JOIN Site.{ substitutePath moduleProperty('EHR', 'EHRStudyContainer') }.study.vetAssignment_projects AS assignedProject
ON assignedProject.id = nhp.id
WHERE demographics.Calculated_Status = 'Alive'
AND nhp.id NOT LIKE '[A-Z]%'
AND housing.enddate IS NULL


/*
study.vetAssignment_demographics.sql

Replaces: study.vetAssignmentDemographics

Returns at least one record for all living NHPs at the center.
Notes:
* Multiple open cases and multiple assignments for a single animal
result in open cases * assignments for that animal
* Left joins are needed for CMUcases and assignedProject as those tables
might not have records for an animal ID. Left joins are used for all
other joins to be resiliant against missing data.

Future modifications:
* Limit open cases to those that are not inactive
*/
/*
SELECT demographics.id
, CMUcases.assignedVet.displayName AS caseVet
, CMUcases.DATE AS caseDate
, housing.room
, housing.room.area
, assignedProject.project AS project
, assignedProject.Protocol AS protocol
, assignedProject.PI AS protocolPI
, assignedProject.projectType AS assignmentType
, demographics.calculated_status
, demographics.gender
, demographics.species
, demographics.history
FROM Site.{ substitutePath moduleProperty('EHR', 'EHRStudyContainer') }.study.animal AS nhp
LEFT JOIN Site.{ substitutePath moduleProperty('EHR', 'EHRStudyContainer') }.study.ClinicalCases_Open AS CMUcases
ON CMUcases.id = nhp.id
LEFT JOIN Site.{ substitutePath moduleProperty('EHR', 'EHRStudyContainer') }.study.housing AS housing
ON housing.id = nhp.id
LEFT JOIN Site.{ substitutePath moduleProperty('EHR', 'EHRStudyContainer') }.study.demographics AS demographics
ON demographics.id = nhp.id
LEFT JOIN Site.{ substitutePath moduleProperty('EHR', 'EHRStudyContainer') }.study.vetAssignment_projects AS assignedProject
ON assignedProject.id = nhp.id
WHERE demographics.Calculated_Status = 'Alive'
AND nhp.id NOT LIKE '[A-Z]%'
AND (
housing.enddateTimeCoalesced >= Curdate()
OR housing.enddateTimeCoalesced IS NULL
)

*/
AND housing.enddate IS NULL
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<query xmlns="http://labkey.org/data/xml/query">
<metadata>
<tables xmlns="http://labkey.org/data/xml">
<table tableName="vetAssignment_filter" tableDbType="NOT_IN_DB">
<tableTitle>Vet Assignment Raw Data</tableTitle>
</table>
</tables>
</metadata>
</query>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<query xmlns="http://labkey.org/data/xml/query">
<metadata>
<tables xmlns="http://labkey.org/data/xml">
<table tableName="vetAssignment_projects" tableDbType="NOT_IN_DB">
<tableTitle>Active Assignments to Projects</tableTitle>
</table>
</tables>
</metadata>
</query>
52 changes: 10 additions & 42 deletions onprc_ehr/resources/queries/study/vetAssignment_projects.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ Notes:
* Animals solely on the P51 are not considered "assigned" in PRIMe
and won't have records returned by this query
* Uses Curdate() to avoid issues where an assignment ends on the day this
query is run
query is run. Datetime data is stored in this table with the time as
00:00:00
* Returns the actual use category instead of "Project Resource Assigned"
or "Project Research Assigned"
* enddate (Release Date) is the first date the animal is NOT on the assignment,
EXCEPT when enddate = date, in which case enddate is treated as though
it was the following date for a single-day day lease
*/

SELECT Id
Expand All @@ -26,45 +30,9 @@ SELECT Id
, enddate
, project.use_category AS projectType
FROM study.assignment
WHERE date <= Curdate()
WHERE Curdate() >= date -- The assignment has started
AND (
enddate IS NULL
OR enddate >= Curdate()
)

/*
study.vetAssignment_projects.sql

Replaces:
* study.vetAssignedResearch
* study.vetAssignedResearch_project
* study.vetAssignedResource
* study.vetAssignedResource_project

Returns a record for each active assignment of an animal ID to a project.
Notes:
* Animals assigned to multiple projects results in multiple records
* Animals solely on the P51 are not considered "assigned" in PRIMe
and won't have records returned by this query
* Uses Curdate() to avoid issues where an assignment ends on the day this
query is run
* Returns the actual use category instead of "Project Resource Assigned"
or "Project Research Assigned"
*/

/*
SELECT Id
, project
, project.protocol.external_id AS Protocol
, project.protocol.investigatorID.lastname AS PI
, date
, enddate
, project.use_category AS projectType
FROM study.assignment
WHERE date <= Curdate()
AND (
enddate IS NULL
OR enddate >= Curdate()
)

*/
enddate IS NULL -- Not a day lease or scheduled end of assignment
OR Curdate() < enddate -- The assignment hasn't yet ended
OR (Curdate() = enddate AND enddate = date) -- 1-day day lease where date = enddate
)
36 changes: 18 additions & 18 deletions onprc_ehr/resources/queries/study/vetassignment_filter.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Notes:

SELECT *
FROM (
SELECT d.Id
, CASE
SELECT d.Id,
CASE
WHEN d.CaseVet IS NOT NULL THEN d.CaseVet
WHEN R01.UserID IS NOT NULL THEN R01.UserID.DisplayName
WHEN R02.UserID IS NOT NULL THEN R02.UserID.DisplayName
Expand All @@ -45,8 +45,8 @@ FROM (
WHEN R21.UserID IS NOT NULL THEN R21.UserID.DisplayName
WHEN R22.UserID IS NOT NULL THEN R22.UserID.DisplayName
ELSE 'Unassigned'
END AS AssignedVet
, CASE
END AS AssignedVet,
CASE
WHEN d.CaseVet IS NOT NULL THEN 'Open Case'
WHEN R01.UserID IS NOT NULL THEN 'Room Priority'
WHEN R02.UserID IS NOT NULL THEN 'Area Priority'
Expand All @@ -71,17 +71,18 @@ FROM (
WHEN R21.UserID IS NOT NULL THEN 'Room'
WHEN R22.UserID IS NOT NULL THEN 'Area'
ELSE 'No Matching Rule'
END AS AssignmentType
, d.CaseVet
, d.CaseDate
, d.Project
, d.AssignmentType AS ProjectType
, d.Protocol
, d.ProtocolPI
, d.Room
, d.Area
, d.Calculated_status
, CASE
END AS AssignmentType,
d.CaseVet,
d.CaseDate,
d.Project,
d.AssignmentType AS ProjectType,
d.Protocol,
d.ProtocolPI,
d.Room,
d.Area,
d.Species,
d.Calculated_status,
CASE
WHEN d.CaseVet IS NOT NULL THEN 0
WHEN R01.UserID IS NOT NULL THEN 1
WHEN R02.UserID IS NOT NULL THEN 2
Expand All @@ -106,7 +107,7 @@ FROM (
WHEN R21.UserID IS NOT NULL THEN 21
WHEN R22.UserID IS NOT NULL THEN 22
ELSE 99
END AS MatchedRule
END AS MatchedRule
FROM study.vetAssignment_demographics AS d
/* R01 Room Priority */ LEFT JOIN onprc_ehr.vet_assignment R01 ON (R01.Room = d.Room AND R01.Area IS NULL AND R01.Project IS NULL AND R01.Protocol IS NULL AND R01.Priority = true)
/* R02 Area Priority */ LEFT JOIN onprc_ehr.vet_assignment R02 ON (R02.Area = d.Area AND R02.Room IS NULL AND R02.Project IS NULL AND R02.Protocol IS NULL AND R02.Priority = true)
Expand Down Expand Up @@ -140,5 +141,4 @@ FROM (

/* R21 Room */ LEFT JOIN onprc_ehr.vet_assignment R21 ON (R21.Room = d.Room AND R21.Area IS NULL AND R21.Protocol IS NULL AND R21.Project IS NULL AND R21.Priority = false)
/* R22 Area */ LEFT JOIN onprc_ehr.vet_assignment R22 ON (R22.Area = d.Area AND R22.Room IS NULL AND R22.Protocol IS NULL AND R22.Project IS NULL AND R22.Priority = false)
) placeholderAlias

) AS placeholderAlias
2 changes: 1 addition & 1 deletion onprc_ehr/resources/views/vetReview.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<!--Update 2023-09-30 Final Code for Ve3t assignment-->
<!--Update 2023-09-30 Final Code for Vet assignment-->
<script type="text/javascript">
Ext4.onReady(function (){
EHR.Security.init({
Expand Down