Created
September 7, 2020 18:15
-
-
Save bgweber/3ac321f9a22b036cea4a15db1438e636 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flask | |
import fakeredis | |
import json | |
server = fakeredis.FakeServer() | |
redis = fakeredis.FakeStrictRedis(server=server) | |
app = flask.Flask(__name__) | |
# endpoint for profile updates | |
@app.route("/update", methods=["GET","POST"]) | |
def update(): | |
# get the player ID to update | |
event = flask.request.json | |
playerID = event.get('playerID') | |
# CREATE: heck if a record exists | |
record = redis.get(playerID) | |
if record is None: | |
profile = {"goals": 0, "shots": 0, "assists": 0, "hits": 0 } | |
redis.set(playerID, json.dumps(profile)) | |
# READ: get the user summary | |
record = redis.get(playerID) | |
profile = json.loads(record) | |
# UPDATE: add the new attributes | |
profile['goals'] += event['goals'] | |
profile['shots'] += event['shots'] | |
profile['assists'] += event['assists'] | |
profile['hits'] += event['hits'] | |
redis.set(playerID, json.dumps(profile)) | |
# return the updated profile | |
return flask.jsonify(profile) | |
# endpoint for model serving | |
@app.route("/score", methods=["GET"]) | |
def score(): | |
result = {} | |
try: | |
# get the user profile | |
playerID = flask.request.args['playerID'] | |
record = redis.get(playerID) | |
profile = json.loads(record) | |
# calculate a regression value | |
score = 1 + profile['goals'] * 10.0 \ | |
+ profile['shots'] * 1.0 \ | |
+ profile['assists'] * 2.0 \ | |
+ profile['hits'] * 0.5 | |
result['score'] = score | |
except: | |
None | |
return flask.jsonify(result) | |
# start the flask app, allow remote connections | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment