4
0

ffpb_netstatus.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # -*- coding: utf-8 -*-
  2. from __future__ import print_function
  3. import willie
  4. import json
  5. import shelve
  6. import time
  7. import urllib2
  8. from ffpb import pretty_date
  9. from batcave import BatcaveClient
  10. __batcave = None
  11. highscores = None
  12. def setup(bot):
  13. """Called by willie upon loading this plugin."""
  14. global __batcave, highscores
  15. __batcave = BatcaveClient(bot.config.ffpb.batcave_url)
  16. # load highscores from disk
  17. highscores = shelve.open('highscoredata', writeback=True)
  18. if not 'nodes' in highscores:
  19. highscores['nodes'] = 0
  20. highscores['nodes_ts'] = time.time()
  21. if not 'clients' in highscores:
  22. highscores['clients'] = 0
  23. highscores['clients_ts'] = time.time()
  24. def shutdown(bot):
  25. """Called by willie upon loading this plugin."""
  26. global highscores
  27. # store highscores
  28. if not highscores is None:
  29. highscores.sync()
  30. highscores.close()
  31. highscores = None
  32. @willie.module.interval(5)
  33. def ffpb_get_stats(bot):
  34. """Fetch current statistics, if the highscore changes signal this."""
  35. status = __batcave.get_status()
  36. if status is None:
  37. bot.say('Yikes, offenbar ist das allwissende Auge gerade schlafen.')
  38. return
  39. bot.memory['ffpb_stats'] = status
  40. (nodes_active, clients_count) = \
  41. (status['nodes_active'], status['clients_unique'])
  42. highscore_changed = False
  43. if nodes_active > highscores['nodes']:
  44. highscores['nodes'] = nodes_active
  45. highscores['nodes_ts'] = time.time()
  46. highscore_changed = True
  47. if clients_count > highscores['clients']:
  48. highscores['clients'] = clients_count
  49. highscores['clients_ts'] = time.time()
  50. highscore_changed = True
  51. if highscore_changed:
  52. print('HIGHSCORE changed: {0} nodes ({1}), {2} clients ({3})'.format(
  53. highscores['nodes'],
  54. highscores['nodes_ts'],
  55. highscores['clients'],
  56. highscores['clients_ts'],
  57. ))
  58. if not bot.config.ffpb.msg_target is None:
  59. action_msg = 'notiert sich den neuen Highscore: {0} Knoten ({1}), {2} Clients ({3})'
  60. action_target = bot.config.ffpb.msg_target
  61. if not bot.config.ffpb.msg_target_public is None:
  62. action_target = bot.config.ffpb.msg_target_public
  63. bot.msg(action_target, '\x01ACTION %s\x01' % action_msg.format(
  64. highscores['nodes'], pretty_date(int(highscores['nodes_ts'])),
  65. highscores['clients'], pretty_date(int(highscores['clients_ts'])),
  66. ))
  67. @willie.module.commands('status')
  68. def ffpb_status(bot, trigger):
  69. """State of the network: count of nodes + clients"""
  70. stats = bot.memory.get('ffpb_stats')
  71. if stats is None:
  72. bot.say('Uff, kein Plan wo der Zettel ist. Fragst du später nochmal?')
  73. return
  74. bot.say('Es sind {0} Knoten und ca. {1} Clients online.'.format(
  75. stats["nodes_active"], stats["clients_unique"]))
  76. @willie.module.commands('raw-status')
  77. def ffpb_batcave_status(bot, trigger):
  78. """State as given by BATCAVE."""
  79. status = __batcave.get_status()
  80. bot.say('Status: ' + str(json.dumps(status))[1:-1])
  81. @willie.module.commands('highscore')
  82. def ffpb_highscore(bot, trigger):
  83. """Print current highscores (nodes + clients)."""
  84. bot.say('Highscore: {0} Knoten ({1}), {2} Clients ({3})'.format(
  85. highscores['nodes'], pretty_date(int(highscores['nodes_ts'])),
  86. highscores['clients'], pretty_date(int(highscores['clients_ts']))))
  87. @willie.module.commands('rollout-status')
  88. def ffpb_rolloutstatus(bot, trigger):
  89. """Display statistic on how many nodes have installed which firmware."""
  90. # initialize results dictionary
  91. result = {}
  92. skipped = 0
  93. # inform users about changed command parameters
  94. if not trigger.group(2) is None:
  95. bot.reply('Dieses Kommando nimmt keinen Parameter mehr an.')
  96. return
  97. nodes = __batcave.get_nodes()
  98. # check each node in ALFRED data
  99. for item in nodes:
  100. release = item.get('firmware')
  101. branch = item.get('autoupdater')
  102. enabled = branch != 'off'
  103. if release is None or branch is None:
  104. skipped += 1
  105. continue
  106. if not release in result or result[release] is None:
  107. result[release] = {'stable': None, 'testing': None, }
  108. if not branch in result[release] or result[release][branch] is None:
  109. result[release][branch] = {'auto': 0, 'manual': 0, 'total': 0, }
  110. result[release][branch]['total'] += 1
  111. mode = 'auto' if enabled else 'manual'
  112. result[release][branch][mode] += 1
  113. # respond to user
  114. releases = sorted([x for x in result])
  115. for release in releases:
  116. output = 'Rollout von \'{0}\':'.format(release)
  117. branches = sorted([x for x in result[release]])
  118. first = True
  119. for branch in branches:
  120. item = result[release][branch]
  121. if item is None:
  122. continue
  123. if not first:
  124. output += ','
  125. first = False
  126. auto_count = item['auto']
  127. manual_count = item['manual']
  128. output += ' {1} {0}'.format(branch, auto_count)
  129. if manual_count > 0:
  130. output += ' (+{0} manuell)'.format(manual_count)
  131. bot.say(output)
  132. # output count of nodes for which the autoupdater's branch and/or
  133. # firmware version could not be retrieved
  134. if skipped > 0:
  135. bot.say('plus {0} Knoten mit unklarem Status'.format(skipped))
  136. @willie.module.commands('providers')
  137. def ffpb_providers(bot, trigger):
  138. """Fetch the top 5 providers from BATCAVE."""
  139. providers = __batcave.get_providers()
  140. providers.sort(key=lambda x: x['count'], reverse=True)
  141. top5 = providers[:5]
  142. top5 = ['{0} ({1:.0f}%)'.format(x['name'], x['percentage']) for x in top5]
  143. bot.say('Unsere Top 5 Provider: ' + ', '.join(top5))