This repository has been archived by the owner on Mar 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSourceNamespaces.py
86 lines (72 loc) · 2.58 KB
/
SourceNamespaces.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
import json
from pprint import pprint
from pymongo import MongoClient
class SourceNamespaces:
"""
SourceNamespaces class which takes a URI or SRV Connection string as an argument
"""
def __init__(self, connection_string):
"""
Create the class object
"""
self.client = MongoClient(connection_string)
self.namespaces = self._get_namespaces()
def _get_namespaces(self):
"""Initializes the namespace dictionary with the source namespaces and calls GenGetCollections()
to add a nested dictionary of specific collstats and index shapes
Arguments:
self
"""
namespaces = {}
for db in self.client.database_names():
if db not in ('admin', 'local', 'config'):
for coll in self.gen_get_collections(db):
namespaces.update(coll)
return namespaces
def gen_get_DBstats(self, db):
"""
Retrieves the dbstats at KB resolution from the provide MongoDB instance(s)
Arguments:
self
db -- string, database name
"""
yield self.client[db].command('dbstats', scale=1024)
def gen_get_collections(self, db):
"""
Generator yields the statistics and index definitions at KB scale from the provided database to be added
to the namespace dictionary on a per collection basis
Arguments:
self
db -- string, database name
"""
for coll in self.client[db].collection_names():
data = self.client[db].command('collstats', coll, scale=1024)
target = {
data.get('ns'): {
'count': data.get('count'),
'size': data.get('size'),
'avgObjSize': data.get('avgObjSize'),
'capped': data.get('capped'),
'nindexes': data.get('nindexes'),
'totalIndexSize': data.get('totalIndexSize'),
'indexSizes': data.get('indexSizes'),
'indexes': self.get_indexes(db, coll)
}
}
yield target
def get_indexes(self, db, coll):
"""
Gathers index structure for a particular namespace into a dictionary
Arguments:
self
db -- string, database name
coll -- string collection name
"""
return self.client[db][coll].index_information()
def print_namespaces(self):
"""
Prints the namespaces dictionary
Arguments:
self
"""
pprint(self.namespaces)