public static ApplicationReport convertToApplicationReport()

in hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/main/java/org/apache/hadoop/yarn/util/timeline/TimelineEntityV2Converter.java [253:461]


  public static ApplicationReport convertToApplicationReport(
      TimelineEntity entity) {
    String user = null;
    String queue = null;
    String name = null;
    String type = null;
    boolean unmanagedApplication = false;
    long createdTime = 0;
    long launchTime = 0;
    long finishedTime = 0;
    float progress = 0.0f;
    int applicationPriority = 0;
    ApplicationAttemptId latestApplicationAttemptId = null;
    String diagnosticsInfo = null;
    FinalApplicationStatus finalStatus = FinalApplicationStatus.UNDEFINED;
    YarnApplicationState state = YarnApplicationState.ACCEPTED;
    ApplicationResourceUsageReport appResources = null;
    Set<String> appTags = null;
    String appNodeLabelExpression = null;
    String amNodeLabelExpression = null;
    Map<String, Object> entityInfo = entity.getInfo();
    if (entityInfo != null) {
      if (entityInfo.containsKey(
          ApplicationMetricsConstants.USER_ENTITY_INFO)) {
        user =
            entityInfo.get(ApplicationMetricsConstants.USER_ENTITY_INFO)
                .toString();
      }
      if (entityInfo.containsKey(
          ApplicationMetricsConstants.QUEUE_ENTITY_INFO)) {
        queue =
            entityInfo.get(ApplicationMetricsConstants.QUEUE_ENTITY_INFO)
                .toString();
      }
      if (entityInfo.containsKey(
          ApplicationMetricsConstants.NAME_ENTITY_INFO)) {
        name =
            entityInfo.get(ApplicationMetricsConstants.NAME_ENTITY_INFO)
                .toString();
      }
      if (entityInfo.containsKey(
          ApplicationMetricsConstants.TYPE_ENTITY_INFO)) {
        type =
            entityInfo.get(ApplicationMetricsConstants.TYPE_ENTITY_INFO)
                .toString();
      }
      if (entityInfo.containsKey(
          ApplicationMetricsConstants.TYPE_ENTITY_INFO)) {
        type =
            entityInfo.get(ApplicationMetricsConstants.TYPE_ENTITY_INFO)
                .toString();
      }
      if (entityInfo
          .containsKey(
              ApplicationMetricsConstants.UNMANAGED_APPLICATION_ENTITY_INFO)) {
        unmanagedApplication =
            Boolean.parseBoolean(entityInfo.get(
                ApplicationMetricsConstants.UNMANAGED_APPLICATION_ENTITY_INFO)
                .toString());
      }
      if (entityInfo
          .containsKey(ApplicationMetricsConstants.APPLICATION_PRIORITY_INFO)) {
        applicationPriority = Integer.parseInt(entityInfo.get(
            ApplicationMetricsConstants.APPLICATION_PRIORITY_INFO).toString());
      }
      if (entityInfo.containsKey(ApplicationMetricsConstants.APP_TAGS_INFO)) {
        appTags = new HashSet<>();
        Object obj = entityInfo.get(ApplicationMetricsConstants.APP_TAGS_INFO);
        if (obj != null && obj instanceof Collection<?>) {
          for(Object o : (Collection<?>)obj) {
            if (o != null) {
              appTags.add(o.toString());
            }
          }
        }
      }
      if (entityInfo
          .containsKey(
              ApplicationMetricsConstants.LATEST_APP_ATTEMPT_EVENT_INFO)) {
        latestApplicationAttemptId = ApplicationAttemptId.fromString(
            entityInfo.get(
                ApplicationMetricsConstants.LATEST_APP_ATTEMPT_EVENT_INFO)
                .toString());
      }
      if (entityInfo.containsKey(
          ApplicationMetricsConstants.DIAGNOSTICS_INFO_EVENT_INFO)) {
        diagnosticsInfo =
            entityInfo.get(
                ApplicationMetricsConstants.DIAGNOSTICS_INFO_EVENT_INFO)
                .toString();
      }
      if (entityInfo
          .containsKey(ApplicationMetricsConstants.FINAL_STATUS_EVENT_INFO)) {
        finalStatus =
            FinalApplicationStatus.valueOf(entityInfo.get(
                ApplicationMetricsConstants.FINAL_STATUS_EVENT_INFO)
                .toString());
      }
      if (entityInfo
          .containsKey(ApplicationMetricsConstants.STATE_EVENT_INFO)) {
        state =
            YarnApplicationState.valueOf(entityInfo.get(
                ApplicationMetricsConstants.STATE_EVENT_INFO).toString());
      }
    }

    Map<String, String> configs = entity.getConfigs();
    if (configs
        .containsKey(ApplicationMetricsConstants.APP_NODE_LABEL_EXPRESSION)) {
      appNodeLabelExpression = configs
          .get(ApplicationMetricsConstants.APP_NODE_LABEL_EXPRESSION);
    }
    if (configs
        .containsKey(ApplicationMetricsConstants.AM_NODE_LABEL_EXPRESSION)) {
      amNodeLabelExpression =
          configs.get(ApplicationMetricsConstants.AM_NODE_LABEL_EXPRESSION);
    }

    Set<TimelineMetric> metrics = entity.getMetrics();
    if (metrics != null) {
      long vcoreSeconds = 0;
      long memorySeconds = 0;
      long preemptedVcoreSeconds = 0;
      long preemptedMemorySeconds = 0;

      for (TimelineMetric metric : metrics) {
        switch (metric.getId()) {
        case ApplicationMetricsConstants.APP_CPU_METRICS:
          vcoreSeconds = getAverageValue(metric.getValues().values());
          break;
        case ApplicationMetricsConstants.APP_MEM_METRICS:
          memorySeconds = getAverageValue(metric.getValues().values());
          break;
        case ApplicationMetricsConstants.APP_MEM_PREEMPT_METRICS:
          preemptedVcoreSeconds = getAverageValue(metric.getValues().values());
          break;
        case ApplicationMetricsConstants.APP_CPU_PREEMPT_METRICS:
          preemptedVcoreSeconds = getAverageValue(metric.getValues().values());
          break;
        default:
          // Should not happen..
          break;
        }
      }
      Map<String, Long> resourceSecondsMap = new HashMap<>();
      Map<String, Long> preemptedResourceSecondsMap = new HashMap<>();
      resourceSecondsMap
          .put(ResourceInformation.MEMORY_MB.getName(), memorySeconds);
      resourceSecondsMap
          .put(ResourceInformation.VCORES.getName(), vcoreSeconds);
      preemptedResourceSecondsMap.put(ResourceInformation.MEMORY_MB.getName(),
          preemptedMemorySeconds);
      preemptedResourceSecondsMap
          .put(ResourceInformation.VCORES.getName(), preemptedVcoreSeconds);

      appResources = ApplicationResourceUsageReport
          .newInstance(0, 0, null, null, null, resourceSecondsMap, 0, 0,
              preemptedResourceSecondsMap);
    }

    NavigableSet<TimelineEvent> events = entity.getEvents();
    long updatedTimeStamp = 0L;
    if (events != null) {
      for (TimelineEvent event : events) {
        if (event.getId().equals(
            ApplicationMetricsConstants.CREATED_EVENT_TYPE)) {
          createdTime = event.getTimestamp();
        } else if (event.getId().equals(
            ApplicationMetricsConstants.LAUNCHED_EVENT_TYPE)) {
          launchTime = event.getTimestamp();
        } else if (event.getId().equals(
            ApplicationMetricsConstants.UPDATED_EVENT_TYPE)) {
          // This type of events are parsed in time-stamp descending order
          // which means the previous event could override the information
          // from the later same type of event. Hence compare timestamp
          // before over writing.
          if (event.getTimestamp() > updatedTimeStamp) {
            updatedTimeStamp = event.getTimestamp();
          }
        } else if (event.getId().equals(
            ApplicationMetricsConstants.STATE_UPDATED_EVENT_TYPE)) {
          Map<String, Object> eventInfo = event.getInfo();
          if (eventInfo == null) {
            continue;
          }
          if (eventInfo.containsKey(
              ApplicationMetricsConstants.STATE_EVENT_INFO)) {
            if (state == YarnApplicationState.ACCEPTED) {
              state = YarnApplicationState.valueOf(eventInfo.get(
                  ApplicationMetricsConstants.STATE_EVENT_INFO).toString());
            }
          }
        } else if (event.getId().equals(
            ApplicationMetricsConstants.FINISHED_EVENT_TYPE)) {
          progress=1.0F;
          state = YarnApplicationState.FINISHED;
          finishedTime = event.getTimestamp();
        }
      }
    }
    return ApplicationReport.newInstance(
        ApplicationId.fromString(entity.getId()),
        latestApplicationAttemptId, user, queue, name, null, -1, null, state,
        diagnosticsInfo, null, createdTime, launchTime,
        finishedTime, finalStatus, appResources, null,
        progress, type, null, appTags, unmanagedApplication,
        Priority.newInstance(applicationPriority), appNodeLabelExpression,
        amNodeLabelExpression);
  }