new address with online/offline overview
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Sat, 31 Jul 2010 16:08:02 +0000 (16:08 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Sat, 31 Jul 2010 16:08:02 +0000 (16:08 +0000)
bin/djais/urls.py
bin/djais/views.py
bin/inputs/common.py
bin/inputs/stats.py
html_templates/base.html
html_templates/index.html
html_templates/sources.html

index 3f5f42d95d4a1e475a6739860a4e663cc2af000e..02f2393181711451ff2400d579575dee44b24893 100644 (file)
@@ -25,7 +25,8 @@ urlpatterns = patterns('',
     (r'^user/add$', 'ais.djais.views.user_edit', {'login':None} ),
     (r'^user/(?P<login>[a-zA-Z0-9_]+)/change_password$', 'ais.djais.views.user_change_password'),
     (r'^user/(?P<login>[a-zA-Z0-9_]+)/delete$', 'ais.djais.views.user_delete'),
-    (r'^source/$', 'ais.djais.views.sources'),
+    (r'^source/$', 'ais.djais.views.sources_index'),
+    (r'^source/stats$', 'ais.djais.views.sources_stats'),
     (r'^logout$', 'ais.djais.views.logout'),
 
     # Example:
index 3da2ca12a05e0e846aadd9a76c5a33917dc5c758..39a9cb82f74b33c171d0f6c5e13ec6d09f85f077 100644 (file)
@@ -20,6 +20,7 @@ from ais.djais.models import *
 from ais.show_targets_ships import *
 from ais.common import Nmea, NmeaFeeder, strmmsi_to_mmsi, SHIP_TYPES, STATUS_CODES, AIS_STATUS_NOT_AVAILABLE, AIS_ROT_NOT_AVAILABLE, AIS_LATLON_SCALE, AIS_LON_NOT_AVAILABLE, AIS_LAT_NOT_AVAILABLE, AIS_COG_SCALE, AIS_COG_NOT_AVAILABLE, AIS_NO_HEADING, AIS_SOG_SCALE, AIS_SOG_NOT_AVAILABLE, AIS_SOG_MAX_SPEED, add_nmea1, add_nmea5_partial, load_fleet_to_uset
 from ais.ntools import datetime_to_timestamp, clean_ais_charset
+from ais.inputs.common import is_id4_active
 from ais.inputs.stats import STATS_DIR
 from ais.inputs.config import peers_get_config
 
@@ -647,31 +648,31 @@ def logout(request):
     #response = render_to_response('logout.html', {}, RequestContext(request))
     #return response
 
+periods = ({
+    #'name_tiny': '2h',
+    #'name_long': '2 hours',
+    #'seconds': 2*60*60
+    #}, {
+    'name_tiny': '6h',
+    'name_long': '6 hours',
+    'seconds': 6*60*60,
+    }, {
+    #'name_tiny': '2d',
+    #'name_long': '2 days',
+    #'seconds': 2*24*60*60
+    #}, {
+    'name_tiny': '14d',
+    'name_long': '2 weeks',
+    'seconds': 14*24*60*60
+    }, {
+    'name_tiny': '90d',
+    'name_long': '3 monthes',
+    'seconds': 90*24*60*60
+    })
+        
 @http_authenticate(auth, 'ais')
-def sources(request):
+def sources_stats(request):
     now = int(get_timestamp())
-    periods = ({
-        #'name_tiny': '2h',
-        #'name_long': '2 hours',
-        #'seconds': 2*60*60
-        #}, {
-        'name_tiny': '6h',
-        'name_long': '6 hours',
-        'seconds': 6*60*60,
-        }, {
-        #'name_tiny': '2d',
-        #'name_long': '2 days',
-        #'seconds': 2*24*60*60
-        #}, {
-        'name_tiny': '14d',
-        'name_long': '2 weeks',
-        'seconds': 14*24*60*60
-        }, {
-        'name_tiny': '90d',
-        'name_long': '3 monthes',
-        'seconds': 90*24*60*60
-        })
-        
     sources = peers_get_config()
     for id4, source in sources.iteritems():
         source['id4'] = id4
@@ -704,3 +705,14 @@ def sources(request):
             rrdtool.graph(*args)
 
     return render_to_response('sources.html', {'sources':sources.values(), 'periods': periods}, RequestContext(request))
+
+
+@http_authenticate(auth, 'ais')
+def sources_index(request):
+    peers_config = peers_get_config()
+    for id4, peer in peers_config.iteritems():
+        peer['id4'] = id4
+        peer['id2'] = id4[2:]
+        peer['active'] = is_id4_active(id4)
+        
+    return render_to_response('sources_index.html', {'sources':peers_config.values(), 'periods': periods}, RequestContext(request))
index cad9c803b23223dcbb2b0690853b2f5b54ad70da..00f6aeb5e5a6539ab95e38d33fc2e937daff90b5 100644 (file)
@@ -8,12 +8,12 @@ __all__ = [
     'DEFAULT_MTU',
     'SourceLogger',
     'Source', 
-    'get_source_by_id4', 'refresh_all_stats',
+    'get_source_by_id4', 'is_id4_active', 'refresh_all_stats',
     ]
 
 import os.path
 import logging
-from datetime import datetime
+from datetime import datetime, timedelta
 
 from ais.inputs.stats import InStats
 from ais.inputs.config import peers_get_config
@@ -23,6 +23,11 @@ from ais.inputs.outpeer import outpeers_from_config
 NMEA_DIR = '/var/lib/ais/nmea'
 DEFAULT_MTU = 1500
 
+def _get_log_filename(id4, date):
+    localfilename = '%s-%s.dump' \
+                    % (date.strftime('%Y%m%d'), id4)
+    return os.path.join(NMEA_DIR, localfilename)
+
 class SourceLogger:
     def __init__(self, id4):
         self.id4 = id4
@@ -44,9 +49,7 @@ class SourceLogger:
             if dumpdate != self.last_dumpdate:
                 if self.dumpfile:
                     self.dumpfile.close()
-                localfilename = '%s-%s.dump' \
-                                % (dumpdate.strftime('%Y%m%d'), self.id4)
-                filename = os.path.join(NMEA_DIR, localfilename)
+                filename = _get_log_filename(self.id4, dumpdate)
                 logging.info('Opening %s', filename)
                 self.dumpfile = file(filename, 'a', 1)
                 self.last_dumpdate = dumpdate
@@ -86,6 +89,17 @@ def get_source_by_id4(id4):
     '''
     return __all_sources__.get(id4, None) or Source(id4)
 
+def is_id4_active(id4):
+    reference_time =  datetime.utcnow() - timedelta(seconds=15)
+    logging.error('reference_time=%s', reference_time)
+    logfilename = _get_log_filename(id4, reference_time.date())
+    logging.error('logfilename=%s', logfilename)
+    if not os.path.exists(logfilename):
+        return False
+    mod_timestamp = os.path.getmtime(logfilename)
+    logging.error('file_time=%s', datetime.utcfromtimestamp(mod_timestamp))
+    return datetime.utcfromtimestamp(mod_timestamp) > reference_time
+
 
 def refresh_all_stats():
     '''
index 68825b240f39d678f9a0b824d7dae6d462e06eec..4ca5c89ec388df0cea06e8d9a18c2eb08fe70d49 100644 (file)
@@ -136,4 +136,3 @@ class InStats:
                          rates.rpackets, rates.rlines,
                          8*rates.rbytes, 8*rates.rbytes_ethernet)
             rates.save(self.id4+'.rrd')
-
index d4313b992a01daffcc8e4f59ce192a77542da83c..c89e4d89cbfd52f9cc58b48f1086dd409c26849c 100644 (file)
@@ -44,6 +44,9 @@ a[href] {
 a[href]:hover {
     text-decoration: underline;
 }
+li {
+    list-style: square;
+}
 </style>
 {% block style_extra %}{% endblock %}
 <div id=header>
@@ -63,7 +66,6 @@ a[href]:hover {
 You are here: <a href='/'>home</a>
 {% endblock %}
 <br>
-<br>
 
 {% block content %}{% endblock %}
 </div>
index efec3190fb34b1ed38e0a268bb043d9700561429..a0f8554352933b58a46e48148931c4859147f0ff 100644 (file)
@@ -2,9 +2,6 @@
 
 {% block style_extra %}
 <style type="text/css">
-li {
-    list-style: square;
-}
 ul.bodymenu li {
     font-size: 180%;
 }
index c47aee38227c11fa6aa1a9f107437ca04a611031..1c32b0cc1b514aa4cfaf47c2f477dc08bc81e30a 100644 (file)
@@ -1,8 +1,8 @@
-{% extends "base.html" %}
+{% extends "sources_index.html" %}
 
 {% block breadcrumbs %}
 {{ block.super }}
-&gt; <a href="/source/">sources</a>
+&gt; <a href="/source/stats">stats</a>
 {% endblock %}
 
 {% block content %}