-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels.py
256 lines (207 loc) · 7.23 KB
/
models.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import datetime
import json
import pymongo
import uuid
from mongokit import Document
import config
from database import connection, DBLayer
@connection.register
class DataSetMeta(Document):
"""Meta data for the data set
Meta data is made up of;
- source: data set source (eg: IP address)
- label
- field count: total # of fields in the data set
- field types: data type of each field
- field headers: if no headers provided then use first record data
- collection: where data set is stored
"""
__collection__ = 'data_set_meta'
use_dot_notation = True
structure = {
'source': unicode,
'label': unicode,
'field_types': list,
'collection_id': basestring,
}
def __init__(self, source=None, field_types=None):
super(DataSetMeta, self).__init__()
self.source = source
self.field_types = field_types
self._id = None
self.collection_id = None
self.label = None
self.dblayer = DBLayer()
def __repr__(self):
return u"<Meta {0}>".format(self.get_label())
def set_values(self, data):
if data is None:
return
self.set_id(data)
self.set_label(data)
self.set_collection_id(data)
def set_id(self, data):
if data.get('_id', False):
self._id = u"{0}".format(data.get('_id'))
else:
self._id = None
def get_id(self):
return self._id
def set_label(self, data):
self.label = data.get('label', None)
if self.label is None:
self.label = self.get_source()
def set_collection_id(self, data):
self.collection_id = data.get('collection_id', None)
def get_collection_id(self):
return u"{0}".format(self.collection_id)
def get_source(self):
return self.source
def get_field_types(self):
return self.field_types
def get_label(self):
if self.label is None:
return self.get_source()
return self.label
def get(self):
"""Pull data from database"""
db = self.dblayer.get_db()
data = db.data_set_meta.find_one({
'source': self.get_source(),
'field_types': self.get_field_types()
})
self.set_values(data)
def to_json_friendly(self):
data = {
'source': self.get_source(),
'field_types': self.get_field_types(),
'collection_id': self.get_collection_id(),
'label': self.get_label()
}
if self.get_id() is not None:
data['_id'] = self.get_id()
return data
def get_meta_data_key(self):
return json.dumps((
self.source,
self.field_types
))
def get_data(self, limit=config.RESULT_SET_LIMIT):
"""Get data and split into separate streams based on the number of fields
Example:
Data received [10, 34], [13, 53] ...
Return:
data[1] = [10, 14]
data[2] = [34, 53]
"""
db = self.dblayer.get_db()
result = db[self.get_collection_id()].find(
sort=[("_id", pymongo.DESCENDING)],
limit=limit
)
data = []
for x in result:
data = self.format_data(x, data)
return data
def format_data(self, data, data_list=[]):
current_data = data.get("data")
if not data_list:
data_list = [[] for field in range(len(current_data))]
for i in range(len(current_data)):
data_list[i].append(current_data[i])
return data_list
def save(self):
"""Save data to database
If no collection value need to create one
"""
if self.collection_id is None:
self.collection_id = u"{0}".format(uuid.uuid1())
db = self.dblayer.get_db()
db.data_set_meta.save(self.to_json_friendly())
self.get()
def save_data(self, data):
"""Dave data, make sure we have a timestamp
If no timestamp, then add
"""
db = self.dblayer.get_db()
if not data.get("timestamp", False):
data.update(
timestamp=datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%s")
)
db[self.get_collection_id()].insert(data)
return DataSet(data).to_json_friendly()
class DataSet(object):
"""The raw data stored in collections determined by meta data"""
def __init__(self, data):
self.data = data
def to_json_friendly(self):
data = {
'data': self.data.get('data')
}
if self.data.get('_id') is not None:
data['_id'] = u"{0}".format(self.data.get("_id"))
return data
class DataSetHandler(object):
"""Wrapper to data set information"""
def __init__(self):
self.data_set_meta = {}
self.dblayer = DBLayer()
def get_data_set_meta(self):
db = self.dblayer.get_db()
for meta in db.data_set_meta.find():
meta_data = DataSetMeta(
meta.get("source"),
meta.get("field_types")
)
meta_data.set_values(meta)
self.data_set_meta[meta_data.get_meta_data_key()] = meta_data
return self.data_set_meta
def add(self, data):
"""When adding determine whether there is matching meta data by;
- source
- field count
- field types
If match is found then store in relevant collection, otherwise
create new meta data record and collection
"""
complete_data = json.loads(data)
source = self.get_source(complete_data)
field_types = self.get_field_types(complete_data)
meta_data = self.get_or_create_meta(source, field_types)
new_data = meta_data.save_data(self.get_raw_data(complete_data))
response = meta_data.to_json_friendly()
response["data"] = meta_data.format_data(new_data)
return response
def get_or_create_meta(self, source, field_types):
"""Check for matching meta data record otherwise create one"""
meta_data = DataSetMeta(source, field_types)
meta_data.get()
if meta_data.get_meta_data_key() not in self.data_set_meta.keys():
if meta_data.get_id() is None:
meta_data.save()
self.data_set_meta[meta_data.get_meta_data_key()] = meta_data
return meta_data
def get_source(self, data):
"""Determine source of data"""
return data.get('source')
def get_field_types(self, data):
"""Determine the field types of raw data"""
raw_data = self.get_raw_data(data)
if raw_data.get("data", False):
values_list = raw_data.get("data")
else:
values_list = raw_data.values()
field_types = []
for field in values_list:
field_types.append("{0}".format(type(field)))
return field_types
def get_raw_data(self, data):
"""Remove irrelevant fields and leave just the actual data"""
raw_data = data.copy()
irrelevant_fields = [
"source",
]
for irrelevant_field in irrelevant_fields:
if raw_data.get(irrelevant_field):
raw_data.pop(irrelevant_field)
return raw_data