From 638e9782b2f124139bb2ae8c71a5d6ee492e6338 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sun, 16 Jan 2011 14:00:20 +0100 Subject: [PATCH] Add more information to the geo table. --- www/templates/modules/stasy-table-geo.html | 13 ++++++++++++ www/templates/stasy-stats-geo.html | 2 +- www/webapp/__init__.py | 1 + www/webapp/backend/geoip.py | 19 ++++++++++++++++++ www/webapp/backend/stasy.py | 3 +++ www/webapp/ui_modules.py | 23 ++++++++++++++++++++++ 6 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 www/templates/modules/stasy-table-geo.html diff --git a/www/templates/modules/stasy-table-geo.html b/www/templates/modules/stasy-table-geo.html new file mode 100644 index 00000000..231369ad --- /dev/null +++ b/www/templates/modules/stasy-table-geo.html @@ -0,0 +1,13 @@ + + {% for country in countries %} + + + + + + {% end %} +
+ + {{ country.name }} +

 

{{ "%.2f" % country.value }}%
+
diff --git a/www/templates/stasy-stats-geo.html b/www/templates/stasy-stats-geo.html index a6079dff..f5a38991 100644 --- a/www/templates/stasy-stats-geo.html +++ b/www/templates/stasy-stats-geo.html @@ -29,5 +29,5 @@ This chart shows us in which country IPFire is running.

{% end %} - {{ modules.StasyTable(geo_locations, sortby="percentage", flags=True) }} + {{ modules.StasyGeoTable(geo_locations) }} {% end block %} diff --git a/www/webapp/__init__.py b/www/webapp/__init__.py index 065dade3..7274e109 100644 --- a/www/webapp/__init__.py +++ b/www/webapp/__init__.py @@ -36,6 +36,7 @@ class Application(tornado.web.Application): "SidebarRelease" : SidebarReleaseModule, "StasyTable" : StasyTableModule, "StasyDeviceTable" : StasyDeviceTableModule, + "StasyGeoTable" : StasyGeoTableModule, "TrackerPeerList": TrackerPeerListModule, }, xsrf_cookies = True, diff --git a/www/webapp/backend/geoip.py b/www/webapp/backend/geoip.py index b7f3e999..9a68fe39 100644 --- a/www/webapp/backend/geoip.py +++ b/www/webapp/backend/geoip.py @@ -1,11 +1,16 @@ #!/usr/bin/python +import re + from databases import Databases from misc import Singleton class GeoIP(object): __metaclass__ = Singleton + def __init__(self): + self.__country_codes = self.db.query("SELECT code, name FROM iso3166_countries") + @property def db(self): return Databases().geoip @@ -32,6 +37,20 @@ class GeoIP(object): return self.db.get("SELECT * FROM locations WHERE id = %s", int(location)) + def get_country_name(self, code): + name = "Unknown" + + code = code.upper() + for country in self.__country_codes: + if country.code == code: + name = country.name + break + + # Fix some weird strings + name = re.sub(r"(.*) (.* Republic of)", r"\2 \1", name) + + return name + if __name__ == "__main__": g = GeoIP() diff --git a/www/webapp/backend/stasy.py b/www/webapp/backend/stasy.py index 61b93a86..0eab5578 100644 --- a/www/webapp/backend/stasy.py +++ b/www/webapp/backend/stasy.py @@ -680,6 +680,9 @@ class Stasy(object): count += geo_locations[geo_location] + for geo_location in geo_locations.keys(): + geo_locations[geo_location] /= count + return geo_locations def get_models_by_vendor(self, subsystem, vendor_id): diff --git a/www/webapp/ui_modules.py b/www/webapp/ui_modules.py index 40f09b0a..ba836087 100644 --- a/www/webapp/ui_modules.py +++ b/www/webapp/ui_modules.py @@ -33,6 +33,10 @@ class UIModule(tornado.web.UIModule): def releases(self): return self.handler.releases + @property + def geoip(self): + return self.handler.geoip + class MenuModule(UIModule): def render(self): @@ -201,3 +205,22 @@ class StasyDeviceTableModule(UIModule): return self.render_string("modules/stasy-table-devices.html", groups=groups.items()) + + +class StasyGeoTableModule(UIModule): + def render(self, items): + _ = self.locale.translate + + # Sort all items by value + items = sorted(items.items(), key=operator.itemgetter(1), reverse=True) + + countries = [] + for code, value in items: + country = tornado.database.Row({ + "code" : code.lower(), + "name" : _(self.geoip.get_country_name(code)), + "value" : value * 100, + }) + countries.append(country) + + return self.render_string("modules/stasy-table-geo.html", countries=countries) -- 2.39.2