]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
pybind/mgr/mgr_util: move format_ helpers out of status module
authorSage Weil <sage@redhat.com>
Fri, 30 Nov 2018 14:28:21 +0000 (08:28 -0600)
committerSage Weil <sage@redhat.com>
Tue, 18 Dec 2018 19:30:54 +0000 (13:30 -0600)
Signed-off-by: Sage Weil <sage@redhat.com>
src/pybind/mgr/mgr_util.py [new file with mode: 0644]
src/pybind/mgr/status/module.py

diff --git a/src/pybind/mgr/mgr_util.py b/src/pybind/mgr/mgr_util.py
new file mode 100644 (file)
index 0000000..38c33d8
--- /dev/null
@@ -0,0 +1,68 @@
+
+(
+    BLACK,
+    RED,
+    GREEN,
+    YELLOW,
+    BLUE,
+    MAGENTA,
+    CYAN,
+GRAY
+) = range(8)
+
+RESET_SEQ = "\033[0m"
+COLOR_SEQ = "\033[1;%dm"
+COLOR_DARK_SEQ = "\033[0;%dm"
+BOLD_SEQ = "\033[1m"
+UNDERLINE_SEQ = "\033[4m"
+
+def colorize(msg, color, dark=False):
+    """
+    Decorate `msg` with escape sequences to give the requested color
+    """
+    return (COLOR_DARK_SEQ if dark else COLOR_SEQ) % (30 + color) \
+        + msg + RESET_SEQ
+
+def bold(msg):
+    """
+    Decorate `msg` with escape sequences to make it appear bold
+    """
+    return BOLD_SEQ + msg + RESET_SEQ
+
+def format_units(n, width, colored, decimal):
+    """
+    Format a number without units, so as to fit into `width` characters, substituting
+    an appropriate unit suffix.
+
+    Use decimal for dimensionless things, use base 2 (decimal=False) for byte sizes/rates.
+    """
+
+    factor = 1000 if decimal else 1024
+    units = [' ', 'k', 'M', 'G', 'T', 'P', 'E']
+    unit = 0
+    while len("%s" % (int(n) // (factor**unit))) > width - 1:
+        unit += 1
+
+    if unit > 0:
+        truncated_float = ("%f" % (n / (float(factor) ** unit)))[0:width - 1]
+        if truncated_float[-1] == '.':
+            truncated_float = " " + truncated_float[0:-1]
+    else:
+        truncated_float = "%{wid}d".format(wid=width-1) % n
+    formatted = "%s%s" % (truncated_float, units[unit])
+
+    if colored:
+        if n == 0:
+            color = BLACK, False
+        else:
+            color = YELLOW, False
+        return bold(colorize(formatted[0:-1], color[0], color[1])) \
+            + bold(colorize(formatted[-1], BLACK, False))
+    else:
+        return formatted
+
+def format_dimless(n, width, colored=True):
+    return format_units(n, width, colored, decimal=True)
+
+def format_bytes(n, width, colored=True):
+    return format_units(n, width, colored, decimal=False)
index bb985bb2754df4822627df76f0182c2869b6a014..c7b36ff26d6045967a348c8c3f2f24dcb480afde 100644 (file)
@@ -7,6 +7,7 @@ from collections import defaultdict
 from prettytable import PrettyTable
 import errno
 import fnmatch
+import mgr_util
 import prettytable
 import six
 
@@ -29,73 +30,6 @@ class Module(MgrModule):
         },
     ]
 
-    (
-        BLACK,
-        RED,
-        GREEN,
-        YELLOW,
-        BLUE,
-        MAGENTA,
-        CYAN,
-        GRAY
-    ) = range(8)
-
-    RESET_SEQ = "\033[0m"
-    COLOR_SEQ = "\033[1;%dm"
-    COLOR_DARK_SEQ = "\033[0;%dm"
-    BOLD_SEQ = "\033[1m"
-    UNDERLINE_SEQ = "\033[4m"
-
-    def colorize(self, msg, color, dark=False):
-        """
-        Decorate `msg` with escape sequences to give the requested color
-        """
-        return (self.COLOR_DARK_SEQ if dark else self.COLOR_SEQ) % (30 + color) \
-               + msg + self.RESET_SEQ
-
-    def bold(self, msg):
-        """
-        Decorate `msg` with escape sequences to make it appear bold
-        """
-        return self.BOLD_SEQ + msg + self.RESET_SEQ
-
-    def format_units(self, n, width, colored, decimal):
-        """
-        Format a number without units, so as to fit into `width` characters, substituting
-        an appropriate unit suffix.
-        
-        Use decimal for dimensionless things, use base 2 (decimal=False) for byte sizes/rates.
-        """
-        
-        factor = 1000 if decimal else 1024
-        units = [' ', 'k', 'M', 'G', 'T', 'P', 'E']
-        unit = 0
-        while len("%s" % (int(n) // (factor**unit))) > width - 1:
-            unit += 1
-
-        if unit > 0:
-            truncated_float = ("%f" % (n / (float(factor) ** unit)))[0:width - 1]
-            if truncated_float[-1] == '.':
-                truncated_float = " " + truncated_float[0:-1]
-        else:
-            truncated_float = "%{wid}d".format(wid=width-1) % n
-        formatted = "%s%s" % (truncated_float, units[unit])
-
-        if colored:
-            if n == 0:
-                color = self.BLACK, False
-            else:
-                color = self.YELLOW, False
-            return self.bold(self.colorize(formatted[0:-1], color[0], color[1])) \
-                + self.bold(self.colorize(formatted[-1], self.BLACK, False))
-        else:
-            return formatted
-
-    def format_dimless(self, n, width, colored=True):
-        return self.format_units(n, width, colored, decimal=True)
-    
-    def format_bytes(self, n, width, colored=True):
-        return self.format_units(n, width, colored, decimal=False)
         
     def get_latest(self, daemon_type, daemon_name, stat):
         data = self.get_counter(daemon_type, daemon_name, stat)[stat]
@@ -168,7 +102,7 @@ class Module(MgrModule):
                     activity = ""
 
                     if state == "active":
-                        activity = "Reqs: " + self.format_dimless(
+                        activity = "Reqs: " + mgr_util.format_dimless(
                             self.get_rate("mds", info['name'], "mds_server.handle_client_request"),
                             5
                         ) + "/s"
@@ -178,8 +112,8 @@ class Module(MgrModule):
                     rank_table.add_row([
                         self.bold(rank.__str__()), c_state, info['name'],
                         activity,
-                        self.format_dimless(dns, 5),
-                        self.format_dimless(inos, 5)
+                        mgr_util.format_dimless(dns, 5),
+                        mgr_util.format_dimless(inos, 5)
                     ])
 
                 else:
@@ -195,7 +129,7 @@ class Module(MgrModule):
                 inos = self.get_latest("mds", daemon_info['name'], "mds_mem.ino")
                 dns = self.get_latest("mds", daemon_info['name'], "mds_mem.dn")
 
-                activity = "Evts: " + self.format_dimless(
+                activity = "Evts: " + mgr_util.format_dimless(
                     self.get_rate("mds", daemon_info['name'], "mds_log.replayed"),
                     5
                 ) + "/s"
@@ -206,8 +140,8 @@ class Module(MgrModule):
                 rank_table.add_row([
                     "{0}-s".format(daemon_info['rank']), "standby-replay",
                     daemon_info['name'], activity,
-                    self.format_dimless(dns, 5),
-                    self.format_dimless(inos, 5)
+                    mgr_util.format_dimless(dns, 5),
+                    mgr_util.format_dimless(inos, 5)
                 ])
 
             df = self.get("df")
@@ -223,8 +157,8 @@ class Module(MgrModule):
                 stats = pool_stats[pool_id]
                 pools_table.add_row([
                     pools[pool_id]['pool_name'], pool_type,
-                    self.format_bytes(stats['bytes_used'], 5),
-                    self.format_bytes(stats['max_avail'], 5)
+                    mgr_util.format_bytes(stats['bytes_used'], 5),
+                    mgr_util.format_bytes(stats['max_avail'], 5)
                 ])
 
             output += "{0} - {1} clients\n".format(
@@ -298,13 +232,13 @@ class Module(MgrModule):
                 kb_avail = stats['kb_avail'] * 1024
 
             osd_table.add_row([osd_id, hostname,
-                               self.format_bytes(kb_used, 5),
-                               self.format_bytes(kb_avail, 5),
-                               self.format_dimless(self.get_rate("osd", osd_id.__str__(), "osd.op_w") +
+                               mgr_util.format_bytes(kb_used, 5),
+                               mgr_util.format_bytes(kb_avail, 5),
+                               mgr_util.format_dimless(self.get_rate("osd", osd_id.__str__(), "osd.op_w") +
                                self.get_rate("osd", osd_id.__str__(), "osd.op_rw"), 5),
-                               self.format_bytes(self.get_rate("osd", osd_id.__str__(), "osd.op_in_bytes"), 5),
-                               self.format_dimless(self.get_rate("osd", osd_id.__str__(), "osd.op_r"), 5),
-                               self.format_bytes(self.get_rate("osd", osd_id.__str__(), "osd.op_out_bytes"), 5),
+                               mgr_util.format_bytes(self.get_rate("osd", osd_id.__str__(), "osd.op_in_bytes"), 5),
+                               mgr_util.format_dimless(self.get_rate("osd", osd_id.__str__(), "osd.op_r"), 5),
+                               mgr_util.format_bytes(self.get_rate("osd", osd_id.__str__(), "osd.op_out_bytes"), 5),
                                ','.join(osd['state']),
                                ])