jianghuazhu commented on code in PR #5882:
URL: https://github.com/apache/ozone/pull/5882#discussion_r1457414097


##########
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/node/SCMNodeManager.java:
##########
@@ -1113,11 +1117,94 @@ public Map<String, Map<String, String>> 
getNodeStatusInfo() {
         map.put(httpsPort.getName().toString(),
                   httpsPort.getValue().toString());
       }
+      String capacity = calculateStorageCapacity(dni);
+      map.put(totalCapacity, capacity);
+      double[] storagePercentage = calculateStoragePercentage(dni);
+      double scmUsedPerc = storagePercentage[0];
+      double nonScmUsedPerc = storagePercentage[1];
+      map.put(usedSpacePercent,
+          "scmUsed: " + scmUsedPerc + "%, nonScmUsed: " + nonScmUsedPerc + 
"%");
       nodes.put(hostName, map);
     }
     return nodes;
   }
 
+  /**
+   * Calculate the storage capacity of the DataNode node.
+   * @param dni DataNode node that needs to be calculated.
+   * @return
+   */
+  public String calculateStorageCapacity(DatanodeInfo dni) {
+    long capacityByte = 0;
+    List<StorageReportProto> storageReports = dni.getStorageReports();
+    if (storageReports != null && !storageReports.isEmpty()) {
+      for (StorageReportProto storageReport : storageReports) {
+        capacityByte += storageReport.getCapacity();
+      }
+    }
+
+    double ua = capacityByte;
+    StringBuilder unit = new StringBuilder("B");
+    if (ua > 1024) {
+      ua = ua / 1024;
+      unit.replace(0, 1, "KB");
+    }
+    if (ua > 1024) {
+      ua = ua / 1024;
+      unit.replace(0, 2, "MB");
+    }
+    if (ua > 1024) {
+      ua = ua / 1024;
+      unit.replace(0, 2, "GB");
+    }
+    if (ua > 1024) {
+      ua = ua / 1024;
+      unit.replace(0, 2, "TB");
+    }
+
+    DecimalFormat decimalFormat = new DecimalFormat("#0.0");
+    decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
+    double capacity = Double.valueOf(decimalFormat.format(ua));
+    return capacity + unit.toString();
+  }
+
+  /**
+   * Calculate the storage usage percentage of a DataNode node.
+   * @param dni DataNode node that needs to be calculated.
+   * @return
+   */
+  public double[] calculateStoragePercentage(DatanodeInfo dni) {
+    double[] storagePercentage = new double[2];
+    double usedPercentage = 0;
+    double nonUsedPercentage = 0;
+    List<StorageReportProto> storageReports = dni.getStorageReports();
+    if (storageReports != null && !storageReports.isEmpty()) {
+      long capacity = 0;
+      long scmUsed = 0;
+      long remaining = 0;
+      for (StorageReportProto storageReport : storageReports) {
+        capacity += storageReport.getCapacity();
+        scmUsed += storageReport.getScmUsed();
+        remaining += storageReport.getRemaining();
+      }
+      long scmNonUsed = capacity - scmUsed - remaining;
+
+      DecimalFormat decimalFormat = new DecimalFormat("#0.00");
+      decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
+
+      double usedPerc = ((double) scmUsed / capacity) * 100;
+      usedPerc = usedPerc > 100.0 ? 100.0 : usedPerc;
+      double nonUsedPerc = ((double) scmNonUsed / capacity) * 100;
+      nonUsedPerc = nonUsedPerc > 100.0 ? 100.0 : nonUsedPerc;
+      usedPercentage = Double.valueOf(decimalFormat.format(usedPerc));
+      nonUsedPercentage = Double.valueOf(decimalFormat.format(nonUsedPerc));

Review Comment:
   I will update soon.



-- 
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