|
| 1 | +# Copyright 2025 IBM, Red Hat |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +This sub-module exists primarily to be used internally by the RayJob object |
| 17 | +(in the rayjob sub-module) for pretty-printing job status and details. |
| 18 | +""" |
| 19 | + |
| 20 | +from rich.console import Console |
| 21 | +from rich.table import Table |
| 22 | +from rich.panel import Panel |
| 23 | +from typing import Tuple, Optional |
| 24 | + |
| 25 | +from .status import RayJobDeploymentStatus, RayJobInfo |
| 26 | + |
| 27 | + |
| 28 | +def print_job_status(job_info: RayJobInfo): |
| 29 | + """ |
| 30 | + Pretty print the job status in a format similar to cluster status. |
| 31 | + """ |
| 32 | + status_display, header_color = _get_status_display(job_info.status) |
| 33 | + |
| 34 | + # Create main info table |
| 35 | + table = _create_info_table(header_color, job_info.name, status_display) |
| 36 | + table.add_row(f"[bold]Job ID:[/bold] {job_info.job_id}") |
| 37 | + table.add_row(f"[bold]Status:[/bold] {job_info.status.value}") |
| 38 | + table.add_row(f"[bold]RayCluster:[/bold] {job_info.cluster_name}") |
| 39 | + table.add_row(f"[bold]Namespace:[/bold] {job_info.namespace}") |
| 40 | + |
| 41 | + # Add timing information if available |
| 42 | + if job_info.start_time: |
| 43 | + table.add_row() |
| 44 | + table.add_row(f"[bold]Duration:[/bold] {job_info.duration}") |
| 45 | + |
| 46 | + # Add attempt counts if there are failures |
| 47 | + if job_info.failed_attempts > 0: |
| 48 | + table.add_row(f"[bold]Failed Attempts:[/bold] {job_info.failed_attempts}") |
| 49 | + |
| 50 | + _print_table_in_panel(table) |
| 51 | + |
| 52 | + |
| 53 | +def print_no_job_found(job_name: str, namespace: str): |
| 54 | + """ |
| 55 | + Print a message when no job is found. |
| 56 | + """ |
| 57 | + # Create table with error message |
| 58 | + table = _create_info_table( |
| 59 | + "[white on red][bold]Name", job_name, "[bold red]No RayJob found" |
| 60 | + ) |
| 61 | + table.add_row() |
| 62 | + table.add_row("Have you run rayjob.submit() yet?") |
| 63 | + table.add_row() |
| 64 | + table.add_row(f"[bold]Namespace:[/bold] {namespace}") |
| 65 | + |
| 66 | + _print_table_in_panel(table) |
| 67 | + |
| 68 | + |
| 69 | +def _get_status_display(status: RayJobDeploymentStatus) -> Tuple[str, str]: |
| 70 | + """ |
| 71 | + Get the display string and header color for a given status. |
| 72 | +
|
| 73 | + Returns: |
| 74 | + Tuple of (status_display, header_color) |
| 75 | + """ |
| 76 | + status_mapping = { |
| 77 | + RayJobDeploymentStatus.COMPLETE: ( |
| 78 | + "Complete :white_heavy_check_mark:", |
| 79 | + "[white on green][bold]Name", |
| 80 | + ), |
| 81 | + RayJobDeploymentStatus.RUNNING: ("Running :gear:", "[white on blue][bold]Name"), |
| 82 | + RayJobDeploymentStatus.FAILED: ("Failed :x:", "[white on red][bold]Name"), |
| 83 | + RayJobDeploymentStatus.SUSPENDED: ( |
| 84 | + "Suspended :pause_button:", |
| 85 | + "[white on yellow][bold]Name", |
| 86 | + ), |
| 87 | + } |
| 88 | + |
| 89 | + return status_mapping.get( |
| 90 | + status, ("Unknown :question:", "[white on red][bold]Name") |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +def _create_info_table(header_color: str, name: str, status_display: str) -> Table: |
| 95 | + """ |
| 96 | + Create a standardized info table with header and status. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + Table with header row, name/status row, and empty separator row |
| 100 | + """ |
| 101 | + table = Table(box=None, show_header=False) |
| 102 | + table.add_row(header_color) |
| 103 | + table.add_row("[bold underline]" + name, status_display) |
| 104 | + table.add_row() # Empty separator row |
| 105 | + return table |
| 106 | + |
| 107 | + |
| 108 | +def _print_table_in_panel(table: Table): |
| 109 | + """ |
| 110 | + Print a table wrapped in a consistent panel format. |
| 111 | + """ |
| 112 | + console = Console() |
| 113 | + main_table = Table( |
| 114 | + box=None, title="[bold] :package: CodeFlare RayJob Status :package:" |
| 115 | + ) |
| 116 | + main_table.add_row(Panel.fit(table)) |
| 117 | + console.print(main_table) |
0 commit comments