feat(Segments): Write rules as plain JSON - #8245
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. 3 Skipped Deployments
|
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
📝 WalkthroughWalkthroughThe change adds typed, persisted Estimated code review effort: 4 (Complex) | ~60 minutes ✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
e997681 to
d1b4523
Compare
Docker builds report
|
There was a problem hiding this comment.
Actionable comments posted: 5
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 405482df-5f69-4b7d-b284-074ab96f0eb7
📒 Files selected for processing (15)
api/segments/migrations/0031_add_segment_rules_data.pyapi/segments/models.pyapi/segments/serializers.pyapi/segments/services.pyapi/segments/types.pyapi/segments/validators.pyapi/tests/conftest.pyapi/tests/types.pyapi/tests/unit/segments/conftest.pyapi/tests/unit/segments/test_unit_segments_models.pyapi/tests/unit/segments/test_unit_segments_services.pyapi/tests/unit/segments/test_unit_segments_views.pydocs/docs/deployment-self-hosting/observability/_events-catalogue.mdmcp/src/flagsmith_mcp/openapi.jsonopenapi.yaml
✅ private-cloud · depot-ubuntu-latest-16 — run #19219 (attempt 1)Playwright Test Results (private-cloud - depot-ubuntu-latest-16)Details
🗂️ Previous results✅ private-cloud · depot-ubuntu-latest-arm-16 — run #19219 (attempt 1)Playwright Test Results (private-cloud - depot-ubuntu-latest-arm-16)Details
✅ oss · depot-ubuntu-latest-arm-16 — run #19219 (attempt 1)Playwright Test Results (oss - depot-ubuntu-latest-arm-16)Details
✅ oss · depot-ubuntu-latest-arm-16 — run #19220 (attempt 1)Playwright Test Results (oss - depot-ubuntu-latest-arm-16)Details
✅ oss · depot-ubuntu-latest-16 — run #19219 (attempt 1)Playwright Test Results (oss - depot-ubuntu-latest-16)Details
✅ oss · depot-ubuntu-latest-16 — run #19220 (attempt 1)Playwright Test Results (oss - depot-ubuntu-latest-16)Details
✅ oss · depot-ubuntu-latest-arm-16 — run #19218 (attempt 1)Playwright Test Results (oss - depot-ubuntu-latest-arm-16)Details
✅ private-cloud · depot-ubuntu-latest-16 — run #19216 (attempt 1)Playwright Test Results (private-cloud - depot-ubuntu-latest-16)Details
✅ private-cloud · depot-ubuntu-latest-arm-16 — run #19216 (attempt 1)Playwright Test Results (private-cloud - depot-ubuntu-latest-arm-16)Details
✅ private-cloud · depot-ubuntu-latest-16 — run #19217 (attempt 1)Playwright Test Results (private-cloud - depot-ubuntu-latest-16)Details
✅ oss · depot-ubuntu-latest-16 — run #19218 (attempt 1)Playwright Test Results (oss - depot-ubuntu-latest-16)Details
✅ oss · depot-ubuntu-latest-arm-16 — run #19216 (attempt 1)Playwright Test Results (oss - depot-ubuntu-latest-arm-16)Details
|
Visual Regression19 screenshots compared. See report for details. |
There was a problem hiding this comment.
Actionable comments posted: 2
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 316a442d-abe2-48f0-8658-5ed94d52184c
📒 Files selected for processing (10)
api/integrations/launch_darkly/services.pyapi/segments/migrations/0031_add_segment_rules_data.pyapi/segments/models.pyapi/segments/serializers.pyapi/tests/unit/integrations/launch_darkly/snapshots/test_process_import_request__large_segments__correctly_imported__rules_data.jsonapi/tests/unit/integrations/launch_darkly/test_services.pyapi/tests/unit/segments/conftest.pyapi/tests/unit/segments/test_unit_segments_migrations.pyapi/tests/unit/segments/test_unit_segments_views.pydocs/docs/deployment-self-hosting/observability/_events-catalogue.md
ef573e1 to
ef93dd7
Compare
ef93dd7 to
04b05a6
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 8f8878c4-fd1a-4a42-af9c-99b117204a52
📒 Files selected for processing (7)
api/integrations/launch_darkly/services.pyapi/segments/migrations/0031_add_segment_rules_data.pyapi/segments/serializers.pyapi/segments/types.pyapi/tests/conftest.pyapi/tests/unit/segments/test_unit_segments_migrations.pydocs/docs/deployment-self-hosting/observability/_events-catalogue.md
💤 Files with no reviewable changes (1)
- api/tests/conftest.py
| rules = segment.rules_data or [ # LaunchDarkly environments share the segment | ||
| {"type": constants.ALL_RULE, "conditions": [], "rules": []} | ||
| ] | ||
| rules[0]["rules"] += subrules | ||
| segment.rules_data = rules | ||
| segment.save(update_fields=["rules_data"]) |
There was a problem hiding this comment.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
rg -n -C 12 \
'process_import_request|_create_feature_segment_from_clauses|_create_segment_rule_for_segment|transaction\.atomic' \
api --glob '*.py'Repository: Flagsmith/flagsmith
Length of output: 50375
🏁 Script executed:
#!/bin/bash
set -euo pipefail
printf '%s\n' '--- transaction imports and uses ---'
rg -n '(^|[^[:alnum:]_])transaction|def _complete_import_request|with _complete_import_request|def _import_segments|_create_feature_segment_from_clauses' api/integrations/launch_darkly/services.py
printf '%s\n' '--- services.py imports and transaction context ---'
sed -n '1,90p' api/integrations/launch_darkly/services.py
sed -n '1180,1285p' api/integrations/launch_darkly/services.py
printf '%s\n' '--- segment import caller ---'
sed -n '1040,1165p' api/integrations/launch_darkly/services.pyRepository: Flagsmith/flagsmith
Length of output: 11928
🏁 Script executed:
#!/bin/bash
set -euo pipefail
printf '%s\n' '--- _complete_import_request implementation ---'
sed -n '75,145p' api/integrations/launch_darkly/services.py
printf '%s\n' '--- process_import_request body ---'
sed -n '1230,1415p' api/integrations/launch_darkly/services.py
printf '%s\n' '--- transaction.atomic references in the LaunchDarkly integration ---'
rg -n -C 3 'transaction\.atomic|atomic\(' api/integrations/launch_darkly --glob '*.py'Repository: Flagsmith/flagsmith
Length of output: 5918
Wrap the JSON and relational writes in one transaction.
_complete_import_request does not use transaction.atomic(). If _create_segment_rule_for_segment or a later Condition write fails, rules_data can persist without matching relational rows.
Source: Learnings
this ain't Backdoors
9eff8a3 to
2904c84
Compare
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
api/integrations/launch_darkly/services.py (1)
1195-1198: 🗄️ Data Integrity & Integration | 🔴 Critical | ⚡ Quick winPersist
root_ruleinstead of undefinedsubrules.Line 1196 references
subrules, but_create_segments_from_ldonly definesroot_rule. Every imported non-deleted LaunchDarkly segment reaches this statement and raisesNameError. The import can then leave legacy rows withoutrules_data.Proposed fix
- segment.rules_data = [ - {"type": constants.ALL_RULE, "conditions": [], "rules": subrules} - ] + segment.rules_data = [root_rule]
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: ea285ce1-bc5a-44bb-9fdd-b9ceeba0171a
📒 Files selected for processing (7)
api/experimentation/services.pyapi/integrations/launch_darkly/services.pyapi/segments/types.pyapi/tests/unit/experimentation/test_services.pyapi/tests/unit/integrations/launch_darkly/test_services.pyapi/tests/unit/segments/test_unit_segments_views.pydocs/docs/deployment-self-hosting/observability/_events-catalogue.md
|
|
||
|
|
||
| def test_update_segment__rules_and_conditions_with_ids__ignores_ids( | ||
| admin_client: APIClient, |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win
Use admin_client_original for this single-client test.
admin_client is deprecated and will change its execution model. This test needs one client only. Replace it with admin_client_original.
I have added information todocs/if required so people know about the feature.Changes
Closes #7815
This is the first step towards getting rid of
SegmentRuleandConditionmodels, and the tree complexity they have brought upon us thus far.We achieve:
Segmentmodel earns a newrules_dataattributerules_datarules_data.rules_data.Unplanned things we also achieve:
Chose not to achieve:
rulesshould need no special treatment as a JSON field, properly validated by the interface.Deleted LoC will look great in #7818!
How did you test this code?
Exhaustive unit testing. The migration was also benchmarked and optimised.
Important
This includes a data migration to backfill active segments with JSON rules. The migration code was exhaustively tested and tweaked. On a very limited Postgres local container (1 CPU, 2 GB memory), it took ~16s to rasterise rules of 100K segments with varying depth of rules.