György Kohut avatar György Kohut committed 23bda10

code cleanup

Comments (0)

Files changed (11)

hbwebui/decorators.py

 def ident_required_simple(func):
     return ident_required(func, simple=True)
 
-def query_filter(func):
-    @wraps(func, assigned=available_attrs(func))
-    def inner(request, *args, **kwargs):
-        #filter_exp = kwargs['filter_exp']
-        #filters_map = {}
-        #if filter_exp:
-        #    try:
-        #        for cond in re.split(r'//*', filter_exp):
-        #            k, v = re.split(r':', cond)
-        #            v = filter(None, set(r.split(r',', v)))
-        #            if v:
-        #                filters_map[k] = v
-        #    except Exception:
-        #            pass
-        #else:
-        #        filters_map = {}
-        return func(request, *args, **kwargs)
-    return inner
-
 def handle_filter(name, r):
     def decorator(func):
         @wraps(func, assigned=available_attrs(func))

hbwebui/queries.py

 
 class QueryBase(object):
     def __init__(self):
-        self.map = {}
-        self.list = []
+        self.parts_map = {}
+        self.parts_list = []
         self.cursor = None
     
     def part_init(self, part):
-        self.map[part] = []
-        self.map[part].append(set()) # [0]: set of subpart names, to filter duplicates, trigger insertion
-        self.map[part].append('') # [1]: query fragment text
-        self.map[part].append([]) # [2]: query params
-        self.list.append(part)
+        self.parts_map[part] = []
+        self.parts_map[part].append(set()) # [0]: set of subpart names, to filter duplicates, trigger insertion
+        self.parts_map[part].append('') # [1]: query fragment text
+        self.parts_map[part].append([]) # [2]: query params
+        self.parts_list.append(part)
     
     def part_append(self, part, subpart, sep, text, params):
-        if subpart in self.map[part][0]: # filter duplicate subparts
+        if subpart in self.parts_map[part][0]: # filter duplicate subparts
             return False
         else:
-            if len(self.map[part][0]):
-                self.map[part][1] += sep + text
+            if len(self.parts_map[part][0]):
+                self.parts_map[part][1] += sep + text
             else:
-                self.map[part][1] += text
-            self.map[part][2].extend(params)
-            self.map[part][0].add(subpart)
+                self.parts_map[part][1] += text
+            self.parts_map[part][2].extend(params)
+            self.parts_map[part][0].add(subpart)
             return True
     
     def part_append2(self, part, text):
-        self.map[part][1] += text
+        self.parts_map[part][1] += text
     
     def part_init2(self, part, text):
         self.part_init(part)
         params = []
         format_params = {}
         
-        for part in self.list:
-            if len(self.map[part][0]): # got subparts
-                format_params[part] = self.map[part][1]
-                params.extend(self.map[part][2])
+        for part in self.parts_list:
+            if len(self.parts_map[part][0]): # got subparts
+                format_params[part] = self.parts_map[part][1]
+                params.extend(self.parts_map[part][2])
             else:
                 format_params[part] = ''
         
         self.order_by_append('order_n_count', 'n_count', desc)
 
 
-class Stats_Binaries_TimeRange(QueryBase):
-    db = 'hbstats'
-    tpl = \
-        """%(with_clause)s
-        
-        SELECT
-            md5, sum(t.n_count)::bigint as n_count,
-            count(DISTINCT ident_id) AS n_ident, count(DISTINCT source_ip) AS n_source_ip, count(DISTINCT target_port) AS n_target_port,
-            max(t.ts_last) AS ts_last, min(t.ts_first) AS ts_first
-        FROM
-            (SELECT
-                agg_id, sum(n_count) as n_count, max(ts_min) as ts_last, min(ts_min) as ts_first
-                FROM ts_main_min
-                WHERE agg_id IN (SELECT agg_id FROM agg_main %(where_clause)s)
-                GROUP BY agg_id
-            ) as t  %(with_tables)s
-        JOIN
-            agg_main ON t.agg_id = agg_main.agg_id
-        JOIN
-            dim_binary ON agg_main.binary_id = dim_binary.id
-        GROUP BY md5
-        %(order_by_clause)s"""
-    
-    def __init__(self):
-        super(Stats_Binaries_TimeRange, self).__init__()
-        self.with_init()
-        self.where_init()
-        self.order_by_init()
-        
-    def filter_time_range_recent(self, hours):
-        b = self.where_append('filter_time_range', 'ts_min <= tr_end AND ts_min > tr_begin', [begin, end])
-        if b:
-            self.with_append('filter_time_range',
-                "tr AS SELECT max(ts_min) AS tr_end, max(ts_min) - interval '%s hour' AS tr_begin from ts_main_min)",
-                'tr',
-                [hours]
-            )
-    
-    def filter_time_range(self, begin, end):
-        self.where_append('filter_time_range', 'ts_min >= %s AND ts_min <= %s', [begin, end])
-    
-    def filter_ident(self, idents):
-        self.where_append('filter_ident', 'ident_id IN (SELECT id from dim_ident WHERE ident IN %s)', [tuple(idents)])
-    
-    def filter_source_ip(self, source_ips):
-        self.where_append('filter_source_ip', 'source_ip IN %s', [tuple(source_ips)])
-    
-    def order_n_count(self, desc=False):
-        self.order_by_append('order_n_count', 'n_count', desc)
-
-
 class Stats_Binaries_Summary(QueryBase):
     db = 'hbstats'
     tpl = \
         
     def order_n_count(self, desc=False):
         self.order_by_append('order_n_count', 'n_count', desc)
+
+
+class Stats_Binaries_TimeRange(QueryBase):
+    db = 'hbstats'
+    tpl = \
+        """%(with_clause)s
+        
+        SELECT
+            md5, sum(t.n_count)::bigint as n_count,
+            count(DISTINCT ident_id) AS n_ident, count(DISTINCT source_ip) AS n_source_ip, count(DISTINCT target_port) AS n_target_port,
+            max(t.ts_last) AS ts_last, min(t.ts_first) AS ts_first
+        FROM
+            (SELECT
+                agg_id, sum(n_count) as n_count, max(ts_min) as ts_last, min(ts_min) as ts_first
+                FROM ts_main_min
+                WHERE agg_id IN (SELECT agg_id FROM agg_main %(where_clause)s)
+                GROUP BY agg_id
+            ) as t  %(with_tables)s
+        JOIN
+            agg_main ON t.agg_id = agg_main.agg_id
+        JOIN
+            dim_binary ON agg_main.binary_id = dim_binary.id
+        GROUP BY md5
+        %(order_by_clause)s"""
+    
+    def __init__(self):
+        super(Stats_Binaries_TimeRange, self).__init__()
+        self.with_init()
+        self.where_init()
+        self.order_by_init()
+        
+    def filter_time_range_recent(self, hours):
+        b = self.where_append('filter_time_range', 'ts_min <= tr_end AND ts_min > tr_begin', [begin, end])
+        if b:
+            self.with_append('filter_time_range',
+                "tr AS SELECT max(ts_min) AS tr_end, max(ts_min) - interval '%s hour' AS tr_begin from ts_main_min)",
+                'tr',
+                [hours]
+            )
+    
+    def filter_time_range(self, begin, end):
+        self.where_append('filter_time_range', 'ts_min >= %s AND ts_min <= %s', [begin, end])
+    
+    def filter_ident(self, idents):
+        self.where_append('filter_ident', 'ident_id IN (SELECT id from dim_ident WHERE ident IN %s)', [tuple(idents)])
+    
+    def filter_source_ip(self, source_ips):
+        self.where_append('filter_source_ip', 'source_ip IN %s', [tuple(source_ips)])
+    
+    def order_n_count(self, desc=False):
+        self.order_by_append('order_n_count', 'n_count', desc)
+
+    

hbwebui/templates/stats_asn.html

 {% include "navbar.html" %}
 
 <div class="container-fluid">
-    <h3>Stats for ASN {{ asn }}</h3>
+    <h3>Stats for ASN: {{ asn }}</h3>
     <hr/>
     <div class="row-fluid">
         <h3>By Ident</h3>

hbwebui/templates/stats_asns.html

     urls = {
         'mod_summary': '{% url r_stats_asns_summary %}',
         'mod_table': '{% url r_stats_asns %}',
-    };
-    
-    var idents = {{ idents|safe }};
-    
+    };    
     
     $(
         function() {

hbwebui/templates/stats_binaries.html

     urls = {
         'mod_summary': '{% url r_stats_binaries_summary %}',
         'mod_table': '{% url r_stats_binaries %}',
-    };
-    
-    var idents = {{ idents|safe }};
-    
+    };    
     
     $(
         function() {

hbwebui/templates/stats_binary.html

 {% include "navbar.html" %}
 
 <div class="container-fluid">
-    <h3>Stats for binary {{ md5 }}</h3>
+    <h3>Stats for binary: {{ md5 }}</h3>
     <hr/>
     <div class="row-fluid">
         <h3>By Ident</h3>

hbwebui/templates/stats_countries.html

     urls = {
         'mod_summary': '{% url r_stats_countries_summary %}',
         'mod_table': '{% url r_stats_countries %}',
-    };
-    
-    var idents = {{ idents|safe }};
-    
+    };    
     
     $(
         function() {

hbwebui/templates/stats_idents.html

     urls = {
         'mod_summary': '{% url r_stats_idents_summary %}',
         'mod_table': '{% url r_stats_idents %}',
-    };
-    
-    var idents = {{ idents|safe }};
-    
+    };    
     
     $(
         function() {

hbwebui/templates/stats_source_ips.html

     urls = {
         'mod_summary': '{% url r_stats_source_ips_summary %}',
         'mod_table': '{% url r_stats_source_ips %}',
-    };
-    
-    var idents = {{ idents|safe }};
-    
+    };    
     
     $(
         function() {
     url(r'^r/stats/idents/summary$', 'r_stats_idents_summary', name='r_stats_idents_summary'),
     
     url(r'^r/stats/target_ports$', 'r_stats_target_ports', name='r_stats_target_ports'),
-
-    
-    url(r'^r/myidents', 'myidents', name='myidents'),
     
+        
     url(r'^r/sensoractivity_24', 'sensoractivity', {'hours': 24}, name='sensoractivity_24'),
     url(r'^r/sensoractivity_168', 'sensoractivity', {'hours': 168}, name='sensoractivity_168'),
     
     t = loader.get_template('stats_binaries.html')
     c = RequestContext(request, {
             'page': 'stats_binaries',
-            'idents': json.dumps(idents),
-            'r': json.dumps(r)
     })
     return HttpResponse(t.render(c))
 
     t = loader.get_template('stats_source_ips.html')
     c = RequestContext(request, {
             'page': 'stats_source_ips',
-            'idents': json.dumps(idents),
     })
     return HttpResponse(t.render(c))
 
     t = loader.get_template('stats_asns.html')
     c = RequestContext(request, {
             'page': 'stats_asns',
-            'idents': json.dumps(idents),
     })
     return HttpResponse(t.render(c))
 
     t = loader.get_template('stats_countries.html')
     c = RequestContext(request, {
             'page': 'stats_countries',
-            'idents': json.dumps(idents),
     })
     return HttpResponse(t.render(c))
 
     t = loader.get_template('stats_idents.html')
     c = RequestContext(request, {
             'page': 'stats_idents',
-            'idents': json.dumps(idents),
     })
     return HttpResponse(t.render(c))
 
     c = RequestContext(request, {
             'page': 'stats_binary',
             'md5': md5,
-            'idents': json.dumps(idents),
     })
     return HttpResponse(t.render(c))
 
     c = RequestContext(request, {
             'page': 'stats_ident',
             'ident': ident,
-            'idents': json.dumps(idents),
     })
     return HttpResponse(t.render(c))
 
     c = RequestContext(request, {
             'page': 'stats_source_ip',
             'source_ip': source_ip,
-            'idents': json.dumps(idents),
     })
     return HttpResponse(t.render(c))
 
     c = RequestContext(request, {
             'page': 'stats_asn',
             'asn': asn,
-            'idents': json.dumps(idents),
     })
     return HttpResponse(t.render(c))
 
     c = RequestContext(request, {
             'page': 'stats_country',
             'cc': cc,
-            'idents': json.dumps(idents),
     })
     return HttpResponse(t.render(c))
 
-# example
-@login_required_simple
-@ident_required_simple
-def myidents(request):
-    idents = request.session['idents']
-    
-    response = HttpResponse(mimetype='application/json')
-    json.dump(idents, response)
-        
-    return response
-
 #example
 @login_required_simple
 @ident_required_simple
             SELECT
                 extract(epoch from ts_min)::bigint AS ts_min,
                 ident, md5, source_ip,
-                sum(t.n_count) AS n_count       
+                sum(t.n_count) AS n_count  
             FROM
                 (SELECT
                      ts_min, agg_id, n_count
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.