comparison frontends/src/jp/cmd_roster.py @ 1796:314d2eb7fbaa

jp: add command "roster stats"
author souliane <souliane@mailoo.org>
date Wed, 13 Jan 2016 18:54:58 +0100
parents f39ca2832774
children 40cda0c08727
comparison
equal deleted inserted replaced
1795:c38233e12c69 1796:314d2eb7fbaa
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. 19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 20
21 import base 21 import base
22 from sat_frontends.jp.constants import Const as C 22 from sat_frontends.jp.constants import Const as C
23 from sat.core.i18n import _ 23 from sat.core.i18n import _
24
25 from twisted.words.protocols.jabber import jid
26 from collections import OrderedDict
24 27
25 __commands__ = ["Roster"] 28 __commands__ = ["Roster"]
26 29
27 30
28 31
74 for contact in to_remove: 77 for contact in to_remove:
75 self.host.bridge.delContact(contact, profile_key=self.host.profile, callback=lambda dummy: None, errback=lambda failure: None) 78 self.host.bridge.delContact(contact, profile_key=self.host.profile, callback=lambda dummy: None, errback=lambda failure: None)
76 self.host.quit() 79 self.host.quit()
77 80
78 81
82 class Stats(base.CommandBase):
83
84 def __init__(self, host):
85 super(Stats, self).__init__(host, 'stats', help=_('Display show statistics about a roster'))
86
87 def add_parser_options(self):
88 pass
89
90 def connected(self):
91 self.need_loop = True
92 super(Stats, self).connected()
93 self.host.bridge.getContacts(profile_key=self.host.profile, callback=self.gotContacts, errback=self.error)
94
95 def error(self, failure):
96 print (_("Error while retrieving the contacts [%s]") % failure)
97 self.host.quit(1)
98
99 def gotContacts(self, contacts):
100 """Process the list of contacts.
101
102 @param contacts(list[tuple]): list of contacts with their attributes and groups
103 """
104 hosts = {}
105 no_subscription = 0
106 no_from_subscription = 0
107 no_to_subscription = 0
108 no_group = 0
109 total_group_subscription = 0
110 for contact, attrs, groups in contacts:
111 from_, to = C.bool(attrs["from"]), C.bool(attrs["to"])
112 if not from_:
113 if not to:
114 no_subscription += 1
115 else:
116 no_from_subscription += 1
117 elif not to:
118 no_to_subscription += 1
119 host = jid.JID(contact).host
120 hosts.setdefault(host, 0)
121 hosts[host] += 1
122 if groups:
123 total_group_subscription += len(groups)
124 if not groups:
125 no_group += 1
126 hosts = OrderedDict(sorted(hosts.items(), key=lambda item:-item[1]))
127
128 print
129 print "Total number of contacts: %d" % len(contacts)
130 print
131 for host, count in hosts.iteritems():
132 print "Contacts on {host}: {count} ({rate:.1f}%)".format(host=host, count=count, rate=100 * float(count) / len(contacts))
133 print
134 print "Contacts with no 'from' subscription: %d" % no_from_subscription
135 print "Contacts with no 'to' subscription: %d" % no_to_subscription
136 print "Contacts with no subscription at all: %d" % no_subscription
137 print
138 print "Contacts not assigned to any group: %d" % no_group
139 print "Average groups' subscriptions per contact: %.1f" % (float(total_group_subscription) / len(contacts))
140 self.host.quit()
141
142
79 class Roster(base.CommandBase): 143 class Roster(base.CommandBase):
80 subcommands = (Purge,) 144 subcommands = (Purge, Stats)
81 145
82 def __init__(self, host): 146 def __init__(self, host):
83 super(Roster, self).__init__(host, 'roster', use_profile=True, help=_("Manage an entity's roster")) 147 super(Roster, self).__init__(host, 'roster', use_profile=True, help=_("Manage an entity's roster"))