2014-01-07 22:20:29 +01:00
|
|
|
import time
|
2023-04-06 20:58:14 +02:00
|
|
|
from typing import (
|
|
|
|
Any,
|
|
|
|
Callable,
|
|
|
|
Dict,
|
|
|
|
Iterable,
|
|
|
|
List,
|
|
|
|
Mapping,
|
|
|
|
Optional,
|
|
|
|
Sequence,
|
|
|
|
TypeVar,
|
|
|
|
Union,
|
|
|
|
overload,
|
|
|
|
)
|
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
|
2016-06-05 04:20:00 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
CursorObj = TypeVar("CursorObj", bound=cursor)
|
2022-03-25 01:48:52 +01:00
|
|
|
Query = Union[str, bytes, Composable]
|
|
|
|
Params = Union[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
|
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."""
|
|
|
|
|
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
|
|
|
|
2022-03-25 01:48:52 +01:00
|
|
|
def executemany(self, query: Query, vars: Iterable[Params]) -> None: # nocoverage
|
|
|
|
wrapper_execute(self, super().executemany, query, vars)
|
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:
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
self.queries: List[Dict[str, str]] = []
|
2017-10-27 08:28:23 +02:00
|
|
|
super().__init__(*args, **kwargs)
|
2014-01-07 22:20:29 +01:00
|
|
|
|
2023-04-06 20:58:14 +02:00
|
|
|
@overload
|
|
|
|
def cursor(
|
|
|
|
self,
|
|
|
|
name: Union[str, bytes, None] = ...,
|
|
|
|
*,
|
|
|
|
withhold: bool = ...,
|
|
|
|
scrollable: Optional[bool] = ...,
|
|
|
|
) -> TimeTrackingCursor:
|
|
|
|
...
|
|
|
|
|
|
|
|
@overload
|
|
|
|
def cursor(
|
|
|
|
self,
|
|
|
|
name: Union[str, bytes, None] = ...,
|
|
|
|
*,
|
|
|
|
cursor_factory: Callable[..., CursorT] = ...,
|
|
|
|
withhold: bool = ...,
|
|
|
|
scrollable: Optional[bool] = ...,
|
|
|
|
) -> CursorT:
|
|
|
|
...
|
|
|
|
|
|
|
|
@overload
|
|
|
|
def cursor(
|
|
|
|
self,
|
|
|
|
name: Union[str, bytes, None],
|
|
|
|
cursor_factory: Callable[..., CursorT] = ...,
|
|
|
|
withhold: bool = ...,
|
|
|
|
scrollable: Optional[bool] = ...,
|
|
|
|
) -> CursorT:
|
|
|
|
...
|
|
|
|
|
|
|
|
def cursor(self, *args: Any, **kwargs: Any) -> cursor:
|
|
|
|
kwargs["cursor_factory"] = TimeTrackingCursor
|
|
|
|
return super().cursor(*args, **kwargs)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def reset_queries() -> None:
|
2014-01-07 22:20:29 +01:00
|
|
|
from django.db import connections
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2014-01-07 22:20:29 +01:00
|
|
|
for conn in connections.all():
|
2015-11-24 07:01:35 +01:00
|
|
|
if conn.connection is not None:
|
|
|
|
conn.connection.queries = []
|