ffpb_netstatus.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. print('Failed to fetch BATCAVE status.')
  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. if nodes is None:
  99. bot.reply('Hmpf, ich kriege gerade keine Infos. Das ist doch Mist so.')
  100. return
  101. # check each node in ALFRED data
  102. for item in nodes:
  103. release = item.get('firmware')
  104. branch = item.get('autoupdater')
  105. enabled = branch != 'off'
  106. if release is None or branch is None:
  107. skipped += 1
  108. continue
  109. if not release in result or result[release] is None:
  110. result[release] = {'stable': None, 'testing': None, }
  111. if not branch in result[release] or result[release][branch] is None:
  112. result[release][branch] = {'auto': 0, 'manual': 0, 'total': 0, }
  113. result[release][branch]['total'] += 1
  114. mode = 'auto' if enabled else 'manual'
  115. result[release][branch][mode] += 1
  116. # respond to user
  117. releases = sorted([x for x in result])
  118. for release in releases:
  119. output = 'Rollout von \'{0}\':'.format(release)
  120. branches = sorted([x for x in result[release]])
  121. first = True
  122. for branch in branches:
  123. item = result[release][branch]
  124. if item is None:
  125. continue
  126. if not first:
  127. output += ','
  128. first = False
  129. auto_count = item['auto']
  130. manual_count = item['manual']
  131. output += ' {1} {0}'.format(branch, auto_count)
  132. if manual_count > 0:
  133. output += ' (+{0} manuell)'.format(manual_count)
  134. bot.say(output)
  135. # output count of nodes for which the autoupdater's branch and/or
  136. # firmware version could not be retrieved
  137. if skipped > 0:
  138. bot.say('plus {0} Knoten mit unklarem Status'.format(skipped))
  139. @willie.module.commands('providers')
  140. def ffpb_providers(bot, trigger):
  141. """Fetch the top 5 providers from BATCAVE."""
  142. providers = __batcave.get_providers()
  143. providers.sort(key=lambda x: x['count'], reverse=True)
  144. top5 = providers[:5]
  145. top5 = ['{0} ({1:.0f}%)'.format(x['name'], x['percentage']) for x in top5]
  146. bot.say('Unsere Top 5 Provider: ' + ', '.join(top5))