Avoid duplicate time parsings when multiple LogTimeProcessors are used.
authorW. Trevor King <wking@drexel.edu>
Mon, 20 Feb 2012 13:37:27 +0000 (08:37 -0500)
committerW. Trevor King <wking@drexel.edu>
Mon, 20 Feb 2012 13:37:27 +0000 (08:37 -0500)
Now later LogTimeProcessors can use the parsed value from the first
LogTimeProcessor in your processor stack.

apachelog-process.py
apachelog/processor/time.py

index 531a8eec2a7d61ef85efa2d79a9829ef1667d533..6eae031dc639e5c41fcdca00b9fa0d32f745f253 100755 (executable)
@@ -21,6 +21,7 @@ from apachelog.processor.bandwidth import (
     IPBandwidthProcessor as _IPBandwidthProcessor)
 from apachelog.processor.set import SetProcessor as _SetProcessor
 from apachelog.processor.status import StatusProcessor as _StatusProcessor
+from apachelog.processor.time import LogTimeProcessor as _LogTimeProcessor
 from apachelog.resolve import Resolver as _Resolver
 
 
@@ -127,6 +128,7 @@ if __name__ == '__main__':
         resolver = None
 
     processors = []
+    log_time_processor = None
     for processor in sorted(PROCESSORS.keys()):
         pattr = processor.replace('-', '_')
         if not getattr(args, pattr):
@@ -134,7 +136,12 @@ if __name__ == '__main__':
         kwargs = {}
         if pattr == 'set':
             kwargs['keys'] = args.key
+        if (log_time_processor is not None and
+            issubclass(PROCESSORS[processor], _LogTimeProcessor)):
+            kwargs['previous_log_time_processor'] = log_time_processor
         p = PROCESSORS[processor](**kwargs)
+        if log_time_processor is None and isinstance(p, _LogTimeProcessor):
+            log_time_processor = p
         processors.append(p)
 
     for filename in args.file:
index 5281b081849a51a9257947fbb05c569c292b5ef7..c6c1aae3bbd41d0e28bb35fb82729620cbd7b177 100644 (file)
@@ -28,11 +28,15 @@ class LogTimeProcessor (_Processor):
     >>> ltp.total_seconds()
     15.0
     """
-    def __init__(self):
+    def __init__(self, previous_log_time_processor=None):
+        self.previous_log_time_processor = previous_log_time_processor
         self.last_time = self.start_time = self.stop_time = None
 
     def process(self, data):
-        time = _parse_time(data['%t'])
+        if self.previous_log_time_processor is None:
+            time = _parse_time(data['%t'])
+        else:  # avoid re-parsing the time data
+            time = self.previous_log_time_processor.last_time
         self.last_time = time  # for use by subclasses or other processors
         if self.start_time is None or time < self.start_time:
             self.start_time = time