-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.py
137 lines (130 loc) · 8.14 KB
/
converter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import json
import csv
import ast
import datetime
import sys
import optparse
def main(opts):
now = datetime.datetime.now().strftime('%s')+".csv"
f = csv.writer(open(now, "wb+"))
f.writerow(["received_timestamp","UNIX_received","sent_timestamp", "UNIX_sent","diff", "vin", "power_ID", "event", "rpm_value", "odometer_value", "total_fuel_value", "fuel_level_value", "speed_value", "engine_hours_value", "ignition", "jbus_connected", "esn", "wheels_in_motion", "city", "lat", "long", "description", "hdop", "country", "time_since_update", "state", "GPS_valid", "satellites", "accuracy", "heading","event_type"])
if len(sys.argv) > 1:
with open(opts.input_file) as my_file:
for line in my_file.readlines():
try:
if 'Sending:' in line:
if "count\'" in line:
columns = line.split(' [DEBUG] Sending:')
k1 = json.dumps(ast.literal_eval(columns[1].strip()))
entry = json.loads(k1)
f.writerow([columns[0],
datetime.datetime.strptime(columns[0], '%Y-%m-%d %H:%M:%S.%f').strftime('%s.%f'),
datetime.datetime.fromtimestamp((entry['timestamp'] / 1000.00) + (opts.time_zone * 3600)).strftime('%Y-%m-%d %H:%M:%S.%f'),
entry['timestamp']/1000.00,
(float((datetime.datetime.strptime(columns[0], '%Y-%m-%d %H:%M:%S.%f').strftime('%s.%f')))-(entry['timestamp']/1000.0)),
entry['data'][0]['powerunit_vin'],
entry['data'][0]['powerunit_id'],
entry['event'],
entry['data'][0]['engine_parameters']['rpm']['value'],
entry['data'][0]['engine_parameters']['odometer']['value'],
entry['data'][0]['engine_parameters']['total_fuel_used']['value'],
entry['data'][0]['engine_parameters']['fuel_level']['value'],
entry['data'][0]['engine_parameters']['speed']['value'],
entry['data'][0]['engine_parameters']['engine_hours']['value'],
entry['data'][0]['ignition'],
entry['data'][0]['jbus_connected'],
entry['data'][0]['esn'],
entry['data'][0]['wheels_in_motion'],
entry['data'][0]['location']['city'],
entry['data'][0]['location']['lat'],
entry['data'][0]['location']['lon'],
entry['data'][0]['location']['description'],
entry['data'][0]['location']['hdop'],
entry['data'][0]['location']['country'],
entry['data'][0]['location']['time_since_update'],
entry['data'][0]['location']['state'],
entry['data'][0]['location']['valid'],
entry['data'][0]['location']['satellites'],
entry['data'][0]['location']['accuracy'],
entry['data'][0]['location']['heading']
])
else:
columns = line.split(' [DEBUG] Sending:')
k1 = json.dumps(ast.literal_eval(columns[1].strip()))
entry = json.loads(k1)
f.writerow([columns[0],
datetime.datetime.strptime(columns[0], '%Y-%m-%d %H:%M:%S.%f').strftime('%s.%f'),
datetime.datetime.fromtimestamp((entry['timestamp'] / 1000.00) + (opts.time_zone * 3600)).strftime('%Y-%m-%d %H:%M:%S.%f'),
entry['timestamp']/1000.00,
(float((datetime.datetime.strptime(columns[0], '%Y-%m-%d %H:%M:%S.%f').strftime('%s.%f'))) - (entry['timestamp'] / 1000.0)),
None,
None,
'periodic_update',
entry['engine_parameters']['rpm']['value'],
entry['engine_parameters']['odometer']['value'],
None,
None,
entry['engine_parameters']['speed']['value'],
entry['engine_parameters']['engine_hours']['value'],
None,
None,
None,
None,
entry['location']['city'],
entry['location']['lat'],
entry['location']['lon'],
entry['location']['description'],
entry['location']['hdop'],
entry['location']['country'],
entry['location']['time_since_update'],
entry['location']['state'],
entry['location']['valid'],
entry['location']['satellites'],
entry['location']['accuracy'],
entry['location']['heading'],
None])
except:
f.writerow([columns[0],
datetime.datetime.strptime(columns[0], '%Y-%m-%d %H:%M:%S.%f').strftime('%s.%f'),
datetime.datetime.fromtimestamp((entry['timestamp'] / 1000.00) + (opts.time_zone * 3600)).strftime('%Y-%m-%d %H:%M:%S.%f'),
entry['timestamp']/1000.00,
(float((datetime.datetime.strptime(columns[0], '%Y-%m-%d %H:%M:%S.%f').strftime('%s.%f'))) - (entry['timestamp'] / 1000.0)),
None,
None,
entry['event'],
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
None,
entry['type']])
else:
exit('No file provided')
if __name__ == '__main__':
# This If Passes only if the currently file is directly run by the user and not imported by another file.
DESCRIPTION = """
converting json logs to .csv format
"""
parser = optparse.OptionParser(description=DESCRIPTION)
# creating a object for the option parser
parser.add_option('-f', '--input_file', dest='input_file', help='Input Log File')
parser.add_option('-t', '--time_zone', dest='time_zone', type=int, help='Time Change from Time Zone of Input File')
(opts, args) = parser.parse_args()
main(opts)