Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken posthog api #753

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: Fix broken posthog api
  • Loading branch information
KIRA009 committed Jun 14, 2024
commit 21bcd5404f6d64b2877583c9e7824b5d5fa0e38a
12 changes: 9 additions & 3 deletions openadapt/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ def get_events(
list: A list of action events.
"""
posthog = utils.get_posthog_instance()
posthog.capture("get_events.started", {"recording_id": recording.id})
posthog.capture(
event="get_events.started", properties={"recording_id": recording.id}
)
start_time = time.time()
action_events = crud.get_action_events(db, recording)
window_events = crud.get_window_events(db, recording)
Expand All @@ -48,7 +50,9 @@ def get_events(
if recording.original_recording_id:
# if recording is a copy, it already has its events processed when it
# was created, return only the top level events
posthog.capture("get_events.completed", {"recording_id": recording.id})
posthog.capture(
event="get_events.completed", properties={"recording_id": recording.id}
)
return [event for event in action_events if event.parent_id is None]

raw_action_event_dicts = utils.rows2dicts(action_events)
Expand Down Expand Up @@ -121,7 +125,9 @@ def get_events(
end_time = time.time()
duration = end_time - start_time
logger.info(f"{duration=}")
posthog.capture("get_events.completed", {"recording_id": recording.id})
posthog.capture(
event="get_events.completed", properties={"recording_id": recording.id}
)

return action_events # , window_events, screenshots

Expand Down
7 changes: 5 additions & 2 deletions openadapt/replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def replay(
bool: True if replay was successful, None otherwise.
"""
utils.configure_logging(logger, LOG_LEVEL)
posthog.capture("replay.started", {"strategy_name": strategy_name})
posthog.capture(event="replay.started", properties={"strategy_name": strategy_name})

if status_pipe:
# TODO: move to Strategy?
Expand Down Expand Up @@ -102,7 +102,10 @@ def replay(

if status_pipe:
status_pipe.send({"type": "replay.stopped"})
posthog.capture("replay.stopped", {"strategy_name": strategy_name, "success": rval})
posthog.capture(
event="replay.stopped",
properties={"strategy_name": strategy_name, "success": rval},
)

if record:
sleep(1)
Expand Down
6 changes: 4 additions & 2 deletions openadapt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,13 +885,15 @@ def split_by_separators(text: str, seps: list[str]) -> list[str]:
class DistinctIDPosthog(Posthog):
"""Posthog client with a distinct ID injected into all events."""

def capture(self, **kwargs: Any) -> None:
def capture(self, *args: Any, **kwargs: Any) -> None:
"""Capture an event with the distinct ID.

Args:
*args: The event name.
**kwargs: The event properties.
"""
super().capture(distinct_id=config.UNIQUE_USER_ID, **kwargs)
kwargs.setdefault("distinct_id", config.UNIQUE_USER_ID)
super().capture(*args, **kwargs)


def get_posthog_instance() -> DistinctIDPosthog:
Expand Down
13 changes: 11 additions & 2 deletions openadapt/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,21 @@ def main(

assert not all([recording, recording_id]), "Only one may be specified."

posthog.capture("visualize.started", {"recording_id": recording_id})
posthog.capture(
event="visualize.started", properties={"recording_id": recording_id}
)

session = crud.get_new_session(read_only=True)

if recording_id:
recording = crud.get_recording_by_id(session, recording_id)
elif recording is None:
recording = crud.get_latest_recording(session)

if recording is None:
logger.error("No recording found")
return False

if SCRUB:
from openadapt.privacy.providers.presidio import PresidioScrubbingProvider

Expand Down Expand Up @@ -399,7 +406,9 @@ def _cleanup() -> None:

if cleanup:
Timer(1, _cleanup).start()
posthog.capture("visualize.completed", {"recording_id": recording.id})
posthog.capture(
event="visualize.completed", properties={"recording_id": recording.id}
)
return True


Expand Down
15 changes: 15 additions & 0 deletions tests/openadapt/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""Test openadapt.utils."""

from unittest.mock import patch

from openadapt import utils
from openadapt.config import config


def test_get_scale_ratios() -> None:
Expand All @@ -14,3 +17,15 @@ def test_get_scale_ratios() -> None:
assert isinstance(
height, (int, float)
), f"Expected height to be int or float, got {type(height).__name__}"


def test_posthog_capture() -> None:
"""Tests utils.get_posthog_instance."""
with patch("posthog.Posthog.capture") as mock_capture:
posthog = utils.get_posthog_instance()
posthog.capture(event="test_event", properties={"test_prop": "test_val"})
mock_capture.assert_called_once_with(
event="test_event",
properties={"test_prop": "test_val"},
distinct_id=config.UNIQUE_USER_ID,
)
Loading