recovery fixes

This commit is contained in:
Michaela 2021-06-08 18:52:17 +10:00
parent b44566f935
commit a99f6de735

View File

@ -139,20 +139,23 @@ def put(event, context):
sonde_last_data = getSonde(recovered["serial"]) sonde_last_data = getSonde(recovered["serial"])
if recovered["serial"] == "":
return {"statusCode": 400, "body": json.dumps({"message": "serial cannot be empty"})}
if len(sonde_last_data) == 0: if len(sonde_last_data) == 0:
return {"statusCode": 400, "body": json.dumps({"message":"serial not found in db"})} return {"statusCode": 400, "body": json.dumps({"message": "serial not found in db"})}
already_recovered = getRecovered(recovered["serial"]) already_recovered = getRecovered(recovered["serial"])
if len(already_recovered) != 0: if len(already_recovered) != 0:
recovered_by = already_recovered[0]['fields']['recovered_by.keyword'][0] recovered_by = already_recovered[0]['fields']['recovered_by.keyword'][0]
return {"statusCode": 400, "body": json.dumps({"message":f"Already recovered by {recovered_by}"})} return {"statusCode": 400, "body": json.dumps({"message": f"Already recovered by {recovered_by}"})}
recovered['position'] = [recovered['lon'], recovered['lat']] recovered['position'] = [recovered['lon'], recovered['lat']]
result = es_request(recovered, "recovered/_doc", "POST") result = es_request(recovered, "recovered/_doc", "POST")
# add in elasticsearch extra position field # add in elasticsearch extra position field
return {"statusCode": 200, "body": json.dumps({"message":"telm logged. Have a good day ^_^"})} return {"statusCode": 200, "body": json.dumps({"message": "telm logged. Have a good day ^_^"})}
def get(event, context): def get(event, context):
@ -213,10 +216,47 @@ def get(event, context):
"bool": { "bool": {
"filter": filters "filter": filters
} }
},
"aggs": {
"2": {
"terms": {
"field": "serial.keyword",
"order": {
"2-orderAgg": "desc"
},
"size": 500
},
"aggs": {
"2-orderAgg": {
"max": {
"field": "datetime"
},
},
"1": {
"top_hits": {
"_source": True,
"size": 1,
"sort": [
{
"recovered": {
"order": "desc"
}
},
{
"datetime": {
"order": "desc"
}
}
]
}
}
}
}
} }
} }
results = es_request(query, "recovered*/_search", "POST") results = es_request(query, "recovered*/_search", "POST")
output = [x["_source"] for x in results['hits']['hits']] output = [x['1']['hits']['hits'][0]["_source"]
for x in results['aggregations']['2']['buckets']]
return {"statusCode": 200, "body": json.dumps(output)} return {"statusCode": 200, "body": json.dumps(output)}
@ -259,16 +299,16 @@ if __name__ == "__main__":
"timeEpoch": 1612051825409, "timeEpoch": 1612051825409,
}, },
"body": json.dumps({ "body": json.dumps({
"datetime": "2021-06-06T01:10:07.629Z", "datetime": "2021-06-06T01:10:07.629Z",
"serial": "string", "serial": "string",
"lat": 0, "lat": 0,
"lon": 0, "lon": 0,
"alt": 0, "alt": 0,
"recovered": True, "recovered": True,
"recovered_by": "string", "recovered_by": "string",
"description": "string" "description": "string"
}), }),
"isBase64Encoded": False, "isBase64Encoded": False,
} }
print(put(payload, {})) # print(put(payload, {}))
# print(get(payload,{})) print(get(payload, {}))