1- from typing import List , Optional
1+ from typing import Any , Dict , List
22
33import pandas as pd
44from nisystemlink .clients .testmonitor .models import Result
55from nisystemlink .clients .testmonitor .utilities .constants import DataFrameHeaders
66
77
88def convert_results_to_dataframe (
9- results : List [Result ], set_id_as_index : Optional [ bool ] = False
9+ results : List [Result ], set_id_as_index : bool = True
1010) -> pd .DataFrame :
1111 """Creates a Pandas DataFrame for the results.
1212
@@ -25,8 +25,11 @@ def convert_results_to_dataframe(
2525 - Properties: All the properties will be split into separate columns. For example,
2626 properties.property1, properties.property2, etc.
2727 """
28- results_dict = __format_results_dictionary (results )
29- normalized_dataframe = pd .json_normalize (results_dict , sep = "." )
28+ results_dict = [result .dict (exclude_none = True ) for result in results ]
29+ results_dict_with_normalized_status = __format_results_status (results_dict )
30+ normalized_dataframe = pd .json_normalize (
31+ results_dict_with_normalized_status , sep = "."
32+ )
3033 normalized_dataframe = __format_results_columns (
3134 results_dataframe = normalized_dataframe
3235 )
@@ -36,8 +39,8 @@ def convert_results_to_dataframe(
3639 return normalized_dataframe
3740
3841
39- def __format_results_dictionary ( results : List [Result ] ) -> List [dict ]:
40- """Get list of results and modifies the status object.
42+ def __format_results_status ( results_dict : List [Dict [ str , Any ]] ) -> List [Dict [ str , Any ] ]:
43+ """Gets dictionary of results data and modifies the status object.
4144
4245 Args:
4346 results: List of results.
@@ -46,18 +49,14 @@ def __format_results_dictionary(results: List[Result]) -> List[dict]:
4649 A list of result fields as dictionary. If status.status_type is "CUSTOM"
4750 the status field takes the value of "status_name", else value of "status_type" is used.
4851 """
49- return [
50- {
51- ** (result_dict := result .dict (exclude_none = True )),
52- "status" : (
53- result_dict ["status" ]["status_type" ].value
54- if "status" in result_dict
55- and result_dict ["status" ]["status_type" ] != "CUSTOM"
56- else result_dict ["status" ]["status_name" ]
57- ),
58- }
59- for result in results
60- ]
52+ for result in results_dict :
53+ status = result .get ("status" , {})
54+ if status .get ("status_type" ) == "CUSTOM" :
55+ result ["status" ] = status ["status_name" ]
56+ else :
57+ result ["status" ] = status ["status_type" ].value
58+
59+ return results_dict
6160
6261
6362def __format_results_columns (results_dataframe : pd .DataFrame ) -> pd .DataFrame :
0 commit comments