-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PyMongo 4 is too advanced for us (#142)
* PyMongo 4 is too advanced for us * Motor 3 is too advanced for us
- Loading branch information
Showing
10 changed files
with
145 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# fmt:off | ||
|
||
import os | ||
from typing import Any, cast, Dict | ||
|
||
from pymongo import MongoClient # type: ignore[import] | ||
from pymongo.database import Database # type: ignore[import] | ||
|
||
# PyMongo profiling level constants from PyMongo 3 (removed in PyMongo 4) | ||
# See: https://api.mongodb.com/python/3.0.3/api/pymongo/database.html#pymongo.ALL | ||
# See: https://www.mongodb.com/docs/manual/reference/command/profile/#mongodb-dbcommand-dbcmd.profile | ||
OFF = 0 | ||
SLOW_ONLY = 1 | ||
ALL = 2 | ||
|
||
FCDoc = Dict[str, Any] | ||
|
||
env = { | ||
'TEST_DATABASE_HOST': 'localhost', | ||
'TEST_DATABASE_PORT': 27017, | ||
} | ||
for k in env: | ||
if k in os.environ: | ||
if isinstance(env[k], int): | ||
env[k] = int(os.environ[k]) | ||
elif isinstance(env[k], float): | ||
env[k] = float(os.environ[k]) | ||
else: | ||
env[k] = os.environ[k] | ||
|
||
test_database_host = str(env['TEST_DATABASE_HOST']) | ||
test_database_port = int(str(env['TEST_DATABASE_PORT'])) | ||
db: Database[FCDoc] = cast(Database[FCDoc], MongoClient(host=test_database_host, port=test_database_port).file_catalog) | ||
# db.set_profiling_level(pymongo.OFF) | ||
# See: https://pymongo.readthedocs.io/en/stable/migrate-to-pymongo4.html#database-set-profiling-level-is-removed | ||
db.command('profile', ALL, filter={'op': 'query'}) | ||
print('MongoDB profiling enabled') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# fmt:off | ||
|
||
import os | ||
from typing import Any, cast, Dict | ||
|
||
from pymongo import MongoClient # type: ignore[import] | ||
from pymongo.database import Database # type: ignore[import] | ||
|
||
# PyMongo profiling level constants from PyMongo 3 (removed in PyMongo 4) | ||
# See: https://api.mongodb.com/python/3.0.3/api/pymongo/database.html#pymongo.ALL | ||
# See: https://www.mongodb.com/docs/manual/reference/command/profile/#mongodb-dbcommand-dbcmd.profile | ||
OFF = 0 | ||
SLOW_ONLY = 1 | ||
ALL = 2 | ||
|
||
FCDoc = Dict[str, Any] | ||
|
||
env = { | ||
'TEST_DATABASE_HOST': 'localhost', | ||
'TEST_DATABASE_PORT': 27017, | ||
} | ||
for k in env: | ||
if k in os.environ: | ||
if isinstance(env[k], int): | ||
env[k] = int(os.environ[k]) | ||
elif isinstance(env[k], float): | ||
env[k] = float(os.environ[k]) | ||
else: | ||
env[k] = os.environ[k] | ||
|
||
test_database_host = str(env['TEST_DATABASE_HOST']) | ||
test_database_port = int(str(env['TEST_DATABASE_PORT'])) | ||
db: Database[FCDoc] = cast(Database[FCDoc], MongoClient(host=test_database_host, port=test_database_port).file_catalog) | ||
|
||
# level = db.profiling_level() | ||
# See: https://pymongo.readthedocs.io/en/stable/migrate-to-pymongo4.html#database-profiling-level-is-removed | ||
profile: Dict[str, Any] = db.command('profile', -1) | ||
level = profile['was'] | ||
if level != ALL: | ||
raise Exception('profiling disabled') | ||
# db.set_profiling_level(pymongo.OFF) | ||
# See: https://pymongo.readthedocs.io/en/stable/migrate-to-pymongo4.html#database-set-profiling-level-is-removed | ||
db.command('profile', ALL, filter={'op': 'query'}) | ||
|
||
unrealistic_queries = [ | ||
{'locations.archive': True}, | ||
{'locations.archive': False}, | ||
{'locations.archive': None}, | ||
{'locations.archive': None, 'run.first_event': {'$lte': 400}, 'run.last_event': {'$gte': 400}}, | ||
# the query to get the queries that we're profiling doesn't count! | ||
{'op': {'$nin': ['command', 'insert']}}, | ||
] | ||
|
||
bad_queries = [] | ||
ret = db.system.profile.find({'op': {'$nin': ['command', 'insert']}}) | ||
for query in ret: | ||
try: | ||
# exclude unrealistic test queries | ||
if 'filter' in query['command'] and query['command']['filter'] in unrealistic_queries: | ||
continue | ||
if 'planSummary' not in query: | ||
print(query) | ||
continue | ||
if 'IXSCAN' not in query['planSummary']: | ||
bad_queries.append((query['command'], query['planSummary'])) | ||
except Exception: | ||
print(query) | ||
raise | ||
|
||
if bad_queries: | ||
for q, p in bad_queries: | ||
print(q) | ||
print(p) | ||
print('---') | ||
raise Exception('Non-indexed queries') | ||
|
||
print('MongoDB profiling OK') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters