# HG changeset patch # User souliane # Date 1452707698 -3600 # Node ID 314d2eb7fbaaf9f03a35eb949dab9cde2dd6e3fc # Parent c38233e12c69fcc251493740f058d6a570a4e648 jp: add command "roster stats" diff -r c38233e12c69 -r 314d2eb7fbaa frontends/src/jp/cmd_roster.py --- 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"))