Created
March 13, 2020 17:26
-
-
Save nabam/8729bfab439084a46d6a972661504028 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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