@@ -9,6 +9,7 @@ defmodule Transport.IRVE.Validator do
99 df
1010 |> Transport.IRVE.Validator.DataFrameValidation . setup_computed_field_validation_columns ( schema )
1111 |> Transport.IRVE.Validator.DataFrameValidation . setup_computed_row_validation_column ( )
12+ |> Transport.IRVE.Validator.DataFrameValidation . setup_computed_warning_columns ( )
1213 end
1314
1415 @ doc """
@@ -55,6 +56,7 @@ defmodule Transport.IRVE.Validator do
5556 { valid_count , invalid_count } = summarize_total_counts ( df )
5657 column_errors = summarize_column_errors ( df )
5758 error_samples = error_samples ( df , column_errors )
59+ warnings = summarize_warnings ( df )
5860
5961 % Transport.IRVE.Validator.Summary {
6062 valid: invalid_count == 0 ,
@@ -63,7 +65,9 @@ defmodule Transport.IRVE.Validator do
6365 total_row_count: valid_count + invalid_count ,
6466 file_level_errors: [ ] ,
6567 column_errors: column_errors ,
66- error_samples: error_samples
68+ error_samples: error_samples ,
69+ warnings: warnings ,
70+ warning_samples: warning_samples ( df , warnings )
6771 }
6872 end
6973
@@ -91,10 +95,37 @@ defmodule Transport.IRVE.Validator do
9195 total_row_count: nil ,
9296 file_level_errors: [ Exception . message ( error ) ] ,
9397 column_errors: % { } ,
94- error_samples: [ ]
98+ error_samples: [ ] ,
99+ warnings: % { } ,
100+ warning_samples: [ ]
95101 }
96102 end
97103
104+ @ max_samples_per_group 5
105+
106+ # Maps each warning to the raw input column (not immediately constructed from the warning name)
107+ @ warning_value_columns % { "lon_lat_inverted" => "coordonneesXY" }
108+
109+ defp summarize_warnings ( df ) do
110+ warnings = Explorer.DataFrame . select ( df , & String . starts_with? ( & 1 , "warning_" ) )
111+
112+ warnings
113+ |> Explorer.DataFrame . names ( )
114+ |> Map . new ( fn col -> { String . replace_prefix ( col , "warning_" , "" ) , Explorer.Series . sum ( warnings [ col ] ) } end )
115+ |> Map . reject ( fn { _name , count } -> count == 0 end )
116+ end
117+
118+ defp warning_samples ( df , warnings ) do
119+ warnings
120+ |> Enum . flat_map ( fn { warning_name , _count } ->
121+ value_col = Map . fetch! ( @ warning_value_columns , warning_name )
122+
123+ df
124+ |> Explorer.DataFrame . filter_with ( & & 1 [ "warning_#{ warning_name } " ] )
125+ |> take_samples ( value_col , :warning , warning_name )
126+ end )
127+ end
128+
98129 defp summarize_total_counts ( df ) do
99130 valid_count = df [ "check_row_valid" ] |> Explorer.Series . sum ( )
100131 invalid_count = Explorer.DataFrame . n_rows ( df ) - valid_count
@@ -114,17 +145,19 @@ defmodule Transport.IRVE.Validator do
114145 defp error_samples ( df , column_errors ) do
115146 column_errors
116147 |> Enum . flat_map ( fn { field_name , _error_count } ->
117- check_col = "check_column_#{ field_name } _valid"
118-
119148 df
120- |> Explorer.DataFrame . filter_with ( & ( & 1 [ check_col ] |> Explorer.Series . not ( ) ) )
121- |> Explorer.DataFrame . select ( [ "id_pdc_itinerance" , field_name ] )
122- # Limit to 5 samples per error column
123- |> Explorer.DataFrame . head ( 5 )
124- |> Explorer.DataFrame . to_rows ( )
125- |> Enum . map ( fn row ->
126- % { id_pdc_itinerance: row [ "id_pdc_itinerance" ] , column: field_name , value: row [ field_name ] }
127- end )
149+ |> Explorer.DataFrame . filter_with ( & Explorer.Series . not ( & 1 [ "check_column_#{ field_name } _valid" ] ) )
150+ |> take_samples ( field_name , :column , field_name )
151+ end )
152+ end
153+
154+ defp take_samples ( df , value_col , tag_key , name ) do
155+ df
156+ |> Explorer.DataFrame . select ( [ "id_pdc_itinerance" , value_col ] )
157+ |> Explorer.DataFrame . head ( @ max_samples_per_group )
158+ |> Explorer.DataFrame . to_rows ( )
159+ |> Enum . map ( fn row ->
160+ % { :id_pdc_itinerance => row [ "id_pdc_itinerance" ] , tag_key => name , :value => row [ value_col ] }
128161 end )
129162 end
130163end
0 commit comments