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"])
if recovered["serial"] == "":
return {"statusCode": 400, "body": json.dumps({"message": "serial cannot be empty"})}
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"])
if len(already_recovered) != 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']]
result = es_request(recovered, "recovered/_doc", "POST")
# 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):
@ -213,10 +216,47 @@ def get(event, context):
"bool": {
"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")
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)}
@ -267,8 +307,8 @@ if __name__ == "__main__":
"recovered": True,
"recovered_by": "string",
"description": "string"
}),
}),
"isBase64Encoded": False,
}
print(put(payload, {}))
# print(get(payload,{}))
# print(put(payload, {}))
print(get(payload, {}))