Skip to content

Instantly share code, notes, and snippets.

@nabam
Created March 13, 2020 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nabam/8729bfab439084a46d6a972661504028 to your computer and use it in GitHub Desktop.
Save nabam/8729bfab439084a46d6a972661504028 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import json
events = []
device_state = {}
for line in sys.stdin:
events.append(json.loads(line))
events.sort(key=lambda e: e['event_timestamp'])
# for event in events:
# print(event)
#
# sys.exit(0)
for event in events:
device_id = event['device_id']
status_id = event['status_id']
if device_id not in device_state:
device_state[device_id] = {
'device_id': device_id,
'status_id': status_id,
'start_timestamp': event['event_timestamp']
}
if device_id in device_state:
if not device_state[device_id]['status_id'] == status_id:
device_state[device_id]['end_timestamp'] = event['event_timestamp']
device_state[device_id]['duration'] = event['event_timestamp'] - device_state[device_id]['start_timestamp']
print(json.dumps({'index': {'_index': 'x4-states-0'}}))
print(json.dumps(device_state[device_id]))
device_state[device_id] = {
'device_id': device_id,
'status_id': status_id,
'start_timestamp': event['event_timestamp']
}
else:
device_state[device_id]['end_timestamp'] = event['event_timestamp']
device_state[device_id]['duration'] = event['event_timestamp'] - device_state[device_id]['start_timestamp']
for k in device_state:
print(json.dumps({'index': {'_index': 'x4-states-0'}}))
print(json.dumps(device_state[k]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment