-
Notifications
You must be signed in to change notification settings - Fork 2
/
nlm_general.py
411 lines (377 loc) · 11.9 KB
/
nlm_general.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import datetime
from pytz import timezone
from server.storage import nosql_db as sandbox_db
COLLECTION_LIST = [
{
"col_name": "usage",
"indices": [
[("user_id", 1)],
[("user_id", 1), ("reported_on", 1)],
],
},
{
"col_name": "nlm_subscriptions",
"indices": [
[("subs_name", 1)],
],
},
{
"col_name": "nlm_settings",
"indices": [
[("id", 1)],
],
},
{
"col_name": "nlm_catalog",
"indices": [
[("id", 1)],
],
},
]
NLM_SETTINGS = [
{
"id": "rate_limit_excluded_domains",
"value": ["nlmatics.com", "nlmatics.io"],
},
]
CATALOGS = [
{
"id": "num_workspaces",
"feature": "Workspace",
"renewable": {
"general_usage": False,
"dev_api_usage": True,
},
},
{
"id": "num_docs",
"feature": "Documents",
"renewable": {
"general_usage": False,
"dev_api_usage": True,
},
},
{
"id": "num_fields",
"feature": "Custom Fields",
"renewable": {
"general_usage": False,
"dev_api_usage": False,
},
},
{
"id": "num_search",
"feature": "Search",
"renewable": {
"general_usage": True,
"dev_api_usage": True,
},
},
{
"id": "num_pages",
"feature": "PDF Pages",
"renewable": {
"general_usage": False,
"dev_api_usage": True,
},
},
{
"id": "doc_size",
"feature": "Size",
"renewable": {
"general_usage": False,
"dev_api_usage": True,
},
},
{
"id": "pdf_parser_pages",
"feature": "Parser API PDF Pages",
"renewable": {
"general_usage": False,
"dev_api_usage": True,
},
},
{
"id": "search_api_search",
"feature": "Search API Search",
"renewable": {
"general_usage": False,
"dev_api_usage": True,
},
},
]
SUBSCRIPTIONS = [
{
"subs_name": "plan0",
"quota_limits": {
"general_usage": {
"num_workspaces": 1,
"num_docs": 10,
"num_pages": 10,
"doc_size": 10000,
"num_search": 50,
"num_fields": 10,
},
"dev_api_usage": {
"num_workspaces": 1,
"num_docs": 10,
"num_pages": 10,
"doc_size": 10000,
"num_search": 50,
"num_fields": 10,
},
},
},
{
"subs_name": "BASIC",
"quota_limits": {
"general_usage": {
"num_workspaces": 10,
"num_docs": 1000, # Average 100 document per workspace
"num_pages": 20000, # Average 20 pages per document
"doc_size": 5000000000, # 5 MB per document
"num_search": 20000, # 20 searches per document
"num_fields": 50, # 5 per Workspace
},
"dev_api_usage": {
"num_workspaces": 10,
"num_docs": 1000, # Average 100 document per workspace
"num_pages": 20000, # Average 20 pages per document
"doc_size": 5000000000, # 5 MB per document
"num_search": 20000, # 20 searches per document
"num_fields": 50, # 5 per Workspace
"pdf_parser_pages": 20000, # PDF Parser Pages
"search_api_search": 20000, # Search API Search
},
},
},
]
INDICES_LIST = [
{
"col_name": "document",
"indices": [
[
("is_deleted", 1),
("parent_folder", 1),
("workspace_id", 1),
("created_on", 1),
],
[
("is_deleted", 1),
("parent_folder", 1),
("workspace_id", 1),
("status", 1),
],
[
("is_deleted", 1),
("parent_folder", 1),
("workspace_id", 1),
("name", "text"),
],
[
("is_deleted", 1),
("parent_folder", 1),
("workspace_id", 1),
("meta.pubDate", 1),
],
],
},
]
def create_collections():
"""
Creates collections and the associated indices for each of the collection.
:return: VOID
"""
col_names = sandbox_db.db.list_collection_names()
for col in COLLECTION_LIST:
if col["col_name"] not in col_names:
for index in col["indices"]:
sandbox_db.db[col["col_name"]].create_index(index)
def add_nlm_settings():
for entry in NLM_SETTINGS:
query = {
"id": entry["id"],
}
sandbox_db.db["nlm_settings"].update_one(
query,
{"$set": entry},
upsert=True,
)
print("NLM Settings added.")
def add_catalog():
sandbox_db.db["nlm_catalog"].insert_many(
CATALOGS,
ordered=False,
)
print("NLM Catalogs added.")
def add_subscriptions(subscription_plans):
sandbox_db.db["nlm_subscriptions"].insert_many(
subscription_plans,
ordered=False,
)
print("NLM Subscriptions added.")
def update_users_with_plan_and_expiry_time():
tz = timezone("UTC")
for user in sandbox_db.db["user"].find({}):
if user["email_id"].split("@")[1] in ["nlmatics.com", "nlmatics.io"]:
user["expiry_time"] = (
datetime.datetime.now(tz) + datetime.timedelta(days=3650)
).strftime("%Y-%m-%d %H:%M:%S %Z%z")
else:
user["expiry_time"] = (
datetime.datetime.now(tz) + datetime.timedelta(days=30)
).strftime("%Y-%m-%d %H:%M:%S %Z%z")
user["subscription_plan"] = "BASIC"
sandbox_db.db["user"].replace_one({"id": user["id"]}, user)
def add_default_usage_metrics():
catalogs = sandbox_db.retrieve_catalogs()
user_find_res = sandbox_db.db["user"].find({}, {"_id": 0})
user_list = [u["id"] for u in user_find_res]
for user in user_list:
metric_data = {
"user_id": user,
"reported_on": datetime.datetime.now().strftime("%Y-%m"),
}
for key in ["general_usage", "dev_api_usage"]:
metric_data[key] = {}
for cat_key in catalogs:
metric_data[key][cat_key] = 0
sandbox_db.db["usage"].insert_one(metric_data)
def add_bundle_type():
import re
ws_stream = sandbox_db.db["workspace"].find({"active": True}, {"id": 1})
for ws in ws_stream:
f_bundle_stream = sandbox_db.db["field_bundle"].find(
{"workspace_id": ws["id"]},
{"bundle_name": 1, "id": 1},
)
for bundle in f_bundle_stream:
if bundle.get("id", None) and bundle.get("bundle_name", None):
if re.search(r"(?i)Default", bundle["bundle_name"]):
sandbox_db.db["field_bundle"].update_one(
{"id": bundle.get("id")},
{"$set": {"bundle_type": "DEFAULT"}},
)
print(f"Updated to DEFAULT {bundle.get('id')} for {ws['id']}")
else:
sandbox_db.db["field_bundle"].update_one(
{"id": bundle.get("id")},
{"$set": {"bundle_type": "PUBLIC"}},
)
print(f"Updated to PUBLIC {bundle.get('id')} for {ws['id']}")
def update_user_data(email_set_dict_tuple_list):
for email, set_dict in email_set_dict_tuple_list:
print("Setting: ", email, "===>", set_dict)
sandbox_db.db["user"].update_one({"email_id": email}, {"$set": set_dict})
def add_new_indices():
col_names = sandbox_db.db.list_collection_names()
for col in INDICES_LIST:
if col["col_name"] in col_names:
for index in col["indices"]:
sandbox_db.db[col["col_name"]].create_index(index)
def add_settings_to_workspaces():
for ws in sandbox_db.db["workspace"].find(
{"active": True},
{"_id": 0, "id": 1, "settings": 1},
):
settings = ws.get("settings", {})
changed = False
if not settings.get("domain", None):
settings["domain"] = "general"
changed = True
if not settings.get("search_settings", None):
settings["search_settings"] = {
"table_search": False,
}
changed = True
if changed:
sandbox_db.db["workspace"].update_one(
{
"id": ws["id"],
},
{
"$set": {
"settings": settings,
},
},
)
def add_statistics_to_workspaces():
for ws in sandbox_db.db["workspace"].find(
{"active": True},
{"_id": 0, "id": 1, "statistics": 1},
):
statistics = ws.get("statistics", {})
changed = False
if not statistics:
doc_query = {
"is_deleted": False,
"parent_folder": "root",
"workspace_id": ws["id"],
}
bundle_query = {
"workspace_id": ws["id"],
"active": True,
}
statistics = {
"document": {
"total": sandbox_db.db["document"].count_documents(doc_query),
},
"field_bundle": {
"total": sandbox_db.db["field_bundle"].count_documents(
bundle_query,
),
},
"fields": {
"total": sandbox_db.db["field"].count_documents(bundle_query),
},
}
changed = True
if changed:
sandbox_db.db["workspace"].update_one(
{"id": ws["id"]},
{
"$set": {
"statistics": statistics,
},
},
)
def modify_ws_notification_settings():
for user in sandbox_db.db["user"].find({"active": True}, {"_id": 0}):
changed = False
workspace_notification_settings = {}
user_ws_not_settings = user.get("workspace_notification_settings", None)
if user_ws_not_settings is not None:
if isinstance(user_ws_not_settings, list):
for ws_not in user_ws_not_settings:
if ws_not["email_frequency"] not in workspace_notification_settings:
workspace_notification_settings[ws_not["email_frequency"]] = []
if (
ws_not["workspace_id"]
not in workspace_notification_settings[
ws_not["email_frequency"]
]
):
workspace_notification_settings[
ws_not["email_frequency"]
].append(ws_not["workspace_id"])
changed = True
if changed:
sandbox_db.db["user"].update_one(
{"id": user["id"]},
{
"$set": {
"workspace_notification_settings": workspace_notification_settings,
},
},
)
# create_collections()
# add_nlm_settings()
# add_catalog()
# add_subscriptions(SUBSCRIPTIONS)
# update_users_with_plan_and_expiry_time()
# add_default_usage_metrics()
# add_bundle_type()
# add_new_indices()
# add_settings_to_workspaces()
# add_statistics_to_workspaces()
# modify_ws_notification_settings()