You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-issues@hadoop.apache.org by "ayushtkn (via GitHub)" <gi...@apache.org> on 2023/06/14 19:41:52 UTC

[GitHub] [hadoop] ayushtkn commented on a diff in pull request #5716: YARN-11506.The formatted yarn queue list is displayed on CLI

ayushtkn commented on code in PR #5716:
URL: https://github.com/apache/hadoop/pull/5716#discussion_r1230080986


##########
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/util/FormattingCLIUtils.java:
##########
@@ -0,0 +1,302 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.yarn.client.util;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * The main core class that generates the ASCII TABLE.
+ */
+public final class FormattingCLIUtils {
+  /** Table title. */
+  private String title;
+  /** Last processed row type. */
+  private TableRowType lastTableRowType;
+  /** StringBuilder object used to concatenate strings. */
+  private StringBuilder join;
+  /** An ordered Map that holds each row of data. */
+  private List<TableRow> tableRows;
+  /** Maps the maximum length of each column. */
+  private Map<Integer, Integer> maxColMap;
+
+  /**
+   * Default empty constructor.
+   */
+  public FormattingCLIUtils() {
+    this.init();
+  }
+
+  /**
+   * Contains the title constructor.
+   * @param title titleName
+   */
+  public FormattingCLIUtils(String title) {
+    this.init();
+    this.title = title;
+  }
+
+  /**
+   * Initialize the data.
+   */
+  private void init() {
+    this.join = new StringBuilder();
+    this.tableRows = new ArrayList<>();
+    this.maxColMap = new HashMap<>();
+  }
+
+  /**
+   * Adds elements from the collection to the header data in the table.
+   * @param headers Header data
+   * @return FormattingCLIUtils object
+   */
+  public FormattingCLIUtils addHeaders(List<?> headers) {
+    return this.appendRows(TableRowType.HEADER, headers.toArray());
+  }
+
+  /**
+   * Adds header data to the table.
+   * @param objects Header data
+   * @return FormattingCLIUtils object
+   */
+  public FormattingCLIUtils addHeaders(Object... objects) {
+    return this.appendRows(TableRowType.HEADER, objects);
+  }

Review Comment:
   where is this being used?



##########
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/util/FormattingCLIUtils.java:
##########
@@ -0,0 +1,302 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.yarn.client.util;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * The main core class that generates the ASCII TABLE.
+ */
+public final class FormattingCLIUtils {
+  /** Table title. */
+  private String title;
+  /** Last processed row type. */
+  private TableRowType lastTableRowType;
+  /** StringBuilder object used to concatenate strings. */
+  private StringBuilder join;
+  /** An ordered Map that holds each row of data. */
+  private List<TableRow> tableRows;
+  /** Maps the maximum length of each column. */
+  private Map<Integer, Integer> maxColMap;
+
+  /**
+   * Default empty constructor.
+   */
+  public FormattingCLIUtils() {
+    this.init();
+  }
+
+  /**
+   * Contains the title constructor.
+   * @param title titleName
+   */
+  public FormattingCLIUtils(String title) {
+    this.init();
+    this.title = title;
+  }
+
+  /**
+   * Initialize the data.
+   */
+  private void init() {
+    this.join = new StringBuilder();
+    this.tableRows = new ArrayList<>();
+    this.maxColMap = new HashMap<>();
+  }
+
+  /**
+   * Adds elements from the collection to the header data in the table.
+   * @param headers Header data
+   * @return FormattingCLIUtils object
+   */
+  public FormattingCLIUtils addHeaders(List<?> headers) {
+    return this.appendRows(TableRowType.HEADER, headers.toArray());
+  }
+
+  /**
+   * Adds header data to the table.
+   * @param objects Header data
+   * @return FormattingCLIUtils object
+   */
+  public FormattingCLIUtils addHeaders(Object... objects) {
+    return this.appendRows(TableRowType.HEADER, objects);
+  }
+
+  /**
+   * Adds a row of normal data to the table.
+   * @param datas Common row data
+   * @return FormattingCLIUtils object
+   */
+  public FormattingCLIUtils addDatas(List<?> datas) {
+    return this.appendRows(TableRowType.DATA, datas.toArray());

Review Comment:
   unused??



##########
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/util/FormattingCLIUtils.java:
##########
@@ -0,0 +1,302 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.yarn.client.util;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * The main core class that generates the ASCII TABLE.
+ */
+public final class FormattingCLIUtils {
+  /** Table title. */
+  private String title;
+  /** Last processed row type. */
+  private TableRowType lastTableRowType;
+  /** StringBuilder object used to concatenate strings. */
+  private StringBuilder join;
+  /** An ordered Map that holds each row of data. */
+  private List<TableRow> tableRows;
+  /** Maps the maximum length of each column. */
+  private Map<Integer, Integer> maxColMap;
+
+  /**
+   * Default empty constructor.
+   */
+  public FormattingCLIUtils() {
+    this.init();
+  }
+
+  /**
+   * Contains the title constructor.
+   * @param title titleName
+   */
+  public FormattingCLIUtils(String title) {
+    this.init();
+    this.title = title;
+  }
+
+  /**
+   * Initialize the data.
+   */
+  private void init() {
+    this.join = new StringBuilder();
+    this.tableRows = new ArrayList<>();
+    this.maxColMap = new HashMap<>();
+  }
+
+  /**
+   * Adds elements from the collection to the header data in the table.
+   * @param headers Header data
+   * @return FormattingCLIUtils object
+   */
+  public FormattingCLIUtils addHeaders(List<?> headers) {
+    return this.appendRows(TableRowType.HEADER, headers.toArray());
+  }
+
+  /**
+   * Adds header data to the table.
+   * @param objects Header data
+   * @return FormattingCLIUtils object
+   */
+  public FormattingCLIUtils addHeaders(Object... objects) {
+    return this.appendRows(TableRowType.HEADER, objects);
+  }
+
+  /**
+   * Adds a row of normal data to the table.
+   * @param datas Common row data
+   * @return FormattingCLIUtils object
+   */
+  public FormattingCLIUtils addDatas(List<?> datas) {
+    return this.appendRows(TableRowType.DATA, datas.toArray());
+  }
+
+  /**
+   * Adds a row of normal data to the table.
+   * @param objects Common row data
+   * @return FormattingCLIUtils object
+   */
+  public FormattingCLIUtils addDatas(Object... objects) {
+    return this.appendRows(TableRowType.DATA, objects);
+  }
+
+  /**
+   * Adds the middle row of data to the table.
+   * @param tableRowType TableRowType
+   * @param objects Table row data
+   * @return FormattingCLIUtils object
+   */
+  private FormattingCLIUtils appendRows(TableRowType tableRowType, Object... objects) {
+    if (objects != null && objects.length > 0) {
+      int len = objects.length;
+      if (this.maxColMap.size() > len) {
+        throw new IllegalArgumentException("The number of columns that inserted a row " +
+            "of data into the table is different from the number of previous columns, check!");
+      }
+      List<String> datas = new ArrayList<>();
+      for (int i = 0; i < len; i++) {
+        Object o = objects[i];
+        String value = o == null ? "null" : o.toString();
+        datas.add(value);
+        Integer maxColSize = this.maxColMap.get(i);
+        if (maxColSize == null) {
+          this.maxColMap.put(i, value.length());
+          continue;
+        }
+        if (value.length() > maxColSize) {
+          this.maxColMap.put(i, value.length());
+        }
+      }
+      this.tableRows.add(new TableRow(tableRowType, datas));
+    }
+    return this;
+  }
+
+  /**
+   * Builds the string for the row of the table title.
+   */
+  private void buildTitle() {
+    if (this.title != null) {
+      int maxTitleSize = 0;
+      for (Integer maxColSize : this.maxColMap.values()) {
+        maxTitleSize += maxColSize;
+      }
+      maxTitleSize += 3 * (this.maxColMap.size() - 1);
+      if (this.title.length() > maxTitleSize) {
+        this.title = this.title.substring(0, maxTitleSize);
+      }
+      this.join.append("+");
+      for (int i = 0; i < maxTitleSize + 2; i++) {
+        this.join.append("-");
+      }
+      this.join.append("+\n")
+          .append("|")
+          .append(StrUtils.center(this.title, maxTitleSize + 2, ' '))
+          .append("|\n");
+      this.lastTableRowType = TableRowType.TITLE;
+    }
+  }
+
+  /**
+   * Build the table, first build the title, and then walk through each row of data to build.
+   */
+  private void buildTable() {
+    this.buildTitle();
+    for (int i = 0, len = this.tableRows.size(); i < len; i++) {
+      List<String> datas = this.tableRows.get(i).datas;
+      switch (this.tableRows.get(i).tableRowType) {
+      case HEADER:
+        if (this.lastTableRowType != TableRowType.HEADER) {
+          this.buildRowBorder(datas);
+        }
+        this.buildRowData(datas);
+        this.buildRowBorder(datas);
+        break;
+      case DATA:
+        this.buildRowData(datas);
+        if (i == len - 1) {
+          this.buildRowBorder(datas);
+        }
+        break;
+      default:
+        break;
+      }
+    }
+  }
+
+  /**
+   * Method to build a border row.
+   * @param datas dataLine
+   */
+  private void buildRowBorder(List<String> datas) {
+    this.join.append("+");
+    for (int i = 0, len = datas.size(); i < len; i++) {
+      for (int j = 0; j < this.maxColMap.get(i) + 2; j++) {
+        this.join.append("-");
+      }
+      this.join.append("+");
+    }
+    this.join.append("\n");
+  }
+
+  /**
+   * A way to build rows of data.
+   * @param datas dataLine
+   */
+  private void buildRowData(List<String> datas) {
+    this.join.append("|");
+    for (int i = 0, len = datas.size(); i < len; i++) {
+      this.join.append(StrUtils.center(datas.get(i), this.maxColMap.get(i) + 2, ' '))
+          .append("|");
+    }
+    this.join.append("\n");
+  }
+
+  /**
+   * Rendering is born as a result.
+   * @return ASCII string of Table
+   */
+  public String render() {
+    this.buildTable();
+    return this.join.toString();
+  }
+
+  /**
+   * The type of each table row and the entity class of the data.
+   */
+  private static class TableRow {
+    private TableRowType tableRowType;
+    private List<String> datas;
+    TableRow(TableRowType tableRowType, List<String> datas) {
+      this.tableRowType = tableRowType;
+      this.datas = datas;
+    }
+  }
+
+  /**
+   * An enumeration class that distinguishes between table headers and normal table data.
+   */
+  private enum TableRowType {
+    TITLE, HEADER, DATA
+  }
+
+  /**
+   * String utility class.
+   */
+  private static final class StrUtils {
+    /**
+     * Puts a string in the middle of a given size.
+     * @param str
+     * @param size
+     * @param padChar
+     * @return
+     */

Review Comment:
   fill these values for javadoc



-- 
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: common-issues-unsubscribe@hadoop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: common-issues-unsubscribe@hadoop.apache.org
For additional commands, e-mail: common-issues-help@hadoop.apache.org