|
| 1 | +from flask import Flask, render_template, request |
| 2 | +import flask_resize |
| 3 | +import redis |
| 4 | +import time |
| 5 | +import boto3 |
| 6 | + |
| 7 | +w = redis.Redis(host='oktank-cluster.wraqgj.0001.usw2.cache.amazonaws.com', port=6379, db=0, charset="utf-8", decode_responses=True) |
| 8 | +r = redis.Redis(host='oktank-cluster.wraqgj.0001.usw2.cache.amazonaws.com', port=6379, db=0, charset="utf-8", decode_responses=True) |
| 9 | +dynamodb = boto3.resource('dynamodb', region_name='us-west-2') |
| 10 | +table = dynamodb.Table('oktank-Books') |
| 11 | + |
| 12 | + |
| 13 | +def get_hit_count(): |
| 14 | + retries = 5 |
| 15 | + while True: |
| 16 | + try: |
| 17 | + return w.incr("foo") |
| 18 | + except redis.exceptions.ConnectionError as exc: |
| 19 | + if retries == 0: |
| 20 | + raise exc |
| 21 | + retries -= 1 |
| 22 | + time.sleep(0.5) |
| 23 | + |
| 24 | +def show_top10_NA(): |
| 25 | + retries = 5 |
| 26 | + while True: |
| 27 | + try: |
| 28 | + phones_table = r.zrevrange("TopBooks:NorthAmerica:AllTime", 0, 9, withscores=True) |
| 29 | + return phones_table |
| 30 | + except redis.exceptions.ConnectionError as exc: |
| 31 | + if retries == 0: |
| 32 | + raise exc |
| 33 | + retries -= 1 |
| 34 | + time.sleep(0.5) |
| 35 | + |
| 36 | + |
| 37 | +def show_NA_DDB(): |
| 38 | + list_today_NA_DDB = [] |
| 39 | + i = 0 |
| 40 | + while i < len(top10_NA): |
| 41 | + resp = table.get_item(Key={"id": (top10_NA[i])}, AttributesToGet=["name"]) |
| 42 | + list_NA_DDB.append(resp['Item']) |
| 43 | + i = i + 1 |
| 44 | + return list_NA_DDB |
| 45 | + |
| 46 | +def show_top10_EU(): |
| 47 | + retries = 5 |
| 48 | + while True: |
| 49 | + try: |
| 50 | + tablets_table = r.zrevrange("TopBooks:Europe:AllTime", 0, 9, withscores=True) |
| 51 | + return tablets_table |
| 52 | + except redis.exceptions.ConnectionError as exc: |
| 53 | + if retries == 0: |
| 54 | + raise exc |
| 55 | + retries -= 1 |
| 56 | + time.sleep(0.5) |
| 57 | + |
| 58 | +def show_top10_all(): |
| 59 | + retries = 5 |
| 60 | + while True: |
| 61 | + try: |
| 62 | + #w.zunionstore('top10:all', ['top10:phones', 'top10:tablets']) |
| 63 | + all_table = r.zrevrange("TopBooks:AllTime", 0, 9, withscores=True) |
| 64 | + return all_table |
| 65 | + except redis.exceptions.ConnectionError as exc: |
| 66 | + if retries == 0: |
| 67 | + raise exc |
| 68 | + retries -= 1 |
| 69 | + time.sleep(0.5) |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +def show_today_NA(): |
| 74 | + retries = 5 |
| 75 | + while True: |
| 76 | + try: |
| 77 | + today_NA_table = r.zrevrange("TopBooks:NorthAmerica:Today", 0, 9, withscores=True) |
| 78 | + return today_NA_table |
| 79 | + except redis.exceptions.ConnectionError as exc: |
| 80 | + if retries == 0: |
| 81 | + raise exc |
| 82 | + retries -= 1 |
| 83 | + time.sleep(0.5) |
| 84 | + |
| 85 | +def show_today_EU(): |
| 86 | + retries = 5 |
| 87 | + while True: |
| 88 | + try: |
| 89 | + today_EU_table = r.zrevrange("TopBooks:Europe:Today", 0, 9, withscores=True) |
| 90 | + return today_EU_table |
| 91 | + except redis.exceptions.ConnectionError as exc: |
| 92 | + if retries == 0: |
| 93 | + raise exc |
| 94 | + retries -= 1 |
| 95 | + time.sleep(0.5) |
| 96 | + |
| 97 | +def show_today_all(): |
| 98 | + retries = 5 |
| 99 | + while True: |
| 100 | + try: |
| 101 | + today_all_table = r.zrevrange("TopBooks:Today", 0, 9, withscores=True) |
| 102 | + return today_all_table |
| 103 | + except redis.exceptions.ConnectionError as exc: |
| 104 | + if retries == 0: |
| 105 | + raise exc |
| 106 | + retries -= 1 |
| 107 | + time.sleep(0.5) |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +app = Flask(__name__) |
| 114 | +@app.route("/") |
| 115 | +@app.route("/index") |
| 116 | +def index(): |
| 117 | + count = get_hit_count() |
| 118 | + top10_EU = show_top10_EU() |
| 119 | + top10_NA = show_top10_NA() |
| 120 | + top10_all = show_top10_all() |
| 121 | + |
| 122 | + today_NA = show_today_NA() |
| 123 | + today_EU = show_today_EU() |
| 124 | + today_all = show_today_all() |
| 125 | + |
| 126 | + NA_DDB = show_NA_DDB() |
| 127 | + |
| 128 | + return render_template("index.html", title="Dashboard", count=count,top10_EU=top10_EU, top10_NA =top10_NA, top10_all=top10_all, today_NA=today_NA, today_EU=today_EU, today_all=today_all, NA_DDB=NA_DDB, len_NA_DDB = len(NA_DDB), len_NA = len(top10_NA), len_EU = len(top10_EU), len_all = len(top10_all), len_today_NA = len(today_NA), len_today_EU = len(today_EU), len_today_all = len(today_all),) |
| 129 | + |
| 130 | +@app.after_request |
| 131 | +def add_header(response): |
| 132 | + response.cache_control.max_age = 300 |
| 133 | + return response |
| 134 | + |
| 135 | + |
| 136 | +@app.route("/input") |
| 137 | +def input(): |
| 138 | + return render_template("input.html", title="Dashboard") |
| 139 | + |
| 140 | + |
| 141 | +@app.route("/save", methods=['POST']) |
| 142 | +def save(): |
| 143 | + field = request.form['field'] |
| 144 | + value = request.form['value'] |
| 145 | + ret = w.zincrby('TopBooks:AllTime', field, value) |
| 146 | + app.logger.debug(ret) |
| 147 | + new_value = r.zscore('TopBooks:AllTime', value) |
| 148 | + return render_template('output.html', saved=1, phone=value, value=new_value,) |
| 149 | + |
| 150 | +@app.route("/save_tablet", methods=['POST']) |
| 151 | +def save_tablet(): |
| 152 | + field_tablet = request.form['TopBooks:Europe:AllTime'] |
| 153 | + value_tablet = request.form['TopBooks:Europe:AllTime'] |
| 154 | + ret = w.zincrby('top10:tablets', field_tablet, value_tablet) |
| 155 | + app.logger.debug(ret) |
| 156 | + new_value_tablet = r.zscore('TopBooks:AllTime', value_tablet) |
| 157 | + return render_template('output.html', saved_tablet=1, tablet=value_tablet, value_tablet=new_value_tablet,) |
| 158 | + |
| 159 | + |
| 160 | +@app.route("/get", methods=['POST']) |
| 161 | +def get(): |
| 162 | + field = request.form['field'] |
| 163 | + value = r.get(field) |
| 164 | + if value is None: |
| 165 | + return render_template('output.html', field=field, value="Not defined yet") |
| 166 | + return render_template('output.html', field=field, value=value) |
| 167 | + |
| 168 | +if __name__ == "__main__": |
| 169 | + app.run(host= '0.0.0.0', debug=True) |
| 170 | + app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 |
| 171 | + |
| 172 | + |
| 173 | + |
0 commit comments