Skip to content

Commit 11881c0

Browse files
author
Priyadarshini Piramanayagam
committed
fix: comments
1 parent b2f6f8f commit 11881c0

File tree

3 files changed

+151
-125
lines changed

3 files changed

+151
-125
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from ._dataframe_utilities import (
22
convert_results_to_dataframe,
33
convert_steps_to_dataframe,
4+
is_default_measurement_data_parameter,
45
)
56

67
# flake8: noqa

nisystemlink/clients/testmonitor/utilities/_dataframe_utilities.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55
from nisystemlink.clients.testmonitor.utilities.constants import DataFrameHeaders
66

77

8+
def is_default_measurement_data_parameter(measurement_data: Dict[str, Any]) -> bool:
9+
"""Checks if a measurement is valid by ensuring it has both 'name' and 'measurement' fields.
10+
11+
Args:
12+
measurement: A dictionary containing measurement data.
13+
14+
Returns:
15+
bool: True if the measurement has both 'name' and 'measurement' fields, False otherwise.
16+
"""
17+
allowed_measurement_keys = ["name", "measurement"]
18+
return all(key in measurement_data for key in allowed_measurement_keys)
19+
20+
821
def convert_results_to_dataframe(
922
results: List[Result], set_id_as_index: bool = True
1023
) -> pd.DataFrame:
@@ -21,7 +34,6 @@ def convert_results_to_dataframe(
2134
Following fields are split into sub-columns.
2235
- status_type_summary: All the entries will be split into separate columns.
2336
For example, status_type_summary.LOOPING, status_type_summary.PASSED, etc
24-
- status: Split into status.status_type and status.status_name columns.
2537
- Properties: All the properties will be split into separate columns. For example,
2638
properties.property1, properties.property2, etc.
2739
"""
@@ -41,13 +53,15 @@ def convert_results_to_dataframe(
4153

4254
def convert_steps_to_dataframe(
4355
steps: List[Step],
44-
is_valid_measurement: Optional[Callable[[Dict[str, Any]], bool]] = None,
56+
is_measurement_data_parameter: Optional[
57+
Callable[[Dict[str, Any]], bool]
58+
] = is_default_measurement_data_parameter,
4559
) -> pd.DataFrame:
4660
"""Converts a list of steps into a normalized dataframe.
4761
4862
Args:
4963
steps: A list of steps.
50-
is_valid_measurement: Optional function to check if a measurement is valid. The method takes
64+
is_measurement_data_parameter: Optional function to check if a measurement is valid. The method takes
5165
a dictionary as input and returns a boolean value. If the function is not provided, the
5266
default behavior is to keep only those measurements that have both 'name' and 'measurement' fields.
5367
If none of the measurement data have the desired fields, the data.parameters will not
@@ -65,7 +79,7 @@ def convert_steps_to_dataframe(
6579
all other step fields are duplicated.
6680
"""
6781
DATA_PARAMETERS = "data.parameters"
68-
step_dicts = __convert_steps_to_dict(steps, is_valid_measurement)
82+
step_dicts = __convert_steps_to_dict(steps, is_measurement_data_parameter)
6983
steps_dataframe = pd.json_normalize(step_dicts, sep=".")
7084
steps_dataframe = __explode_and_normalize(
7185
steps_dataframe, DATA_PARAMETERS, f"{DATA_PARAMETERS}."
@@ -166,7 +180,7 @@ def __is_property_header(header: str) -> bool:
166180

167181
def __convert_steps_to_dict(
168182
steps: List[Step],
169-
is_valid_measurement: Optional[Callable[[Dict[str, Any]], bool]] = None,
183+
is_measurement_data_parameter: Optional[Callable[[Dict[str, Any]], bool]] = None,
170184
) -> List[Dict[str, Any]]:
171185
"""Converts a list of steps to dictionaries, excluding None values.
172186
@@ -180,9 +194,13 @@ def __convert_steps_to_dict(
180194
for step in steps:
181195
single_step_dict = step.dict(exclude_none=True)
182196

183-
if step.data is not None and step.data.parameters is not None:
184-
single_step_dict["data"]["parameters"] = __get_valid_data_parameters(
185-
single_step_dict, is_valid_measurement
197+
if (
198+
is_measurement_data_parameter is not None
199+
and step.data is not None
200+
and step.data.parameters is not None
201+
):
202+
single_step_dict["data"]["parameters"] = __get_measurement_data_parameters(
203+
single_step_dict, is_measurement_data_parameter
186204
)
187205

188206
__normalize_inputs_outputs(single_step_dict, step)
@@ -191,27 +209,24 @@ def __convert_steps_to_dict(
191209
return steps_dict
192210

193211

194-
def __get_valid_data_parameters(
212+
def __get_measurement_data_parameters(
195213
step_dict: Dict[str, Any],
196-
is_valid_measurement: Optional[Callable[[Dict[str, Any]], bool]] = None,
214+
is_measurement_data_parameter: Optional[Callable[[Dict[str, Any]], bool]] = None,
197215
) -> List[Dict[str, Any]]:
198216
"""Gets valid measurement data parameters from the step dictionary.
199217
200218
Args:
201219
step_dict: A dictionary with step information.
202-
is_valid_measurement: Optional callback function to check if a measurement is valid. The method takes
220+
is_measurement_data_parameter: Optional callback function to check if a measurement is valid. The method takes
203221
a dictionary as input and returns a boolean value. If the function is not provided, the
204222
default behavior is to keep only those measurements that have both 'name' and 'measurement' fields.
205223
206224
Returns:
207225
List[Dict[str, Any]]: A list of dictionaries containing valid measurement data.
208226
"""
209-
allowed_measurement_keys = ["name", "units"]
210227
valid_measurement_parameters = []
211228
for parameter in step_dict["data"]["parameters"]:
212-
if (is_valid_measurement and is_valid_measurement(parameter)) or all(
213-
key in parameter for key in allowed_measurement_keys
214-
):
229+
if is_measurement_data_parameter and is_measurement_data_parameter(parameter):
215230
valid_measurement_parameters.append(parameter)
216231

217232
return valid_measurement_parameters
@@ -244,10 +259,8 @@ def __normalize_inputs_outputs(
244259

245260
def __normalize_step_status(step_dict: Dict[str, Any]) -> None:
246261
step_status = step_dict.get("status", {})
247-
if step_status.get("status_type") == "CUSTOM":
248-
step_dict["status"] = step_status.get("status_name", None)
249-
else:
250-
step_dict["status"] = step_status["status_type"].value
262+
step_dict["status"] = step_status.get("status_name", None) if step_status.get(
263+
"status_type") == "CUSTOM" else step_status["status_type"].value
251264

252265

253266
def __explode_and_normalize(

0 commit comments

Comments
 (0)