|
| 1 | +import os |
| 2 | +import sys |
| 3 | +import multiprocessing |
| 4 | +import json |
| 5 | +import gzip |
| 6 | +from io import BytesIO |
| 7 | +import mimetypes |
| 8 | + |
| 9 | +import psycopg_pool |
| 10 | +import psycopg.rows |
| 11 | + |
| 12 | +import bottle |
| 13 | + |
| 14 | +bottle.BaseRequest.MEMFILE_MAX = 31*1024*1024 |
| 15 | + |
| 16 | +from bottle import Bottle, route, request, response, static_file |
| 17 | + |
| 18 | + |
| 19 | +app = Bottle() |
| 20 | + |
| 21 | + |
| 22 | +# -- Dataset and constants -------------------------------------------------------- |
| 23 | + |
| 24 | +CPU_COUNT = int(multiprocessing.cpu_count()) |
| 25 | +WRK_COUNT = min(len(os.sched_getaffinity(0)), 128) |
| 26 | +WRK_COUNT = max(WRK_COUNT, 4) |
| 27 | + |
| 28 | +DATASET_LARGE_PATH = "/data/dataset-large.json" |
| 29 | +DATASET_PATH = os.environ.get("DATASET_PATH", "/data/dataset.json") |
| 30 | +DATASET_ITEMS = None |
| 31 | +try: |
| 32 | + with open(DATASET_PATH) as file: |
| 33 | + DATASET_ITEMS = json.load(file) |
| 34 | +except Exception: |
| 35 | + pass |
| 36 | + |
| 37 | + |
| 38 | +# -- Postgres DB ------------------------------------------------------------ |
| 39 | + |
| 40 | +DATABASE_URL = os.environ.get("DATABASE_URL", '') |
| 41 | +DATABASE_POOL = None |
| 42 | +DATABASE_QUERY = ( |
| 43 | + "SELECT id, name, category, price, quantity, active, tags, rating_score, rating_count" |
| 44 | + " FROM items" |
| 45 | + " WHERE price BETWEEN %s AND %s LIMIT %s" |
| 46 | +) |
| 47 | +if DATABASE_URL and DATABASE_URL.startswith("postgres://"): |
| 48 | + DATABASE_URL = "postgresql://" + DATABASE_URL[len("postgres://"):] |
| 49 | + |
| 50 | +PG_POOL_MIN_SIZE = 1 |
| 51 | +PG_POOL_MAX_SIZE = 2 |
| 52 | + |
| 53 | +def db_close(): |
| 54 | + global DATABASE_POOL |
| 55 | + if DATABASE_POOL: |
| 56 | + try: |
| 57 | + DATABASE_POOL.close() |
| 58 | + except Exception: |
| 59 | + pass |
| 60 | + DATABASE_POOL = None |
| 61 | + |
| 62 | +def db_setup(): |
| 63 | + global DATABASE_POOL, DATABASE_URL, PG_POOL_MIN_SIZE, PG_POOL_MAX_SIZE, WRK_COUNT |
| 64 | + db_close() |
| 65 | + if not DATABASE_URL: |
| 66 | + return |
| 67 | + DATABASE_MAX_CONN = os.environ.get("DATABASE_MAX_CONN", None) |
| 68 | + if DATABASE_MAX_CONN: |
| 69 | + avr_pool_size = int(DATABASE_MAX_CONN) * 0.92 / WRK_COUNT |
| 70 | + #PG_POOL_MIN_SIZE = int(avr_pool_size + 0.35) |
| 71 | + PG_POOL_MAX_SIZE = int(avr_pool_size + 0.95) |
| 72 | + try: |
| 73 | + DATABASE_POOL = psycopg_pool.ConnectionPool( |
| 74 | + conninfo = DATABASE_URL, |
| 75 | + min_size = max(PG_POOL_MIN_SIZE, 1), |
| 76 | + max_size = max(PG_POOL_MAX_SIZE, 2), |
| 77 | + kwargs = { 'row_factory': psycopg.rows.dict_row }, |
| 78 | + ) |
| 79 | + #DATABASE_POOL.wait() |
| 80 | + except Exception: |
| 81 | + DATABASE_POOL = None |
| 82 | + |
| 83 | +db_setup() |
| 84 | + |
| 85 | + |
| 86 | +# -- Bug Fix for chunked body via gunicorn --------------------------------------------- |
| 87 | + |
| 88 | +@app.hook('before_request') |
| 89 | +def fix_chunked_body(): |
| 90 | + if request.chunked: |
| 91 | + request.environ['HTTP_TRANSFER_ENCODING'] = '_C_H_U_N_K_E_D_' |
| 92 | + body = BytesIO() |
| 93 | + while True: |
| 94 | + chunk = request.environ['wsgi.input'].read(8192) |
| 95 | + if not chunk: |
| 96 | + break |
| 97 | + body.write(chunk) |
| 98 | + size = body.tell() |
| 99 | + body.seek(0) |
| 100 | + request.environ['wsgi.input'] = body |
| 101 | + request.environ['CONTENT_LENGTH'] = size |
| 102 | + |
| 103 | + |
| 104 | +# -- Routes ------------------------------------------------------------------ |
| 105 | + |
| 106 | +@app.get('/pipeline') |
| 107 | +def pipeline(): |
| 108 | + response.content_type = 'text/plain; charset=utf-8' |
| 109 | + return b'ok' |
| 110 | + |
| 111 | + |
| 112 | +@app.route('/baseline11', method=['GET', 'POST']) |
| 113 | +def baseline11(): |
| 114 | + total = int(request.query.a) + int(request.query.b) |
| 115 | + if request.method == 'POST': |
| 116 | + total += int(request.body.read(100)) |
| 117 | + response.content_type = 'text/plain; charset=utf-8' |
| 118 | + return str(total) |
| 119 | + |
| 120 | + |
| 121 | +@app.get('/json/<count:int>') |
| 122 | +def json_endpoint(count: int): |
| 123 | + global DATASET_ITEMS |
| 124 | + if not DATASET_ITEMS: |
| 125 | + response.content_type = 'text/plain; charset=utf-8' |
| 126 | + return "No dataset", 500 |
| 127 | + m_val = float(request.query.m) |
| 128 | + items = [ ] |
| 129 | + for idx, dsitem in enumerate(DATASET_ITEMS): |
| 130 | + if idx >= count: |
| 131 | + break |
| 132 | + item = dict(dsitem) |
| 133 | + item["total"] = dsitem["price"] * dsitem["quantity"] * m_val |
| 134 | + items.append(item) |
| 135 | + return { 'items': items, 'count': len(items) } |
| 136 | + |
| 137 | + |
| 138 | +@app.get('/async-db') |
| 139 | +def async_db_endpoint(): |
| 140 | + global DATABASE_POOL |
| 141 | + if not DATABASE_POOL: |
| 142 | + return { "items": [ ], "count": 0 } |
| 143 | + try: |
| 144 | + min_val = float(request.query.min) |
| 145 | + max_val = float(request.query.max) |
| 146 | + limit = int(request.query.limit) |
| 147 | + with DATABASE_POOL.connection() as db_conn: |
| 148 | + rows = db_conn.execute(DATABASE_QUERY, (min_val, max_val, limit)).fetchall() |
| 149 | + items = [ |
| 150 | + { |
| 151 | + 'id' : row['id'], |
| 152 | + 'name' : row['name'], |
| 153 | + 'category': row['category'], |
| 154 | + 'price' : row['price'], |
| 155 | + 'quantity': row['quantity'], |
| 156 | + 'active' : row['active'], |
| 157 | + 'tags' : json.loads(row['tags']) if isinstance(row['tags'], str) else row['tags'], |
| 158 | + 'rating': { |
| 159 | + 'score': row['rating_score'], |
| 160 | + 'count': row['rating_count'], |
| 161 | + } |
| 162 | + } |
| 163 | + for row in rows |
| 164 | + ] |
| 165 | + return { "items": items, "count": len(items) } |
| 166 | + except Exception: |
| 167 | + return { "items": [ ], "count": 0 } |
| 168 | + |
| 169 | + |
| 170 | +@app.post('/upload') |
| 171 | +def upload_endpoint(): |
| 172 | + size = 0 |
| 173 | + try: |
| 174 | + body = request.body |
| 175 | + while True: |
| 176 | + chunk = body.read(256*1024) |
| 177 | + if not chunk: |
| 178 | + break |
| 179 | + size += len(chunk) |
| 180 | + except Exception: |
| 181 | + pass |
| 182 | + response.content_type = 'text/plain; charset=utf-8' |
| 183 | + return str(size) |
| 184 | + |
| 185 | + |
| 186 | +mimetypes.add_type('.woff2', 'font/woff2') |
| 187 | +mimetypes.add_type('.webp', 'image/webp') |
| 188 | + |
| 189 | +@app.route('/static/<filepath:path>') |
| 190 | +def send_static_file(filepath): |
| 191 | + return static_file(filepath, root = '/data/static') |
| 192 | + |
0 commit comments