changeset 1796:314d2eb7fbaa

jp: add command "roster stats"
author souliane <souliane@mailoo.org>
date Wed, 13 Jan 2016 18:54:58 +0100
parents c38233e12c69
children 40cda0c08727
files frontends/src/jp/cmd_roster.py
diffstat 1 files changed, 65 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/jp/cmd_roster.py	Wed Jan 13 18:55:30 2016 +0100
+++ b/frontends/src/jp/cmd_roster.py	Wed Jan 13 18:54:58 2016 +0100
@@ -22,6 +22,9 @@
 from sat_frontends.jp.constants import Const as C
 from sat.core.i18n import _
 
+from twisted.words.protocols.jabber import jid
+from collections import OrderedDict
+
 __commands__ = ["Roster"]
 
 
@@ -76,8 +79,69 @@
         self.host.quit()
 
 
+class Stats(base.CommandBase):
+
+    def __init__(self, host):
+        super(Stats, self).__init__(host, 'stats', help=_('Display show statistics about a roster'))
+
+    def add_parser_options(self):
+        pass
+
+    def connected(self):
+        self.need_loop = True
+        super(Stats, self).connected()
+        self.host.bridge.getContacts(profile_key=self.host.profile, callback=self.gotContacts, errback=self.error)
+
+    def error(self, failure):
+        print (_("Error while retrieving the contacts [%s]") % failure)
+        self.host.quit(1)
+
+    def gotContacts(self, contacts):
+        """Process the list of contacts.
+
+        @param contacts(list[tuple]): list of contacts with their attributes and groups
+        """
+        hosts = {}
+        no_subscription = 0
+        no_from_subscription = 0
+        no_to_subscription = 0
+        no_group = 0
+        total_group_subscription = 0
+        for contact, attrs, groups in contacts:
+            from_, to = C.bool(attrs["from"]), C.bool(attrs["to"])
+            if not from_:
+                if not to:
+                    no_subscription += 1
+                else:
+                    no_from_subscription += 1
+            elif not to:
+                no_to_subscription += 1
+            host = jid.JID(contact).host
+            hosts.setdefault(host, 0)
+            hosts[host] += 1
+            if groups:
+                total_group_subscription += len(groups)
+            if not groups:
+                no_group += 1
+        hosts = OrderedDict(sorted(hosts.items(), key=lambda item:-item[1]))
+
+        print
+        print "Total number of contacts: %d" % len(contacts)
+        print
+        for host, count in hosts.iteritems():
+            print "Contacts on {host}: {count} ({rate:.1f}%)".format(host=host, count=count, rate=100 * float(count) / len(contacts))
+        print
+        print "Contacts with no 'from' subscription: %d" % no_from_subscription
+        print "Contacts with no 'to' subscription: %d" % no_to_subscription
+        print "Contacts with no subscription at all: %d" % no_subscription
+        print
+        print "Contacts not assigned to any group: %d" % no_group
+        print "Average groups' subscriptions per contact: %.1f" % (float(total_group_subscription) / len(contacts))
+        self.host.quit()
+
+
 class Roster(base.CommandBase):
-    subcommands = (Purge,)
+    subcommands = (Purge, Stats)
 
     def __init__(self, host):
         super(Roster, self).__init__(host, 'roster', use_profile=True, help=_("Manage an entity's roster"))