From aa0e1d48799434f4f1aded9e7c52a17ab4133e7b Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Wed, 18 Dec 2024 09:05:26 -0600 Subject: [PATCH 1/5] INTPYTHON-456 Fix handling of server time --- .../langchain_mongodb/indexes.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/libs/langchain-mongodb/langchain_mongodb/indexes.py b/libs/langchain-mongodb/langchain_mongodb/indexes.py index 3673649..2302a8b 100644 --- a/libs/langchain-mongodb/langchain_mongodb/indexes.py +++ b/libs/langchain-mongodb/langchain_mongodb/indexes.py @@ -2,12 +2,14 @@ from __future__ import annotations import functools +import warnings from typing import Any, Dict, List, Optional, Sequence from langchain_core.indexing.base import RecordManager from langchain_core.runnables.config import run_in_executor from pymongo import MongoClient from pymongo.collection import Collection +from pymongo.errors import OperationFailure class MongoDBRecordManager(RecordManager): @@ -87,9 +89,16 @@ async def aupdate( def get_time(self) -> float: """Get the current server time as a timestamp.""" - server_info = self._collection.database.command("hostInfo") - local_time = server_info["system"]["currentTime"] - timestamp = local_time.timestamp() + try: + server_info = self._collection.database.command("hostInfo") + local_time = server_info["system"]["currentTime"] + timestamp = local_time.timestamp() + except OperationFailure: + with warnings.filterwarnings("once"): + warnings.warn("Could not get high-resolution timestamp, falling back to low-resolution", stacklevel=2) + ping = self._collection.database['ping'] + local_time = ping['operationTime'] + timestamp = float(local_time.time) return timestamp async def aget_time(self) -> float: From cdf4b4312b57178cfdfbd095649091ba86788d69 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Wed, 18 Dec 2024 09:32:09 -0600 Subject: [PATCH 2/5] fix filter --- libs/langchain-mongodb/langchain_mongodb/indexes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/langchain-mongodb/langchain_mongodb/indexes.py b/libs/langchain-mongodb/langchain_mongodb/indexes.py index 2302a8b..2886a9d 100644 --- a/libs/langchain-mongodb/langchain_mongodb/indexes.py +++ b/libs/langchain-mongodb/langchain_mongodb/indexes.py @@ -94,7 +94,7 @@ def get_time(self) -> float: local_time = server_info["system"]["currentTime"] timestamp = local_time.timestamp() except OperationFailure: - with warnings.filterwarnings("once"): + with warnings.catch_warnings(action="once"): warnings.warn("Could not get high-resolution timestamp, falling back to low-resolution", stacklevel=2) ping = self._collection.database['ping'] local_time = ping['operationTime'] From ccbed7f627f988de6176949477e79836e9e1d617 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Wed, 18 Dec 2024 09:44:47 -0600 Subject: [PATCH 3/5] fix filter --- libs/langchain-mongodb/langchain_mongodb/indexes.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libs/langchain-mongodb/langchain_mongodb/indexes.py b/libs/langchain-mongodb/langchain_mongodb/indexes.py index 2886a9d..cdf9cbe 100644 --- a/libs/langchain-mongodb/langchain_mongodb/indexes.py +++ b/libs/langchain-mongodb/langchain_mongodb/indexes.py @@ -94,7 +94,8 @@ def get_time(self) -> float: local_time = server_info["system"]["currentTime"] timestamp = local_time.timestamp() except OperationFailure: - with warnings.catch_warnings(action="once"): + with warnings.catch_warnings(): + warnings.simplefilter("once") warnings.warn("Could not get high-resolution timestamp, falling back to low-resolution", stacklevel=2) ping = self._collection.database['ping'] local_time = ping['operationTime'] From 68e841b60191df1ef022c70100f8ad93d0248414 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Wed, 18 Dec 2024 10:04:18 -0600 Subject: [PATCH 4/5] fix operation --- libs/langchain-mongodb/langchain_mongodb/indexes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/langchain-mongodb/langchain_mongodb/indexes.py b/libs/langchain-mongodb/langchain_mongodb/indexes.py index cdf9cbe..0fb7c02 100644 --- a/libs/langchain-mongodb/langchain_mongodb/indexes.py +++ b/libs/langchain-mongodb/langchain_mongodb/indexes.py @@ -97,7 +97,7 @@ def get_time(self) -> float: with warnings.catch_warnings(): warnings.simplefilter("once") warnings.warn("Could not get high-resolution timestamp, falling back to low-resolution", stacklevel=2) - ping = self._collection.database['ping'] + ping = self._collection.database.command('ping') local_time = ping['operationTime'] timestamp = float(local_time.time) return timestamp From b414d070992c1dc836e5de2e7ccd873d8d97078c Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Wed, 18 Dec 2024 12:48:24 -0600 Subject: [PATCH 5/5] lint --- libs/langchain-mongodb/langchain_mongodb/indexes.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libs/langchain-mongodb/langchain_mongodb/indexes.py b/libs/langchain-mongodb/langchain_mongodb/indexes.py index 0fb7c02..f502f8d 100644 --- a/libs/langchain-mongodb/langchain_mongodb/indexes.py +++ b/libs/langchain-mongodb/langchain_mongodb/indexes.py @@ -96,9 +96,12 @@ def get_time(self) -> float: except OperationFailure: with warnings.catch_warnings(): warnings.simplefilter("once") - warnings.warn("Could not get high-resolution timestamp, falling back to low-resolution", stacklevel=2) - ping = self._collection.database.command('ping') - local_time = ping['operationTime'] + warnings.warn( + "Could not get high-resolution timestamp, falling back to low-resolution", + stacklevel=2, + ) + ping = self._collection.database.command("ping") + local_time = ping["operationTime"] timestamp = float(local_time.time) return timestamp