slfan1989 commented on code in PR #5030:
URL: https://github.com/apache/hadoop/pull/5030#discussion_r1001269099


##########
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-router/src/main/java/org/apache/hadoop/yarn/server/router/webapp/AppsBlock.java:
##########
@@ -81,53 +155,57 @@ protected void render(Block html) {
 
     // Render the applications
     StringBuilder appsTableData = new StringBuilder("[\n");
-    for (AppInfo app : apps.getApps()) {
-      try {
-
-        String percent = String.format("%.1f", app.getProgress() * 100.0F);
-        String trackingURL =
-            app.getTrackingUrl() == null ? "#" : app.getTrackingUrl();
-        // AppID numerical value parsed by parseHadoopID in yarn.dt.plugins.js
-        appsTableData.append("[\"")
-            .append("<a href='").append(trackingURL).append("'>")
-            .append(app.getAppId()).append("</a>\",\"")
-            .append(escape(app.getUser())).append("\",\"")
-            .append(escape(app.getName())).append("\",\"")
-            .append(escape(app.getApplicationType())).append("\",\"")
-            .append(escape(app.getQueue())).append("\",\"")
-            .append(String.valueOf(app.getPriority())).append("\",\"")
-            .append(app.getStartTime()).append("\",\"")
-            .append(app.getFinishTime()).append("\",\"")
-            .append(app.getState()).append("\",\"")
-            .append(app.getFinalStatus()).append("\",\"")
-            // Progress bar
-            .append("<br title='").append(percent).append("'> <div class='")
-            .append(C_PROGRESSBAR).append("' title='")
-            .append(join(percent, '%')).append("'> ").append("<div class='")
-            .append(C_PROGRESSBAR_VALUE).append("' style='")
-            .append(join("width:", percent, '%')).append("'> </div> </div>")
-            // History link
-            .append("\",\"<a href='").append(trackingURL).append("'>")
-            .append("History").append("</a>");
-        appsTableData.append("\"],\n");
-
-      } catch (Exception e) {
-        LOG.info(
-            "Cannot add application {}: {}", app.getAppId(), e.getMessage());
+
+    if (appsInfo != null) {
+      Collection<AppInfo> apps = appsInfo.getApps();
+      if (CollectionUtils.isNotEmpty(apps)) {
+        int numApps = apps.size();
+        int i = 0;
+        for (AppInfo app : apps) {
+          try {
+            String percent = String.format("%.1f", app.getProgress() * 100.0F);
+            String trackingURL =
+                app.getTrackingUrl() == null ? "#" : app.getTrackingUrl();
+
+            // AppID numerical value parsed by parseHadoopID in 
yarn.dt.plugins.js
+            appsTableData.append("[\"")
+                .append("<a href='").append(trackingURL).append("'>")
+                .append(app.getAppId()).append("</a>\",\"")
+                .append(escape(app.getUser())).append("\",\"")
+                .append(escape(app.getName())).append("\",\"")
+                .append(escape(app.getApplicationType())).append("\",\"")
+                .append(escape(app.getQueue())).append("\",\"")
+                .append(app.getPriority()).append("\",\"")
+                .append(app.getStartTime()).append("\",\"")
+                .append(app.getFinishTime()).append("\",\"")
+                .append(app.getState()).append("\",\"")
+                .append(app.getFinalStatus()).append("\",\"")
+                // Progress bar
+                .append("<br title='").append(percent).append("'> <div 
class='")
+                .append(C_PROGRESSBAR).append("' title='")
+                .append(join(percent, '%')).append("'> ").append("<div 
class='")
+                .append(C_PROGRESSBAR_VALUE).append("' style='")
+                .append(join("width:", percent, '%')).append("'> </div> 
</div>")
+                // History link
+                .append("\",\"<a href='").append(trackingURL).append("'>")
+                .append("History").append("</a>");
+            appsTableData.append("\"]\n");
+
+            if (i < numApps - 1) {
+              appsTableData.append(",");
+            }
+          } catch (Exception e) {
+            LOG.info("Cannot add application {}: {}", app.getAppId(), 
e.getMessage());
+          }
+          i++;

Review Comment:
   I read this part of the code carefully, the original code is reasonable, we 
should remove the extra comma outside the loop because we can't tell where the 
last comma is
   
   For example:
   We have 4 apps, A, B, C, D, and we expect the result to be [A, B, C, D], but 
when traversing the app list, the last app D fails, and we end up with [A 
,B,C,], the last app D fails, we can't handle this situation inside the loop
   
   But the readability of the original logic is not good, I refactored this 
part of the code.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to