This commit implements a new item on the right of the MapOSMatic website, which gives users the current time delta between our GIS database and the official OSM database.
In order to implement this, we created a small gisdb.py module which factorizes the work of connecting to the GIS database, now used by both the Nominatim code and our new code that fetches the last update of the GIS database through the maposmatic_admin table. Signed-off-by: Thomas Petazzoni <[email protected]> --- www/maposmatic/context_processors.py | 19 ++++++++++++++++++ www/maposmatic/nominatim.py | 35 ++++++++++++--------------------- www/templates/maposmatic/base.html | 8 +++++++ 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/www/maposmatic/context_processors.py b/www/maposmatic/context_processors.py index 2a96826..58dc05f 100644 --- a/www/maposmatic/context_processors.py +++ b/www/maposmatic/context_processors.py @@ -27,10 +27,28 @@ import feedparser from models import MapRenderingJob import www.settings +from www.maposmatic import gisdb +import psycopg2 + def get_latest_blog_posts(): f = feedparser.parse("http://news.maposmatic.org/?feed=rss2") return f.entries[:5] +def get_osm_database_last_update(): + db = gisdb.get() + if db is None: + return None + cursor = db.cursor() + query = "select last_update from maposmatic_admin;" + try: + cursor.execute(query) + except psycopg2.ProgrammingError: + db.rollback() + return None + # Extract datetime object. It is located as the first element + # of a tuple, itself the first element of an array. + return cursor.fetchall()[0][0] + def all(request): # Do not add the useless overhead of parsing blog entries when generating # the rss feed @@ -40,4 +58,5 @@ def all(request): 'randommap': MapRenderingJob.objects.get_random_with_thumbnail(), 'blogposts': get_latest_blog_posts(), 'MAPOSMATIC_DAEMON_RUNNING': www.settings.is_daemon_running(), + 'osm_date': get_osm_database_last_update(), } diff --git a/www/maposmatic/nominatim.py b/www/maposmatic/nominatim.py index e06afa9..7fc3cdd 100644 --- a/www/maposmatic/nominatim.py +++ b/www/maposmatic/nominatim.py @@ -42,6 +42,7 @@ from xml.etree.ElementTree import parse as XMLTree from ocitysmap2 import coords import www.settings +from www.maposmatic import gisdb NOMINATIM_BASE_URL = 'http://nominatim.openstreetmap.org' NOMINATIM_MAX_RESULTS_PER_RESPONSE = 10 @@ -306,16 +307,8 @@ def _prepare_and_filter_entries(entries): if not www.settings.has_gis_database(): return entries - try: - conn = psycopg2.connect("dbname='%s' user='%s' host='%s' password='%s' port='%s'" % - (www.settings.GIS_DATABASE_NAME, - www.settings.GIS_DATABASE_USER, - www.settings.GIS_DATABASE_HOST, - www.settings.GIS_DATABASE_PASSWORD, - www.settings.GIS_DATABASE_PORT)) - except psycopg2.OperationalError, e: - l.warning("Could not connect to the PostGIS database: %s" % - str(e)[:-1]) + db = gisdb.get() + if db is None: return entries place_tags = [ 'city', 'town', 'municipality', @@ -323,24 +316,22 @@ def _prepare_and_filter_entries(entries): 'island', 'islet', 'locality', 'administrative' ] filtered_results = [] - try: - cursor = conn.cursor() - for entry in entries: - # Ignore uninteresting tags - if not entry.get("type") in place_tags: - continue + cursor = db.cursor() + for entry in entries: + + # Ignore uninteresting tags + if not entry.get("type") in place_tags: + continue - # Our entry wil be part of the result - filtered_results.append(entry) + # Our entry wil be part of the result + filtered_results.append(entry) - # Enrich the entry with more info - _prepare_entry(cursor, entry) + # Enrich the entry with more info + _prepare_entry(cursor, entry) # Some cleanup cursor.close() - finally: - conn.close() return filtered_results diff --git a/www/templates/maposmatic/base.html b/www/templates/maposmatic/base.html index a85d756..2a70429 100644 --- a/www/templates/maposmatic/base.html +++ b/www/templates/maposmatic/base.html @@ -85,6 +85,14 @@ </td> <td id="meta"> + <div id="bosmtimestamp"> + <h3>{% trans "OSM database status" %}</h3> + {% if osm_date %} + <p>{% blocktrans with osm_date|timesince as date %}Lag of MapOSMatic OSM database: {{ date }}.{% endblocktrans %}</p> + {% else %} + <p>{% blocktrans %}Lag of MapOSMatic OSM database: unknown.{% endblocktrans %}</p> + {% endif %} + </div> <div id="bmap"> <h3>{% trans "Random map" %}</h3> {% if randommap %}<p class="randommap"> -- 1.7.4.1
