mirror of
https://github.com/projecthorus/sondehub-infra.git
synced 2024-12-19 21:27:55 +00:00
38 lines
1.4 KiB
Python
38 lines
1.4 KiB
Python
import json
|
|
import es
|
|
|
|
|
|
|
|
def lambda_handler(event, context):
|
|
payloads = {}
|
|
for record in event['Records']:
|
|
sns_message = json.loads(record["body"])
|
|
if type(json.loads(sns_message["Message"])) == dict:
|
|
incoming_payloads = [json.loads(sns_message["Message"])]
|
|
else:
|
|
incoming_payloads = json.loads(sns_message["Message"])
|
|
for payload in incoming_payloads:
|
|
index = payload['datetime'][:7]
|
|
try:
|
|
payload['position'] = f'{payload["lat"]},{payload["lon"]}' # make elk happy location
|
|
except:
|
|
pass
|
|
if index not in payloads: # create index if not exists
|
|
payloads[index] = []
|
|
|
|
payloads[index].append(payload)
|
|
|
|
for index in payloads:
|
|
body=""
|
|
for payload in payloads[index]:
|
|
body += "{\"index\":{}}\n" + json.dumps(payload) + "\n"
|
|
body += "\n"
|
|
|
|
result = es.request(body, f"ham-telm-{index}/_bulk", "POST")
|
|
if 'errors' in result and result['errors'] == True:
|
|
error_types = [x['index']['error']['type'] for x in result['items'] if 'error' in x['index']] # get all the error types
|
|
print(event)
|
|
print(result)
|
|
error_types = [a for a in error_types if a != 'mapper_parsing_exception'] # filter out mapper failures since they will never succeed
|
|
if error_types:
|
|
raise RuntimeError |