/src/services/Statistics.py

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