|
1 | 1 | """ |
2 | 2 | Prettify the execution information of the graph. |
3 | 3 | """ |
4 | | -import pandas as pd |
| 4 | +from typing import Union |
5 | 5 |
|
6 | | -def prettify_exec_info(complete_result: list[dict]) -> pd.DataFrame: |
| 6 | +def prettify_exec_info(complete_result: list[dict], as_string: bool = True) -> Union[str, list[dict]]: |
7 | 7 | """ |
8 | | - Transforms the execution information of a graph into a DataFrame for enhanced visualization. |
| 8 | + Formats the execution information of a graph showing node statistics. |
9 | 9 |
|
10 | 10 | Args: |
11 | | - complete_result (list[dict]): The complete execution information of the graph. |
| 11 | + complete_result (list[dict]): The execution information containing node statistics. |
| 12 | + as_string (bool, optional): If True, returns a formatted string table. |
| 13 | + If False, returns the original list. Defaults to True. |
12 | 14 |
|
13 | 15 | Returns: |
14 | | - pd.DataFrame: A DataFrame that organizes the execution information |
15 | | - for better readability and analysis. |
16 | | -
|
17 | | - Example: |
18 | | - >>> prettify_exec_info([{'node': 'A', 'status': 'success'}, |
19 | | - {'node': 'B', 'status': 'failure'}]) |
20 | | - DataFrame with columns 'node' and 'status' showing execution results for each node. |
| 16 | + Union[str, list[dict]]: A formatted string table if as_string=True, |
| 17 | + otherwise the original list of dictionaries. |
21 | 18 | """ |
| 19 | + if not as_string: |
| 20 | + return complete_result |
| 21 | + |
| 22 | + if not complete_result: |
| 23 | + return "Empty result" |
| 24 | + |
| 25 | + # Format the table |
| 26 | + lines = [] |
| 27 | + lines.append("Node Statistics:") |
| 28 | + lines.append("-" * 100) |
| 29 | + lines.append(f"{'Node':<20} {'Tokens':<10} {'Prompt':<10} {'Compl.':<10} {'Requests':<10} {'Cost ($)':<10} {'Time (s)':<10}") |
| 30 | + lines.append("-" * 100) |
| 31 | + |
| 32 | + for item in complete_result: |
| 33 | + node = item['node_name'] |
| 34 | + tokens = item['total_tokens'] |
| 35 | + prompt = item['prompt_tokens'] |
| 36 | + completion = item['completion_tokens'] |
| 37 | + requests = item['successful_requests'] |
| 38 | + cost = f"{item['total_cost_USD']:.4f}" |
| 39 | + time = f"{item['exec_time']:.2f}" |
22 | 40 |
|
23 | | - df_nodes = pd.DataFrame(complete_result) |
| 41 | + lines.append( |
| 42 | + f"{node:<20} {tokens:<10} {prompt:<10} {completion:<10} {requests:<10} {cost:<10} {time:<10}" |
| 43 | + ) |
24 | 44 |
|
25 | | - return df_nodes |
| 45 | + return "\n".join(lines) |
0 commit comments