forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo_generic.py
274 lines (209 loc) · 8.2 KB
/
mongo_generic.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from copy import copy
from datetime import date, time
from syscore.objects import arg_not_supplied, missingData, existingData, missing_data
from sysdata.mongodb.mongo_connection import (
mongoConnection,
MONGO_ID_KEY,
mongo_clean_ints,
clean_mongo_host,
)
class mongoDataWithSingleKey(object):
"""
Read and write data class to get data from a mongo database
"""
def __init__(self, collection_name, key_name, mongo_db=arg_not_supplied):
self.init_mongo(collection_name, key_name, mongo_db=mongo_db)
def init_mongo(
self,
collection_name: str,
key_name: str,
mongo_db=arg_not_supplied,
):
mongo_object = mongoConnection(collection_name, mongo_db=mongo_db)
self._mongo = mongo_object
self._key_name = key_name
# this won't create the index if it already exists
# if a different index exists (FIX ME WILL HAPPEN UNTIL NEW DATA READY)...
try:
self._mongo.create_index(self.key_name)
except:
pass
## no big deal
def __repr__(self):
return self.name
@property
def key_name(self) -> str:
return self._key_name
@property
def name(self) -> str:
col = self._mongo.collection_name
db = self._mongo.database_name
host = clean_mongo_host(self._mongo.host)
return f"mongoData connection for {col}/{db}, {host}"
@property
def collection(self):
return self._mongo.collection
def get_list_of_keys(self) -> list:
return self.collection.distinct(self.key_name)
def get_max_of_keys(self) -> int:
doc = self.collection.find_one(sort=[(self.key_name, -1)])
if doc is None:
return 0
if self.key_name in doc:
return doc[self.key_name]
else:
return 0
def get_list_of_values_for_dict_key(self, dict_key):
return [
row[dict_key]
for row in self.collection.find(
{self.key_name: {"$exists": True}}, {dict_key: 1}
)
]
def get_result_dict_for_key(self, key) -> dict:
key_name = self.key_name
result_dict = self.collection.find_one({key_name: key})
if result_dict is None:
return missing_data
result_dict.pop(MONGO_ID_KEY)
return result_dict
def get_result_dict_for_key_without_key_value(self, key) -> dict:
key_name = self.key_name
result_dict = self.get_result_dict_for_key(key)
if result_dict is missing_data:
return missing_data
result_dict.pop(key_name)
return result_dict
def get_list_of_result_dict_for_custom_dict(self, custom_dict: dict) -> list:
cursor = self._mongo.collection.find(custom_dict)
dict_list = [db_entry for db_entry in cursor]
_ = [dict_item.pop(MONGO_ID_KEY) for dict_item in dict_list]
return dict_list
def key_is_in_data(self, key):
result = self.get_result_dict_for_key(key)
if result is missing_data:
return False
else:
return True
def delete_data_without_any_warning(self, key):
key_name = self.key_name
if not self.key_is_in_data(key):
raise missingData("%s:%s not in data %s" % (key_name, key, self.name))
self.collection.remove({key_name: key})
def delete_data_with_any_warning_for_custom_dict(self, custom_dict: dict):
self.collection.remove(custom_dict)
def add_data(self, key, data_dict: dict, allow_overwrite=False, clean_ints=True):
if clean_ints:
cleaned_data_dict = mongo_clean_ints(data_dict)
else:
cleaned_data_dict = copy(data_dict)
if self.key_is_in_data(key):
if allow_overwrite:
self._update_existing_data_with_cleaned_dict(key, cleaned_data_dict)
else:
raise existingData(
"Can't overwite existing data %s/%s for %s"
% (self.key_name, key, self.name)
)
else:
try:
self._add_new_cleaned_dict(key, cleaned_data_dict)
except:
## this could happen if the key has just been added most likely for logs
raise existingData(
"Can't overwite existing data %s/%s for %s"
% (self.key_name, key, self.name)
)
def _update_existing_data_with_cleaned_dict(self, key, cleaned_data_dict):
key_name = self.key_name
self.collection.update_one({key_name: key}, {"$set": cleaned_data_dict})
def _add_new_cleaned_dict(self, key, cleaned_data_dict):
key_name = self.key_name
cleaned_data_dict[key_name] = key
self.collection.insert_one(cleaned_data_dict)
class mongoDataWithMultipleKeys(object):
"""
Read and write data class to get data from a mongo database
Use this if you aren't using a specific key as the index
"""
def __init__(self, collection_name: str, mongo_db=arg_not_supplied):
self.init_mongo(collection_name, mongo_db=mongo_db)
def init_mongo(
self,
collection_name: str,
mongo_db=arg_not_supplied,
):
mongo_object = mongoConnection(collection_name, mongo_db=mongo_db)
self._mongo = mongo_object
def __repr__(self):
return self.name
@property
def name(self) -> str:
mongo_object = self._mongo
name = "mongoData connection for mongodb %s/%s @ %s -p %s " % (
mongo_object.database_name,
mongo_object.collection_name,
mongo_object.host,
mongo_object.port,
)
return name
def get_list_of_all_dicts(self) -> list:
cursor = self._mongo.collection.find()
dict_list = [db_entry for db_entry in cursor]
_ = [dict_item.pop(MONGO_ID_KEY) for dict_item in dict_list]
return dict_list
def get_result_dict_for_dict_keys(self, dict_of_keys: dict) -> dict:
result_dict = self._mongo.collection.find_one(dict_of_keys)
if result_dict is None:
return missing_data
result_dict.pop(MONGO_ID_KEY)
return result_dict
def get_list_of_result_dicts_for_dict_keys(self, dict_of_keys: dict) -> list:
cursor_of_result_dicts = self._mongo.collection.find(dict_of_keys)
if cursor_of_result_dicts is None:
return []
list_of_result_dicts = list(cursor_of_result_dicts)
_ = [result_dict.pop(MONGO_ID_KEY) for result_dict in list_of_result_dicts]
return list_of_result_dicts
def key_dict_is_in_data(self, dict_of_keys: dict) -> bool:
result = self.get_result_dict_for_dict_keys(dict_of_keys)
if result is missing_data:
return False
else:
return True
def add_data(
self,
dict_of_keys: dict,
data_dict: dict,
allow_overwrite=False,
clean_ints=True,
):
if clean_ints:
cleaned_data_dict = mongo_clean_ints(data_dict)
else:
cleaned_data_dict = copy(data_dict)
if self.key_dict_is_in_data(dict_of_keys):
if allow_overwrite:
self._update_existing_data_with_cleaned_dict(
dict_of_keys, cleaned_data_dict
)
else:
raise existingData(
"Can't overwite existing data %s for %s"
% (str(dict_of_keys), self.name)
)
else:
self._add_new_cleaned_dict(dict_of_keys, cleaned_data_dict)
def _update_existing_data_with_cleaned_dict(
self, dict_of_keys: dict, cleaned_data_dict: dict
):
self._mongo.collection.update_one(dict_of_keys, {"$set": cleaned_data_dict})
def _add_new_cleaned_dict(self, dict_of_keys: dict, cleaned_data_dict: dict):
dict_with_both_keys_and_data = {}
dict_with_both_keys_and_data.update(cleaned_data_dict)
dict_with_both_keys_and_data.update(dict_of_keys)
self._mongo.collection.insert_one(dict_with_both_keys_and_data)
def delete_data_without_any_warning(self, dict_of_keys):
self._mongo.collection.remove(dict_of_keys)
_date = date
_time = time