Commits

Chris Perl committed 05aaf83

Adding two more aggregators

  • Participants
  • Parent commits 3d75cf7

Comments (0)

Files changed (1)

 #       TotalHitsByTimeToRender
 #       HitsPerUrlPerIP
 #       same as above with per unit time
+#       same as above with time to render
 #       HitsPerIPPerUrl
 #       same as above with per unit time
+#       same as above with time to render
 
 class CmdResponse(object):
     def __init__(self):
             display_obj.write(format_str % (key, self.aggr_data[key]))
 
 class HitsPerUrl(CountAggr):
-    # __call__ requires start, end keyword args
+    # __call__ requires start, end, re_filter keyword args
     def _aggregate(self, datetimeobj, url, ms, ip):
         self._key = url
         super(HitsPerUrl, self)._aggregate(datetimeobj, url, ms, ip)
         super(HitsPerUrl, self).display(display_obj, num, "%-100s %10d\n")
 
 class HitsPerIP(CountAggr):
-    # __call__ requires start, end keyword args
+    # __call__ requires start, end, re_filter keyword args
     def _aggregate(self, datetimeobj, url, ms, ip):
         self._key = ip
         super(HitsPerIP, self)._aggregate(datetimeobj, url, ms, ip)
     # __call__ requires start, end, step, re_filter keyword args
     def _aggregate(self, datetimeobj, url, ms, ip):
         self._key = url 
-        return super(HitsPerUrlPerUnitTime, self)._aggregate(datetimeobj, url, ms, ip)
+        super(HitsPerUrlPerUnitTime, self)._aggregate(datetimeobj, url, ms, ip)
 
 class HitsPerIPPerUnitTime(PerUnitTimeAggr):
     # __call__ requires start, end, step, re_filter keyword args
     def _aggregate(self, datetimeobj, url, ms, ip):
         self._key = ip
-        return super(HitsPerIPPerUnitTime, self)._aggregate(datetimeobj, url, ms, ip)
+        super(HitsPerIPPerUnitTime, self)._aggregate(datetimeobj, url, ms, ip)
 
 class TimeToRenderLinearAggr(Aggregator):
     def _aggregate(self, datetimeobj, url, ms, ip):
     # __call__ requires start, end, ms_step, re_filter keyword args
     def _aggregate(self, datetimeobj, url, ms, ip):
         self._key = url
-        return super(HitsPerUrlByTimeToRenderLinear, self)._aggregate(datetimeobj, url, ms, ip)
+        super(HitsPerUrlByTimeToRenderLinear, self)._aggregate(datetimeobj, url, ms, ip)
 
 class HitsPerIPByTimeToRenderLinear(TimeToRenderLinearAggr):
     # __call__ requires start, end, ms_step, re_filter keyword args
     def _aggregate(self, datetimeobj, url, ms, ip):
         self._key = ip
-        return super(HitsPerIPByTimeToRenderLinear, self)._aggregate(datetimeobj, url, ms, ip)
+        super(HitsPerIPByTimeToRenderLinear, self)._aggregate(datetimeobj, url, ms, ip)
+
+class UrlAndIPAggr(Aggregator):
+    def _aggregate(self, datetimeobj, url, ms, ip):
+        if self._re_filter and not self._re_filter.search(self._key1):
+            return
+        sub_aggr = self.aggr_data.setdefault(self._key1, {'hits': 0})
+        sub_aggr['hits'] += 1
+        hits_for_second_key = sub_aggr.setdefault(self._key2, 0)
+        sub_aggr[self._key2] = hits_for_second_key + 1
+
+    def display(self, display_obj):
+        sorted_keys = sorted(self.aggr_data, lambda a,b: cmp(self.aggr_data[a]['hits'], self.aggr_data[b]['hits']))
+        for key in sorted_keys:
+            display_obj.write(self._key1_fmt % (key, self.aggr_data[key]['hits']))
+            second_level_keys = [ x for x in self.aggr_data[key] if x != 'hits' ]
+            sorted_second_level_keys = sorted(second_level_keys, lambda a,b: cmp(self.aggr_data[key][a], self.aggr_data[key][b]))
+            for second_key in sorted_second_level_keys:
+                display_obj.write(self._key2_fmt % (second_key, self.aggr_data[key][second_key]))
+            display_obj.write("\n")
+
+class HitsPerUrlPerIP(UrlAndIPAggr):
+    # __call__ requires start, end, re_filter keyword args
+    def _aggregate(self, datetimeobj, url, ms, ip):
+        # TODO: move this stuff into __init__
+        self._key1 = url
+        self._key2 = ip
+        self._key1_fmt = "%-100s %10d\n"
+        self._key2_fmt = "\t%16s %10d\n"
+        super(HitsPerUrlPerIP, self)._aggregate(datetimeobj, url, ms, ip)
+
+class HitsPerIPPerUrl(UrlAndIPAggr):
+    # __call__ requires start, end, re_filter keyword args
+    def _aggregate(self, datetimeobj, url, ms, ip):
+        # TODO: move this stuff into __init__
+        self._key1 = ip
+        self._key2 = url
+        self._key1_fmt = "%16s %10d\n"
+        self._key2_fmt = "\t%-100s %10d\n"
+        super(HitsPerIPPerUrl, self)._aggregate(datetimeobj, url, ms, ip)
 
 class LogAnalyzer(object):
 
         self._setup_parser_do_toptalkers()
         self._setup_parser_do_url_hits_per_time()
         self._setup_parser_do_url_time_to_render()
+        self._setup_parser_do_url_hits_per_ip()
         self._setup_parser_do_ip_hits_per_time()
         self._setup_parser_do_ip_time_to_render()
+        self._setup_parser_do_ip_hits_per_url()
 
         self._hpu      = HitsPerUrl(self)
         self._hpi      = HitsPerIP(self)
         self._hpiput   = HitsPerIPPerUnitTime(self)
         self._hpubttrl = HitsPerUrlByTimeToRenderLinear(self)
         self._hpibttrl = HitsPerIPByTimeToRenderLinear(self)
+        self._hpupi    = HitsPerUrlPerIP(self)
+        self._hpipu    = HitsPerIPPerUrl(self)
 
     def cmd_loop(self):
         while True:
         self._parser_do_url_time_to_render.add_option('-s', '--step', action='store', type='int', dest='step')
         self._parser_do_url_time_to_render.add_option('-f', '--filter', action='store', type='str', dest='re_filter')
 
+    def _setup_parser_do_url_hits_per_ip(self):
+        self._parser_do_url_hits_per_ip = NonExitingOptionParser(usage="""url-hits-per-ip --filter FILTER""")
+        self._parser_do_url_hits_per_ip.add_option('-f', '--filter', action='store', type='str', dest='re_filter')
+
     def _setup_parser_do_toptalkers(self):
         self._parser_do_toptalkers = NonExitingOptionParser(usage="toptalkers [number of IPs to display]")
         self._parser_do_toptalkers.add_option('-f', '--filter', action='store', type='str', dest='re_filter')
         self._parser_do_ip_time_to_render.add_option('-s', '--step', action='store', type='int', dest='step')
         self._parser_do_ip_time_to_render.add_option('-f', '--filter', action='store', type='str', dest='re_filter')
 
+    def _setup_parser_do_ip_hits_per_url(self):
+        self._parser_do_ip_hits_per_url = NonExitingOptionParser(usage="""ip-hits-per-url --filter FILTER""")
+        self._parser_do_ip_hits_per_url.add_option('-f', '--filter', action='store', type='str', dest='re_filter')
+
     def _do_set(self, *cmd_args):
         options, args = self._parser_do_set.parse_args(list(cmd_args))
         if len(args) != 2:
         self._hpibttrl(start=self.start, end=self.end, ms_step=step, re_filter=options.re_filter)
         self._hpibttrl.display(self._display_obj)
 
+    def _do_url_hits_per_ip(self, *cmd_args):
+        options, args = self._parser_do_url_hits_per_ip.parse_args(list(cmd_args))
+        self._hpupi(start=self.start, end=self.end, re_filter=options.re_filter)
+        self._hpupi.display(self._display_obj)
+
+    def _do_ip_hits_per_url(self, *cmd_args):
+        options, args = self._parser_do_ip_hits_per_url.parse_args(list(cmd_args))
+        self._hpipu(start=self.start, end=self.end, re_filter=options.re_filter)
+        self._hpipu.display(self._display_obj)
+
 def main():
     log_analyzer = LogAnalyzer()
     for path in sys.argv[1:]: