Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[US_IX] Remove folks with sex offender alerts and violent charges fro…
…m CRC opps (Recidiviz/recidiviz-data#29170) ## Description of the change - Sex offenders: I created new state-specific criteria and added it to all CRC opps. - Violent offenses: I used a general criteria view and added it to all CRC opps. I uploaded this to sandbox (`_hsalas`), and it does slightly reduce the number of people eligible: - CRC resident worker: from 2,536 to 1,833 - CRC work-release: 1,768 to 1,248 - XCRC: 277 to 222 ## Related issues Closes Recidiviz/recidiviz-data#5299 ## Checklists ### Development **This box MUST be checked by the submitter prior to merging**: - [ ] **Double- and triple-checked that there is no Personally Identifiable Information (PII) being mistakenly added in this pull request** These boxes should be checked by the submitter prior to merging: - [ ] Tests have been written to cover the code changed/added as part of this pull request ### Code review These boxes should be checked by reviewers prior to merging: - [ ] This pull request has a descriptive title and information useful to a reviewer - [ ] Potential security implications or infrastructural changes have been considered, if relevant GitOrigin-RevId: f155107ea2ae89af76c242d1c01f1cfe648b3948
- Loading branch information
Showing
6 changed files
with
112 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
recidiviz/task_eligibility/criteria/state_specific/us_ix/no_sex_offender_alert.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Recidiviz - a data platform for criminal justice reform | ||
# Copyright (C) 2024 Recidiviz, Inc. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
# ============================================================================= | ||
|
||
"""Defines a criteria view that shows spans of time for | ||
which residents do not have a sex offender alert""" | ||
|
||
from recidiviz.calculator.query.sessions_query_fragments import ( | ||
create_sub_sessions_with_attributes, | ||
) | ||
from recidiviz.calculator.query.bq_utils import ( | ||
nonnull_end_date_clause, | ||
) | ||
from recidiviz.calculator.query.state.dataset_config import NORMALIZED_STATE_DATASET | ||
from recidiviz.common.constants.states import StateCode | ||
from recidiviz.ingest.direct.dataset_config import raw_latest_views_dataset_for_region | ||
from recidiviz.ingest.direct.types.direct_ingest_instance import DirectIngestInstance | ||
from recidiviz.task_eligibility.task_criteria_big_query_view_builder import ( | ||
StateSpecificTaskCriteriaBigQueryViewBuilder, | ||
) | ||
from recidiviz.utils.environment import GCP_PROJECT_STAGING | ||
from recidiviz.utils.metadata import local_project_id_override | ||
|
||
_CRITERIA_NAME = "US_IX_NO_SEX_OFFENDER_ALERT" | ||
|
||
_DESCRIPTION = """Defines a criteria view that shows spans of time for | ||
which residents do not have a sex offender alert""" | ||
|
||
_SEX_OFFENDER_CODES = [ | ||
"61", # Supervise as Sex offender | ||
"62", # Sex Offender Registration Required | ||
] | ||
|
||
_QUERY_TEMPLATE = f""" | ||
WITH sex_offender_alert AS ( | ||
SELECT | ||
peid.state_code, | ||
peid.person_id, | ||
SAFE_CAST(LEFT(off.StartDate, 10) AS DATE) AS start_date, | ||
SAFE_CAST(LEFT(off.EndDate, 10) AS DATE) AS end_date, | ||
off.AlertId AS alert_id, | ||
ia.AlertDesc AS alert_description, | ||
FROM `{{project_id}}.{{us_ix_raw_data_up_to_date_dataset}}.ind_Offender_Alert_latest` off | ||
LEFT JOIN `{{project_id}}.{{us_ix_raw_data_up_to_date_dataset}}.ind_Alert_latest` ia | ||
USING(AlertId) | ||
INNER JOIN `{{project_id}}.{{normalized_state_dataset}}.state_person_external_id` peid | ||
ON peid.external_id = off.OffenderId | ||
AND peid.state_code = 'US_IX' | ||
AND peid.id_type = 'US_IX_DOC' | ||
-- Alerts related to sex offenses | ||
WHERE off.AlertId IN {tuple(_SEX_OFFENDER_CODES)} | ||
), | ||
{create_sub_sessions_with_attributes(table_name='sex_offender_alert')} | ||
SELECT | ||
state_code, | ||
person_id, | ||
start_date, | ||
end_date, | ||
False AS meets_criteria, | ||
TO_JSON(STRUCT(STRING_AGG(DISTINCT alert_description) AS latest_alert_descriptions, | ||
STRING_AGG(DISTINCT alert_id) AS latest_alert_ids)) AS reason | ||
FROM sub_sessions_with_attributes | ||
WHERE start_date < {nonnull_end_date_clause('end_date')} | ||
GROUP BY 1,2,3,4""" | ||
|
||
VIEW_BUILDER: StateSpecificTaskCriteriaBigQueryViewBuilder = ( | ||
StateSpecificTaskCriteriaBigQueryViewBuilder( | ||
criteria_name=_CRITERIA_NAME, | ||
description=_DESCRIPTION, | ||
state_code=StateCode.US_IX, | ||
criteria_spans_query_template=_QUERY_TEMPLATE, | ||
us_ix_raw_data_up_to_date_dataset=raw_latest_views_dataset_for_region( | ||
state_code=StateCode.US_IX, instance=DirectIngestInstance.PRIMARY | ||
), | ||
normalized_state_dataset=NORMALIZED_STATE_DATASET, | ||
meets_criteria_default=True, | ||
) | ||
) | ||
|
||
if __name__ == "__main__": | ||
with local_project_id_override(GCP_PROJECT_STAGING): | ||
VIEW_BUILDER.build_and_print() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters