-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from prefeitura-rio/feat/improved-datalake-up…
…load Feat/improved datalake upload
- Loading branch information
Showing
7 changed files
with
640 additions
and
105 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from datalake.utils import register_formatter | ||
from datalake.formatters import * |
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,137 @@ | ||
# -*- coding: utf-8 -*- | ||
# ============================================= | ||
# Formatters that are responsible for converting | ||
# raw JSON records to Datalake table rows. | ||
# ============================================= | ||
from typing import List, Tuple | ||
from datalake.utils import flatten, register_formatter | ||
from datalake.models import ( | ||
SMSRioCnsProvisorio, | ||
SMSRioPaciente, | ||
SMSRioTelefone, | ||
VitacarePaciente, | ||
VitacarePacienteHistorico, | ||
VitacareAtendimento, | ||
VitacareCondicao, | ||
VitacareAlergia, | ||
VitacareEncaminhamento, | ||
VitacareExameSolicitado, | ||
VitacareIndicador, | ||
VitacarePrescricao, | ||
VitacareVacina, | ||
) | ||
|
||
|
||
@register_formatter(system="smsrio", entity="patientrecords") | ||
def format_smsrio_patient( | ||
raw_record: dict | ||
) -> Tuple[List[SMSRioPaciente], List[SMSRioTelefone], List[SMSRioCnsProvisorio]]: | ||
# Convert source_updated_at to string | ||
raw_record['source_updated_at'] = str(raw_record['source_updated_at']) | ||
|
||
# Flatten Record | ||
flattened_patient = flatten(raw_record) | ||
|
||
# Initialize Tables | ||
rows = { | ||
"pacientes": [SMSRioPaciente(**flattened_patient)], | ||
"telefones": [], | ||
"cns_provisorio": [], | ||
} | ||
|
||
# Create Tables for List Fields | ||
for field_name, FieldModel in [ | ||
('telefones', SMSRioTelefone), | ||
('cns_provisorio', SMSRioCnsProvisorio) | ||
]: | ||
# If field not in record, skip | ||
if field_name not in raw_record['data']: | ||
continue | ||
|
||
for value in raw_record['data'].pop(field_name) or []: | ||
rows[field_name].append( | ||
FieldModel( | ||
value=value, | ||
patient_cpf=raw_record.get("patient_cpf"), | ||
source_updated_at=raw_record.get("source_updated_at") | ||
) | ||
) | ||
|
||
return rows['pacientes'], rows['telefones'], rows['cns_provisorio'] | ||
|
||
|
||
@register_formatter(system="vitacare", entity="patientrecords") | ||
def format_vitacare_patient( | ||
raw_record: dict | ||
) -> Tuple[List[VitacarePaciente | VitacarePacienteHistorico]]: | ||
# Convert source_updated_at to string | ||
raw_record['source_updated_at'] = str(raw_record['source_updated_at']) | ||
|
||
flattened = flatten(raw_record, list_max_depth=0) | ||
|
||
# Temp criterium to discriminate between Routine and Historic format | ||
if 'AP' in raw_record['data'].keys(): | ||
return ([VitacarePacienteHistorico(**flattened)],) | ||
else: | ||
return ([VitacarePaciente(**flattened)],) | ||
|
||
|
||
@register_formatter(system="vitacare", entity="encounter") | ||
def format_vitacare_encounter( | ||
raw_record: dict | ||
) -> Tuple[ | ||
List[VitacareAtendimento], | ||
List[VitacareCondicao], | ||
List[VitacareAlergia], | ||
List[VitacareEncaminhamento], | ||
List[VitacareExameSolicitado], | ||
List[VitacareIndicador], | ||
List[VitacarePrescricao], | ||
List[VitacareVacina], | ||
]: | ||
# Convert source_updated_at to string | ||
raw_record['source_updated_at'] = str(raw_record['source_updated_at']) | ||
|
||
# Flatten Record | ||
flattened = flatten( | ||
raw_record, | ||
dict_max_depth=3, | ||
) | ||
|
||
# Initialize Tables | ||
rows = { | ||
"encounter": [VitacareAtendimento(**flattened)], | ||
"condicoes": [], | ||
"alergias_anamnese": [], | ||
"encaminhamentos": [], | ||
"exames_solicitados": [], | ||
"indicadores": [], | ||
"prescricoes": [], | ||
"vacinas": [], | ||
} | ||
|
||
# Create Tables for List Fields | ||
for field_name, FieldModel in [ | ||
('condicoes', VitacareCondicao), | ||
('alergias_anamnese', VitacareAlergia), | ||
('encaminhamentos', VitacareEncaminhamento), | ||
('exames_solicitados', VitacareExameSolicitado), | ||
('indicadores', VitacareIndicador), | ||
('prescricoes', VitacarePrescricao), | ||
('vacinas', VitacareVacina) | ||
]: | ||
# If field not in record, skip | ||
if field_name not in raw_record['data']: | ||
continue | ||
|
||
for fields in raw_record['data'].pop(field_name) or []: | ||
rows[field_name].append( | ||
FieldModel( | ||
patient_cpf=raw_record.get("patient_cpf"), | ||
atendimento_id=raw_record.get("source_id"), | ||
source_updated_at=raw_record.get("source_updated_at"), | ||
**fields | ||
) | ||
) | ||
|
||
return tuple(rows.values()) |
Oops, something went wrong.