ffpb_netstatus.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 ffpb_fetch_stats, get_alfred_data, pretty_date
  9. highscores = None
  10. def setup(bot):
  11. global highscores
  12. # load highscores from disk
  13. highscores = shelve.open('highscoredata', writeback=True)
  14. if not 'nodes' in highscores:
  15. highscores['nodes'] = 0
  16. highscores['nodes_ts'] = time.time()
  17. if not 'clients' in highscores:
  18. highscores['clients'] = 0
  19. highscores['clients_ts'] = time.time()
  20. def shutdown(bot):
  21. global highscores
  22. # store highscores
  23. if not highscores is None:
  24. highscores.sync()
  25. highscores.close()
  26. highscores = None
  27. @willie.module.interval(15)
  28. def ffpb_get_stats(bot):
  29. """Fetch current statistics, if the highscore changes signal this."""
  30. (nodes_active, nodes_total, clients_count) = ffpb_fetch_stats(bot, 'http://map.paderborn.freifunk.net/nodes.json', 'ffpb_stats')
  31. highscore_changed = False
  32. if nodes_active > highscores['nodes']:
  33. highscores['nodes'] = nodes_active
  34. highscores['nodes_ts'] = time.time()
  35. highscore_changed = True
  36. if clients_count > highscores['clients']:
  37. highscores['clients'] = clients_count
  38. highscores['clients_ts'] = time.time()
  39. highscore_changed = True
  40. if highscore_changed:
  41. print('HIGHSCORE changed: {0} nodes ({1}), {2} clients ({3})'.format(highscores['nodes'], highscores['nodes_ts'], highscores['clients'], highscores['clients_ts']))
  42. if not (bot.config.ffpb.msg_target is None):
  43. action_msg = 'notiert sich den neuen Highscore: {0} Knoten ({1}), {2} Clients ({3})'.format(highscores['nodes'], pretty_date(int(highscores['nodes_ts'])), highscores['clients'], pretty_date(int(highscores['clients_ts'])))
  44. action_target = bot.config.ffpb.msg_target
  45. if (not bot.config.ffpb.msg_target_public is None):
  46. action_target = bot.config.ffpb.msg_target_public
  47. bot.msg(action_target, '\x01ACTION %s\x01' % action_msg)
  48. @willie.module.commands('status')
  49. def ffpb_status(bot, trigger):
  50. """State of the network: count of nodes + clients"""
  51. stats = bot.memory['ffpb_stats'] if 'ffpb_stats' in bot.memory else None
  52. if stats is None:
  53. bot.say('Uff, kein Plan wo der Zettel ist. Fragst du später nochmal?')
  54. return
  55. bot.say('Es sind {0} Knoten und ca. {1} Clients online.'.format(stats["nodes_active"], stats["clients"]))
  56. @willie.module.commands('highscore')
  57. def ffpb_highscore(bot, trigger):
  58. bot.say('Highscore: {0} Knoten ({1}), {2} Clients ({3})'.format(
  59. highscores['nodes'], pretty_date(int(highscores['nodes_ts'])),
  60. highscores['clients'], pretty_date(int(highscores['clients_ts']))))
  61. @willie.module.commands('rollout-status')
  62. def ffpb_rolloutstatus(bot, trigger):
  63. """Display statistic on how many nodes have installed the given firmware version."""
  64. # initialize results dictionary
  65. result = { }
  66. skipped = 0
  67. # inform users about changed command parameters
  68. if not trigger.group(2) is None:
  69. bot.reply('Dieses Kommando nimmt keinen Parameter mehr an.')
  70. return
  71. # get ALFRED data (and ensure it is current)
  72. alfred_data = get_alfred_data(bot, True)
  73. if alfred_data is None:
  74. bot.say('Ich habe irgendein Memo verpasst, sorry - bitte später nochmal fragen.')
  75. return
  76. # check each node in ALFRED data
  77. for nodeid in alfred_data:
  78. item = alfred_data[nodeid]
  79. if (not 'software' in item) or (not 'firmware' in item['software']) or (not 'autoupdater' in item['software']):
  80. skipped+=1
  81. continue
  82. release = item['software']['firmware']['release']
  83. branch = item['software']['autoupdater']['branch']
  84. enabled = item['software']['autoupdater']['enabled']
  85. if not release in result or result[release] is None:
  86. result[release] = { 'stable': None, 'testing': None }
  87. if not branch in result[release] or result[release][branch] is None:
  88. result[release][branch] = { 'auto': 0, 'manual': 0, 'total': 0 }
  89. result[release][branch]['total'] += 1
  90. mode = 'auto' if enabled else 'manual'
  91. result[release][branch][mode] += 1
  92. # respond to user
  93. releases = sorted([x for x in result])
  94. for release in releases:
  95. output = 'Rollout von \'{0}\':'.format(release)
  96. branches = sorted([x for x in result[release]])
  97. first = True
  98. for branch in branches:
  99. item = result[release][branch]
  100. if item is None: continue
  101. if not first:
  102. output += ','
  103. first = False
  104. total = item['total']
  105. auto_count = item['auto']
  106. manual_count = item['manual']
  107. output += ' {2} {0}'.format(branch, total, auto_count, manual_count)
  108. if manual_count > 0:
  109. output += ' (+{3} manuell)'.format(branch, total, auto_count, manual_count)
  110. bot.say(output)
  111. # output count of nodes for which the autoupdater's branch and/or
  112. # firmware version could not be retrieved
  113. if skipped > 0:
  114. bot.say('plus {0} Knoten deren Status gerade nicht abfragbar war'.format(skipped))
  115. @willie.module.commands('providers')
  116. def ffpb_providers(bot, trigger):
  117. """Fetch the top 5 providers from BATCAVE."""
  118. providers = json.load(urllib2.urlopen('http://[fdca:ffee:ff12:a255::253]:8888/providers?format=json'))
  119. providers.sort(key=lambda x: x['count'], reverse=True)
  120. bot.say('Unsere Top 5 Provider: ' + ', '.join(['{0} ({1:.0f}%)'.format(x['name'], x['percentage']) for x in providers[:5]]))