From ad8b4dca6c09652362d9928469b0cbd07fc817a0 Mon Sep 17 00:00:00 2001 From: Matthew Elwell Date: Tue, 12 Jul 2022 16:59:01 +0100 Subject: [PATCH] Use feature name instead of feature id --- flagsmith/analytics.py | 4 ++-- flagsmith/models.py | 4 ++-- tests/test_analytics.py | 18 +++++++++--------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/flagsmith/analytics.py b/flagsmith/analytics.py index 8055631..97b596e 100644 --- a/flagsmith/analytics.py +++ b/flagsmith/analytics.py @@ -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() diff --git a/flagsmith/models.py b/flagsmith/models.py index 6eb2988..4284774 100644 --- a/flagsmith/models.py +++ b/flagsmith/models.py @@ -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 diff --git a/tests/test_analytics.py b/tests/test_analytics.py index 2b3cc68..60126b4 100644 --- a/tests/test_analytics.py +++ b/tests/test_analytics.py @@ -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 == {} @@ -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( @@ -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()