|
| 1 | +""" |
| 2 | +Utility functions for showing evaluation results URLs and checking server status. |
| 3 | +""" |
| 4 | + |
| 5 | +import socket |
| 6 | +import urllib.parse |
| 7 | + |
| 8 | + |
| 9 | +def is_server_running(host: str = "localhost", port: int = 8000) -> bool: |
| 10 | + """ |
| 11 | + Check if a server is running on the specified host and port. |
| 12 | +
|
| 13 | + Args: |
| 14 | + host: The host to check (default: "localhost") |
| 15 | + port: The port to check (default: 8000) |
| 16 | +
|
| 17 | + Returns: |
| 18 | + True if server is running, False otherwise |
| 19 | + """ |
| 20 | + try: |
| 21 | + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
| 22 | + s.settimeout(1) |
| 23 | + result = s.connect_ex((host, port)) |
| 24 | + return result == 0 |
| 25 | + except Exception: |
| 26 | + return False |
| 27 | + |
| 28 | + |
| 29 | +def generate_invocation_filter_url(invocation_id: str, base_url: str = "http://localhost:8000") -> str: |
| 30 | + """ |
| 31 | + Generate a URL for viewing results filtered by invocation_id. |
| 32 | +
|
| 33 | + Args: |
| 34 | + invocation_id: The invocation ID to filter results by |
| 35 | + base_url: The base URL for the UI (default: "http://localhost:8000") |
| 36 | +
|
| 37 | + Returns: |
| 38 | + URL-encoded URL with filter configuration |
| 39 | + """ |
| 40 | + filter_config = [ |
| 41 | + { |
| 42 | + "logic": "AND", |
| 43 | + "filters": [ |
| 44 | + { |
| 45 | + "field": "$.execution_metadata.invocation_id", |
| 46 | + "operator": "==", |
| 47 | + "value": invocation_id, |
| 48 | + "type": "text", |
| 49 | + } |
| 50 | + ], |
| 51 | + } |
| 52 | + ] |
| 53 | + |
| 54 | + # URL encode the filter config |
| 55 | + filter_config_json = str(filter_config).replace("'", '"') |
| 56 | + encoded_filter = urllib.parse.quote(filter_config_json) |
| 57 | + |
| 58 | + return f"{base_url}?filterConfig={encoded_filter}" |
| 59 | + |
| 60 | + |
| 61 | +def show_results_url(invocation_id: str) -> None: |
| 62 | + """ |
| 63 | + Show URLs for viewing evaluation results filtered by invocation_id. |
| 64 | +
|
| 65 | + If the server is not running, prints a message to run "ep logs" to start the local UI. |
| 66 | + If the server is running, prints URLs to view results filtered by invocation_id. |
| 67 | +
|
| 68 | + Args: |
| 69 | + invocation_id: The invocation ID to filter results by |
| 70 | + """ |
| 71 | + if is_server_running(): |
| 72 | + pivot_url = generate_invocation_filter_url(invocation_id, "http://localhost:8000/pivot") |
| 73 | + table_url = generate_invocation_filter_url(invocation_id, "http://localhost:8000/table") |
| 74 | + print("View your evaluation results:") |
| 75 | + print(f" π Aggregate scores: {pivot_url}") |
| 76 | + print(f" π Trajectories: {table_url}") |
| 77 | + else: |
| 78 | + pivot_url = generate_invocation_filter_url(invocation_id, "http://localhost:8000/pivot") |
| 79 | + table_url = generate_invocation_filter_url(invocation_id, "http://localhost:8000/table") |
| 80 | + print("Start the local UI with 'ep logs', then visit:") |
| 81 | + print(f" π Aggregate scores: {pivot_url}") |
| 82 | + print(f" π Trajectories: {table_url}") |
0 commit comments