2014-01-07 22:20:29 +01:00
|
|
|
import time
|
2024-07-12 02:30:25 +02:00
|
|
|
from collections.abc import Callable, Iterable, Mapping, Sequence
|
|
|
|
from typing import Any, TypeAlias, TypeVar
|
2014-01-07 22:20:29 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
from psycopg2.extensions import connection, cursor
|
|
|
|
from psycopg2.sql import Composable
|
2024-07-12 02:30:25 +02:00
|
|
|
from typing_extensions import override
|
2016-06-05 04:20:00 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
CursorObj = TypeVar("CursorObj", bound=cursor)
|
2024-07-12 02:30:23 +02:00
|
|
|
Query: TypeAlias = str | bytes | Composable
|
|
|
|
Params: TypeAlias = Sequence[object] | Mapping[str, object] | None
|
2021-02-12 08:20:45 +01:00
|
|
|
ParamsT = TypeVar("ParamsT")
|
2016-06-05 04:20:00 +02:00
|
|
|
|
2023-02-02 04:35:24 +01:00
|
|
|
|
2014-01-07 22:20:29 +01:00
|
|
|
# Similar to the tracking done in Django's CursorDebugWrapper, but done at the
|
|
|
|
# psycopg2 cursor level so it works with SQLAlchemy.
|
2021-02-12 08:19:30 +01:00
|
|
|
def wrapper_execute(
|
2022-03-25 01:48:52 +01:00
|
|
|
self: CursorObj, action: Callable[[Query, ParamsT], None], sql: Query, params: ParamsT
|
|
|
|
) -> None:
|
2014-01-07 22:20:29 +01:00
|
|
|
start = time.time()
|
|
|
|
try:
|
2022-03-25 01:48:52 +01:00
|
|
|
action(sql, params)
|
2014-01-07 22:20:29 +01:00
|
|
|
finally:
|
|
|
|
stop = time.time()
|
|
|
|
duration = stop - start
|
2023-11-14 01:01:40 +01:00
|
|
|
assert isinstance(self.connection, TimeTrackingConnection)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.connection.queries.append(
|
|
|
|
{
|
2021-02-12 08:20:45 +01:00
|
|
|
"time": f"{duration:.3f}",
|
2021-02-12 08:19:30 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2014-01-07 22:20:29 +01:00
|
|
|
|
|
|
|
class TimeTrackingCursor(cursor):
|
|
|
|
"""A psycopg2 cursor class that tracks the time spent executing queries."""
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2022-03-25 01:48:52 +01:00
|
|
|
def execute(self, query: Query, vars: Params = None) -> None:
|
|
|
|
wrapper_execute(self, super().execute, query, vars)
|
2014-01-07 22:20:29 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2023-11-15 22:25:00 +01:00
|
|
|
def executemany(self, query: Query, vars_list: Iterable[Params]) -> None: # nocoverage
|
|
|
|
wrapper_execute(self, super().executemany, query, vars_list)
|
2014-01-07 22:20:29 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-12-23 06:51:09 +01:00
|
|
|
CursorT = TypeVar("CursorT", bound=cursor)
|
|
|
|
|
|
|
|
|
2014-01-07 22:20:29 +01:00
|
|
|
class TimeTrackingConnection(connection):
|
|
|
|
"""A psycopg2 connection class that uses TimeTrackingCursors."""
|
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
2024-07-12 02:30:17 +02:00
|
|
|
self.queries: list[dict[str, str]] = []
|
2017-10-27 08:28:23 +02:00
|
|
|
super().__init__(*args, **kwargs)
|