This repository has been archived by the owner on Nov 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
62 lines (49 loc) · 1.74 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from flask import Flask, request, render_template, abort
from flask_jsonpify import jsonify
from access_control_decorator import crossdomain, requires_auth
import redis
import urlparse
app = Flask(__name__)
if __name__ == "__main__":
app.config.from_object('config.ConfigLocal')
else:
app.config.from_object('config.ConfigProduction')
#REDIS_URL contains auth+port, but StrictRedis constructor needs it separately
redis_url = urlparse.urlparse(app.config['REDIS_URL'])
r = redis.StrictRedis(host=redis_url.hostname,
port=redis_url.port,
password=redis_url.password)
@app.route("/")
def home():
return "Host Counter"
@app.route("/log", methods=['POST','OPTIONS'])
@crossdomain(origin='*')
def log():
if request.form:
data = request.form
if 'host' in data:
#cache key as {host}:{campaign}:{stat}
host_key = '%(host)s:%(campaign)s:%(stat)s' % data
r.zincrby('host', host_key, 1)
campaign_key = '%(campaign)s:%(stat)s' % data
r.zincrby('campaign', campaign_key, 1)
stat_key = '%(stat)s' % data
r.zincrby('stat', stat_key, 1)
return jsonify({'status':"OK"})
else:
return abort(422, {'error':"missing host"})
return abort(400, {'error':"missing data"})
@app.route('/list', methods=['GET',])
@requires_auth
def list():
hosts = r.zrevrange('host',0,10, withscores=True)
#clean up for display
top_hosts = []
for (key,val) in hosts:
d = { 'host': key.split(':')[0],
'hits': int(val)
}
top_hosts.append(d)
return render_template('list.html', top_hosts=top_hosts)
if __name__ == "__main__":
app.run(host='0.0.0.0')