Skip to content

Commit 16d510f

Browse files
KIRA009abrichr
andauthored
fix(posthog): fix broken posthog api (#753)
* fix: Fix broken posthog api * fix types --------- Co-authored-by: Richard Abrich <richard.abrich@gmail.com>
1 parent 6e93921 commit 16d510f

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

openadapt/events.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ def get_events(
3939
list: A list of action events.
4040
"""
4141
posthog = utils.get_posthog_instance()
42-
posthog.capture("get_events.started", {"recording_id": recording.id})
42+
posthog.capture(
43+
event="get_events.started", properties={"recording_id": recording.id}
44+
)
4345
start_time = time.time()
4446
action_events = crud.get_action_events(db, recording)
4547
window_events = crud.get_window_events(db, recording)
@@ -48,7 +50,9 @@ def get_events(
4850
if recording.original_recording_id:
4951
# if recording is a copy, it already has its events processed when it
5052
# was created, return only the top level events
51-
posthog.capture("get_events.completed", {"recording_id": recording.id})
53+
posthog.capture(
54+
event="get_events.completed", properties={"recording_id": recording.id}
55+
)
5256
return [event for event in action_events if event.parent_id is None]
5357

5458
raw_action_event_dicts = utils.rows2dicts(action_events)
@@ -121,7 +125,9 @@ def get_events(
121125
end_time = time.time()
122126
duration = end_time - start_time
123127
logger.info(f"{duration=}")
124-
posthog.capture("get_events.completed", {"recording_id": recording.id})
128+
posthog.capture(
129+
event="get_events.completed", properties={"recording_id": recording.id}
130+
)
125131

126132
return action_events # , window_events, screenshots
127133

openadapt/replay.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def replay(
4949
bool: True if replay was successful, None otherwise.
5050
"""
5151
utils.configure_logging(logger, LOG_LEVEL)
52-
posthog.capture("replay.started", {"strategy_name": strategy_name})
52+
posthog.capture(event="replay.started", properties={"strategy_name": strategy_name})
5353

5454
if status_pipe:
5555
# TODO: move to Strategy?
@@ -102,7 +102,10 @@ def replay(
102102

103103
if status_pipe:
104104
status_pipe.send({"type": "replay.stopped"})
105-
posthog.capture("replay.stopped", {"strategy_name": strategy_name, "success": rval})
105+
posthog.capture(
106+
event="replay.stopped",
107+
properties={"strategy_name": strategy_name, "success": rval},
108+
)
106109

107110
if record:
108111
sleep(1)

openadapt/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -885,13 +885,15 @@ def split_by_separators(text: str, seps: list[str]) -> list[str]:
885885
class DistinctIDPosthog(Posthog):
886886
"""Posthog client with a distinct ID injected into all events."""
887887

888-
def capture(self, **kwargs: Any) -> None:
888+
def capture(self, *args: tuple, **kwargs: dict) -> None:
889889
"""Capture an event with the distinct ID.
890890
891891
Args:
892+
*args: The event name.
892893
**kwargs: The event properties.
893894
"""
894-
super().capture(distinct_id=config.UNIQUE_USER_ID, **kwargs)
895+
kwargs.setdefault("distinct_id", config.UNIQUE_USER_ID)
896+
super().capture(*args, **kwargs)
895897

896898

897899
def get_posthog_instance() -> DistinctIDPosthog:

openadapt/visualize.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,21 @@ def main(
174174

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

177-
posthog.capture("visualize.started", {"recording_id": recording_id})
177+
posthog.capture(
178+
event="visualize.started", properties={"recording_id": recording_id}
179+
)
178180

179181
session = crud.get_new_session(read_only=True)
180182

181183
if recording_id:
182184
recording = crud.get_recording_by_id(session, recording_id)
183185
elif recording is None:
184186
recording = crud.get_latest_recording(session)
187+
188+
if recording is None:
189+
logger.error("No recording found")
190+
return False
191+
185192
if SCRUB:
186193
from openadapt.privacy.providers.presidio import PresidioScrubbingProvider
187194

@@ -399,7 +406,9 @@ def _cleanup() -> None:
399406

400407
if cleanup:
401408
Timer(1, _cleanup).start()
402-
posthog.capture("visualize.completed", {"recording_id": recording.id})
409+
posthog.capture(
410+
event="visualize.completed", properties={"recording_id": recording.id}
411+
)
403412
return True
404413

405414

tests/openadapt/test_utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"""Test openadapt.utils."""
22

3+
from unittest.mock import patch
4+
35
from openadapt import utils
6+
from openadapt.config import config
47

58

69
def test_get_scale_ratios() -> None:
@@ -14,3 +17,15 @@ def test_get_scale_ratios() -> None:
1417
assert isinstance(
1518
height, (int, float)
1619
), f"Expected height to be int or float, got {type(height).__name__}"
20+
21+
22+
def test_posthog_capture() -> None:
23+
"""Tests utils.get_posthog_instance."""
24+
with patch("posthog.Posthog.capture") as mock_capture:
25+
posthog = utils.get_posthog_instance()
26+
posthog.capture(event="test_event", properties={"test_prop": "test_val"})
27+
mock_capture.assert_called_once_with(
28+
event="test_event",
29+
properties={"test_prop": "test_val"},
30+
distinct_id=config.UNIQUE_USER_ID,
31+
)

0 commit comments

Comments
 (0)