-
Notifications
You must be signed in to change notification settings - Fork 1
/
json2mongo.py
173 lines (155 loc) · 5.54 KB
/
json2mongo.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import database
from datetime import datetime
from mapval import MappingValidator
import re
reference = {
'Id': str,
'type': int,
'Measurement_Id': int,
'time': str,
'date': str,
'tag': str,
'val0': lambda val: type(val) is int or type(val) is float,
'val1': lambda val: type(val) is int or type(val) is float,
'val2': lambda val: type(val) is int or type(val) is float
}
class Json2Mongo:
def __init__(self):
self._validator = MappingValidator(reference)
self._db_inserts, self._db_extended = database.init()
def _to_db(self, user, json):
"""
JUST USE THIS METHOD IF YOU USED check_format!
Better use check_and_commit
"""
return self._db_inserts.insert_csv_row(user, json)
def check_and_commit(self, json):
new_json = self.check(json)
if new_json:
return self._to_db(new_json['UserID'], new_json)
def check(self, json):
if self._validator.validate(json):
time_stamp_str = '%s,%s' % (json['date'],json['time'])
new_json = {
"tag": json['tag'],
"UserID": json['Id'].replace('_at_', '@'),
"type": json['type'],
"time_stamp": datetime.strptime(time_stamp_str, '%Y.%m.%d,%H:%M:%S'),
"val1": json['val1'],
"val2": json['val2'],
"val0": json['val0']
}
return new_json
return None
def check_many(self, json_lst):
bulk = []
for json in json_lst:
new_json = self.check(json)
if new_json:
bulk.append(new_json)
else:
return None
if not bulk:
return None
return bulk
def _to_db_many(self, user, json_lst):
return self._db_inserts.insert_csv_row_many(user, json_lst)
def check_and_commit_many(self, json_lst):
new_json_lst = self.check_many(json_lst)
if new_json_lst:
return self._to_db_many(new_json_lst[0]['UserID'], new_json_lst)
if __name__ == '__main__':
j2m = Json2Mongo()
date = datetime(2016, 1, 18, 11, 22, 55)
date2 = datetime(2016, 1, 19, 11, 22, 55)
measurements = {
'arrayOfMeasurements': [
{
'Id': 1,
'Type': 'bal',
'Timestamp': '2016, 1, 18, 11, 22, 55',
'values': [
{
"type": 4,
"Id": "[email protected]",
"Measurement_Id": 1,
"tag": "Idle",
'time': date.strftime('%H:%M:%S'),
'date': date.strftime('%Y.%m.%d'),
"val2": 0,
"val1": 0,
"val0": 1111
},
{
"type": 4,
"Id": "[email protected]",
"Measurement_Id": 1,
"tag": "Idle",
'time': date.strftime('%H:%M:%S'),
'date': date.strftime('%Y.%m.%d'),
"val2": 0,
"val1": 0,
"val0": 1112
},
{
"type": 4,
"Id": "[email protected]",
"Measurement_Id": 1,
"tag": "Idle",
'time': date.strftime('%H:%M:%S'),
'date': date.strftime('%Y.%m.%d'),
"val2": 0,
"val1": 0,
"val0": 1113
}]
},
{
'Id': 2,
'Type': 'bal',
'Timestamp': '2016, 1, 19, 11, 22, 55',
'values': [
{
"type": 4,
"Id": "[email protected]",
"Measurement_Id": 2,
"tag": "Idle",
'time': date2.strftime('%H:%M:%S'),
'date': date2.strftime('%Y.%m.%d'),
"val2": 0,
"val1": 0,
"val0": 1114
},
{
"type": 4,
"Id": "[email protected]",
"Measurement_Id": 2,
"tag": "Idle",
'time': date2.strftime('%H:%M:%S'),
'date': date2.strftime('%Y.%m.%d'),
"val2": 0,
"val1": 0,
"val0": 1115
},
{
"type": 4,
"Id": "[email protected]",
"Measurement_Id": 2,
"tag": "Idle",
'time': date2.strftime('%H:%M:%S'),
'date': date2.strftime('%Y.%m.%d'),
"val2": 0,
"val1": 0,
"val0": 1116
}]
}
]
}
import pprint
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(measurements)
for measurement in measurements['arrayOfMeasurements']:
for index, measured_value in enumerate(measurement['values']):
pp.pprint(measured_value)
measurement['values'][index] = j2m.check(measured_value)
pp.pprint(measured_value)
pp.pprint(measurements)