Changeset 9549


Ignore:
Timestamp:
Aug 23, 2011, 8:38:09 PM (13 years ago)
Author:
rick
Message:
  • Caches are now fully-file based as memcached was not working well with large entries (like images).
  • OSM django proxy is way to slow, rewrite to use apache balanced proxy members.
  • Use HTTPD mod_cache to cache heavy image generation work.
Location:
src/django_gheat
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • src/django_gheat/settings.py

    r9545 r9549  
    77DJANGO_BALANCERS = OSM_PROXY_BALANCERS = ['a','b','c','d','e','f']
    88OSM_PROXY_CDN_DOMAINS = [d + '.osmproxy.wirelessleiden.nl' for d in DJANGO_BALANCERS]
     9OSM_PREFIX = '/osm-tile-proxy'
    910DJANGO_CDN_DOMAINS = [d + '.maps.wirelessleiden.nl' for d in DJANGO_BALANCERS]
    1011
     
    3839        'LOCATION': '127.0.0.1:11211',
    3940        'TIMEOUT': 3600 * 24 * 7,
     41        'OPTIONS': {
     42            'MAX_ENTRIES': 100000
     43        }
     44    },
     45    'osm_proxy': {
     46        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
     47        'LOCATION': '/usr/local/var/django/cache/osm_proxy',
     48        'TIMEOUT': 3600 * 24 * 7,
     49        'OPTIONS': {
     50            'MAX_ENTRIES': 100000
     51        }
     52    },
     53    'tile_cache': {
     54        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
     55        'LOCATION': '/usr/local/var/django/cache/tile_cache',
     56        'TIMEOUT': 3600 * 24,
    4057        'OPTIONS': {
    4158            'MAX_ENTRIES': 100000
  • src/django_gheat/wlheatmap/osm_proxy.py

    r9398 r9549  
    1616
    1717# Create your views here.
    18 @cache_page(60 * 60 * 24 * 7, key_prefix="osm_proxy")
     18@cache_page(60 * 60 * 24 * 7, cache="osm_proxy")
    1919def osm_proxy(request,zoom,x,y):
    2020  mirror = random.choice(['a','b','c'])
  • src/django_gheat/wlheatmap/static/heatmap.js

    r9546 r9549  
    5959  map.addLayer(layerHeatmap3);
    6060
    61 //  layerHeatmap = new OpenLayers.Layer.OSM.Overlay1("Original Heatmap generator", {isBaseLayer: false, visibility: false});
    62 //  map.addLayer(layerHeatmap);
     61  layerHeatmap = new OpenLayers.Layer.OSM.Overlay1("Original Heatmap generator", {isBaseLayer: false, visibility: false});
     62  map.addLayer(layerHeatmap);
    6363       
    6464  var lonLat = new OpenLayers.LonLat(lon, lat).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
     
    7373   */
    7474
    75 //  $("#default").append("<span id=\"" + layerHeatmap.name + "\"><div id='layer_switcher_colour' style='background-color:rgb(" + layerHeatmap.colour + ")' /><input type=checkbox id=togglelayer /><font>" + layerHeatmap.name + " </font><br /></span>");
     75  $("#default").append("<span id=\"" + layerHeatmap.name + "\"><div id='layer_switcher_colour' style='background-color:rgb(" + layerHeatmap.colour + ")' /><input type=checkbox id=togglelayer /><font>" + layerHeatmap.name + " </font><br /></span>");
    7676
    7777  $("#default").append("<span id=\"" + layerHeatmap2.name + "\"><div id='layer_switcher_colour' style='background-color:rgb(" + layerHeatmap2.colour + ")' /><input type=checkbox id=togglelayer /><font>" + layerHeatmap2.name + " </font><br /></span>");
  • src/django_gheat/wlheatmap/templates/js/LayerBase.js

    r9544 r9549  
    5454     */
    5555    initialize: function(name, options) {
    56         var url = get_balanced_urls('http://', {{ settings.OSM_PROXY_CDN_DOMAINS|safe }}, '{{ settings.DJANGO_PREFIX }}/osm-proxy/${z}/${x},${y}.png');
     56        var url = get_balanced_urls('http://', {{ settings.OSM_PROXY_CDN_DOMAINS|safe }}, '{{ settings.OSM_PREFIX }}/${z}/${x}/${y}.png');
    5757        options = OpenLayers.Util.extend({ numZoomLevels: 19 }, options);
    5858        var newArguments = [name, url, options];
  • src/django_gheat/wlheatmap/tile.py

    r9398 r9549  
    99from django.views.decorators.cache import cache_page
    1010from gheat.models import *
     11import os
    1112import pygame
    1213import sys
     
    3637    self.surf = new_surf
    3738
    38   def write(self, fh,format='png'):
    39     # XXX: How to get a PNG stream directly to the output
    40     f = tempfile.NamedTemporaryFile(suffix=format)
    41     pygame.image.save(self.surf,f.name)
    42     f.seek(0)
    43     fh.write(f.read())
     39  def save_and_get_image(self,filename):
     40    """ Save the file to the location and return the file """
     41    basedir = os.path.dirname(filename)
     42    if not os.path.isdir(basedir):
     43      os.makedirs(basedir)
     44    pygame.image.save(self.surf,filename)
     45    return open(filename,'r').read()
    4446
    4547  def get_image(self,format='png'):
     
    4850    f.seek(0)
    4951    return f.read()
     52
    5053
    5154
     
    199202    'longitude__gte' : nw_deg.lon
    200203  })
    201   # TODO: This is currently hard-coded to display _all_ metingen
    202   metingen = Meting.objects.select_related().filter(**filter)
     204  # Limit such that high level zooms does not get the whole database
     205  metingen = Meting.objects.filter(**filter)[:1000].values_list('latitude', 'longitude', 'signaal')
    203206
    204207  # XXX: Signal is not normalized in the database making it unknown when a
     
    219222    return max(x,y) - min(x,y)
    220223 
    221   for meting in metingen:
    222     lat_min = min(lat_min, meting.latitude)
    223     lat_max = max(lat_max, meting.latitude)
    224     lon_min = min(lon_min, meting.longitude)
    225     lon_max = max(lon_max, meting.longitude)
    226     xcoord = int(dif(nw_deg.lon,meting.longitude) / (resolution_deg.lon))
    227     ycoord = int(dif(nw_deg.lat,meting.latitude) / (resolution_deg.lat))
     224  for (latitude, longitude, signaal) in metingen:
     225    lat_min = min(lat_min, latitude)
     226    lat_max = max(lat_max, latitude)
     227    lon_min = min(lon_min, longitude)
     228    lon_max = max(lon_max, longitude)
     229    xcoord = int(dif(nw_deg.lon,longitude) / (resolution_deg.lon))
     230    ycoord = int(dif(nw_deg.lat,latitude) / (resolution_deg.lat))
    228231
    229232    # TODO: Please note that this 'logic' technically does apply to WiFi signals,
     
    239242    # with examples like this:
    240243    # http://stackoverflow.com/questions/3104781/inverse-distance-weighted-idw-interpolation-with-python
    241     signal_normalized = MAX_RANGE - (MAX_SIGNAL - meting.signaal)
    242     im.add_circle((xcoord,ycoord),float(signal_normalized) / meters_per_pixel,colour, MAX_SIGNAL - meting.signaal)
     244    signal_normalized = MAX_RANGE - (MAX_SIGNAL - signaal)
     245    im.add_circle((xcoord,ycoord),float(signal_normalized) / meters_per_pixel,colour, MAX_SIGNAL - signaal)
    243246    #im.add_point((xcoord,ycoord),float(signal_normalized) / meters_per_pixel,colour, MAX_SIGNAL - meting.signaal)
    244247 
     
    246249  return im
    247250
    248 
    249 # Create your views here.
    250 @cache_page(60 * 60 * 24, key_prefix="wlheatmap_tile")
    251 def serve_tile(request,zoom,x,y):
     251def pre_process_tile(request,zoom,x,y):
    252252  filter = {}
    253253  colour = (255,0,0)
     
    259259  now = time.time()
    260260  im = make_tile(int(x),int(y),int(zoom),filter=filter,colour=colour)
     261  return im
     262
     263# Create your views here.
     264@cache_page(60 * 60 * 24, cache="tile_cache")
     265def serve_tile(request,zoom,x,y):
     266  im = pre_process_tile(request,zoom,x,y)
    261267  data = im.get_image('png')
    262 
    263   response = HttpResponse(mimetype="image/png")
    264   response.write(data)
    265   return response
    266 
     268  return HttpResponse(data,mimetype="image/png")
     269
     270def fixed_wl_only(request,zoom,x,y):
     271  """ Pre-render and save attempt """
     272  im = pre_process_tile(request,zoom,x,y)
     273  data = im.save_and_get_image('/usr/local/var/django/tile/fixed/wl-only/%s/%s,%s.png' % (zoom, x, y))
     274  return HttpResponse(data,mimetype="image/png")
  • src/django_gheat/wlheatmap/urls.py

    r9544 r9549  
    1414    url(r'^$', direct_to_template, {'template' : 'home.html'}),
    1515    url(r'^js/LayerBase.js$', 'js_layer_base'),
     16    url(r'^tile/fixed/wl-only/(?P<zoom>\d+)/(?P<x>\d+),(?P<y>\d+).png$', 'tile.fixed_wl_only'),
    1617    url(r'^tile/(?P<zoom>\d+)/(?P<x>\d+),(?P<y>\d+).png$', 'tile.serve_tile'),
    1718    url(r'^nodelist/(?P<zoom>\d+),(?P<lat>[0-9.]+),(?P<lon>[0-9.]+)$', 'nodelist.serve_nodelist'),
Note: See TracChangeset for help on using the changeset viewer.