content: package org.flexlb.service.grpc;
  
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  
  import org.flexlb.dao.master.CacheStatus;
  import org.flexlb.dao.master.TaskInfo;
  import org.flexlb.domain.worker.WorkerStatusResponse;
  import org.flexlb.engine.grpc.EngineRpcService;
  
  /**
   * Converter between gRPC protobuf messages and Java objects for engine status
   */
  public class EngineStatusConverter {
  
      /**
       * Convert WorkerStatusPB to WorkerStatusResponse
       */
      public static WorkerStatusResponse convertToWorkerStatusResponse(EngineRpcService.WorkerStatusPB workerStatusPB) {
          WorkerStatusResponse response = new WorkerStatusResponse();
          
          // Set role directly as string
          response.setRole(workerStatusPB.getRole());
          response.setAvailableConcurrency(workerStatusPB.getAvailableConcurrency());
          response.setRunningQueryLen(workerStatusPB.getRunningQueryLen());
          response.setWaitingQueryLen(workerStatusPB.getWaitingQueryLen());
          response.setStepLatencyMs(workerStatusPB.getStepLatencyMs());
          response.setIterateCount(workerStatusPB.getIterateCount());
          response.setDpSize(workerStatusPB.getDpSize());
          response.setTpSize(workerStatusPB.getTpSize());
          response.setVersion(workerStatusPB.getVersion());
          response.setStatusVersion(workerStatusPB.getStatusVersion());
          response.setAlive(workerStatusPB.getAlive());
  
          // Convert running task info
          response.setRunningTaskInfo(convertToTaskInfoList(workerStatusPB.getRunningTaskInfoList()));
          
          // Convert finished task list
          response.setFinishedTaskList(convertToTaskInfoList(workerStatusPB.getFinishedTaskListList()));
          
          return response;
      }
  
      /**
       * Convert CacheStatusPB to CacheStatus
       */
      public static CacheStatus convertToCacheStatus(EngineRpcService.CacheStatusPB cacheStatusPB) {
          CacheStatus cacheStatus = new CacheStatus();
          cacheStatus.setAvailableKvCache(cacheStatusPB.getAvailableKvCache());
          cacheStatus.setTotalKvCache(cacheStatusPB.getTotalKvCache());
          cacheStatus.setBlockSize(cacheStatusPB.getBlockSize());
          cacheStatus.setVersion(cacheStatusPB.getVersion());
  
          Map<Long, Boolean> cacheKeysMap = cacheStatusPB.getCacheKeysMap();
          Set<Long> cachedKeysSet = cacheKeysMap.keySet();
          cacheStatus.setCachedKeys(cachedKeysSet);
          cacheStatus.setCacheKeySize(cacheKeysMap.size());
          return cacheStatus;
      }
  
      /**
       * Convert list of TaskInfoPB to list of TaskInfo
       */
      private static List<TaskInfo> convertToTaskInfoList(List<EngineRpcService.TaskInfoPB> taskInfoPBList) {
          List<TaskInfo> taskInfoList = new ArrayList<>();
          
          for (EngineRpcService.TaskInfoPB taskInfoPB : taskInfoPBList) {
              TaskInfo taskInfo = new TaskInfo();
  
              taskInfo.setInterRequestId(taskInfoPB.getInterRequestId());
              taskInfo.setPrefixLength(taskInfoPB.getPrefixLength());
              taskInfo.setInputLength(taskInfoPB.getInputLength());
              taskInfo.setWaitingTime(taskInfoPB.getWaitingTimeMs());
              taskInfo.setIterateCount(taskInfoPB.getIterateCount());
              taskInfo.setEndTimeMs(taskInfoPB.getEndTimeMs());
              taskInfo.setDpRank(taskInfoPB.getDpRank());
              
              taskInfoList.add(taskInfo);
          }
          
          return taskInfoList;
      }
  }