diff --git a/ennstatus/stats/functions.py b/ennstatus/stats/functions.py new file mode 100644 --- /dev/null +++ b/ennstatus/stats/functions.py @@ -0,0 +1,54 @@ +from collections import Counter, defaultdict + +from flask import current_app + +import pygal + + +from ennstatus.status.functions import split_all_servers_to_types + +COUNTRIES_TO_ISO = dict( + zip(pygal.i18n.COUNTRIES.values(), pygal.i18n.COUNTRIES.keys())) +COUNTRIES_TO_ISO['Isle of Man'] = 'gb' + +def _make_country_list(): + + servers = split_all_servers_to_types() + countries = defaultdict(list) + + for key, value in servers.items(): + for server in value: + country = COUNTRIES_TO_ISO[server['country']] + countries[key].append(country) + + return countries + + +def make_worldmap(server_type, style): + + arguments = { + 'title': '%s nodes' % server_type.capitalize(), + 'style': pygal.style.styles[style], + 'legend_at_bottom': True, + 'disable_xml_declaration': True, + 'pretty_print': True, + } + + countries = _make_country_list() + + exits_count = Counter(countries['Exit']) + relays_count = Counter(countries['Relay']) + bridges_count = Counter(countries['Bridge']) + + plot = pygal.Worldmap(**arguments) + + if server_type in ('all', 'exit'): + plot.add('Exits', exits_count) + + if server_type in ('all', 'relay'): + plot.add('Relays', relays_count) + + if server_type in ('all', 'bridge'): + plot.add('Bridges', bridges_count) + + return plot