Ejemplo n.º 1
0
def infer_release():
    """Runs infer release for the given regions."""
    region_codes = validate_regions(
        get_str_param_values("region", request.args))
    regions = [get_region(region_code) for region_code in region_codes]

    for region in regions:
        with monitoring.push_tags(
            {monitoring.TagKey.REGION: region.region_code}):
            if region.agency_type != "jail":
                continue

            session = sessions.get_most_recent_completed_session(
                region.region_code)
            if session:
                logging.info(
                    "Got most recent completed session for [%s] with "
                    "start time [%s]",
                    region.region_code,
                    session.start,
                )
                persistence.infer_release_on_open_bookings(
                    region.region_code, session.start,
                    _get_custody_status(region))
                sessions.update_phase(session, scrape_phase.ScrapePhase.DONE)

    return "", HTTPStatus.OK
Ejemplo n.º 2
0
    def _do_cleanup(self, args: GcsfsIngestArgs):
        """If this job is the last for the day, call infer_release before
        continuing to further jobs."""
        self.fs.mv_path_to_processed_path(args.file_path)

        if self._is_last_job_for_day(args):
            persistence.infer_release_on_open_bookings(
                self.region.region_code,
                self._get_ingest_metadata(args).ingest_time,
                CustodyStatus.INFERRED_RELEASE)

        parts = filename_parts_from_path(args.file_path)
        self._move_processed_files_to_storage_as_necessary(
            last_processed_date_str=parts.date_str)
Ejemplo n.º 3
0
    def test_inferReleaseDateOnOpenBookings(self):
        # Arrange
        hold = county_entities.Hold.new_with_defaults(hold_id=ID,
                                                      status=HoldStatus.ACTIVE,
                                                      status_raw_text='ACTIVE')
        sentence = county_entities.Sentence.new_with_defaults(
            sentence_id=ID,
            status=SentenceStatus.SERVING,
            status_raw_text='SERVING',
            booking_id=ID)
        bond = county_entities.Bond.new_with_defaults(
            bond_id=ID,
            status=BondStatus.SET,
            status_raw_text='NOT_REQUIRED',
            booking_id=ID)
        charge = county_entities.Charge.new_with_defaults(
            charge_id=ID,
            status=ChargeStatus.PENDING,
            status_raw_text='PENDING',
            sentence=sentence,
            bond=bond)
        booking_open = county_entities.Booking.new_with_defaults(
            booking_id=ID,
            custody_status=CustodyStatus.IN_CUSTODY,
            custody_status_raw_text='IN CUSTODY',
            admission_date=DATE,
            last_seen_time=SCRAPER_START_DATETIME - timedelta(days=1),
            first_seen_time=SCRAPER_START_DATETIME - timedelta(days=1),
            charges=[charge],
            holds=[hold])
        booking_resolved = attr.evolve(booking_open,
                                       booking_id=ID_2,
                                       custody_status=CustodyStatus.RELEASED,
                                       custody_status_raw_text='RELEASED',
                                       release_date=DATE_2,
                                       charges=[],
                                       holds=[])
        booking_open_most_recent_scrape = attr.evolve(
            booking_open,
            booking_id=ID_3,
            last_seen_time=SCRAPER_START_DATETIME,
            charges=[],
            holds=[])

        person = county_entities.Person.new_with_defaults(
            person_id=ID,
            region=REGION_1,
            jurisdiction_id=JURISDICTION_ID,
            bookings=[booking_open, booking_resolved])
        person_unmatched = county_entities.Person.new_with_defaults(
            person_id=ID_2,
            region=REGION_1,
            jurisdiction_id=JURISDICTION_ID,
            bookings=[booking_open_most_recent_scrape])

        session = SessionFactory.for_schema_base(JailsBase)
        database.write_person(session, person, DEFAULT_METADATA)
        database.write_person(session, person_unmatched, DEFAULT_METADATA)
        session.commit()
        session.close()

        expected_hold = attr.evolve(hold,
                                    status=HoldStatus.REMOVED_WITHOUT_INFO,
                                    status_raw_text=None)
        expected_sentence = attr.evolve(
            sentence,
            status=SentenceStatus.REMOVED_WITHOUT_INFO,
            status_raw_text=None)
        expected_bond = attr.evolve(bond,
                                    status=BondStatus.REMOVED_WITHOUT_INFO,
                                    status_raw_text=None)
        expected_charge = attr.evolve(charge,
                                      status=ChargeStatus.REMOVED_WITHOUT_INFO,
                                      status_raw_text=None,
                                      bond=expected_bond,
                                      sentence=expected_sentence)
        expected_resolved_booking = attr.evolve(
            booking_open,
            custody_status=CustodyStatus.INFERRED_RELEASE,
            custody_status_raw_text=None,
            release_date=SCRAPER_START_DATETIME.date(),
            release_date_inferred=True,
            charges=[expected_charge],
            holds=[expected_hold])
        expected_person = attr.evolve(
            person, bookings=[expected_resolved_booking, booking_resolved])

        # Act
        persistence.infer_release_on_open_bookings(
            REGION_1, SCRAPER_START_DATETIME, CustodyStatus.INFERRED_RELEASE)

        # Assert
        people = county_dao.read_people(
            SessionFactory.for_schema_base(JailsBase))
        self.assertCountEqual(people, [expected_person, person_unmatched])