Commits

Chris Perl committed beb02bc

Iterating a little further

  • Participants
  • Parent commits c1c3f8d

Comments (0)

Files changed (1)

         if self._start == start and self._end == end: 
             self.output(num_urls)
         else:
+            self.clear()
             self._start = start
             self._end = end
             for datetimeobj, url, ms, ip in self._log_analyzer.log_data:
                 print "%6d %-40s" % (self[url]['count'], url)
 
 class LinearQuantizeTimeOfDay(UrlAggregator):
-    def __init__(self, period, start, end):
-        self._time_period = end - start
-        self._period = int(period)
-        self._num_buckets = (self._time_period.seconds + (self._time_period.days*24*3600)) / self._period
-        self._start = start
-        self._end = end
+    def __init__(self, log_analyzer):
+        self._log_analyzer = log_analyzer
+        self._start = None
+        self._end = None
 
-    def add(self, datetimeobj, url, ip, ms):
+    def __call__(self, start, end, period):
+        if self._start == start and self._end == end and self._period == int(period):
+            self.output()
+        else:
+            self.clear()
+            self._time_period = end - start
+            self._period = int(period)
+            # FIXME: Calculation of _num_buckets is wrong when there are say 3599 seconds in
+            # _time_period
+            self._num_buckets = (self._time_period.seconds + (self._time_period.days*24*3600)) / self._period
+            self._start = start
+            self._end = end
+            for datetimeobj, url, ms, ip in self._log_analyzer.log_data:
+                if start <= datetimeobj <= end:
+                    self.update(datetimeobj, url, ms, ip)
+            self.output()
+
+    def update(self, datetimeobj, url, ms, ip):
         url_dict = self.setdefault(url, {'count': 0, 
                                          'buckets': [0 for x in range(self._num_buckets)]})
         url_dict['count'] += 1
         "Dec": 12
     }
 
-
     def __init__(self):
         self.log_data = []
         self.start = None
         self.end = None
 
         self.cmd_map = {
-            "topurls": TopUrls(self)
+            "topurls": TopUrls(self),
+            "lquantize-timeofday": LinearQuantizeTimeOfDay(self),
         }
 
     def cmd_loop(self):
         while True:
             try:
-                line = raw_input("\rLogAnalyzer> ")
+                line = raw_input("\rstart:%s, end:%s >> " % (self.start, self.end))
                 if not line or line.lstrip().rstrip() == '':
                     continue
+
+                # TODO: This splitting is not appropriate for all cases and causes exceptions, i.e.
+                # 'nocmd'
+                func, args = line.split(' ', 1)
+                if func.startswith('\\'):
+                        # internal command, not for an aggregator
+                        # i.e. setting start and end times
+                        func = func[1:]
+                        method = "_%s" % func
+                        if hasattr(self, method):
+                            getattr(self, method)(*args.split(' '))
+                            continue
+
                 for cmd in self.cmd_map.keys():
-                    func, args = line.split(' ', 1)
                     if func.lower() == cmd:
                         self.cmd_map[cmd](self.start, self.end, *args.split(' '))
                         break
         line = re.sub('&_=\d+', '', line)
         return line
 
+    def _set(self, what, arg):
+        if what == "start":
+            self.start = datetime(*[ int(x) for x in arg.split('-') ])
+        elif what == "end":
+            self.end = datetime(*[int(x) for x in arg.split('-')])
+        else:
+            pass
+        
+
 def main():
     log_analyzer = LogAnalyzer()
     for path in sys.argv[1:]: