Display total count in fingerprint column
[compass/delber.git] / app.py
blob40de2fa7ba4db684179ec824e80ff39a2cf4b275
1 import os
2 import re
3 import compass
4 from flask import Flask, request, redirect, render_template, url_for
6 app = Flask(__name__)
8 class Opt(object):
9 def __init__(self):
10 self.by_as = False
11 self.family = None
12 self.by_country = False
13 self.ases = None
14 self.country = None
15 self.top = -1
16 self.short = None
17 self.links = None
18 self.guards_only = None
19 self.inactive = False
20 self.almost_fast_exits_only = None
21 self.exits_only = False
22 self.download = None
23 self.fast_exits_only = False
24 self.fast_exits_only_any_network = False
25 self.all_relays = False
27 class Result():
28 def __init__(self):
29 self.cw = None
30 self.adv_bw = None
31 self.p_guard = None
32 self.p_exit = None
33 self.p_middle = None
34 self.nick = None
35 self.fp = None
36 self.link = None
37 self.exit = None
38 self.guard = None
39 self.cc = None
40 self.as_no = None
41 self.as_name = None
43 def parse(output_string, grouping=False, sort_key=None):
44 results = []
45 sorted_results = {}
47 for id, line in enumerate(output_string):
48 # skip headings
49 if id == 0: continue
51 result = Result()
52 values = line.split()
54 """
55 This is a super weird hack. When we group by country or AS, the
56 nickname is replaced with '(x relays)' which when split() creates
57 ['(x','relays)']. I need to join this again and then left shift all
58 the elements and delete the last element in the list.
59 """
60 if grouping:
61 values[6] = "%s %s" % (values[6], values[7])
62 for id in xrange(8, len(values)):
63 values[id-1] = values[id]
64 del values[-1]
66 # TODO: change inaccurate value of 10
67 if len(values) > 10:
68 result.cw = values[0]
69 result.adv_bw = values[1]
70 result.p_guard = values[2]
71 result.p_middle = values[3]
72 result.p_exit = values[4]
73 result.nick = values[5]
74 result.fp = values[6]
75 result.exit = values[7]
76 result.guard = values[8]
77 result.cc = values[9]
78 result.as_no = values[10]
79 result.as_name = ' '.join(values[11:])
80 result.as_name = re.sub(r'\([^)]*\)', '', result.as_name)
82 if sort_key:
83 key = float(getattr(result, sort_key)[:-1])
84 if sorted_results.has_key(key):
85 sorted_results[key].append(result)
86 else:
87 sorted_results[key] = [result]
88 else:
89 results.append(result)
90 return results if results else sorted_results
92 @app.route('/')
93 def index():
94 return render_template('index.html')
96 @app.route('/result', methods=['GET'])
97 def result():
98 options = Opt()
99 sort_key = None
100 relays = []
102 for key, value in request.args.items():
103 if key == "top":
104 try:
105 options.top = int(value)
106 except:
107 options.top = -1
108 elif key == "sort":
109 sort_key = value
110 elif key in ["country", "ases"]:
111 if value:
112 setattr(options, key, [value])
113 else:
114 setattr(options, key, None)
115 elif key == "exits":
116 setattr(options, value, True)
117 else:
118 setattr(options, key, value)
120 stats = compass.RelayStats(options)
121 sorted_groups = stats.format_and_sort_groups(stats.relays,
122 by_country=options.by_country,
123 by_as_number=options.by_as,
124 links=options.links)
125 output_string = stats.print_groups(sorted_groups, options.top,
126 by_country=options.by_country,
127 by_as_number=options.by_as,
128 short=None,
129 links=None)
130 results = parse(output_string, options.by_country or options.by_as, sort_key)
132 if sort_key:
133 for key in sorted(results.iterkeys(), reverse=True):
134 for value in results[key]:
135 relays.append(value)
136 else:
137 relays = results
139 return render_template('result.html', results=relays)
141 if __name__ == '__main__':
142 # Bind to PORT if defined, otherwise default to 5000.
143 port = int(os.environ.get('PORT', 5000))
144 app.run(host='0.0.0.0', port=port, debug=True)