~ubuntu-branches/ubuntu/utopic/gozerbot/utopic

« back to all changes in this revision

Viewing changes to build/lib/gplugs/lag.py

  • Committer: Package Import Robot
  • Author(s): Jeremy Malcolm
  • Date: 2012-04-03 21:58:28 UTC
  • mfrom: (3.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20120403215828-6mik0tzug5na93la
Tags: 0.99.1-2
* Removes logfiles on purge (Closes: #668767)
* Reverted location of installed files back to /usr/lib/gozerbot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# plugs/lag.py
 
2
#
 
3
# (c) Wijnand 'tehmaze' Modderman - http://tehmaze.com
 
4
# BSD License
 
5
 
 
6
""" gozerbot lag meter """
 
7
 
 
8
from gozerbot.callbacks import callbacks
 
9
from gozerbot.commands import cmnds
 
10
from gozerbot.examples import examples 
 
11
from gozerbot.fleet import fleet
 
12
from gozerbot.periodical import interval, periodical
 
13
from gozerbot.plughelp import plughelp
 
14
from gozerbot.tests import tests
 
15
 
 
16
import time
 
17
 
 
18
plughelp.add('lag', 'lag measuring')
 
19
 
 
20
lag_wait = 300
 
21
 
 
22
class Lagmeter:
 
23
 
 
24
    """ lag meter class """
 
25
 
 
26
    def __init__(self):
 
27
        self.names = []
 
28
        self.sent  = {}
 
29
        self.lag   = {}
 
30
        self.run   = True
 
31
 
 
32
    def update_names(self):
 
33
        """ grab all names from irc bots in the fleet """ 
 
34
        self.names = [bot.name for bot in fleet.bots if bot.type == 'irc']
 
35
 
 
36
    def getlag(self, name):
 
37
        """ get lag of <botname> """
 
38
        if self.lag.has_key(name):
 
39
            return self.lag[name]
 
40
        else:
 
41
            return False
 
42
 
 
43
    def measure(self, name):
 
44
        """ measure lag of <botname> """
 
45
        bot = fleet.byname(name)
 
46
        if not bot:
 
47
            return
 
48
        if bot.connectok.isSet() and bot.connectok:
 
49
            bot.sendraw('PING :LAG %f' % time.time())
 
50
 
 
51
    def recieved(self, name, sent):
 
52
        """ function called on received PONG """
 
53
        self.lag[name] = time.time() - sent
 
54
 
 
55
    @interval(lag_wait)
 
56
    def loop(self):
 
57
        """ lag meter loop """
 
58
        fleet.startok.wait()
 
59
        self.update_names()
 
60
        for name in self.names:
 
61
            self.measure(name)
 
62
 
 
63
lagmeters = Lagmeter()
 
64
 
 
65
def init():
 
66
    """ start the lag meter loop """
 
67
    lagmeters.loop()
 
68
    return 1
 
69
 
 
70
def shutdown():
 
71
    """ kill all lag meter periodical jobs """ 
 
72
    periodical.kill()
 
73
 
 
74
def handle_lag(bot, ievent): 
 
75
    """ show lag of bot the command is given on """
 
76
    lag = lagmeters.getlag(bot.name)
 
77
    if lag == False:
 
78
        ievent.reply('no lag metered')
 
79
    else:
 
80
        ievent.reply('lag is %f seconds' % lag)
 
81
 
 
82
def connectedcb(bot, ievent):
 
83
    """ callback to be called when bot is connected """
 
84
    lagmeters.update_names()
 
85
    lagmeters.measure(bot.name)
 
86
 
 
87
def pongcb(bot, ievent):
 
88
    """ PONG callback """
 
89
    if len(ievent.arguments) == 3:
 
90
        if ievent.arguments[1].lstrip(':') == 'LAG':
 
91
            try:
 
92
                sent = float(ievent.arguments[2])
 
93
                lagmeters.recieved(bot.name, sent) 
 
94
            except ValueError:
 
95
                pass
 
96
 
 
97
cmnds.add('lag', handle_lag, ['USER'])
 
98
examples.add('lag', 'shows the current lag', 'lag')
 
99
tests.add('lag')
 
100
callbacks.add('001', connectedcb)
 
101
callbacks.add('PONG', pongcb)