Skip to content

Commit

Permalink
Use feature name instead of feature id (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewelwell authored Jul 13, 2022
1 parent 09a74f0 commit 1dffb65
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions flagsmith/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def flush(self):
self.analytics_data.clear()
self._last_flushed = datetime.now()

def track_feature(self, feature_id: int):
self.analytics_data[feature_id] = self.analytics_data.get(feature_id, 0) + 1
def track_feature(self, feature_name: str):
self.analytics_data[feature_name] = self.analytics_data.get(feature_name, 0) + 1
if (datetime.now() - self._last_flushed).seconds > ANALYTICS_TIMER:
self.flush()
4 changes: 2 additions & 2 deletions flagsmith/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ def get_flag(self, feature_name: str) -> BaseFlag:
return self.default_flag_handler(feature_name)
raise FlagsmithClientError("Feature does not exist: %s" % feature_name)

if self._analytics_processor and hasattr(flag, "feature_id"):
self._analytics_processor.track_feature(flag.feature_id)
if self._analytics_processor and hasattr(flag, "feature_name"):
self._analytics_processor.track_feature(flag.feature_name)

return flag

Expand Down
18 changes: 9 additions & 9 deletions tests/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

def test_analytics_processor_track_feature_updates_analytics_data(analytics_processor):
# When
analytics_processor.track_feature(1)
assert analytics_processor.analytics_data[1] == 1
analytics_processor.track_feature("my_feature")
assert analytics_processor.analytics_data["my_feature"] == 1

analytics_processor.track_feature(1)
assert analytics_processor.analytics_data[1] == 2
analytics_processor.track_feature("my_feature")
assert analytics_processor.analytics_data["my_feature"] == 2


def test_analytics_processor_flush_clears_analytics_data(analytics_processor):
analytics_processor.track_feature(1)
analytics_processor.track_feature("my_feature")
analytics_processor.flush()
assert analytics_processor.analytics_data == {}

Expand All @@ -26,13 +26,13 @@ def test_analytics_processor_flush_post_request_data_match_ananlytics_data(
# Given
with mock.patch("flagsmith.analytics.session") as session:
# When
analytics_processor.track_feature(1)
analytics_processor.track_feature(2)
analytics_processor.track_feature("my_feature_1")
analytics_processor.track_feature("my_feature_2")
analytics_processor.flush()
# Then
session.post.assert_called()
post_call = session.mock_calls[0]
assert {"1": 1, "2": 1} == json.loads(post_call[2]["data"])
assert {"my_feature_1": 1, "my_feature_2": 1} == json.loads(post_call[2]["data"])


def test_analytics_processor_flush_early_exit_if_analytics_data_is_empty(
Expand All @@ -57,7 +57,7 @@ def test_analytics_processor_calling_track_feature_calls_flush_when_timer_runs_o
seconds=ANALYTICS_TIMER + 1
)
# When
analytics_processor.track_feature(1)
analytics_processor.track_feature("my_feature")

# Then
session.post.assert_called()

0 comments on commit 1dffb65

Please sign in to comment.