1313from sqlalchemy .dialects import mysql , postgresql , sqlite
1414from ..core import UnsupportedOperationError
1515from .profiles import preflight , validate_connection_url
16+ from .normative import (
17+ catalog as normative_catalog ,
18+ create as create_normative_catalog ,
19+ dataset_id_for_name as normative_dataset_id_for_name ,
20+ finish_operation as finish_normative_operation ,
21+ record_fidelity_events as record_normative_fidelity_events ,
22+ record_operation as record_normative_operation ,
23+ store_imported_dataset as store_normative_dataset ,
24+ )
1625
1726_IDENTIFIER = re .compile (r"[^a-zA-Z0-9_]+" )
1827
@@ -187,28 +196,42 @@ def _event_rows(
187196def _record_failed_preflight (
188197 * , engine : Any , metadata : MetaData , datasets : Table , variable_catalog : Table ,
189198 multiple_response_catalog : Table , fidelity_event_catalog : Table ,
190- operation_catalog : Table , operation_id : str , source_name : str ,
191- variable_count : int , profile_name : str , error : Exception ,
199+ operation_catalog : Table , normative : Any , operation_id : str , source_name : str ,
200+ source_format : str , variable_count : int , profile_name : str , error : Exception ,
192201) -> None :
193202 """Persist a failed preflight without creating any source dataset state."""
194203 with engine .begin () as connection :
195204 metadata .create_all (connection , tables = [
196205 datasets , variable_catalog , multiple_response_catalog ,
197206 fidelity_event_catalog , operation_catalog ,
198207 ])
208+ create_normative_catalog (connection , normative )
209+ failed_at = datetime .now (UTC ).replace (tzinfo = None )
210+ record_normative_operation (
211+ connection , normative , operation_id = operation_id ,
212+ operation_kind = "import" , status = "failed" , source_format = source_format ,
213+ started_at = failed_at , completed_at = failed_at ,
214+ )
199215 connection .execute (insert (operation_catalog ).values (
200216 operation_id = operation_id , direction = "import" , status = "failed" , dataset_id = None ,
201217 source = source_name , created_at = _now (), completed_at = _now (),
202218 details = json .dumps ({"reason" : "preflight" , "variable_count" : variable_count ,
203219 "capability" : getattr (error , "details" , {})}, sort_keys = True ),
204220 ))
221+ failed_events = ({
222+ "code" : getattr (error , "code" , "target-capability-exceeded" ),
223+ "detail" : str (error ), "severity" : "error" , "source_item" : source_name ,
224+ "details" : {"variable_count" : variable_count , "profile" : profile_name ,
225+ ** getattr (error , "details" , {})},
226+ },)
205227 connection .execute (insert (fidelity_event_catalog ), _event_rows (
206- operation_id = operation_id , dataset_id = None , direction = "import" , fidelity_events = ({
207- "code" : getattr (error , "code" , "target-capability-exceeded" ), "detail" : str (error ), "severity" : "error" ,
208- "details" : {"variable_count" : variable_count , "profile" : profile_name ,
209- ** getattr (error , "details" , {})},
210- },),
228+ operation_id = operation_id , dataset_id = None , direction = "import" ,
229+ fidelity_events = failed_events ,
211230 ))
231+ record_normative_fidelity_events (
232+ connection , normative , operation_id = operation_id , dataset_id = None ,
233+ direction = "import" , events = failed_events ,
234+ )
212235
213236def multiple_response_set_catalog (metadata : MetaData ) -> Table :
214237 return Table (
@@ -743,10 +766,12 @@ def create_wide_dataset(
743766 engine = create_engine (database_url )
744767 metadata = MetaData ()
745768 datasets , variable_catalog , fidelity_event_catalog , operation_catalog = catalog (metadata )
769+ normative = normative_catalog (metadata )
746770 multiple_response_catalog = multiple_response_set_catalog (metadata )
747771 source_extensions_catalog = source_extension_catalog (metadata )
748772 documents_catalog , value_labels_catalog , missing_rules_catalog , attributes_catalog = normalized_metadata_tables (metadata )
749773 operation_id = str (uuid4 ())
774+ fidelity_events = tuple (fidelity_events )
750775 try :
751776 preflight (profile , variables )
752777 validate_spss_catalog (
@@ -759,8 +784,9 @@ def create_wide_dataset(
759784 engine = engine , metadata = metadata , datasets = datasets ,
760785 variable_catalog = variable_catalog , multiple_response_catalog = multiple_response_catalog ,
761786 fidelity_event_catalog = fidelity_event_catalog , operation_catalog = operation_catalog ,
762- operation_id = operation_id , source_name = source_name , variable_count = len (variables ),
763- profile_name = profile .name , error = error ,
787+ operation_id = operation_id , source_name = source_name ,
788+ source_format = source_format , variable_count = len (variables ),
789+ profile_name = profile .name , error = error , normative = normative ,
764790 )
765791 raise
766792 data_table = Table (
@@ -771,7 +797,12 @@ def create_wide_dataset(
771797 )
772798 with engine .begin () as connection :
773799 metadata .create_all (connection , tables = [datasets , variable_catalog , multiple_response_catalog , source_extensions_catalog , documents_catalog , value_labels_catalog , missing_rules_catalog , attributes_catalog , fidelity_event_catalog , operation_catalog ])
800+ create_normative_catalog (connection , normative )
774801 _migrate_catalog_columns (connection , datasets , variable_catalog , multiple_response_catalog )
802+ record_normative_operation (
803+ connection , normative , operation_id = operation_id ,
804+ operation_kind = "import" , status = "started" , source_format = source_format ,
805+ )
775806 connection .execute (insert (operation_catalog ).values (
776807 operation_id = operation_id , direction = "import" , status = "running" , dataset_id = dataset_id ,
777808 source = source_name , created_at = _now (), details = json .dumps ({"variable_count" : len (variables ), ** dict (operation_details or {})}, sort_keys = True ),
@@ -821,6 +852,22 @@ def create_wide_dataset(
821852 )
822853 if event_rows :
823854 connection .execute (insert (fidelity_event_catalog ), event_rows )
855+ normative_dataset_id = store_normative_dataset (
856+ connection , normative , dataset_name = dataset_id ,
857+ source_format = source_format , physical_table_name = data_table .name ,
858+ dataset_label = file_label , source_encoding = source_encoding ,
859+ source_hash = source_sha256 , source_case_count = len (materialized ),
860+ imported_at = imported_at or None , variables = variables ,
861+ documents = docs_rows , value_labels = labels_rows ,
862+ missing_rules = missing_rows , attributes = attributes_rows ,
863+ multiple_response_sets = mrset_rows ,
864+ source_extensions = source_extensions or {},
865+ )
866+ record_normative_fidelity_events (
867+ connection , normative , operation_id = operation_id ,
868+ dataset_id = normative_dataset_id , direction = "import" ,
869+ events = fidelity_events ,
870+ )
824871 if materialized :
825872 try :
826873 connection .execute (insert (data_table ), materialized )
@@ -840,6 +887,9 @@ def create_wide_dataset(
840887 connection .execute (update (operation_catalog ).where (operation_catalog .c .operation_id == operation_id ).values (
841888 status = "succeeded" , completed_at = _now (),
842889 ))
890+ finish_normative_operation (
891+ connection , normative , operation_id = operation_id , status = "succeeded" ,
892+ )
843893 return {"dataset_id" : dataset_id , "data_table" : data_table .name , "case_count" : len (materialized ), "operation_id" : operation_id }
844894
845895
@@ -965,11 +1015,20 @@ def record_export_operation(
9651015 engine = create_engine (database_url )
9661016 metadata = MetaData ()
9671017 datasets , variables , fidelity_events , operations = catalog (metadata )
1018+ normative = normative_catalog (metadata )
9681019 multiple_response = multiple_response_set_catalog (metadata )
9691020 operation_id = str (uuid4 ())
9701021 events = tuple (allowed_fidelity_events )
9711022 with engine .begin () as connection :
9721023 metadata .create_all (connection , tables = [datasets , variables , multiple_response , fidelity_events , operations ])
1024+ create_normative_catalog (connection , normative )
1025+ normative_dataset_id = normative_dataset_id_for_name (connection , normative , dataset_id )
1026+ completed_at = datetime .now (UTC ).replace (tzinfo = None )
1027+ record_normative_operation (
1028+ connection , normative , operation_id = operation_id ,
1029+ operation_kind = "export" , status = "succeeded" , source_format = None ,
1030+ started_at = completed_at , completed_at = completed_at ,
1031+ )
9731032 connection .execute (insert (operations ).values (
9741033 operation_id = operation_id , direction = "export" , status = "succeeded" , dataset_id = dataset_id ,
9751034 destination = destination , created_at = _now (), completed_at = _now (),
@@ -983,6 +1042,13 @@ def record_export_operation(
9831042 )
9841043 if rows :
9851044 connection .execute (insert (fidelity_events ), rows )
1045+ record_normative_fidelity_events (
1046+ connection , normative , operation_id = operation_id ,
1047+ dataset_id = normative_dataset_id , direction = "export" ,
1048+ events = ({** event , "severity" : event .get ("severity" , "warning" ),
1049+ "details" : {** event .get ("details" , {}), "accepted_by_user" : True }}
1050+ for event in events ),
1051+ )
9861052 return operation_id
9871053
9881054def validate_wide_dataset (* , database_url : str , dataset_id : str ) -> dict [str , Any ]:
0 commit comments