implemented a config cache
authorCalin Crisan <ccrisan@gmail.com>
Sat, 12 Oct 2013 15:45:02 +0000 (18:45 +0300)
committerCalin Crisan <ccrisan@gmail.com>
Sat, 12 Oct 2013 15:45:02 +0000 (18:45 +0300)
doc/todo.txt
src/config.py
src/handlers.py
src/server.py

index 0ca7dee6b2b9d98301b839f14aaba753995e7d80..79e2361fae8bf2a269af0f6a97ee8bfa1d73217c 100644 (file)
@@ -1,5 +1,4 @@
 
--> authentication
 -> cache configuration
 
 -> prevent Request closed errors by stopping mjpg clients before stopping motion
index 7c7306a10086d3256c34f2fd36a2a49ea0cb0106..3c408d68b9bb7074555ae1dc67a62e7fa702db55 100644 (file)
@@ -14,9 +14,16 @@ _CAMERA_CONFIG_FILE_NAME = 'thread-%(id)s.conf'
 _MAIN_CONFIG_FILE_PATH = os.path.join(settings.CONF_PATH, 'motion.conf')
 _CAMERA_CONFIG_FILE_PATH = os.path.join(settings.CONF_PATH, _CAMERA_CONFIG_FILE_NAME)
 
+_main_config_cache = None
+_camera_config_cache = None
+_camera_ids_cache = None
+
 
 def get_main(as_lines=False):
-    # TODO use a cache
+    global _main_config_cache
+    
+    if not as_lines and _main_config_cache:
+        return _main_config_cache
     
     config_file_path = os.path.join(settings.PROJECT_PATH, _MAIN_CONFIG_FILE_PATH)
     
@@ -57,11 +64,13 @@ def get_main(as_lines=False):
     data = _conf_to_dict(lines, list_names=['thread'])
     _set_default_motion(data)
     
+    _main_config_cache = data
+    
     return data
         
 
 def set_main(data):
-    # TODO use a cache
+    global _main_config_cache
     
     _set_default_motion(data)
     
@@ -101,11 +110,18 @@ def set_main(data):
     
     finally:
         file.close()
-    
+
+    _main_config_cache = data
+
     return data
 
 
 def get_camera_ids():
+    global _camera_ids_cache
+    
+    if _camera_ids_cache:
+        return _camera_ids_cache
+
     config_path = settings.CONF_PATH
     
     logging.debug('listing config dir %(path)s...' % {'path': config_path})
@@ -133,6 +149,8 @@ def get_camera_ids():
         
     camera_ids.sort()
     
+    _camera_ids_cache = camera_ids
+    
     return camera_ids
 
 
@@ -146,7 +164,10 @@ def has_enabled_cameras():
 
 
 def get_camera(camera_id, as_lines=False):
-    # TODO use a cache
+    global _camera_config_cache
+    
+    if not as_lines and _camera_config_cache and camera_id in _camera_config_cache:
+        return _camera_config_cache[camera_id]
     
     camera_config_path = _CAMERA_CONFIG_FILE_PATH % {'id': camera_id}
     
@@ -186,11 +207,16 @@ def get_camera(camera_id, as_lines=False):
 
         _set_default_motion_camera(data)
     
+    if _camera_config_cache is None:
+        _camera_config_cache = {}
+    
+    _camera_config_cache[camera_id] = data
+    
     return data
 
 
 def set_camera(camera_id, data):
-    # TODO use a cache
+    global _camera_config_cache
     
     if data['@proto'] == 'v4l2':
         _set_default_motion_camera(data)
@@ -246,12 +272,19 @@ def set_camera(camera_id, data):
     
     finally:
         file.close()
+        
+    if _camera_config_cache is None:
+        _camera_config_cache = {}
+    
+    _camera_config_cache[camera_id] = data
     
     return data
 
 
 def add_camera(device_details):
-    # TODO use a cache
+    global _camera_ids
+    
+    _camera_ids = None
     
     # determine the last camera id
     camera_ids = get_camera_ids()
@@ -291,8 +324,10 @@ def add_camera(device_details):
 
 
 def rem_camera(camera_id):
-    # TODO use a cache
-
+    global _camera_ids
+    
+    _camera_ids = None
+    
     camera_config_name = _CAMERA_CONFIG_FILE_NAME % {'id': camera_id}
     camera_config_path = _CAMERA_CONFIG_FILE_PATH % {'id': camera_id}
     
index 6b85070ad2d1b9c1ab270e41d3fc17068844c5a8..2343c03b3c3e3c66dda69dc46ea954c684cb931a 100644 (file)
@@ -51,12 +51,10 @@ class BaseHandler(RequestHandler):
                 main_config = config.get_main()
                 
                 if user == main_config.get('@admin_username') and pwd == main_config.get('@admin_password'):
-                    logging.debug('authenticated as admin')
                     
                     return 'admin'
                 
                 elif user == main_config.get('@normal_username') and pwd == main_config.get('@normal_password'):
-                    logging.debug('authenticated as normal user')
                     
                     return 'normal'
                 
index 5cddb1c69931b10aaa66acf83660c52d535de584..006e979250a78e7909d1a4f1455e57349f0a7cc5 100644 (file)
@@ -7,6 +7,21 @@ import settings
 import template
 
 
+def log_request(handler):
+    if handler.get_status() < 400:
+        log_method = logging.debug
+    
+    elif handler.get_status() < 500:
+        log_method = logging.warning
+    
+    else:
+        log_method = logging.error
+    
+    request_time = 1000.0 * handler.request.request_time()
+    log_method("%d %s %.2fms", handler.get_status(),
+               handler._request_summary(), request_time)
+
+
 application = Application(
     [
         (r'^/$', handlers.MainHandler),
@@ -19,7 +34,7 @@ application = Application(
         (r'^/movie/(?P<camera_id>\d+)/(?P<op>download)/(?P<filename>.+)/?$', handlers.MovieHandler),
     ],
     debug=settings.DEBUG,
-    log_function=logging.debug,
+    log_function=log_request,
     static_path=settings.STATIC_PATH,
     static_url_prefix=settings.STATIC_URL
 )