11from typing import Any , Callable , Dict , List , Optional
22
33import pandas as pd
4- from nisystemlink .clients .testmonitor .models import Result , Step , StepProjection
4+ from nisystemlink .clients .testmonitor .models import (
5+ Measurement ,
6+ Result ,
7+ Step ,
8+ StepProjection ,
9+ )
510from nisystemlink .clients .testmonitor .utilities .constants import DataFrameHeaders
611
712
8- def is_step_data_with_name_and_measurement (data : Dict [str , Any ]) -> bool :
9- """Checks if a step data parameter is a measurement data by ensuring it
10- has both 'name' and 'measurement' fields.
13+ def is_step_data_with_name_and_measurement (measurement : Measurement ) -> bool :
14+ """Checks if a measurement data has both 'name' and 'measurement' fields.
1115
1216 Args:
13- measurement: A dictionary containing measurement data.
17+ measurement: A measurement data object
1418
1519 Returns:
1620 bool: True if the measurement has both 'name' and 'measurement' fields, False otherwise.
1721 """
18- required_measurement_fields = ["name" , "measurement" ]
19- return set (required_measurement_fields ).issubset (set (data .keys ()))
22+ return measurement .name is not None and measurement .measurement is not None
2023
2124
2225def convert_results_to_dataframe (
@@ -55,7 +58,7 @@ def convert_results_to_dataframe(
5558def convert_steps_to_dataframe (
5659 steps : List [Step ],
5760 is_measurement_data_parameter : Optional [
58- Callable [[Dict [ str , Any ] ], bool ]
61+ Callable [[Measurement ], bool ]
5962 ] = is_step_data_with_name_and_measurement ,
6063) -> pd .DataFrame :
6164 """Converts a list of steps into a normalized dataframe.
@@ -64,8 +67,8 @@ def convert_steps_to_dataframe(
6467 steps: A list of steps.
6568 is_measurement_data_parameter: Optional callback function that checks if a step data parameter is a
6669 measurement so that only those are included in the returned dataframe. The method takes
67- a dictionary as input and returns a boolean value.
68- The default behavior is to consider only parameters that have both 'name' and 'measurement'
70+ a measurement as input and returns a boolean value.
71+ The default behavior is to consider only measurement data that have both 'name' and 'measurement'
6972 fields as measurement parameters.
7073 If none of the step data parameters have the required fields, the data.parameters will not
7174 appear in the dataframe.
@@ -184,59 +187,53 @@ def __is_property_header(header: str) -> bool:
184187
185188def __convert_steps_to_dict (
186189 steps : List [Step ],
187- is_measurement_data_parameter : Optional [Callable [[Dict [ str , Any ] ], bool ]],
190+ is_measurement_data_parameter : Optional [Callable [[Measurement ], bool ]],
188191) -> List [Dict [str , Any ]]:
189192 """Converts a list of steps to dictionaries, excluding None values.
190193
191194 Args:
192195 steps: A list of steps.
193- is_measurement_data_parameter: Optional callback function to check if a step
194- data parameter has the required fields .
196+ is_measurement_data_parameter: Optional callback function that checks if a step data
197+ parameter is a measurement so that only those are included in the returned dataframe .
195198
196199 Returns:
197200 List[Dict[str, Any]]: A list of dictionaries containing step information.
198201 """
199202 steps_dict = []
200203 for step in steps :
201- single_step_dict = step . dict ( exclude_none = True )
204+ __filter_data_parameters ( step , is_measurement_data_parameter )
202205
203- __filter_data_parameters ( single_step_dict , step , is_measurement_data_parameter )
206+ single_step_dict = step . dict ( exclude_none = True )
204207 __normalize_inputs_outputs (single_step_dict , step )
205208 __normalize_step_status (single_step_dict )
206209 steps_dict .append (single_step_dict )
207210 return steps_dict
208211
209212
210213def __filter_data_parameters (
211- step_dict : Dict [str , Any ],
212214 step : Step ,
213- is_measurement_data_parameter : Optional [Callable [[Dict [ str , Any ] ], bool ]],
215+ is_measurement_data_parameter : Optional [Callable [[Measurement ], bool ]],
214216) -> None :
215217 """Gets data parameters from the step dictionary and filters it based on the callback function.
216218
217219 Args:
218- step_dict: A dictionary with step information.
219220 step: A Step object containing data parameters.
220221 is_measurement_data_parameter: Optional callback function to check if a measurement is valid. The method takes
221222 a dictionary as input and returns a boolean value. The default behavior is to consider only parameters
222223 that have both 'name' and 'measurement' fields as measurement parameters.
223224
224225 Returns:
225- None: The function modifies step_dict in place with the filtered step data parameters.
226+ None: The function modifies step parameters in place with filtered data parameters.
226227 """
227- valid_measurement_parameters = []
228- if (
229- is_measurement_data_parameter is not None
230- and step .data is not None
231- and step .data .parameters is not None
232- ):
233- for parameter in step_dict ["data" ]["parameters" ]:
228+ if step .data and step .data .parameters and is_measurement_data_parameter is not None :
229+ valid_measurement_parameters = []
230+ for measurement in step .data .parameters :
234231 if is_measurement_data_parameter and is_measurement_data_parameter (
235- parameter
232+ measurement
236233 ):
237- valid_measurement_parameters .append (parameter )
234+ valid_measurement_parameters .append (measurement )
238235
239- step_dict [ " data" ][ " parameters" ] = valid_measurement_parameters
236+ step . data . parameters = valid_measurement_parameters
240237
241238
242239def __normalize_inputs_outputs (
@@ -330,9 +327,7 @@ def __group_step_columns(dataframe_columns: List[str]) -> List[str]:
330327 StepProjection .DATA .lower (),
331328 StepProjection .PROPERTIES .lower (),
332329 ]
333- grouped_columns : Dict [str , List [str ]] = {
334- category : [] for category in CATEGORY_KEYS
335- }
330+ grouped_columns : Dict [str , List [str ]] = {category : [] for category in CATEGORY_KEYS }
336331 for column in dataframe_columns :
337332 column_lower = column .lower ()
338333 key = next (
0 commit comments