Mercurial > libervia-backend
view 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 |
line wrap: on
line source
#! /usr/bin/python # -*- coding: utf-8 -*- # jp: a SAT command line tool # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) # Copyright (C) 2003-2016 Adrien Cossa (souliane@mailoo.org) # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import base 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"] class Purge(base.CommandBase): def __init__(self, host): super(Purge, self).__init__(host, 'purge', help=_('Purge the roster from its contacts with no subscription')) def add_parser_options(self): pass def connected(self): self.need_loop = True super(Purge, 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 ask_confirmation(self, to_remove): """Ask the confirmation before removing contacts without subscription. @param to_remove (list[unicode]): list of contacts @return bool """ print "There's no subscription between profile [%s] and the following contacts:" % self.host.profile print " " + "\n ".join(to_remove) message = "REMOVE them from profile [%s]'s roster" % self.host.profile while True: res = raw_input("%s (y/N)? " % message) if not res or res.lower() == 'n': return False if res.lower() == 'y': return True def gotContacts(self, contacts): """Process the list of contacts. @param contacts(list[tuple]): list of contacts with their attributes and groups """ to_remove = [] for contact, attrs, groups in contacts: if not C.bool(attrs["from"]) and not C.bool(attrs["to"]): to_remove.append(contact) if not to_remove: print "Nothing to do - there's a from and/or to subscription(s) between profile [%s] and each of its contacts" % self.host.profile elif self.ask_confirmation(to_remove): for contact in to_remove: self.host.bridge.delContact(contact, profile_key=self.host.profile, callback=lambda dummy: None, errback=lambda failure: None) 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, Stats) def __init__(self, host): super(Roster, self).__init__(host, 'roster', use_profile=True, help=_("Manage an entity's roster"))