Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2to3 #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

2to3 #16

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions mongodbtools/collection_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pymongo import MongoClient
from pymongo import ReadPreference
from optparse import OptionParser
from urllib.parse import quote_plus

def compute_signature(index):
signature = index["ns"]
Expand All @@ -18,7 +19,7 @@ def compute_signature(index):
return signature

def get_collection_stats(database, collection):
print "Checking DB: %s" % collection.full_name
print(("Checking DB: %s" % collection.full_name))
return database.command("collstats", collection.name)

def get_cli_options():
Expand Down Expand Up @@ -58,7 +59,7 @@ def get_cli_options():
def get_client(host, port, username, password):
userPass = ""
if username and password:
userPass = username + ":" + password + "@"
userPass = quote_plus(username) + ":" + quote_plus(password) + "@"

mongoURI = "mongodb://" + userPass + host + ":" + str(port)
client = MongoClient(mongoURI)
Expand Down Expand Up @@ -130,7 +131,7 @@ def main(options):
x.align["Storage Size"] = "r"
x.padding_width = 1

print
print()

for db in all_db_stats:
db_stats = all_db_stats[db]
Expand All @@ -145,19 +146,19 @@ def main(options):
convert_bytes(stat.get("storageSize", 0))
])

print
print x.get_string(sortby="% Size")
print "Total Documents:", summary_stats["count"]
print "Total Data Size:", convert_bytes(summary_stats["size"])
print "Total Index Size:", convert_bytes(summary_stats["indexSize"])
print "Total Storage Size:", convert_bytes(summary_stats["storageSize"])
print()
print((x.get_string(sortby="% Size")))
print(("Total Documents:", summary_stats["count"]))
print(("Total Data Size:", convert_bytes(summary_stats["size"])))
print(("Total Index Size:", convert_bytes(summary_stats["indexSize"])))
print(("Total Storage Size:", convert_bytes(summary_stats["storageSize"])))

# this is only meaningful if we're running the script on localhost
if options.host == "localhost":
ram_headroom = psutil.virtual_memory().total - summary_stats["indexSize"]
print "RAM Headroom:", convert_bytes(ram_headroom)
print "RAM Used: %s (%s%%)" % (convert_bytes(psutil.virtual_memory().used), psutil.virtual_memory().percent)
print "Available RAM Headroom:", convert_bytes((100 - psutil.virtual_memory().percent) / 100 * ram_headroom)
print(("RAM Headroom:", convert_bytes(ram_headroom)))
print(("RAM Used: %s (%s%%)" % (convert_bytes(psutil.virtual_memory().used), psutil.virtual_memory().percent)))
print(("Available RAM Headroom:", convert_bytes((100 - psutil.virtual_memory().percent) / 100 * ram_headroom)))

if __name__ == "__main__":
options = get_cli_options()
Expand Down
33 changes: 17 additions & 16 deletions mongodbtools/index_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pymongo import MongoClient
from pymongo import ReadPreference
from optparse import OptionParser
from urllib.parse import quote_plus

def compute_signature(index):
signature = index["ns"]
Expand All @@ -18,7 +19,7 @@ def compute_signature(index):
return signature

def get_collection_stats(database, collection):
print "Checking DB: %s" % collection.full_name
print(("Checking DB: %s" % collection.full_name))
return database.command("collstats", collection.name)

# From http://www.5dollarwhitebox.org/drupal/node/84
Expand Down Expand Up @@ -78,7 +79,7 @@ def get_cli_options():
def get_client(host, port, username, password):
userPass = ""
if username and password:
userPass = username + ":" + password + "@"
userPass = quote_plus(username) + ":" + quote_plus(password) + "@"

mongoURI = "mongodb://" + userPass + host + ":" + str(port)
return MongoClient(mongoURI)
Expand Down Expand Up @@ -124,7 +125,7 @@ def main(options):
x.align["Index Size"] = "r"
x.padding_width = 1

print
print()

index_size_mapping = {}
for db in all_db_stats:
Expand All @@ -141,34 +142,34 @@ def main(options):
x.add_row(row)


print "Index Overview"
print x.get_string(sortby="Collection")
print("Index Overview")
print((x.get_string(sortby="Collection")))

print
print "Top 5 Largest Indexes"
print()
print("Top 5 Largest Indexes")
x = PrettyTable(["Collection", "Index","% Size", "Index Size"])
x.align["Collection"] = "l"
x.align["Index"] = "l"
x.align["% Size"] = "r"
x.align["Index Size"] = "r"
x.padding_width = 1

top_five_indexes = sorted(index_size_mapping.keys(), reverse=True)[0:5]
top_five_indexes = sorted(list(index_size_mapping.keys()), reverse=True)[0:5]
for size in top_five_indexes:
x.add_row(index_size_mapping.get(size))
print x
print
print(x)
print()

print "Total Documents:", summary_stats["count"]
print "Total Data Size:", convert_bytes(summary_stats["size"])
print "Total Index Size:", convert_bytes(summary_stats["indexSize"])
print(("Total Documents:", summary_stats["count"]))
print(("Total Data Size:", convert_bytes(summary_stats["size"])))
print(("Total Index Size:", convert_bytes(summary_stats["indexSize"])))

# this is only meaningful if we're running the script on localhost
if options.host == "localhost":
ram_headroom = psutil.virtual_memory().total - summary_stats["indexSize"]
print "RAM Headroom:", convert_bytes(ram_headroom)
print "RAM Used: %s (%s%%)" % (convert_bytes(psutil.virtual_memory().used), psutil.virtual_memory().percent)
print "Available RAM Headroom:", convert_bytes((100 - psutil.virtual_memory().percent) / 100 * ram_headroom)
print(("RAM Headroom:", convert_bytes(ram_headroom)))
print(("RAM Used: %s (%s%%)" % (convert_bytes(psutil.virtual_memory().used), psutil.virtual_memory().percent)))
print(("Available RAM Headroom:", convert_bytes((100 - psutil.virtual_memory().percent) / 100 * ram_headroom)))

if __name__ == "__main__":
options = get_cli_options()
Expand Down
2 changes: 1 addition & 1 deletion mongodbtools/query/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ def filter(iterator, field, value):
embedded documents.
"""

return itertools.ifilter(lambda x: _deep_get(x, field) == value, iterator)
return filter(lambda x: _deep_get(x, field) == value, iterator)
18 changes: 9 additions & 9 deletions mongodbtools/query/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
ZeroOrMore, restOfLine, Keyword

def test( str ):
print str,"->"
print(str,"->")
try:
tokens = simpleSQL.parseString( str )
print "tokens = ", tokens
print "tokens.columns =", tokens.columns
print "tokens.tables =", tokens.tables
print "tokens.where =", tokens.where
except ParseException, err:
print " "*err.loc + "^\n" + err.msg
print err
print
print("tokens = ", tokens)
print("tokens.columns =", tokens.columns)
print("tokens.tables =", tokens.tables)
print("tokens.where =", tokens.where)
except ParseException as err:
print(" "*err.loc + "^\n" + err.msg)
print(err)
print()


# define SQL tokens
Expand Down
9 changes: 5 additions & 4 deletions mongodbtools/redundant_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pymongo import MongoClient
from pymongo import ReadPreference
from optparse import OptionParser
from urllib.parse import quote_plus


def get_cli_options():
Expand Down Expand Up @@ -48,7 +49,7 @@ def get_cli_options():
def get_client(host, port, username, password):
userPass = ""
if username and password:
userPass = username + ":" + password + "@"
userPass = quote_plus(username) + ":" + quote_plus(password) + "@"

mongoURI = "mongodb://" + userPass + host + ":" + str(port)
client = MongoClient(mongoURI)
Expand All @@ -68,7 +69,7 @@ def compute_signature(index):
return signature

def report_redundant_indexes(current_db):
print "Checking DB: %s" % current_db.name
print("Checking DB: %s" % current_db.name)
indexes = current_db.system.indexes.find()
index_map = {}
for index in indexes:
Expand All @@ -80,11 +81,11 @@ def report_redundant_indexes(current_db):
if signature == other_sig:
continue
if other_sig.startswith(signature):
print "Index %s[%s] may be redundant with %s[%s]" % (
print("Index %s[%s] may be redundant with %s[%s]" % (
index_map[signature]["ns"],
index_map[signature]["name"],
index_map[other_sig]["ns"],
index_map[other_sig]["name"])
index_map[other_sig]["name"]))

databases= []
if options.database:
Expand Down