def _convert_entity_people_to_schema_people( people: List[entities.EntityPersonType]) -> List[SchemaPersonType]: def _as_schema_person_type(e: DatabaseEntity) -> SchemaPersonType: if not isinstance(e, county_schema.Person) and \ not isinstance(e, state_schema.StatePerson): raise ValueError(f"Unexpected database entity type: [{type(e)}]") return e return [ _as_schema_person_type(p) for p in converter.convert_entities_to_schema(people) ]
def write_person(session: Session, person: entities.EntityPersonType, metadata: IngestMetadata, orphaned_entities: List[Entity] = None): """ Converts the given |person| into a (SchemaPersonType) object and persists the record tree rooted at that |person|. Returns the persisted (SchemaPersonType) object. """ if not orphaned_entities: orphaned_entities = [] persisted_people = _save_record_trees( session, _convert_entity_people_to_schema_people([person]), converter.convert_entities_to_schema(orphaned_entities), metadata) # persisted_people will only contain the single person passed in return one(persisted_people)
def match_people_and_return_error_count( *, db_people: List[entities.Person], ingested_people: List[entities.Person]) -> MatchedEntities: """ Attempts to match all people from |ingested_people| with people from the |db_people|. Returns an MatchedEntities object that contains the results of matching. """ people = [] orphaned_entities = [] error_count = 0 matched_people_by_db_id: Dict[int, entities.Person] = {} for ingested_person in ingested_people: try: ingested_person_orphans: List[Entity] = [] match_person( ingested_person=ingested_person, db_people=db_people, orphaned_entities=ingested_person_orphans, matched_people_by_db_id=matched_people_by_db_id, ) people.append(ingested_person) orphaned_entities.extend(ingested_person_orphans) except EntityMatchingError as e: logging.exception( "Found %s while matching ingested person. \nPerson: %s", e.__class__.__name__, ingested_person, ) increment_error(e.entity_name) error_count += 1 schema_people = converter.convert_entity_people_to_schema_people(people) schema_orphaned_entities = converter.convert_entities_to_schema( orphaned_entities) return MatchedEntities( people=schema_people, orphaned_entities=schema_orphaned_entities, error_count=error_count, )
def write_people(session: Session, people: List[entities.EntityPersonType], metadata: IngestMetadata, orphaned_entities: List[Entity] = None): """ Converts the given |people| into (SchemaPersonType) objects and persists their corresponding record trees. Returns the list of persisted (SchemaPersonType) objects. """ if not orphaned_entities: orphaned_entities = [] logging.info( "Converting [%s] people and [%s] orphaned entities to schema objects.", len(people), len(orphaned_entities)) schema_people = \ _convert_entity_people_to_schema_people(people) schema_orphaned_entities = \ converter.convert_entities_to_schema(orphaned_entities) logging.info("Done converting to schema objects.") return _save_record_trees(session, schema_people, schema_orphaned_entities, metadata)