comparison frontends/src/jp/cmd_roster.py @ 1798:c5d58387d031

jp (roster stats): add some extra information
author souliane <souliane@mailoo.org>
date Wed, 13 Jan 2016 19:46:43 +0100
parents 40cda0c08727
children f1f4775f8cc0
comparison
equal deleted inserted replaced
1797:40cda0c08727 1798:c5d58387d031
45 45
46 def error(self, failure): 46 def error(self, failure):
47 print (_("Error while retrieving the contacts [%s]") % failure) 47 print (_("Error while retrieving the contacts [%s]") % failure)
48 self.host.quit(1) 48 self.host.quit(1)
49 49
50 def ask_confirmation(self, none, no_from, no_to): 50 def ask_confirmation(self, no_sub, no_from, no_to):
51 """Ask the confirmation before removing contacts. 51 """Ask the confirmation before removing contacts.
52 52
53 @param none (list[unicode]): list of contacts with no subscription 53 @param no_sub (list[unicode]): list of contacts with no subscription
54 @param no_from (list[unicode]): list of contacts with no 'from' subscription 54 @param no_from (list[unicode]): list of contacts with no 'from' subscription
55 @param no_to (list[unicode]): list of contacts with no 'to' subscription 55 @param no_to (list[unicode]): list of contacts with no 'to' subscription
56 @return bool 56 @return bool
57 """ 57 """
58 if none: 58 if no_sub:
59 print "There's no subscription between profile [%s] and the following contacts:" % self.host.profile 59 print "There's no subscription between profile [%s] and the following contacts:" % self.host.profile
60 print " " + "\n ".join(none) 60 print " " + "\n ".join(no_sub)
61 if no_from: 61 if no_from:
62 print "There's no 'from' subscription between profile [%s] and the following contacts:" % self.host.profile 62 print "There's no 'from' subscription between profile [%s] and the following contacts:" % self.host.profile
63 print " " + "\n ".join(no_from) 63 print " " + "\n ".join(no_from)
64 if no_to: 64 if no_to:
65 print "There's no 'to' subscription between profile [%s] and the following contacts:" % self.host.profile 65 print "There's no 'to' subscription between profile [%s] and the following contacts:" % self.host.profile
75 def gotContacts(self, contacts): 75 def gotContacts(self, contacts):
76 """Process the list of contacts. 76 """Process the list of contacts.
77 77
78 @param contacts(list[tuple]): list of contacts with their attributes and groups 78 @param contacts(list[tuple]): list of contacts with their attributes and groups
79 """ 79 """
80 none, no_from, no_to = [], [], [] 80 no_sub, no_from, no_to = [], [], []
81 for contact, attrs, groups in contacts: 81 for contact, attrs, groups in contacts:
82 from_, to = C.bool(attrs["from"]), C.bool(attrs["to"]) 82 from_, to = C.bool(attrs["from"]), C.bool(attrs["to"])
83 if not from_: 83 if not from_:
84 if not to: 84 if not to:
85 none.append(contact) 85 no_sub.append(contact)
86 elif self.args.no_from: 86 elif self.args.no_from:
87 no_from.append(contact) 87 no_from.append(contact)
88 elif not to and self.args.no_to: 88 elif not to and self.args.no_to:
89 no_to.append(contact) 89 no_to.append(contact)
90 if not none and not no_from and not no_to: 90 if not no_sub and not no_from and not no_to:
91 print "Nothing to do - there's a from and/or to subscription(s) between profile [%s] and each of its contacts" % self.host.profile 91 print "Nothing to do - there's a from and/or to subscription(s) between profile [%s] and each of its contacts" % self.host.profile
92 elif self.ask_confirmation(none, no_from, no_to): 92 elif self.ask_confirmation(no_sub, no_from, no_to):
93 for contact in none + no_from + no_to: 93 for contact in no_sub + no_from + no_to:
94 self.host.bridge.delContact(contact, profile_key=self.host.profile, callback=lambda dummy: None, errback=lambda failure: None) 94 self.host.bridge.delContact(contact, profile_key=self.host.profile, callback=lambda dummy: None, errback=lambda failure: None)
95 self.host.quit() 95 self.host.quit()
96 96
97 97
98 class Stats(base.CommandBase): 98 class Stats(base.CommandBase):
99 99
100 def __init__(self, host): 100 def __init__(self, host):
101 super(Stats, self).__init__(host, 'stats', help=_('Display show statistics about a roster')) 101 super(Stats, self).__init__(host, 'stats', help=_('Show statistics about a roster'))
102 102
103 def add_parser_options(self): 103 def add_parser_options(self):
104 pass 104 pass
105 105
106 def connected(self): 106 def connected(self):
116 """Process the list of contacts. 116 """Process the list of contacts.
117 117
118 @param contacts(list[tuple]): list of contacts with their attributes and groups 118 @param contacts(list[tuple]): list of contacts with their attributes and groups
119 """ 119 """
120 hosts = {} 120 hosts = {}
121 no_subscription = 0 121 unique_groups = set()
122 no_from_subscription = 0 122 no_sub, no_from, no_to, no_group, total_group_subscription = 0, 0, 0, 0, 0
123 no_to_subscription = 0
124 no_group = 0
125 total_group_subscription = 0
126 for contact, attrs, groups in contacts: 123 for contact, attrs, groups in contacts:
127 from_, to = C.bool(attrs["from"]), C.bool(attrs["to"]) 124 from_, to = C.bool(attrs["from"]), C.bool(attrs["to"])
128 if not from_: 125 if not from_:
129 if not to: 126 if not to:
130 no_subscription += 1 127 no_sub += 1
131 else: 128 else:
132 no_from_subscription += 1 129 no_from += 1
133 elif not to: 130 elif not to:
134 no_to_subscription += 1 131 no_to += 1
135 host = jid.JID(contact).host 132 host = jid.JID(contact).host
136 hosts.setdefault(host, 0) 133 hosts.setdefault(host, 0)
137 hosts[host] += 1 134 hosts[host] += 1
138 if groups: 135 if groups:
136 unique_groups.update(groups)
139 total_group_subscription += len(groups) 137 total_group_subscription += len(groups)
140 if not groups: 138 if not groups:
141 no_group += 1 139 no_group += 1
142 hosts = OrderedDict(sorted(hosts.items(), key=lambda item:-item[1])) 140 hosts = OrderedDict(sorted(hosts.items(), key=lambda item:-item[1]))
143 141
144 print 142 print
145 print "Total number of contacts: %d" % len(contacts) 143 print "Total number of contacts: %d" % len(contacts)
144 print "Number of different hosts: %d" % len(hosts)
146 print 145 print
147 for host, count in hosts.iteritems(): 146 for host, count in hosts.iteritems():
148 print "Contacts on {host}: {count} ({rate:.1f}%)".format(host=host, count=count, rate=100 * float(count) / len(contacts)) 147 print "Contacts on {host}: {count} ({rate:.1f}%)".format(host=host, count=count, rate=100 * float(count) / len(contacts))
149 print 148 print
150 print "Contacts with no 'from' subscription: %d" % no_from_subscription 149 print "Contacts with no 'from' subscription: %d" % no_from
151 print "Contacts with no 'to' subscription: %d" % no_to_subscription 150 print "Contacts with no 'to' subscription: %d" % no_to
152 print "Contacts with no subscription at all: %d" % no_subscription 151 print "Contacts with no subscription at all: %d" % no_sub
153 print 152 print
153 print "Total number of groups: %d" % len(unique_groups)
154 print "Average contacts per group: %.1f" % (float(total_group_subscription) / len(unique_groups))
155 print "Average groups' subscriptions per contact: %.1f" % (float(total_group_subscription) / len(contacts))
154 print "Contacts not assigned to any group: %d" % no_group 156 print "Contacts not assigned to any group: %d" % no_group
155 print "Average groups' subscriptions per contact: %.1f" % (float(total_group_subscription) / len(contacts))
156 self.host.quit() 157 self.host.quit()
157 158
158 159
159 class Roster(base.CommandBase): 160 class Roster(base.CommandBase):
160 subcommands = (Purge, Stats) 161 subcommands = (Purge, Stats)