From 073b3914e77e249d22f392d227b588c51def2bc6 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 20 Feb 2012 08:37:27 -0500 Subject: [PATCH] Avoid duplicate time parsings when multiple LogTimeProcessors are used. Now later LogTimeProcessors can use the parsed value from the first LogTimeProcessor in your processor stack. --- apachelog-process.py | 7 +++++++ apachelog/processor/time.py | 8 ++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/apachelog-process.py b/apachelog-process.py index 531a8ee..6eae031 100755 --- a/apachelog-process.py +++ b/apachelog-process.py @@ -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: diff --git a/apachelog/processor/time.py b/apachelog/processor/time.py index 5281b08..c6c1aae 100644 --- a/apachelog/processor/time.py +++ b/apachelog/processor/time.py @@ -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 -- 2.26.2