-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongo_storage.py
More file actions
47 lines (37 loc) · 1.16 KB
/
mongo_storage.py
File metadata and controls
47 lines (37 loc) · 1.16 KB
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
import pymongo
from pymongo import MongoClient
import settings
class GetClient:
def __init__(self):
self.client=None
def __enter__(self):
self.client = MongoClient(host=settings.MONGOURI, username=settings.MONGOUSER, password=settings.MONGOPWD)
return self.client.cen_store.records
def __exit__(self, *args, **kwargs):
return self.client.close()
def getNextIndex():
with GetClient() as cli:
pp = cli.aggregate(
[
{
"$group":
{
"_id": {},
"maxIndex": {"$max": "$index"}
}
}
]
)
index = list(pp)
if not index:
return 0
return index[0].get('maxIndex') + 1
def create_index():
with GetClient() as cli:
cli.create_index([('carrier_id', pymongo.DESCENDING)], unique=True)
cli.create_index([('index', pymongo.DESCENDING)], unique=True)
def get_failed():
with GetClient() as cli:
failed = cli.find({'loaded': False})
return len(list(failed))
create_index()