Example #1
0
    def _convert_person(self, ingest_person) -> entities.Person:
        """Converts an ingest_info proto Person to a persistence entity."""
        person_builder = entities.Person.builder()

        person.copy_fields_to_builder(person_builder, ingest_person,
                                      self.metadata)

        converted_bookings = [
            self._convert_booking(self.bookings[booking_id])
            for booking_id in ingest_person.booking_ids
        ]

        if len([b for b in converted_bookings if not b.release_date]) > 1:
            raise ValueError(
                f"Multiple open bookings for person with person_id"
                f" [{ingest_person}]")

        # If no bookings were ingested, create booking to house inferred data.
        if not converted_bookings:
            inferred_booking = self._convert_booking(ingest_info_pb2.Booking())
            converted_bookings = [inferred_booking]

        person_builder.bookings = converted_bookings

        converted_person = person_builder.build()

        # Scrub PII if the person either has an external id or has no open
        # bookings.
        if converted_person.external_id \
                or not persistence_utils.has_active_booking(converted_person):
            persistence_utils.remove_pii_for_person(converted_person)

        return converted_person
Example #2
0
    def test_remove_pii_for_person(self):
        person = county_entities.Person.new_with_defaults(
            full_name='TEST', birthdate=datetime.date(1990, 3, 12))

        remove_pii_for_person(person)
        expected_date = datetime.date(1990, 1, 1)

        self.assertEqual(person.birthdate, expected_date)
        self.assertIsNone(person.full_name)
Example #3
0
def infer_release_on_open_bookings(region_code: str,
                                   last_ingest_time: datetime.datetime,
                                   custody_status: CustodyStatus) -> None:
    """
    Look up all open bookings whose last_seen_time is earlier than the
    provided last_ingest_time in the provided region, update those
    bookings to have an inferred release date equal to the provided
    last_ingest_time.

    Args:
        region_code: the region_code
        last_ingest_time: The last time complete data was ingested for this
            region. In the normal ingest pipeline, this is the last start time
            of a background scrape for the region.
        custody_status: The custody status to be marked on the found open
            bookings. Defaults to INFERRED_RELEASE
    """

    session = SessionFactory.for_schema_base(JailsBase)
    try:
        logging.info("Reading all bookings that happened before [%s]",
                     last_ingest_time)
        people = county_dao.read_people_with_open_bookings_scraped_before_time(
            session, region_code, last_ingest_time)

        logging.info(
            "Found [%s] people with bookings that will be inferred released",
            len(people),
        )
        for person in people:
            persistence_utils.remove_pii_for_person(person)
            _infer_release_date_for_bookings(person.bookings, last_ingest_time,
                                             custody_status)
        db_people = converter.convert_entity_people_to_schema_people(people)
        database.write_people(
            session,
            db_people,
            IngestMetadata(region=region_code,
                           jurisdiction_id="",
                           ingest_time=last_ingest_time),
        )
        session.commit()
    except Exception:
        session.rollback()
        raise
    finally:
        session.close()