Skip to content

Instantly share code, notes, and snippets.

@konstruktoid
Last active April 23, 2021 03:54
Show Gist options
  • Save konstruktoid/bcb9daefab6beca67de833b5f547be91 to your computer and use it in GitHub Desktop.
Save konstruktoid/bcb9daefab6beca67de833b5f547be91 to your computer and use it in GitHub Desktop.
replacing eval getReplicationInfo with python functions -- https://jira.mongodb.org/browse/PYTHON-1717
def oplogstats(connection):
c = connection
localdb = c["local"]
if "oplog.rs" in localdb.collection_names():
collstats = dict()
collstats.clear()
collstats = localdb.command("collstats", "oplog.rs")
return collstats
else:
return None
def logsizemb(connection):
c = connection
localdb = c["local"]
if "oplog.rs" in localdb.collection_names():
collstats = dict()
collstats.clear()
collstats = localdb.command("collstats", "oplog.rs")
logsizemb = round((collstats["maxSize"] / (1024 * 1024)), 2)
return logsizemb
else:
return None
def usedmb(connection):
c = connection
localdb = c["local"]
if "oplog.rs" in localdb.collection_names():
collstats = dict()
collstats.clear()
collstats = localdb.command("collstats", "oplog.rs")
usedmb = round(((collstats["size"] / (1024 * 1024)) * 100) / 100, 2)
return usedmb
else:
return None
def tfirst(connection):
c = connection
localdb = c["local"]
if "oplog.rs" in localdb.collection_names():
oplogcol = localdb["oplog.rs"]
firstdoc = oplogcol.find_one(sort=[("$natural", 1)])
return firstdoc["ts"].as_datetime()
else:
return None
def tlast(connection):
c = connection
localdb = c["local"]
if "oplog.rs" in localdb.collection_names():
oplogcol = localdb["oplog.rs"]
lastdoc = oplogcol.find_one(sort=[("$natural", -1)])
return lastdoc["ts"].as_datetime()
else:
return None
def timediff(connection):
oplogcol = connection.local.oplog.rs
tsfirst = oplogcol.find_one(sort=[("$natural", 1)])["ts"]
tslast = oplogcol.find_one(sort=[("$natural", -1)])["ts"]
timediff = tslast.time - tsfirst.time
if isinstance(timediff, int):
return timediff
else:
return None
def timediffhours(connection):
timediffhours = round(((timediff(connection) / 36) / 100), 2)
if isinstance(timediffhours, float):
return timediffhours
else:
return None
@konstruktoid
Copy link
Author

Thanks for the code improvements and answering the question about performance @ShaneHarvey!
I've updated the gist.

Test code:

#!/usr/bin/python3
from pymongo import MongoClient
import getReplicationInfo

connection = MongoClient("mongodb://localhost:27017/")

print("oplogstats: " + str(getReplicationInfo.oplogstats(connection)))
print("logsizemb: " + str(getReplicationInfo.logsizemb(connection)))
print("usedmb: " + str(getReplicationInfo.usedmb(connection)))
print("tfirst: " + str(getReplicationInfo.tfirst(connection)))
print("tlast: " + str(getReplicationInfo.tlast(connection)))
print("timediff: " + str(getReplicationInfo.timediff(connection)))
print("timediffhours: " + str(getReplicationInfo.timediffhours(connection)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment