1111from pathlib import Path
1212from typing import Any
1313
14+ from google .protobuf import json_format
1415from iris .cli .bug_report import gather_bug_report
1516from iris .cli .job import build_job_summary
1617from iris .cli .token_store import cluster_name_from_url , load_any_token , load_token
3031DEFAULT_PROFILE_SECONDS = 1
3132MAX_LIST_JOBS_PAGE_SIZE = 500
3233DEFAULT_LIST_JOBS_LIMIT = 100
34+ _PROTO_TO_DICT_OPTIONS = dict (preserving_proto_field_name = True )
3335
3436_ZEPHYR_PROGRESS_RE = re .compile (
3537 r"\[(?P<stage>[^\]]+)\]\s+"
@@ -183,16 +185,12 @@ def job_status_to_json(job: job_pb2.JobStatus, tasks: Iterable[job_pb2.TaskStatu
183185 }
184186
185187
186- def _attribute_value_to_json (value ) -> Any :
187- kind = value .WhichOneof ("value" )
188- if kind is None :
189- return None
190- return getattr (value , kind )
188+ def _worker_metadata_to_json (metadata : job_pb2 .WorkerMetadata ) -> dict [str , Any ]:
189+ return json_format .MessageToDict (metadata , ** _PROTO_TO_DICT_OPTIONS )
191190
192191
193192def worker_status_to_json (worker : controller_pb2 .Controller .WorkerHealthStatus ) -> dict [str , Any ]:
194193 """Serialize Iris worker health into stable JSON."""
195- metadata = worker .metadata
196194 return {
197195 "worker_id" : worker .worker_id ,
198196 "healthy" : bool (worker .healthy ),
@@ -201,23 +199,7 @@ def worker_status_to_json(worker: controller_pb2.Controller.WorkerHealthStatus)
201199 "running_job_ids" : list (worker .running_job_ids ),
202200 "address" : worker .address ,
203201 "status_message" : worker .status_message ,
204- "metadata" : {
205- "hostname" : metadata .hostname ,
206- "ip_address" : metadata .ip_address ,
207- "cpu_count" : int (metadata .cpu_count ),
208- "memory_bytes" : int (metadata .memory_bytes ),
209- "disk_bytes" : int (metadata .disk_bytes ),
210- "device" : _device_config_to_json (metadata .device ) if metadata .HasField ("device" ) else _cpu_device_json (),
211- "tpu_name" : metadata .tpu_name ,
212- "tpu_worker_id" : metadata .tpu_worker_id ,
213- "gpu_count" : int (metadata .gpu_count ),
214- "gpu_name" : metadata .gpu_name ,
215- "gpu_memory_mb" : int (metadata .gpu_memory_mb ),
216- "gce_instance_name" : metadata .gce_instance_name ,
217- "gce_zone" : metadata .gce_zone ,
218- "git_hash" : metadata .git_hash ,
219- "attributes" : {key : _attribute_value_to_json (value ) for key , value in metadata .attributes .items ()},
220- },
202+ "metadata" : _worker_metadata_to_json (worker .metadata ),
221203 }
222204
223205
0 commit comments