PageRenderTime 39ms CodeModel.GetById 14ms app.highlight 19ms RepoModel.GetById 2ms app.codeStats 0ms

/src/services/Statistics.py

http://pyaimt.googlecode.com/
Python | 72 lines | 56 code | 13 blank | 3 comment | 5 complexity | fc2a4b0b2df80a7b6c2c7aa7a6cbe4d2 MD5 | raw file
 1# Copyright 2004-2006 Daniel Henninger <jadestorm@nc.rr.com>
 2# Licensed for distribution under the GPL version 2, check COPYING for details
 3
 4import utils
 5from twisted.words.xish.domish import Element
 6import config
 7import lang
 8from debug import LogEvent, INFO, WARN, ERROR
 9import globals
10
11class Statistics:
12	def __init__(self, pytrans):
13		self.pytrans = pytrans
14		self.pytrans.adhoc.addCommand("stats", self.incomingIq, "command_Statistics")
15
16		# self.stats is indexed by a unique ID, with value being the value for that statistic
17		self.stats = {}
18		self.stats["Uptime"] = 0
19		self.stats["OnlineSessions"] = 0
20		self.stats["IncomingMessages"] = 0
21		self.stats["OutgoingMessages"] = 0
22		self.stats["TotalSessions"] = 0
23		self.stats["MaxConcurrentSessions"] = 0
24
25		self.sessionstats = {}
26
27	def sessionSetup(self, jid):
28		self.sessionstats[jid] = { } 
29		self.sessionstats[jid]['IncomingMessages'] = 0
30		self.sessionstats[jid]['OutgoingMessages'] = 0
31		self.sessionstats[jid]['Connections'] = 0
32
33	def sessionUpdate(self, jid, setting, value):
34		if not self.sessionstats.has_key(jid):
35			self.sessionSetup(jid)
36		self.sessionstats[jid][setting] += value
37
38	def incomingIq(self, el):
39		to = el.getAttribute("from")
40		ID = el.getAttribute("id")
41		ulang = utils.getLang(el)
42
43		iq = Element((None, "iq"))
44		iq.attributes["to"] = to
45		iq.attributes["from"] = config.jid
46		if ID:
47			iq.attributes["id"] = ID
48		iq.attributes["type"] = "result"
49
50		command = iq.addElement("command")
51		command.attributes["sessionid"] = self.pytrans.makeMessageID()
52		command.attributes["xmlns"] = globals.COMMANDS
53		command.attributes["status"] = "completed"
54
55		x = command.addElement("x")
56		x.attributes["xmlns"] = globals.XDATA
57		x.attributes["type"] = "result"
58
59		title = x.addElement("title")
60		title.addContent(lang.get("command_Statistics", ulang))
61
62		for key in self.stats:
63			label = lang.get("statistics_%s" % key, ulang)
64			description = lang.get("statistics_%s_Desc" % key, ulang)
65			field = x.addElement("field")
66			field.attributes["var"] = key
67			field.attributes["label"] = label
68			field.attributes["type"] = "text-single"
69			field.addElement("value").addContent(str(self.stats[key]))
70			field.addElement("desc").addContent(description)
71
72		self.pytrans.send(iq)