-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathservices.py
More file actions
1181 lines (1030 loc) · 45 KB
/
services.py
File metadata and controls
1181 lines (1030 loc) · 45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import logging
import re
from contextlib import contextmanager
from typing import Any, Callable, Generator, Iterable, Optional, Tuple
from django.conf import settings
from django.core import signing
from django.utils import timezone
from flag_engine.segments import constants
from requests.exceptions import RequestException
from environments.identities.models import Identity
from environments.models import Environment
from features.feature_types import MULTIVARIATE, STANDARD, FeatureType
from features.models import (
Feature,
FeatureSegment,
FeatureState,
FeatureStateValue,
)
from features.multivariate.models import (
MultivariateFeatureOption,
MultivariateFeatureStateValue,
)
from features.value_types import STRING
from integrations.launch_darkly import types as ld_types
from integrations.launch_darkly.client import LaunchDarklyClient
from integrations.launch_darkly.constants import (
LAUNCH_DARKLY_IMPORTED_DEFAULT_TAG_LABEL,
LAUNCH_DARKLY_IMPORTED_TAG_COLOR,
)
from integrations.launch_darkly.exceptions import LaunchDarklyRateLimitError
from integrations.launch_darkly.models import (
LaunchDarklyImportRequest,
LaunchDarklyImportResult,
LaunchDarklyImportStatus,
)
from integrations.launch_darkly.types import Clause
from projects.models import Project
from projects.tags.models import Tag
from segments.models import Condition, Segment, SegmentRule
from users.models import FFAdminUser
from util.db import closing_stale_connections
from util.util import iter_chunked_concat, truncate
logger = logging.getLogger(__name__)
def _sign_ld_value(value: str, user_id: int) -> str:
return signing.dumps(value, salt=f"ld_import_{user_id}")
def _unsign_ld_value(value: str, user_id: int) -> str:
return signing.loads( # type: ignore[no-any-return]
value,
salt=f"ld_import_{user_id}",
)
def _serialize_variation_value(value: Any) -> str:
if isinstance(value, (dict, list)):
return json.dumps(value)
return str(value)
def _log_error(
import_request: LaunchDarklyImportRequest,
error_message: str,
) -> None:
import_request.status["error_messages"] += [error_message]
@contextmanager
def _complete_import_request(
import_request: LaunchDarklyImportRequest,
) -> Generator[None, None, None]:
"""
Wrap code so the import request always ends up completed.
If no exception raised, assume successful import.
In case wrapped code needs to expose an error to the user, it should populate
`import_request.status["error_messages"]` before raising an exception.
"""
result: LaunchDarklyImportResult = "incomplete"
try:
yield
except Exception as exc:
if not isinstance(exc, LaunchDarklyRateLimitError):
result = "failure"
raise exc
else:
if not import_request.status.get("error_messages"):
result = "success"
finally:
import_request.status["result"] = result
if result in {"success", "failure"}:
import_request.ld_token = ""
import_request.completed_at = timezone.now()
import_request.save()
def _create_environments_from_ld(
ld_environments: list[ld_types.Environment],
project_id: int,
) -> dict[str, Environment]:
environments_by_ld_environment_key: dict[str, Environment] = {}
for ld_environment in ld_environments:
(
environments_by_ld_environment_key[ld_environment["key"]],
_,
) = Environment.objects.get_or_create(
name=ld_environment["name"],
project_id=project_id,
)
return environments_by_ld_environment_key
def _create_tags_from_ld(
ld_tags: list[str],
project_id: int,
) -> dict[str, Tag]:
tags_by_ld_tag = {}
for ld_tag in (
*ld_tags,
LAUNCH_DARKLY_IMPORTED_DEFAULT_TAG_LABEL,
):
tags_by_ld_tag[ld_tag], _ = Tag.objects.update_or_create(
label=ld_tag,
project_id=project_id,
defaults={
"color": LAUNCH_DARKLY_IMPORTED_TAG_COLOR,
},
)
return tags_by_ld_tag
def _ld_operator_to_flagsmith_operator(ld_operator: str) -> Optional[str]:
"""
Convert a Launch Darkly operator to its closest Flagsmith equivalent. If not convertible, return None.
Based on: https://docs.launchdarkly.com/sdk/concepts/flag-evaluation-rules#operators
:param ld_operator: the operator of the targeting rule.
:return: the closest Flagsmith equivalent of the given Launch Darkly operator.
"""
return {
"in": constants.IN,
"endsWith": constants.REGEX,
"startsWith": constants.REGEX,
"matches": constants.REGEX,
"contains": constants.CONTAINS,
"lessThan": constants.LESS_THAN,
"lessThanOrEqual": constants.LESS_THAN_INCLUSIVE,
"greaterThan": constants.GREATER_THAN,
"greaterThanOrEqual": constants.GREATER_THAN_INCLUSIVE,
"before": constants.LESS_THAN,
"after": constants.GREATER_THAN,
"semVerEqual": constants.EQUAL,
"semVerLessThan": constants.LESS_THAN,
"semVerGreaterThan": constants.GREATER_THAN,
}.get(ld_operator, None)
def _convert_ld_values(values: list[str], ld_operator: str) -> list[str]:
"""
Convert "values" of a Launch Darkly clause to Flagsmith compatible values. Some matching is converted to
regex and some matching is consolidated into a single value. For example, if "in" operator is used, we join
the values using the comma separator to make it Flagsmith-compliant.
Note that a separate Clause should be created for each value in the lis and those clauses should be "OR"ed.
This is how Launch Darkly handles multiple values for a single operator such as less than.
:param values: the list of values from Launch Darkly's targeting rule.
:param ld_operator: the operator of the targeting rule.
:return: a list of values that is Flagsmith-compliant.
"""
match ld_operator:
case "in":
# TODO: How to escape the comma itself?
return [
*iter_chunked_concat(
values=values,
delimiter=",",
max_len=settings.SEGMENT_CONDITION_VALUE_LIMIT,
)
]
case "endsWith":
return [".*" + re.escape(value) for value in values]
case "startsWith":
return [re.escape(value) + ".*" for value in values]
case "semVerEqual" | "semVerLessThan" | "semVerGreaterThan":
return [value + ":semver" for value in values]
case _:
return [value for value in values]
def _get_segment_name(name: str, env: str) -> str:
"""
Generate a unique and descriptive name for the segment. This name is re-used on consecutive imports to
prevent duplicate segments.
:param name: Name of the Launch Darkly segment.
:param env: Environment name of the Launch Darkly segment.
:return: A unique and descriptive name for the segment targeting a specific environment.
"""
return f"{name} (Override for {env})"
def _create_feature_segments_for_segment_match_clauses(
import_request: LaunchDarklyImportRequest,
clauses: list[Clause],
project: Project,
feature: Feature,
environment: Environment,
segments_by_ld_key: dict[str, Segment],
) -> list[FeatureSegment]:
"""
Creates a feature segment if a rule contains "segmentMatch" operator. This shouldn't be used if clauses
doesn't contain "segmentMatch" operator. Instead use "_create_feature_segment_from_clauses".
This method can only accept clauses that contains "segmentMatch" operator. If there are other operators,
we can't create corresponding Feature Segments. This is a technical limitation of how "FeatureSegment" is
implemented in Flagsmith.
:param clauses: a list of clauses from Launch Darkly's targeting rule.
:param feature: the feature to target for the segment.
:param segments_by_ld_key: a mapping from Launch Darkly segment key to Segment. Used to find right segment
from the "segmentMatch" operator
:return: a list of "FeatureSegment" operators created for each "segmentMatch" operator.
"""
if any(clause["op"] != "segmentMatch" for clause in clauses):
for clause in clauses:
_log_error(
import_request=import_request,
error_message=f"Could not import segment clause {clause['attribute']} {clause['op']} for"
f" {feature.name} in {environment.name}: nested segment match is not supported.",
)
return []
if any(clause["negate"] is True for clause in clauses):
_log_error(
import_request=import_request,
error_message=f"Negated segment match is not supported, skipping"
f" for {feature.name} in {environment.name}",
)
return []
# Complex rules that allow matching segments is not allowed in Flagsmith.
# We can only emulate a single segment match by enabling the segment rule.
all_targeted_segments: list[str] = sum([clause["values"] for clause in clauses], [])
feature_states: list[FeatureState] = []
for index, targeted_segment_key in enumerate(all_targeted_segments):
if targeted_segment_key not in segments_by_ld_key:
_log_error(
import_request=import_request,
error_message=f"Segment {targeted_segment_key} not found, skipping"
f" for {feature.name} in {environment.name}",
)
continue
targeted_segment_name = segments_by_ld_key[targeted_segment_key].name
# We assume segment is already created.
segment = Segment.live_objects.get(name=targeted_segment_name, project=project)
feature_segment, _ = FeatureSegment.objects.update_or_create(
feature=feature,
segment=segment,
environment=environment,
priority=index,
)
# Enable rules by default. In LD, rules are enabled if the flag is on.
feature_state, _ = FeatureState.objects.update_or_create(
feature=feature,
feature_segment=feature_segment,
environment=environment,
defaults={"enabled": True},
)
feature_states.append(feature_state)
return feature_states
def _create_segment_rule_for_segment(
import_request: LaunchDarklyImportRequest,
segment: Segment,
clauses: list[Clause],
) -> SegmentRule:
"""
Create the SegmentRule for the given segment and clauses. This method doesn't handle any feature-specific
segments. Use "_create_feature_segment_from_clauses" for that.
:param segment: the segment to create the rule for.
:param clauses: a list of clauses from Launch Darkly's segment rule. This describes which identities belong
to the given segment.
:return: the SegmentRule created for the given segment.
"""
parent_rule, _ = SegmentRule.objects.get_or_create(
segment=segment, type=SegmentRule.ALL_RULE
)
# TODO: Delete existing rules if parent_rule already exists.
negated_child = None
for clause in clauses:
_property = clause["attribute"]
operator = _ld_operator_to_flagsmith_operator(clause["op"])
values = _convert_ld_values(
[str(value) for value in clause["values"]], clause["op"]
)
if operator is not None:
# Since there is no !X operation in Flagsmith, we wrap negated conditions in a none() rule.
if clause["negate"] is True:
# Create a negated child if it doesn't exist.
if negated_child is None:
negated_child = SegmentRule.objects.create(
rule=parent_rule, type=SegmentRule.NONE_RULE
)
target_rule = negated_child
else:
# Create a new child rule if it doesn't exist. Each child rule is "AND"ed together because
# parent_rule has type of `ALL`. Also note that each Condition added to this child rule is
# "OR"ed together. This is also how Launch Darkly works.
child_rule = SegmentRule.objects.create(
rule=parent_rule, type=SegmentRule.ANY_RULE
)
target_rule = child_rule
# Create a condition for each value. Each condition is "OR"ed together.
for value in values:
if len(value) > settings.SEGMENT_CONDITION_VALUE_LIMIT:
_log_error(
import_request=import_request,
error_message=(
f"Segment condition value '{truncate(value)}' for property '{_property}' exceeds the limit of"
f" {settings.SEGMENT_CONDITION_VALUE_LIMIT} characters,"
f" skipping for segment '{segment.name}'"
),
)
continue
Condition.objects.update_or_create(
rule=target_rule,
property=_property,
value=value,
operator=operator,
created_with_segment=True,
)
else:
_log_error(
import_request=import_request,
error_message=f"Can't map launch darkly operator: {clause['op']}"
f" skipping for segment: {segment.name}",
)
return parent_rule
def _create_feature_segment_from_clauses(
import_request: LaunchDarklyImportRequest,
clauses: list[Clause],
project: Project,
feature: Feature,
environment: Environment,
segments_by_ld_key: dict[str, Segment],
rule_name: str,
) -> list[FeatureState]:
"""
Create one or multiple feature-specific segment for the given clauses. Note that "segmentMatch" operator
is not fully supported. If "segmentMatch" is used, we create a feature rule for the given segment(s) instead
of a feature-specific segment. Thus, we return multiple feature states if there are multiple segments being
targeted.
Also note that "segmentMatch" operator is not supported for nested rules. If a nested rule contains
"segmentMatch", it can't use any other targeting operators. This is because we convert "segmentMatch" into
a segment specific feature value, thus no further filter can be applied.
:param clauses: a list of clauses from Launch Darkly's targeting rule.
:param feature: the feature to target for identities.
:param segments_by_ld_key: a mapping from Launch Darkly segment key to Segment. Used for "segmentMatch" op.
:param rule_name: the name of the rule this feature-specific segment is created for.
:return: a list of FeatureState objects for the newly created feature-specific segments.
"""
# There is no "segmentMatch" operator in flagsmith, instead we create a targeting rule for that
# specific segment.
if "segmentMatch" in [clause["op"] for clause in clauses]:
return _create_feature_segments_for_segment_match_clauses(
import_request=import_request,
clauses=clauses,
project=project,
feature=feature,
environment=environment,
segments_by_ld_key=segments_by_ld_key,
)
# Create a feature specific segment for the rule.
segment, _ = Segment.live_objects.update_or_create(
name=rule_name, project=project, feature=feature
)
# Create a targeting rule for the new feature-specific segment.
_create_segment_rule_for_segment(
import_request=import_request,
segment=segment,
clauses=clauses,
)
# Tie the feature and segment together.
feature_segment, _ = FeatureSegment.objects.update_or_create(
feature=feature,
segment=segment,
environment=environment,
)
# Enable rules by default. In LD, rules are enabled if the flag is on.
return [
FeatureState.objects.update_or_create(
feature=feature,
feature_segment=feature_segment,
environment=environment,
defaults={"enabled": True},
)[0]
]
def _import_targets(
import_request: LaunchDarklyImportRequest,
ld_flag_config: ld_types.FeatureFlagConfig,
feature: Feature,
environment: Environment,
segments_by_ld_key: dict[str, Segment],
mv_feature_options_by_variation: dict[str, MultivariateFeatureOption],
) -> None:
"""
Import the individual targeting rules for the given Launch Darkly's feature flag.
:param ld_flag_config: the feature flag config from Launch Darkly.
:param feature: the feature to target for identities.
:param environment: the environment to target for identities.
:param mv_feature_options_by_variation: a mapping from variation index to MultivariateFeatureOption if the
flag is multivariate.
"""
if "targets" in ld_flag_config:
# Identifiers are grouped by their variation index. So each target has the same variation index.
for target in ld_flag_config["targets"]:
# Create a segment override for those identities. This is a work-around to support individual
# targeting in local evaluation mode.
# TODO: Remove this when https://github.com/Flagsmith/flagsmith/issues/3132 is resolved.
feature_states = _create_feature_segment_from_clauses(
import_request=import_request,
clauses=[
{
"attribute": "key",
"op": "in",
"values": target["values"],
"negate": False,
}
],
project=feature.project,
feature=feature,
environment=environment,
segments_by_ld_key=segments_by_ld_key,
rule_name=f"individual-targeting-variation-{target['variation']}",
)
_set_imported_mv_feature_state_values(
variation_idx=str(target["variation"]),
rollout=None,
feature_states=feature_states,
mv_feature_options_by_variation=mv_feature_options_by_variation,
)
# Create individual identity targets.
for identifier in target["values"]:
identity, _ = Identity.objects.get_or_create(
identifier=identifier,
environment=environment,
)
identity.update_traits(
[
{
"trait_key": "key",
"trait_value": identifier, # type: ignore[typeddict-item]
}
]
)
# Set identity overrides.
if len(mv_feature_options_by_variation) == 0:
FeatureState.objects.update_or_create(
feature=feature,
feature_segment=None,
environment=environment,
identity=identity,
defaults={"enabled": target["variation"] == 0},
)
else:
feature_state, _ = FeatureState.objects.update_or_create(
feature=feature,
feature_segment=None,
environment=environment,
identity=identity,
defaults={"enabled": True},
)
mv_feature_option = mv_feature_options_by_variation[
str(target["variation"])
]
MultivariateFeatureStateValue.objects.update_or_create(
feature_state=feature_state,
multivariate_feature_option=mv_feature_option,
defaults={
"percentage_allocation": 100,
},
)
if "contextTargets" in ld_flag_config and len(ld_flag_config["contextTargets"]) > 0:
if (
sum(
[
len(context_target["values"])
for context_target in ld_flag_config["contextTargets"]
]
)
> 0
):
_log_error(
import_request=import_request,
error_message=f"Context targets are not supported, skipping context targets for feature"
f" {feature.name} in environment {environment.name}",
)
def _set_imported_mv_feature_state_values(
variation_idx: Optional[str],
rollout: Optional[ld_types.Rollout],
feature_states: list[FeatureState],
mv_feature_options_by_variation: dict[str, MultivariateFeatureOption],
) -> None:
"""
Set the feature states and multivariate feature states for recently imported flags.
If none of 'variation_idx' and 'rollout' is set, nothing is done. If the flag is not multivariate,
nothing is done.
:param variation_idx: the variation index to set as the control value. This is the launch darkly variation
index, not the index of the variation in Flagsmith.
:param rollout: the rollout to set as the control value coming from Launch Darkly.
:param feature_states: the feature states to set the values for.
:param mv_feature_options_by_variation: a mapping from variation index to MultivariateFeatureOption if the
flag is multivariate.
"""
# For Multivariate flags, we need to set targeting rules for each variation.
if len(mv_feature_options_by_variation) > 0:
# For each feature state,
for feature_state in feature_states:
if variation_idx is not None:
for mv_variation in mv_feature_options_by_variation:
mv_feature_option = mv_feature_options_by_variation[mv_variation]
# We expect only one variation to be set as the control.
# Control value is set to 100% and rest is set to 0%.
MultivariateFeatureStateValue.objects.update_or_create(
feature_state=feature_state,
multivariate_feature_option=mv_feature_option,
defaults={
"percentage_allocation": (
100 if variation_idx == mv_variation else 0
)
},
)
elif rollout is not None:
cumulative_rollout = rollout_baseline = 0
for weighted_variation in rollout["variations"]:
# Find the corresponding variation value.
weight = weighted_variation["weight"]
cumulative_rollout += weight / 1000 # type: ignore[assignment]
cumulative_rollout_rounded = round(cumulative_rollout)
# LD has weights between 0-100,000. Flagsmith has weights between 0-100.
# While scaling down, we need to keep track of the cumulative rollout so the
# values will add up to 100%.
percentage_allocation = (
cumulative_rollout_rounded - rollout_baseline
)
rollout_baseline = cumulative_rollout_rounded
mv_feature_option = mv_feature_options_by_variation[
str(weighted_variation["variation"])
]
MultivariateFeatureStateValue.objects.update_or_create(
feature_state=feature_state,
multivariate_feature_option=mv_feature_option,
defaults={"percentage_allocation": percentage_allocation},
)
def _import_rules(
import_request: LaunchDarklyImportRequest,
ld_flag_config: ld_types.FeatureFlagConfig,
feature: Feature,
environment: Environment,
segments_by_ld_key: dict[str, Segment],
mv_feature_options_by_variation: dict[str, MultivariateFeatureOption],
) -> None:
"""
Import each rule in the given Launch Darkly's feature flag as a feature-specific segment in Flagsmith.
:param ld_flag_config: the feature flag config from Launch Darkly.
:param feature: the feature to import the rules to.
:param segments_by_ld_key: a mapping from Launch Darkly segment key to Segment. Used for "segmentMatch" op.
:param mv_feature_options_by_variation: a mapping from variation index to MultivariateFeatureOption if the
flag is multivariate. Used for setting multivariate flag weights.
"""
if "prerequisites" in ld_flag_config and len(ld_flag_config["prerequisites"]) > 0:
_log_error(
import_request=import_request,
error_message=f"Prerequisites are not supported, skipping prerequisites for feature"
f" {feature.name} in environment {environment.name}",
)
# For each rule in LD's flag,
if "rules" in ld_flag_config:
for rule in ld_flag_config["rules"]:
# Generate a unique and descriptive name for the rule. This name is re-used on consecutive imports
# to prevent duplicate rules.
rule_name = rule.get("description", "imported-" + rule["_id"])
# Create the feature segment for the given rule and get the feature state objects from those
# newly created feature-specific segments.
feature_states = _create_feature_segment_from_clauses(
import_request=import_request,
clauses=rule["clauses"],
project=feature.project,
feature=feature,
environment=environment,
segments_by_ld_key=segments_by_ld_key,
rule_name=rule_name,
)
_set_imported_mv_feature_state_values(
variation_idx=rule.get("variation", None), # type: ignore[arg-type]
rollout=rule.get("rollout", None),
feature_states=feature_states,
mv_feature_options_by_variation=mv_feature_options_by_variation,
)
def _create_boolean_feature_states_with_segments_identities(
import_request: LaunchDarklyImportRequest,
ld_flag: ld_types.FeatureFlag,
feature: Feature,
environments_by_ld_environment_key: dict[str, Environment],
segments_by_ld_key: dict[str, Segment],
) -> None:
for ld_environment_key, environment in environments_by_ld_environment_key.items():
ld_flag_config = ld_flag["environments"][ld_environment_key]
feature_state, _ = FeatureState.objects.update_or_create(
feature=feature,
feature_segment=None,
environment=environment,
defaults={"enabled": ld_flag_config["on"]},
)
FeatureStateValue.objects.update_or_create(
feature_state=feature_state,
)
# TODO: Move target and rule creation to be invoked directly from `process_import_request`.
# https://github.com/Flagsmith/flagsmith/issues/3383
_import_targets(
import_request=import_request,
ld_flag_config=ld_flag_config,
feature=feature,
environment=environment,
segments_by_ld_key=segments_by_ld_key,
mv_feature_options_by_variation={},
)
_import_rules(
import_request=import_request,
ld_flag_config=ld_flag_config,
feature=feature,
environment=environment,
segments_by_ld_key=segments_by_ld_key,
mv_feature_options_by_variation={},
)
def _create_string_feature_states_with_segments_identities(
import_request: LaunchDarklyImportRequest,
ld_flag: ld_types.FeatureFlag,
feature: Feature,
environments_by_ld_environment_key: dict[str, Environment],
segments_by_ld_key: dict[str, Segment],
) -> None:
variations_by_idx = {
str(idx): variation for idx, variation in enumerate(ld_flag["variations"])
}
for ld_environment_key, environment in environments_by_ld_environment_key.items():
ld_flag_config = ld_flag["environments"][ld_environment_key]
is_flag_on = ld_flag_config["on"]
if is_flag_on:
variation_config_key = "isFallthrough"
else:
variation_config_key = "isOff"
string_value = ""
if ld_flag_config_summary := ld_flag_config.get("_summary"):
enabled_variations = ld_flag_config_summary.get("variations") or {}
for idx, variation_config in enabled_variations.items():
if variation_config.get(variation_config_key):
string_value = _serialize_variation_value(
variations_by_idx[idx]["value"]
)
break
feature_state, _ = FeatureState.objects.update_or_create(
feature=feature,
feature_segment=None,
environment=environment,
defaults={"enabled": is_flag_on},
)
FeatureStateValue.objects.update_or_create(
feature_state=feature_state,
defaults={"type": STRING, "string_value": string_value},
)
# TODO: Move target and rule creation to be invoked directly from `process_import_request`.
# https://github.com/Flagsmith/flagsmith/issues/3383
_import_targets(
import_request=import_request,
ld_flag_config=ld_flag_config,
feature=feature,
environment=environment,
segments_by_ld_key=segments_by_ld_key,
mv_feature_options_by_variation={},
)
_import_rules(
import_request=import_request,
ld_flag_config=ld_flag_config,
feature=feature,
environment=environment,
segments_by_ld_key=segments_by_ld_key,
mv_feature_options_by_variation={},
)
def _create_mv_feature_states_with_segments_identities(
import_request: LaunchDarklyImportRequest,
ld_flag: ld_types.FeatureFlag,
feature: Feature,
environments_by_ld_environment_key: dict[str, Environment],
segments_by_ld_key: dict[str, Segment],
) -> None:
variations = ld_flag["variations"]
variation_values_by_idx: dict[str, str] = {}
mv_feature_options_by_variation: dict[str, MultivariateFeatureOption] = {}
for idx, variation in enumerate(variations):
variation_idx = str(idx)
variation_value = _serialize_variation_value(variation["value"])
variation_values_by_idx[variation_idx] = variation_value
(
mv_feature_options_by_variation[str(idx)],
_,
) = MultivariateFeatureOption.objects.update_or_create(
feature=feature,
string_value=variation_value,
defaults={"default_percentage_allocation": 0, "type": STRING},
)
for ld_environment_key, environment in environments_by_ld_environment_key.items():
ld_flag_config = ld_flag["environments"][ld_environment_key]
is_flag_on = ld_flag_config["on"]
feature_state, _ = FeatureState.objects.update_or_create(
feature=feature,
feature_segment=None,
environment=environment,
defaults={"enabled": is_flag_on},
)
cumulative_rollout = rollout_baseline = 0
if ld_flag_config_summary := ld_flag_config.get("_summary"):
enabled_variations = ld_flag_config_summary.get("variations") or {}
for variation_idx, variation_config in enabled_variations.items():
if variation_config.get("isOff"):
# Set LD's off value as the control value.
# We expect only one off variation.
FeatureStateValue.objects.update_or_create(
feature_state=feature_state,
defaults={
"type": STRING,
"string_value": variation_values_by_idx[variation_idx],
},
)
mv_feature_option = mv_feature_options_by_variation[variation_idx]
percentage_allocation = 0
if variation_config.get("isFallthrough"):
# We expect only one fallthrough variation.
percentage_allocation = 100
elif rollout := variation_config.get("rollout"):
# 50% allocation is recorded as 50000 in LD.
# It's possible to allocate e.g. 50.999, resulting
# in rollout == 50999.
# Round the values nicely by keeping the `cumulative_rollout` tally.
cumulative_rollout += rollout / 1000 # type: ignore[assignment]
cumulative_rollout_rounded = round(cumulative_rollout)
percentage_allocation = (
cumulative_rollout_rounded - rollout_baseline
)
rollout_baseline = cumulative_rollout_rounded
MultivariateFeatureStateValue.objects.update_or_create(
feature_state=feature_state,
multivariate_feature_option=mv_feature_option,
defaults={"percentage_allocation": percentage_allocation},
)
# TODO: Move target and rule creation to be invoked directly from `process_import_request`.
# https://github.com/Flagsmith/flagsmith/issues/3383
_import_targets(
import_request=import_request,
ld_flag_config=ld_flag_config,
feature=feature,
environment=environment,
segments_by_ld_key=segments_by_ld_key,
mv_feature_options_by_variation=mv_feature_options_by_variation,
)
_import_rules(
import_request=import_request,
ld_flag_config=ld_flag_config,
feature=feature,
environment=environment,
segments_by_ld_key=segments_by_ld_key,
mv_feature_options_by_variation=mv_feature_options_by_variation,
)
def _get_feature_type_and_feature_state_factory(
ld_flag: ld_types.FeatureFlag,
) -> Tuple[
FeatureType,
Callable[
[
LaunchDarklyImportRequest,
ld_types.FeatureFlag,
Feature,
dict[str, Environment],
dict[str, Segment],
],
None,
],
]:
match ld_flag["kind"]:
case "multivariate" if len(ld_flag["variations"]) > 2:
feature_type = MULTIVARIATE
feature_state_factory = _create_mv_feature_states_with_segments_identities
case "multivariate":
feature_type = STANDARD
feature_state_factory = (
_create_string_feature_states_with_segments_identities
)
case _: # assume boolean
feature_type = STANDARD
feature_state_factory = (
_create_boolean_feature_states_with_segments_identities
)
return feature_type, feature_state_factory
def _create_feature_from_ld(
import_request: LaunchDarklyImportRequest,
ld_flag: ld_types.FeatureFlag,
environments_by_ld_environment_key: dict[str, Environment],
tags_by_ld_tag: dict[str, Tag],
segments_by_ld_key: dict[str, Segment],
project_id: int,
) -> Feature:
(
feature_type,
feature_state_factory,
) = _get_feature_type_and_feature_state_factory(
ld_flag,
)
tags = [
tags_by_ld_tag[LAUNCH_DARKLY_IMPORTED_DEFAULT_TAG_LABEL],
*(tags_by_ld_tag[ld_tag] for ld_tag in ld_flag["tags"]),
]
feature, _ = Feature.objects.update_or_create(
project_id=project_id,
name=ld_flag["key"],
defaults={
"description": ld_flag.get("description"),
"default_enabled": False,
"type": feature_type,
"is_archived": ld_flag["archived"] or ld_flag["deprecated"],
},
)
feature.tags.set(tags)
feature_state_factory( # type: ignore[call-arg]
import_request=import_request,
ld_flag=ld_flag,
feature=feature,
environments_by_ld_environment_key=environments_by_ld_environment_key,
segments_by_ld_key=segments_by_ld_key,
)
return feature # type: ignore[no-any-return]
def _create_features_from_ld(
import_request: LaunchDarklyImportRequest,
ld_flags: Iterable[ld_types.FeatureFlag],
environments_by_ld_environment_key: dict[str, Environment],
tags_by_ld_tag: dict[str, Tag],
segments_by_ld_key: dict[str, Segment],
project_id: int,
) -> list[Feature]:
return [
_create_feature_from_ld(
import_request=import_request,
ld_flag=ld_flag,
environments_by_ld_environment_key=environments_by_ld_environment_key,
tags_by_ld_tag=tags_by_ld_tag,
segments_by_ld_key=segments_by_ld_key,
project_id=project_id,
)
for ld_flag in ld_flags
]
def _include_users_to_segment(
import_request: LaunchDarklyImportRequest,
segment: Segment,
users: list[str],
negate: bool,
) -> None:
if len(users) == 0:
return
# Find the parent rule of the segment.
parent_rule, _ = SegmentRule.objects.get_or_create(
segment=segment, type=SegmentRule.ALL_RULE
)
# Create a condition to match against those identities via "key" trait.
for identities_string in iter_chunked_concat(
values=users,
delimiter=",",
max_len=settings.SEGMENT_CONDITION_VALUE_LIMIT,
):
if len(identities_string) > settings.SEGMENT_CONDITION_VALUE_LIMIT:
_log_error(
import_request=import_request,
error_message=(
f"Targeting key '{truncate(identities_string)}' exceeds the limit of"
f" {settings.SEGMENT_CONDITION_VALUE_LIMIT} characters, "
f"skipping for segment '{segment.name}'"
),
)
continue
included_rule = SegmentRule.objects.create(
rule=parent_rule,
type=SegmentRule.NONE_RULE if negate else SegmentRule.ANY_RULE,
)
Condition.objects.update_or_create(
rule=included_rule,
property="key",
value=identities_string,
operator=constants.IN,
created_with_segment=True,
)
def _create_segments_from_ld(
import_request: LaunchDarklyImportRequest,
ld_segments: list[tuple[ld_types.UserSegment, str]],