2021-02-15 05:23:51 +00:00
|
|
|
import boto3
|
|
|
|
import json
|
2021-12-20 07:13:24 +00:00
|
|
|
import botocore.exceptions
|
2021-04-03 23:36:05 +00:00
|
|
|
from datetime import datetime, timedelta, timezone
|
2021-04-28 11:32:22 +00:00
|
|
|
import uuid
|
2021-12-20 06:02:02 +00:00
|
|
|
import es
|
2021-08-12 07:46:06 +00:00
|
|
|
|
2021-11-29 10:09:32 +00:00
|
|
|
|
|
|
|
|
2021-04-03 23:36:05 +00:00
|
|
|
def history(event, context):
|
2021-08-12 07:46:06 +00:00
|
|
|
s3 = boto3.resource('s3')
|
|
|
|
serial = str(event["pathParameters"]["serial"])
|
|
|
|
|
|
|
|
# if there's a historic file created for this sonde, use that instead
|
|
|
|
try:
|
|
|
|
object = s3.Object('sondehub-history', f'serial/{serial}.json.gz')
|
2021-08-18 08:50:57 +00:00
|
|
|
|
2021-08-12 07:46:06 +00:00
|
|
|
object.load()
|
2021-08-18 08:50:57 +00:00
|
|
|
lastModified = object.meta.data['LastModified']
|
|
|
|
if not lastModified + timedelta(hours=12) > datetime.now(timezone.utc):
|
|
|
|
return {"statusCode": 302, "headers": {"Location": f'https://{object.bucket_name}.s3.amazonaws.com/{object.key}'}}
|
2021-08-12 07:46:06 +00:00
|
|
|
except botocore.exceptions.ClientError as e:
|
|
|
|
if e.response['Error']['Code'] == "404":
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# Something else has gone wrong.
|
|
|
|
raise
|
2021-04-03 23:36:05 +00:00
|
|
|
path = "telm-*/_search"
|
|
|
|
payload = {
|
|
|
|
"aggs": {
|
|
|
|
"3": {
|
|
|
|
"date_histogram": {
|
|
|
|
"field": "datetime",
|
|
|
|
"fixed_interval": "1s",
|
|
|
|
"min_doc_count": 1,
|
|
|
|
},
|
|
|
|
"aggs": {
|
|
|
|
"1": {
|
|
|
|
"top_hits": {
|
|
|
|
"size": 1,
|
|
|
|
"sort": [{"datetime": {"order": "desc"}}],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"query": {
|
|
|
|
"bool": {
|
|
|
|
"filter": [
|
|
|
|
{"match_all": {}},
|
|
|
|
{
|
2023-07-11 23:59:43 +00:00
|
|
|
"term": {
|
|
|
|
"serial.keyword": str(event["pathParameters"]["serial"])
|
2021-04-03 23:36:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-12-20 06:02:02 +00:00
|
|
|
results = es.request(json.dumps(payload), path, "POST")
|
2021-04-03 23:36:05 +00:00
|
|
|
output = [
|
2021-04-28 11:32:22 +00:00
|
|
|
{k: v for k, v in data["1"]["hits"]["hits"][0]["_source"].items() if k != 'user-agent' and k != 'upload_time_delta'}
|
|
|
|
|
2021-04-03 23:36:05 +00:00
|
|
|
for data in results["aggregations"]["3"]["buckets"]
|
|
|
|
]
|
2021-04-28 11:32:22 +00:00
|
|
|
s3 = boto3.resource('s3')
|
|
|
|
object = s3.Object('sondehub-open-data', 'export/' + str(uuid.uuid4()))
|
|
|
|
object.put(Body=json.dumps(output).encode('utf-8'), ACL='public-read', ContentType='application/json')
|
|
|
|
return {"statusCode": 302, "headers": {"Location": f'https://{object.bucket_name}.s3.amazonaws.com/{object.key}'}}
|
2021-02-15 05:23:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-04-03 23:36:05 +00:00
|
|
|
|
|
|
|
|