[Git][NTPsec/ntpsec][master] Implement mru sort=addr, tweak sort comments

Hal Murray gitlab at mg.gitlab.com
Thu Dec 22 10:22:07 UTC 2016


Hal Murray pushed to branch master at NTPsec / ntpsec


Commits:
7802ce57 by Hal Murray at 2016-12-22T02:21:27-08:00
Implement mru sort=addr, tweak sort comments

- - - - -


2 changed files:

- ntpclients/ntpq
- pylib/packet.py


Changes:

=====================================
ntpclients/ntpq
=====================================
--- a/ntpclients/ntpq
+++ b/ntpclients/ntpq
@@ -1223,7 +1223,8 @@ usage: config_from_file <configuration filename>
                     delta1 = time.time() - self.session.start
                     self.say(ntp.util.MRUSummary.header + "\n")
                     self.say(("=" * ntp.util.MRUSummary.width) + "\n")
-                    # reversed to put most recent entries at the top.
+                    # reversed puts most recent entries at the top if no sort=
+                    # see sort comments in pylib/packet.py
                     for entry in reversed(span.entries):
                         self.say(formatter.summary(entry) + "\n")
                     self.say("# Collected %d slots in %.3f seconds\n" \


=====================================
pylib/packet.py
=====================================
--- a/pylib/packet.py
+++ b/pylib/packet.py
@@ -674,6 +674,23 @@ class MRUEntry:
         last = ntp.ntpc.lfptofloat(self.last)
         first = ntp.ntpc.lfptofloat(self.first)
         return (last - first) / self.ct
+    def sortaddr(self):
+        addr = self.addr
+        if addr[0] == '[':
+            # IPv6   [n:n:n::n:n]:sock
+            # or     [n:n:n::n:n%x]:sock
+            addr = addr[1:addr.find(']')]
+            pct = addr.find('%')
+            if pct > 0:
+                # <addr>%<n> for local IPv6 address on interface n
+                addr = addr[:pct]
+            return socket.inet_pton(socket.AF_INET6, addr)
+        else:
+            # IPv4   a.b.c.d:sock
+            addr = addr[:addr.find(':')]
+            # prefix with 0s so IPv6 sorts after IPv4
+            # Need 16 rather than 12 to catch ::1
+            return '\0'*16 + socket.inet_pton(socket.AF_INET, addr)
     def __repr__(self):
         return "<MRUentry: " + repr(self.__dict__)[1:-1] + ">"
 
@@ -1221,22 +1238,26 @@ class ControlSession:
             if "sort" in variables:
                 sortkey = variables["sort"]
                 del variables["sort"]
-                # FIXME: implement sorting by address, in case anyone cares
-                # Slots are printed in reverse order so this is backwards.
-                # That avoids a sort in the normal case.
-                # Note lstint is backwards since we really sort on now-last
+                # Slots are retrieved oldest first.
+                # Slots are printed in reverse so the normal/no-sort
+                #  case prints youngest first.
+                # That means sort functions are backwards.
+                # Note lstint is backwards again (aka normal/forward)
+                #  since we really want to sort on now-last rather than last.
                 sortdict = {
-                "lstint" : lambda e: e.last,		# lstint ascending
-                "-lstint" : lambda e: -e.last,		# lstint descending
+                "lstint" : lambda e: \
+                        ntp.ntpc.lfptofloat(e.last),	# lstint ascending
+                "-lstint" : lambda e: \
+                        -ntp.ntpc.lfptofloat(e.last),	# lstint descending
                 "avgint" : lambda e: -e.avgint(),	# avgint ascending
                 "-avgint" : lambda e: e.avgint(),	# avgint descending
-                "addr" : None,			# IPv4 asc. then IPv6 asc.
-                "-addr" : None,			# IPv6 desc. then IPv4 desc.
-                "count" : lambda e: -e.ct,	# hit count ascending
-                "-count": lambda e: e.ct,	# hit count descending
+                "addr" : lambda e: e.sortaddr(),    # IPv4 asc. then IPv6 asc.
+                "-addr" : lambda e: e.sortaddr(),   # IPv6 desc. then IPv4 desc.
+                "count" : lambda e: -e.ct,          # hit count ascending
+                "-count": lambda e: e.ct,           # hit count descending
                 }
                 if sortkey == "lstint":
-                    sortkey = None
+                    sortkey = None # normal/default case, no need to sort
                 if sortkey is not None:
                     sorter = sortdict.get(sortkey)
                     if sorter == None:
@@ -1433,6 +1454,9 @@ class ControlSession:
         # Sort for presentation
         if sorter:
             span.entries.sort(key=sorter)
+            if sortkey == "addr":
+                # I don't know how to feed a minus sign to text sort
+                span.entries.reverse()
 
         return span
 



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/7802ce57030329989a150be4a064911b3a9c17c7
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20161222/8bf952af/attachment.html>


More information about the vc mailing list