-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslike.py
304 lines (252 loc) · 9.38 KB
/
slike.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import asyncio
import base64
import json as JSON
import os
import socket
import tracemalloc
from datetime import datetime, timedelta
import httpx
from httpx import ReadTimeout
from httpx._exceptions import HTTPStatusError
import pytz
import redis.asyncio as redis
import reverse_geocode
from authlib.jose import jwt
from cache import AsyncTTL, AsyncLRU
from methodtools import lru_cache
from sanic import Sanic
from sanic.exceptions import SanicException
from sanic.log import logger
from sanic.response import json, file, text
from config import Config
# tracemalloc.start()
app = Sanic('slike')
app.static("/assets/", "web/assets/")
@app.before_server_start
async def bootstrapScoring(app):
await checkKV(None)
with open(Config().scoring_location, 'rb') as f:
scoring_data = f.read()
await setScorer('default', scoring_data)
@app.on_request
async def checkKV(request):
if request and request.name in ['slike.hello']:
logger.debug('skipping kv check for %s' % request.name)
return
if (hasattr(app.ctx, 'kv_time')
and datetime.now().timestamp() - app.ctx.kv_time < Config().redis.reconnect_timer
and hasattr(app.ctx, 'kv')):
try:
logger.debug('testing redis connection with ping')
await app.ctx.kv.ping()
logger.debug('pong')
except Exception as e:
logger.debug('cant connect to redis with existing conn:')
logger.debug(e)
app.ctx.kv = await connectKV()
else:
app.ctx.kv = await connectKV()
@app.get('/')
async def hello(request):
return await file('web/index.html')
@app.get('/profiles')
async def profiles(request):
profiles = []
cursor = '0'
cache_ns = 'scoring:*'
while cursor != 0:
cursor, keys = await app.ctx.kv.scan(cursor=cursor, match=cache_ns, count=5000)
if keys:
profiles.extend([key.decode('utf-8') for key in keys])
return json(profiles)
@app.get('/forecast')
async def forecast(request):
lang = request.args.get('lang')
tz = request.args.get('tz')
lat = request.args.get('lat')
lon = request.args.get('lon')
profile = request.args.get('profile')
if not profile:
profile = 'default'
logger.debug("coords requested (%s, %s)" % (lat, lon))
if Config().geocache.enabled:
lat = ('%.' + str(Config().geocache.latlng_decimals) + 'f') % float(lat)
lon = ('%.' + str(Config().geocache.latlng_decimals) + 'f') % float(lon)
logger.debug("coords rounded to (%s, %s)" % (lat, lon))
local, country = revGeo(lat, lon)
if Config().geocache.enabled:
data = await cacheableGetForecast(lat, lon, lang, country, tz)
else:
data = await getForecast(lat, lon, lang, country, tz)
fetch_timestamp = data['fetchedAt']
forecast = data['forecast']
last_scoring_update = await app.ctx.kv.get('ingest:scoring:%s' % profile)
scored_results = await doScoring(forecast, profile, last_scoring_update)
result = {
'meta': {
'country': country,
'local': local
},
'forecastFetchTime': fetch_timestamp,
'forecast': scored_results
}
logmem('forecast')
return json(result)
@app.put('/ingest/scoring/<profile>')
async def ingest(request, profile):
new_data = request.body
# TODO: validate jsonschema?
await setScorer(profile, new_data)
return text('OK')
@app.post('/cache/wipe')
async def cacheWipe(request):
# TODO: definitely get rid of this after dev
if not (cache_ns := request.args.get('pattern')): cache_ns = 'cache:*'
logger.debug('wiping pattern `%s` from kv' % cache_ns)
cursor = '0'
while cursor != 0:
cursor, keys = await app.ctx.kv.scan(cursor=cursor, match=cache_ns, count=5000)
if keys:
await app.ctx.kv.delete(*keys)
return text('OK')
async def connectKV():
logger.debug('opening new redis connection')
app.ctx.kv_time = datetime.now().timestamp()
return await redis.Redis(
host=os.getenv('SLIKE_SECRET_REDIS_HOST'),
password=os.getenv('SLIKE_SECRET_REDIS_PASSWORD'),
port=Config().redis.port,
decode_responses=False,
health_check_interval=5,
socket_keepalive=False,
socket_connect_timeout=5,
retry_on_timeout=True
)
async def setScorer(name, new_data):
logger.debug('setting scoring data for %s in kv' % name)
await app.ctx.kv.set('scoring:%s' % name, new_data)
ts = datetime.now().timestamp()
await app.ctx.kv.set('ingest:scoring:%s' % name, ts)
logger.debug('set at %s' % ts)
@AsyncLRU(maxsize=Config().memory_caching.maxsize)
async def getScorer(name, buster):
logger.debug('loading scoring data for %s from kv' % name)
return await app.ctx.kv.get('scoring:%s' % name)
async def cacheableGetForecast(lat, lon, lang, country, tz):
cache_key = "cache:forecast:%s%s%s%s%s" % (lat, lon, lang, country, tz)
logger.debug("forecast cache key: %s" % cache_key)
logger.debug("getting cached forecast at %s" % datetime.now().timestamp())
cached_result = await app.ctx.kv.get(cache_key)
logger.debug("finished get cached forecast at %s" % datetime.now().timestamp())
if cached_result is not None:
logger.debug("using cached forecast")
result = JSON.loads(cached_result)
else:
logger.debug("cache miss, getting new forecast")
result = await getForecast(lat, lon, lang, country, tz)
next_hour_ttl = ((datetime.now() + timedelta(hours=1)).replace(microsecond=0, second=0, minute=0) - datetime.now()).seconds
if next_hour_ttl < 1:
# in case we are actually within the last second of the hour
next_hour_ttl = 1
await app.ctx.kv.set(cache_key, JSON.dumps(result), ex=next_hour_ttl)
return result
async def getForecast(lat, lon, lang, country, tz):
logger.debug("getting forecast")
bearer = checkToken()
headers = {"Authorization": "Bearer %s" % bearer.decode('utf-8')}
url = f"https://weatherkit.apple.com/api/v1/weather/{lang}/{lat}/{lon}?countryCode={country}&timezone={tz}&dataSets=forecastHourly,forecastNextHour"
async with httpx.AsyncClient() as client:
for attempt in range(Config().retry.attempts + 1):
try:
r = await client.get(url, headers=headers)
r.raise_for_status()
break
except (HTTPStatusError, ReadTimeout) as err:
status = '408x' if type(err) == ReadTimeout else err.response.status_code
if attempt == Config().retry.attempts:
logger.debug("out of retry attempts")
raise SanicException("Unable to reach WeatherKit (Read Timeout)", status_code=503)
elif status not in Config().retry.codes:
logger.debug("non retryable status %s" % status)
raise SanicException("Unable to reach WeatherKit (%s)" % status, status_code=503)
else:
logger.debug("attempt %s failed (%s), retrying..." % (attempt, status))
await asyncio.sleep(attempt * Config().retry.backoff)
continue
return {"fetchedAt": datetime.now(pytz.timezone(tz)).isoformat(), "forecast": r.json()}
@AsyncLRU(maxsize=Config().memory_caching.maxsize)
async def doScoring(forecast_data, profile, buster):
logger.debug('doing a new scoring')
scoring_data = await getScorer(profile, buster)
forecast = []
for h, hour in enumerate(forecast_data['forecastHourly']['hours']):
row = {}
hour_total = 0
row['time'] = hour['forecastStart']
for scorer in JSON.loads(scoring_data):
val = raw_val = hour[scorer['name']]
if scorer['translation']:
# TODO: try for missing enum here
val = scorer['translation'][str(val)]
diff = val - scorer['ideal']
if scorer['absolute']:
diff = abs(diff)
if scorer['func']:
diff = func(scorer['func'], diff)
score = diff * scorer['normalizer'] * scorer['weight']
row[scorer['name']] = '%.2f (%s)' % (score, raw_val)
hour_total = hour_total + score
row['score'] = '%.2f' % hour_total
forecast.append(row)
logmem('doScoring')
return forecast
@lru_cache(maxsize=Config().memory_caching.maxsize)
def revGeo(lat, lon):
logmem('pre-geo')
coords = reverse_geocode.search([(lat, lon)])
logmem('post-geo')
country =coords[0]['country_code']
local = coords[0]['city']
logger.debug("revgeo'd to %s, %s" % (local, country))
return (local, country)
def func(name, val):
match name:
case 'exp':
retval = (0.33 * val) ** 2
case 'sq':
retval = val ** 2
case _:
retval = val
return retval
def checkToken():
if hasattr(app.ctx, 'bearer') and hasattr(app.ctx, 'bearer_exp') and app.ctx.bearer_exp > datetime.now().timestamp():
logger.debug("reusing token")
return app.ctx.bearer
else:
return newToken()
def newToken():
now = datetime.now()
exp = now + timedelta(minutes=Config().jwt_ttl_mins)
protected = {
'alg': 'ES256',
'kid': os.getenv('SLIKE_SECRET_KEYID'),
'id': '%s.%s' % (os.getenv('SLIKE_SECRET_TEAMID'), os.getenv('SLIKE_SECRET_SERVICEID'))
}
payload = {
'iss': os.getenv('SLIKE_SECRET_TEAMID'),
'sub': os.getenv('SLIKE_SECRET_SERVICEID'),
'iat': now.timestamp(),
'exp': exp.timestamp()
}
secret = base64.b64decode(os.getenv('SLIKE_SECRET_P8KEY')).decode("ascii")
bearer = jwt.encode(protected, payload, secret)
app.ctx.bearer_exp = exp.timestamp()
app.ctx.bearer = bearer
logger.debug("new token signed")
return bearer
def logmem(location):
if tracemalloc.is_tracing():
current, peak = tracemalloc.get_traced_memory()
logger.debug('memory at `%s` current: %.2f MB, peak: %.2f MB' % (location, current/1024/1024, peak/1024/1024))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000, debug=os.getenv('SLIKE_DEBUG_ENABLED') == "TRUE")