Add my apachelog.processor.time.LogTimeProcessor.
authorW. Trevor King <wking@drexel.edu>
Sat, 18 Feb 2012 19:20:56 +0000 (14:20 -0500)
committerW. Trevor King <wking@drexel.edu>
Sat, 18 Feb 2012 19:29:06 +0000 (14:29 -0500)
apachelog/processor/time.py [new file with mode: 0644]

diff --git a/apachelog/processor/time.py b/apachelog/processor/time.py
new file mode 100644 (file)
index 0000000..3fe56e5
--- /dev/null
@@ -0,0 +1,48 @@
+from __future__ import division
+
+import datetime as _datetime
+
+from ..date import parse_time as _parse_time
+from . import Processor as _Processor
+
+
+class LogTimeProcessor (_Processor):
+    r"""Track the initial and final request times.
+
+    >>> import StringIO
+    >>> from apachelog.parser import Parser, FORMATS
+    >>> from apachelog.processor import Processor, process
+    >>> class PrintLogTimeProcessor (Processor):
+    ...     def __init__(self, log_time_processor):
+    ...         self.log_time_processor = log_time_processor
+    ...     def process(self, data):
+    ...         print('{}: {}'.format(data['%t'], self.log_time_processor.last_time))
+    >>> stream = StringIO.StringIO('\n'.join([
+    ...         '192.168.0.1 - - [18/Feb/2012:10:25:43 -0500] "GET / HTTP/1.1" 200 561 "-" "Mozilla/5.0 (...)"',
+    ...         '192.168.0.2 - - [18/Feb/2012:10:25:58 -0500] "GET / HTTP/1.1" 200 561 "-" "Mozilla/5.0 (...)"',
+    ...         ]))
+    >>> parser = Parser(FORMATS['extended'])
+    >>> ltp = LogTimeProcessor()
+    >>> processors = [ltp, PrintLogTimeProcessor(ltp)]
+    >>> process(stream, parser, processors)
+    [18/Feb/2012:10:25:43 -0500]: 2012-02-18 10:25:43-05:00
+    [18/Feb/2012:10:25:58 -0500]: 2012-02-18 10:25:58-05:00
+    >>> ltp.total_seconds()
+    15.0
+    """
+    def __init__(self):
+        self.last_time = self.start_time = self.stop_time = None
+
+    def process(self, data):
+        time = _parse_time(data['%t'])
+        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
+        if self.stop_time is None or time > self.stop_time:
+            self.stop_time = time
+
+    def total_seconds(self):
+        if self.start_time is None:
+            return 0
+        dt = self.stop_time - self.start_time
+        return dt.total_seconds()