Generalize unit scaling
authorSiraaj Khandkar <siraaj@khandkar.net>
Thu, 16 Aug 2018 22:17:22 +0000 (18:17 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Thu, 16 Aug 2018 22:17:22 +0000 (18:17 -0400)
bin/khatus_bar

index 2b294b7..1b9b4d4 100755 (executable)
@@ -9,6 +9,21 @@ BEGIN {
     _total_to_diff["khatus_sensor_net_addr_io", "bytes_written"  ] = 1
     _total_to_diff["khatus_sensor_disk_io"    , "sectors_read"   ] = 1
     _total_to_diff["khatus_sensor_disk_io"    , "sectors_written"] = 1
+
+    # (x * y) / z = x * w
+    #   ==> w = y / z
+    # (x * bytes_per_sector) / bytes_per_mb = x * scaling_factor
+    #   ==> scaling_factor = bytes_per_sector / bytes_per_mb
+    bytes_per_sector = 512
+    bytes_per_mb     = 1024 * 1024
+    _scale["khatus_sensor_disk_io", "sectors_written"] = bytes_per_sector / bytes_per_mb
+    _scale["khatus_sensor_disk_io", "sectors_read"   ] = bytes_per_sector / bytes_per_mb
+    # (x / y) = x * z
+    #   ==> z = 1 / y
+    # x / bytes_per_mb = x * scaling_factor
+    #   ==> scaling_factor = 1 / bytes_per_mb
+    _scale["khatus_sensor_net_addr_io", "bytes_written"] = 1 / bytes_per_mb
+    _scale["khatus_sensor_net_addr_io", "bytes_read"   ] = 1 / bytes_per_mb
 }
 
 # -----------------------------------------------------------------------------
@@ -38,7 +53,9 @@ function Data_update(    src, key, val, len_line, len_head, len_val, time) {
     len_val  = len_line - len_head
     val = substr($0, len_head + 1, len_val)
 
-    Data[src, key] = Data_maybe_total_to_diff(src, key, val)
+    val = Data_maybe_total_to_diff(src, key, val)
+    val = Data_maybe_scale(src, key, val)
+    Data[src, key] = val
     time = Data_get_time()
     M_time[src, key] = time
 
@@ -97,6 +114,15 @@ function Data_maybe_total_to_diff(src, key, val,    key_parts) {
     }
 }
 
+function Data_maybe_scale(src, key, val,    key_parts) {
+    split(key, key_parts, Kfs)
+    if ((src SUBSEP key_parts[1]) in _scale) {
+        return val * _scale[src, key_parts[1]]
+    } else {
+        return val
+    }
+}
+
 # -----------------------------------------------------------------------------
 # Status bar
 # -----------------------------------------------------------------------------
@@ -162,22 +188,11 @@ function make_status_cpu(    l, t, f) {
     return sprintf("C=[%s %s°C %srpm]", l, t, f)
 }
 
-function make_status_disk(    bytes_per_sector, bytes_per_mb, w, w_str, r,
-                              r_str, u_str) {
-    bytes_per_sector = 512
-    bytes_per_mb     = 1024 * 1024
-
-    u_str = Data_get_fmt_def("khatus_sensor_disk_space", "disk_usage_percentage", 10, "%s")
-    Data_get(w, "khatus_sensor_disk_io", "sectors_written", 5)
-    Data_get(r, "khatus_sensor_disk_io", "sectors_read"   , 5)
-
-    # Unit conversion looks like a good candidate for a generic transform
-    w_str = w["is_expired"] ? "--" :\
-      sprintf("%0.3f", (w["value"] * bytes_per_sector) / bytes_per_mb)
-    r_str = r["is_expired"] ? "--" :\
-      sprintf("%0.3f", (r["value"] * bytes_per_sector) / bytes_per_mb)
-
-    return sprintf("D=[%s%% %s▲ %s▼]", u_str, w_str, r_str)
+function make_status_disk(    u, w, r) {
+    u = Data_get_fmt_def("khatus_sensor_disk_space", "disk_usage_percentage", 10, "%s")
+    w = Data_get_fmt_def("khatus_sensor_disk_io"   , "sectors_written"      ,  5, "%0.3f")
+    r = Data_get_fmt_def("khatus_sensor_disk_io"   , "sectors_read"         ,  5, "%0.3f")
+    return sprintf("D=[%s%% %s▲ %s▼]", u, w, r)
 }
 
 function make_status_net(    \
@@ -185,55 +200,37 @@ function make_status_net(    \
     net_interfaces_to_show, \
     sensor_io, \
     sensor_wi, \
-    out, \
-    sep, \
     i, \
     interface, \
     label, \
+    wifi, \
     addr, \
     w, \
     r, \
-    w_str, \
-    r_str, \
-    bytes_per_mb, \
     io_stat, \
-    wifi \
+    out, \
+    sep \
 ) {
     number_of_net_interfaces_to_show = \
         split(opt_net_interfaces_to_show, net_interfaces_to_show, ",")
-
     sensor_io = "khatus_sensor_net_addr_io"
     sensor_wi = "khatus_sensor_net_wifi_status"
-
     out = ""
     sep = ""
     for (i = number_of_net_interfaces_to_show; i > 0; i--) {
         interface = net_interfaces_to_show[i]
         label = substr(interface, 1, 1)
-
-        Data_get(addr, sensor_io, "addr"          Kfs interface, 5)
-        Data_get(w   , sensor_io, "bytes_written" Kfs interface, 5)
-        Data_get(r   , sensor_io, "bytes_read"    Kfs interface, 5)
-
-        if (!addr["is_expired"] && addr["value"]) {
-            # TODO: Generalize unit conversion at Data_update
-            bytes_per_mb = 1024 * 1024
-            w_str = w["is_expired"] ? "--" : sprintf("%0.3f", w["value"] / bytes_per_mb)
-            r_str = r["is_expired"] ? "--" : sprintf("%0.3f", r["value"] / bytes_per_mb)
-            io_stat = sprintf("%s▲ %s▼", w_str, r_str)
-        } else {
-            io_stat = "--"
-        }
-
         if (interface ~ "^w") {
             wifi = Data_get_fmt_def(sensor_wi, "status" Kfs interface, 10, "%s")
             label = label ":" wifi
         }
-
+        addr = Data_get_fmt_def(sensor_io, "addr"          Kfs interface, 5, "%s", "")
+        w    = Data_get_fmt_def(sensor_io, "bytes_written" Kfs interface, 5, "%0.3f")
+        r    = Data_get_fmt_def(sensor_io, "bytes_read"    Kfs interface, 5, "%0.3f")
+        io_stat = addr ? sprintf("%s▲ %s▼", w, r) : "--"
         out = out sep label ":" io_stat
         sep = " "
     }
-
     return sprintf("N[%s]", out)
 }
 
This page took 0.037749 seconds and 4 git commands to generate.