mirror of
https://github.com/supleed2/ELEC60013-ES-CW1.git
synced 2024-12-23 05:55:49 +00:00
Added endpoint for metric summary
This commit is contained in:
parent
78ac68cb2d
commit
9851513288
37
api/data.py
37
api/data.py
|
@ -18,7 +18,7 @@ def uploadReadings():
|
||||||
if body is None:
|
if body is None:
|
||||||
resp = {'error': 'Invalid request - please provide a body'}
|
resp = {'error': 'Invalid request - please provide a body'}
|
||||||
return Response(json.dumps(resp), status=400, mimetype='application/json')
|
return Response(json.dumps(resp), status=400, mimetype='application/json')
|
||||||
body['timestamp'] = time.time()
|
body['timestamp'] = datetime.now().timestamp()
|
||||||
|
|
||||||
# Save all the measurements
|
# Save all the measurements
|
||||||
doc = firestore.client().collection(u'readings').document(deviceId).get()
|
doc = firestore.client().collection(u'readings').document(deviceId).get()
|
||||||
|
@ -119,3 +119,38 @@ def getStepsLastFiveDays():
|
||||||
results = {'daily_steps': listOfDailySteps}
|
results = {'daily_steps': listOfDailySteps}
|
||||||
return Response(json.dumps(results), status=200, mimetype='application/json')
|
return Response(json.dumps(results), status=200, mimetype='application/json')
|
||||||
|
|
||||||
|
@data.route('/readings/metrics-summary', methods=['GET'])
|
||||||
|
def getMetricsSummary():
|
||||||
|
deviceId = request.headers.get('deviceid')
|
||||||
|
if deviceId is None:
|
||||||
|
resp = {'error': 'Device not specified'}
|
||||||
|
return Response(json.dumps(resp), status=400, mimetype='application/json')
|
||||||
|
|
||||||
|
upcomingMidnight = datetime.combine(datetime.today(), time.min) #+ timedelta(days=1)
|
||||||
|
lastMidnight = datetime.combine(datetime.today(), time.min) - timedelta(days=1)
|
||||||
|
doc = firestore.client().collection(u'readings').document(deviceId).get()
|
||||||
|
|
||||||
|
if doc.exists:
|
||||||
|
allData = doc.to_dict()['data']
|
||||||
|
currentDayData = [x for x in allData if x['timestamp'] <= upcomingMidnight.timestamp() and x['timestamp'] >= lastMidnight.timestamp()]
|
||||||
|
if len(currentDayData) >= 1:
|
||||||
|
maxAirTemp = max(currentDayData, key=lambda x: x['air_temp'])['air_temp']
|
||||||
|
maxSkinTemp = max(currentDayData, key=lambda x: x['skin_temp'])['skin_temp']
|
||||||
|
maxHumidity = max(currentDayData, key=lambda x: x['humidity'])['humidity']
|
||||||
|
minAirTemp = min(currentDayData, key=lambda x: x['air_temp'])['air_temp']
|
||||||
|
minSkinTemp = min(currentDayData, key=lambda x: x['skin_temp'])['skin_temp']
|
||||||
|
minHumidity = min(currentDayData, key=lambda x: x['humidity'])['humidity']
|
||||||
|
results = {
|
||||||
|
'last_air_temp': currentDayData[-1]['air_temp'], 'min_air_temp': minAirTemp, 'max_air_temp': maxAirTemp,
|
||||||
|
'last_skin_temp': currentDayData[-1]['skin_temp'], 'min_skin_temp': minSkinTemp, 'max_skin_temp': maxSkinTemp,
|
||||||
|
'last_humidity': currentDayData[-1]['humidity'], 'min_humidity': minHumidity, 'max_humidity': maxHumidity
|
||||||
|
}
|
||||||
|
return Response(json.dumps(results), status=200, mimetype='application/json')
|
||||||
|
else:
|
||||||
|
return Response(json.dumps({'error': 'Could not get data from database'}), status=500, mimetype='application/json')
|
||||||
|
else:
|
||||||
|
return Response(json.dumps({'error': 'Could not get data from database'}), status=500, mimetype='application/json')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue