Skip to content

Commit fdb9244

Browse files
committed
web_sanic improvements
Use a global connection pool, perform queries directly in response functions instead of calling others, delete env after use, localize more functions in parameters.
1 parent 9c44aba commit fdb9244

File tree

1 file changed

+77
-90
lines changed

1 file changed

+77
-90
lines changed

web_sanic.py

+77-90
Original file line numberDiff line numberDiff line change
@@ -70,56 +70,97 @@ def render_worker_map():
7070
))
7171

7272

73-
@app.route('/')
73+
@app.get('/')
7474
async def fullmap(request, html_map=render_map()):
7575
return html_map
7676

7777

78-
@app.route('/data')
79-
async def pokemon_data(request):
80-
last_id = request.args.get('last_id', 0)
81-
return json(await get_pokemarkers_async(last_id))
82-
83-
84-
@app.route('/gym_data')
85-
async def gym_data(request):
86-
return json(await get_gyms_async())
87-
88-
89-
@app.route('/spawnpoints')
90-
async def spawn_points(request):
91-
return json([dict(x) for x in await get_spawnpoints_async()])
92-
93-
94-
@app.route('/pokestops')
95-
async def get_pokestops(request):
96-
return json([dict(x) for x in await get_pokestops_async()])
97-
98-
99-
@app.route('/scan_coords')
100-
async def scan_coords(request):
101-
return json(get_scan_coords())
102-
103-
10478
if conf.MAP_WORKERS:
10579
workers = Workers()
10680

10781

108-
@app.route('/workers_data')
82+
@app.get('/workers_data')
10983
async def workers_data(request):
11084
return json(get_worker_markers(workers))
11185

11286

113-
@app.route('/workers')
87+
@app.get('/workers')
11488
async def workers_map(request, html_map=render_worker_map()):
11589
return html_map
11690

11791

118-
def sighting_to_marker(pokemon, names=POKEMON, moves=MOVES, damage=DAMAGE):
92+
del env
93+
94+
95+
@app.get('/data')
96+
async def pokemon_data(request, _time=time):
97+
last_id = request.args.get('last_id', 0)
98+
async with app.pool.acquire() as conn:
99+
results = await conn.fetch('''
100+
SELECT id, pokemon_id, expire_timestamp, lat, lon, atk_iv, def_iv, sta_iv, move_1, move_2
101+
FROM sightings
102+
WHERE expire_timestamp > {} AND id > {}
103+
'''.format(_time(), last_id))
104+
return json(list(map(sighting_to_marker, results)))
105+
106+
107+
@app.get('/gym_data')
108+
async def gym_data(request, names=POKEMON, _str=str):
109+
async with app.pool.acquire() as conn:
110+
results = await conn.fetch('''
111+
SELECT
112+
fs.fort_id,
113+
fs.id,
114+
fs.team,
115+
fs.prestige,
116+
fs.guard_pokemon_id,
117+
fs.last_modified,
118+
f.lat,
119+
f.lon
120+
FROM fort_sightings fs
121+
JOIN forts f ON f.id=fs.fort_id
122+
WHERE (fs.fort_id, fs.last_modified) IN (
123+
SELECT fort_id, MAX(last_modified)
124+
FROM fort_sightings
125+
GROUP BY fort_id
126+
)
127+
''')
128+
return json([{
129+
'id': 'fort-' + _str(fort['fort_id']),
130+
'sighting_id': fort['id'],
131+
'prestige': fort['prestige'],
132+
'pokemon_id': fort['guard_pokemon_id'],
133+
'pokemon_name': names[fort['guard_pokemon_id']],
134+
'team': fort['team'],
135+
'lat': fort['lat'],
136+
'lon': fort['lon']
137+
} for fort in results])
138+
139+
140+
@app.get('/spawnpoints')
141+
async def spawn_points(request, _dict=dict):
142+
async with app.pool.acquire() as conn:
143+
results = await conn.fetch('SELECT spawn_id, despawn_time, lat, lon, duration FROM spawnpoints')
144+
return json([_dict(x) for x in results])
145+
146+
147+
@app.get('/pokestops')
148+
async def get_pokestops(request, _dict=dict):
149+
async with app.pool.acquire() as conn:
150+
results = await conn.fetch('SELECT external_id, lat, lon FROM pokestops')
151+
return json([_dict(x) for x in results])
152+
153+
154+
@app.get('/scan_coords')
155+
async def scan_coords(request):
156+
return json(get_scan_coords())
157+
158+
159+
def sighting_to_marker(pokemon, names=POKEMON, moves=MOVES, damage=DAMAGE, trash=conf.TRASH_IDS, _str=str):
119160
pokemon_id = pokemon['pokemon_id']
120161
marker = {
121-
'id': 'pokemon-' + str(pokemon['id']),
122-
'trash': pokemon_id in conf.TRASH_IDS,
162+
'id': 'pokemon-' + _str(pokemon['id']),
163+
'trash': pokemon_id in trash,
123164
'name': names[pokemon_id],
124165
'pokemon_id': pokemon_id,
125166
'lat': pokemon['lat'],
@@ -139,70 +180,16 @@ def sighting_to_marker(pokemon, names=POKEMON, moves=MOVES, damage=DAMAGE):
139180
return marker
140181

141182

142-
async def get_pokemarkers_async(after_id):
143-
async with create_pool(**conf.DB) as pool:
144-
async with pool.acquire() as conn:
145-
async with conn.transaction():
146-
results = await conn.fetch('''
147-
SELECT id, pokemon_id, expire_timestamp, lat, lon, atk_iv, def_iv, sta_iv, move_1, move_2
148-
FROM sightings
149-
WHERE expire_timestamp > {ts} AND id > {poke_id}
150-
'''.format(ts=time(), poke_id=after_id))
151-
return tuple(map(sighting_to_marker, results))
152-
153-
154-
async def get_gyms_async(names=POKEMON):
155-
async with create_pool(**conf.DB) as pool:
156-
async with pool.acquire() as conn:
157-
async with conn.transaction():
158-
results = await conn.fetch('''
159-
SELECT
160-
fs.fort_id,
161-
fs.id,
162-
fs.team,
163-
fs.prestige,
164-
fs.guard_pokemon_id,
165-
fs.last_modified,
166-
f.lat,
167-
f.lon
168-
FROM fort_sightings fs
169-
JOIN forts f ON f.id=fs.fort_id
170-
WHERE (fs.fort_id, fs.last_modified) IN (
171-
SELECT fort_id, MAX(last_modified)
172-
FROM fort_sightings
173-
GROUP BY fort_id
174-
)
175-
''')
176-
return [{
177-
'id': 'fort-' + str(fort['fort_id']),
178-
'sighting_id': fort['id'],
179-
'prestige': fort['prestige'],
180-
'pokemon_id': fort['guard_pokemon_id'],
181-
'pokemon_name': names[fort['guard_pokemon_id']],
182-
'team': fort['team'],
183-
'lat': fort['lat'],
184-
'lon': fort['lon']
185-
} for fort in results]
186-
187-
188-
async def get_spawnpoints_async():
189-
async with create_pool(**conf.DB) as pool:
190-
async with pool.acquire() as conn:
191-
async with conn.transaction():
192-
return await conn.fetch('SELECT spawn_id, despawn_time, lat, lon, duration FROM spawnpoints')
193-
194-
195-
async def get_pokestops_async():
196-
async with create_pool(**conf.DB) as pool:
197-
async with pool.acquire() as conn:
198-
async with conn.transaction():
199-
return await conn.fetch('SELECT external_id, lat, lon FROM pokestops')
183+
@app.listener('before_server_start')
184+
async def register_db(app, loop):
185+
app.pool = await create_pool(**conf.DB, loop=loop)
200186

201187

202188
def main():
203189
args = get_args()
204190
app.run(debug=args.debug, host=args.host, port=args.port)
205191

192+
206193
if __name__ == '__main__':
207194
main()
208195

0 commit comments

Comments
 (0)