-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.py
40 lines (30 loc) · 1.36 KB
/
entrypoint.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
# import dependencies
import json
import redis as redis
from flask import Flask, request
from loguru import logger
# define constants
HISTORY_LENGTH = 10
DATA_KEY = "engine_temperature"
# create a Flask server, and allow us to interact with it using the app variable
app = Flask(__name__)
# define an endpoint which accepts POST requests, and is reachable from the /record endpoint
@app.route('/record', methods=['POST'])
def record_engine_temperature():
payload = request.get_json(force=True)
logger.info(f"(*) record request --- {json.dumps(payload)} (*)")
engine_temperature = payload.get("engine_temperature")
logger.info(f"engine temperature to record is: {engine_temperature}")
database = redis.Redis(host="redis", port=6379, db=0, decode_responses=True)
database.lpush(DATA_KEY, engine_temperature)
logger.info(f"stashed engine temperature in redis: {engine_temperature}")
while database.llen(DATA_KEY) > HISTORY_LENGTH:
database.rpop(DATA_KEY)
engine_temperature_values = database.lrange(DATA_KEY, 0, -1)
logger.info(f"engine temperature list now contains these values: {engine_temperature_values}")
logger.info(f"record request successful")
return {"success": True}, 200
# we'll implement this in the next step!
@app.route('/collect', methods=['POST'])
def collect_engine_temperature():
return {"success": True}, 200