forked from LucasHild/flask-matomo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7829a9
commit ff7b993
Showing
4 changed files
with
54 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
"""Track requests to your Flask server with Matomo""" | ||
from flask_matomo2 import trackers | ||
from flask_matomo2.core import Matomo | ||
|
||
__all__ = ["Matomo"] | ||
__all__ = ["trackers", "Matomo"] |
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,35 @@ | ||
import time | ||
import typing | ||
|
||
|
||
class PerfMsTracker: | ||
"""Measure time between enter and exit and records it in state.""" | ||
|
||
def __init__(self, scope: typing.MutableMapping[str, typing.Any], key: str) -> None: | ||
print(f"{scope=}") | ||
self.start_ns = 0.0 | ||
# if "state" not in scope: | ||
# scope["state"] = {} | ||
# if "flask_matomo2" not in scope: | ||
# scope["flask_matomo2"] = {} | ||
self.scope = scope | ||
self.key = key | ||
print(f"{self.scope=}") | ||
|
||
def __enter__(self): | ||
self.start_ns = time.perf_counter_ns() | ||
|
||
def __exit__(self, exc_type, exc_value, exc_tb): | ||
self._record_time(self.key, time.perf_counter_ns()) | ||
|
||
async def __aenter__(self): | ||
self.start_ns = time.perf_counter_ns() | ||
|
||
async def __aexit__(self, exc_type, exc_value, exc_tb): | ||
self._record_time(self.key, time.perf_counter_ns()) | ||
|
||
def _record_time(self, key: str, end_ns: float) -> None: | ||
print(f"{self.scope=}") | ||
|
||
elapsed_time_ms = (end_ns - self.start_ns) / 1000.0 | ||
self.scope["tracking_data"][key] = elapsed_time_ms |
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