diff --git a/mongodbtools/collection_stats.py b/mongodbtools/collection_stats.py index fb54c3e..33bda68 100755 --- a/mongodbtools/collection_stats.py +++ b/mongodbtools/collection_stats.py @@ -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"] @@ -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(): @@ -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) @@ -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] @@ -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() diff --git a/mongodbtools/index_stats.py b/mongodbtools/index_stats.py index c2b006b..ae06884 100755 --- a/mongodbtools/index_stats.py +++ b/mongodbtools/index_stats.py @@ -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"] @@ -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 @@ -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) @@ -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: @@ -141,11 +142,11 @@ 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" @@ -153,22 +154,22 @@ def main(options): 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() diff --git a/mongodbtools/query/helpers.py b/mongodbtools/query/helpers.py index 2c246ad..d8838e5 100644 --- a/mongodbtools/query/helpers.py +++ b/mongodbtools/query/helpers.py @@ -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) diff --git a/mongodbtools/query/parser.py b/mongodbtools/query/parser.py index 86221d1..b350cc2 100644 --- a/mongodbtools/query/parser.py +++ b/mongodbtools/query/parser.py @@ -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 diff --git a/mongodbtools/redundant_indexes.py b/mongodbtools/redundant_indexes.py index 7cf6525..21c2c67 100755 --- a/mongodbtools/redundant_indexes.py +++ b/mongodbtools/redundant_indexes.py @@ -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(): @@ -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) @@ -68,23 +69,26 @@ 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: - signature = compute_signature(index) - index_map[signature] = index - - for signature in index_map.keys(): - for other_sig in index_map.keys(): + collections = current_db.collection_names() + for collection in collections: + indexes = current_db[collection].list_indexes() + for index in indexes: + signature = compute_signature(index) + index_map[signature] = index + + for signature in list(index_map.keys()): + for other_sig in list(index_map.keys()): 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: