]> git.ipfire.org Git - ipfire.org.git/commitdiff
Add more information to the geo table.
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 16 Jan 2011 13:00:20 +0000 (14:00 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 16 Jan 2011 13:00:20 +0000 (14:00 +0100)
www/templates/modules/stasy-table-geo.html [new file with mode: 0644]
www/templates/stasy-stats-geo.html
www/webapp/__init__.py
www/webapp/backend/geoip.py
www/webapp/backend/stasy.py
www/webapp/ui_modules.py

diff --git a/www/templates/modules/stasy-table-geo.html b/www/templates/modules/stasy-table-geo.html
new file mode 100644 (file)
index 0000000..231369a
--- /dev/null
@@ -0,0 +1,13 @@
+<table class="stats">
+       {% for country in countries %}
+               <tr>
+                       <td class="key">
+                               <img src="{{ static_url("images/flags/%s.png" % country.code) }}" />
+                               {{ country.name }}
+                       </td>
+                       <td class="bar"><p style="{{ "width: %.2f%%" % country.value }}">&nbsp;</p></td>
+                       <td class="value">{{ "%.2f" % country.value }}%</td>
+               </tr>
+       {% end %}
+</table>
+<br class="clear" />
index a6079dffcafd55238af2d0e68312965d4ac969cc..f5a38991e76d2aac01837a735348c535efaca359 100644 (file)
@@ -29,5 +29,5 @@
                        This chart shows us in which country IPFire is running.
                </p>
        {% end %}
-       {{ modules.StasyTable(geo_locations, sortby="percentage", flags=True) }}
+       {{ modules.StasyGeoTable(geo_locations) }}
 {% end block %}
index 065dade3402032d16973eac8b22a3eb6e2195da7..7274e109acb44b124595873a7439d2ee4362a032 100644 (file)
@@ -36,6 +36,7 @@ class Application(tornado.web.Application):
                                "SidebarRelease" : SidebarReleaseModule,
                                "StasyTable"     : StasyTableModule,
                                "StasyDeviceTable" : StasyDeviceTableModule,
+                               "StasyGeoTable"  : StasyGeoTableModule,
                                "TrackerPeerList": TrackerPeerListModule,
                        },
                        xsrf_cookies = True,
index b7f3e999b3df4038fef736f766cb89e36e0f7c7d..9a68fe39fd810b7e1a6f0c8ade00e03c7d28ed4e 100644 (file)
@@ -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()
index 61b93a86ebf9c903441cdaefac2fa782d3c709e8..0eab55785df6c62b9f25ac609ebaef7b5c055c6d 100644 (file)
@@ -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):
index 40f09b0a7a65be07d9adf2f1c987a5f3bfc0ef7a..ba8360879cf4789cb803905437fc971978e069de 100644 (file)
@@ -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)