Skip to content

Commit 226bb62

Browse files
committed
Merge branch 'feat/support-evaluation-tracking-api' of github.com:Flagsmith/flagsmith-python-client into feat/support-evaluation-tracking-api
2 parents 9add012 + cd5ab91 commit 226bb62

File tree

6 files changed

+38
-22
lines changed

6 files changed

+38
-22
lines changed

flagsmith/analytics.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,9 @@ def _handle_flush_result(
193193
response = future.result()
194194
response.raise_for_status()
195195
except Exception:
196-
logger.warning("Failed to flush pipeline analytics, re-queuing events", exc_info=True)
196+
logger.warning(
197+
"Failed to flush pipeline analytics, re-queuing events", exc_info=True
198+
)
197199
with self._lock:
198200
self._buffer = events + self._buffer
199201
self._buffer = self._buffer[: self._max_buffer]
@@ -207,9 +209,7 @@ def stop(self) -> None:
207209
self.flush()
208210

209211
def _schedule_flush(self) -> None:
210-
self._timer = threading.Timer(
211-
self._flush_interval_seconds, self._timer_flush
212-
)
212+
self._timer = threading.Timer(self._flush_interval_seconds, self._timer_flush)
213213
self._timer.daemon = True
214214
self._timer.start()
215215

flagsmith/flagsmith.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ def __init__(
114114
self.default_flag_handler = default_flag_handler
115115
self.enable_realtime_updates = enable_realtime_updates
116116
self._analytics_processor: typing.Optional[AnalyticsProcessor] = None
117-
self._pipeline_analytics_processor: typing.Optional[PipelineAnalyticsProcessor] = None
117+
self._pipeline_analytics_processor: typing.Optional[
118+
PipelineAnalyticsProcessor
119+
] = None
118120
self._evaluation_context: typing.Optional[SDKEvaluationContext] = None
119121
self._environment_updated_at: typing.Optional[datetime] = None
120122

@@ -496,5 +498,8 @@ def __del__(self) -> None:
496498
if hasattr(self, "event_stream_thread"):
497499
self.event_stream_thread.stop()
498500

499-
if hasattr(self, "_pipeline_analytics_processor") and self._pipeline_analytics_processor:
501+
if (
502+
hasattr(self, "_pipeline_analytics_processor")
503+
and self._pipeline_analytics_processor
504+
):
500505
self._pipeline_analytics_processor.stop()

flagsmith/models.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ def from_evaluation_result(
6767
evaluation_result: SDKEvaluationResult,
6868
analytics_processor: typing.Optional[AnalyticsProcessor],
6969
default_flag_handler: typing.Optional[typing.Callable[[str], DefaultFlag]],
70-
pipeline_analytics_processor: typing.Optional[PipelineAnalyticsProcessor] = None,
70+
pipeline_analytics_processor: typing.Optional[
71+
PipelineAnalyticsProcessor
72+
] = None,
7173
identity_identifier: typing.Optional[str] = None,
7274
traits: typing.Optional[typing.Dict[str, typing.Any]] = None,
7375
) -> Flags:
@@ -90,7 +92,9 @@ def from_api_flags(
9092
api_flags: typing.Sequence[typing.Mapping[str, typing.Any]],
9193
analytics_processor: typing.Optional[AnalyticsProcessor],
9294
default_flag_handler: typing.Optional[typing.Callable[[str], DefaultFlag]],
93-
pipeline_analytics_processor: typing.Optional[PipelineAnalyticsProcessor] = None,
95+
pipeline_analytics_processor: typing.Optional[
96+
PipelineAnalyticsProcessor
97+
] = None,
9498
identity_identifier: typing.Optional[str] = None,
9599
traits: typing.Optional[typing.Dict[str, typing.Any]] = None,
96100
) -> Flags:

tests/test_flagsmith.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -985,9 +985,7 @@ def test_get_identity_flags_passes_identity_and_traits(
985985
)
986986

987987
responses.add(method="POST", url=flagsmith.identities_url, body=identities_json)
988-
flags = flagsmith.get_identity_flags(
989-
"user123", traits={"plan": "premium"}
990-
)
988+
flags = flagsmith.get_identity_flags("user123", traits={"plan": "premium"})
991989
flags.get_flag("some_feature")
992990

993991
mock_record.assert_called_once_with(

tests/test_models.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,19 @@ def test_get_flag_records_pipeline_evaluation_event(
157157
pipeline_analytics_processor: PipelineAnalyticsProcessor,
158158
) -> None:
159159
flags = Flags(
160-
flags={"my_feature": Flag(enabled=True, value="v1", feature_name="my_feature", feature_id=1)},
160+
flags={
161+
"my_feature": Flag(
162+
enabled=True, value="v1", feature_name="my_feature", feature_id=1
163+
)
164+
},
161165
_pipeline_analytics_processor=pipeline_analytics_processor,
162166
_identity_identifier="user123",
163167
_traits={"plan": "premium"},
164168
)
165169

166-
with mock.patch.object(pipeline_analytics_processor, "record_evaluation_event") as mock_record:
170+
with mock.patch.object(
171+
pipeline_analytics_processor, "record_evaluation_event"
172+
) as mock_record:
167173
flags.get_flag("my_feature")
168174

169175
mock_record.assert_called_once_with(
@@ -177,7 +183,11 @@ def test_get_flag_records_pipeline_evaluation_event(
177183

178184
def test_get_flag_without_pipeline_processor() -> None:
179185
flags = Flags(
180-
flags={"my_feature": Flag(enabled=True, value="v1", feature_name="my_feature", feature_id=1)},
186+
flags={
187+
"my_feature": Flag(
188+
enabled=True, value="v1", feature_name="my_feature", feature_id=1
189+
)
190+
},
181191
)
182192
flag = flags.get_flag("my_feature")
183193
assert flag.enabled is True

tests/test_pipeline_analytics.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_record_evaluation_event_buffers_event(
3636
@pytest.mark.parametrize(
3737
"second_enabled, expected_count",
3838
[
39-
(True, 1), # same fingerprint -> deduplicated
39+
(True, 1), # same fingerprint -> deduplicated
4040
(False, 2), # different fingerprint -> both kept
4141
],
4242
)
@@ -49,7 +49,10 @@ def test_evaluation_event_deduplication(
4949
flag_key="my_flag", enabled=True, value="v1", identity_identifier="user1"
5050
)
5151
pipeline_analytics_processor.record_evaluation_event(
52-
flag_key="my_flag", enabled=second_enabled, value="v1", identity_identifier="user1"
52+
flag_key="my_flag",
53+
enabled=second_enabled,
54+
value="v1",
55+
identity_identifier="user1",
5356
)
5457

5558
assert len(pipeline_analytics_processor._buffer) == expected_count
@@ -72,9 +75,7 @@ def test_dedup_keys_cleared_after_flush(
7275

7376

7477
def test_auto_flush_on_buffer_full() -> None:
75-
config = PipelineAnalyticsConfig(
76-
analytics_server_url="http://test/", max_buffer=5
77-
)
78+
config = PipelineAnalyticsConfig(analytics_server_url="http://test/", max_buffer=5)
7879
processor = PipelineAnalyticsProcessor(config=config, environment_key="key")
7980

8081
with mock.patch("flagsmith.analytics.session"):
@@ -186,9 +187,7 @@ def test_start_stop_lifecycle() -> None:
186187
assert processor._timer.is_alive()
187188

188189
with mock.patch("flagsmith.analytics.session"):
189-
processor.record_evaluation_event(
190-
flag_key="my_flag", enabled=True, value="v1"
191-
)
190+
processor.record_evaluation_event(flag_key="my_flag", enabled=True, value="v1")
192191
processor.stop()
193192

194193
assert len(processor._buffer) == 0

0 commit comments

Comments
 (0)