minecraft-monitor/convert-logs.py

56 lines
2.0 KiB
Python
Raw Permalink Normal View History

2019-03-13 18:38:21 +00:00
from json import loads, dumps
from makeline import make_line
from sys import argv
2019-03-13 18:38:21 +00:00
log_path = '/dev/null'
2019-03-13 18:38:21 +00:00
def parse_logs():
with open('all-time.txt', 'r') as fin:
(last_t, last_ev, last_evt, last_ps) = (0, 'error', 0, [])
2019-03-13 18:38:21 +00:00
with open(log_path+'events.json', 'w') as fout:
cur_t = 0
cur_ps = []
2019-03-13 18:38:21 +00:00
for line in fin:
current = loads(line)
cur_t = current['time']
cur_ps = current['players']
2019-03-13 18:38:21 +00:00
if last_t == 0:
(last_t, last_ev, last_evt, last_ps) = (cur_t, 'online', cur_t, cur_ps)
else:
if cur_t - last_t > 90:
if last_ev == 'online':
fout.write(dumps({'period': [last_evt, last_t], 'status': last_ev, 'players': last_ps})+'\n')
(last_ev, last_evt, last_ps) = ('unreachable', last_t, cur_ps)
else:
if last_ev != 'online':
fout.write(dumps({'period': [last_evt, last_t], 'status': last_ev})+'\n')
(last_ev, last_evt, last_ps) = ('online', last_t, last_ps)
2019-03-13 18:38:21 +00:00
if last_ps != cur_ps:
fout.write(dumps({'period': [last_evt, cur_t], 'status': last_ev, 'players': last_ps})+'\n')
(last_ev, last_evt, last_ps) = ('online', cur_t, cur_ps)
2019-03-13 18:38:21 +00:00
last_t = cur_t
2019-03-13 18:38:21 +00:00
with open(log_path+'current-state.json', 'w') as fout:
fout.write(dumps({'period': [last_evt, last_t], 'status': 'online', 'players': last_ps}))
2019-03-13 18:38:21 +00:00
def events_to_lines():
with open(log_path+'events.json', 'r') as fin:
with open(log_path+'events.log', 'w') as fout:
2019-03-13 18:38:21 +00:00
for l in fin:
fout.write(make_line(loads(l))+'\n')
if __name__ == "__main__":
if len(argv) > 1:
log_path = argv[1]+'/'
parse_logs()
events_to_lines()
else:
print('error: specify target path')