Module: Featurevisor::Evaluate

Defined in:
lib/featurevisor/evaluate.rb

Overview

Evaluation module for feature flag evaluation

Class Method Summary collapse

Class Method Details

.evaluate(options) ⇒ Hash

Main evaluation function

Parameters:

  • options (Hash)

    Evaluation options

Returns:

  • (Hash)

    Evaluation result



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
# File 'lib/featurevisor/evaluate.rb', line 102

def self.evaluate(options)
  type = options[:type]
  feature_key = options[:feature_key]
  variable_key = options[:variable_key]
  context = options[:context]
  logger = options[:logger]
  datafile_reader = options[:datafile_reader]
  sticky = options[:sticky]
  hooks_manager = options[:hooks_manager]

  hooks = hooks_manager.get_all
  evaluation = nil

  begin
    # Root flag evaluation
    flag = nil
    if type != "flag"
      # needed by variation and variable evaluations
      flag = evaluate(options.merge(type: "flag"))

      if flag[:enabled] == false
        evaluation = {
          type: type,
          feature_key: feature_key,
          reason: Featurevisor::EvaluationReason::DISABLED
        }

        feature = datafile_reader.get_feature(feature_key)

        # serve variable default value if feature is disabled (if explicitly specified)
        if type == "variable"
          if feature && variable_key &&
             feature[:variablesSchema] &&
             (feature[:variablesSchema][variable_key] || feature[:variablesSchema][variable_key.to_sym])
            variable_schema = feature[:variablesSchema][variable_key] || feature[:variablesSchema][variable_key.to_sym]

            if variable_schema[:disabledValue]
              # disabledValue: <value>
              evaluation = {
                type: type,
                feature_key: feature_key,
                reason: Featurevisor::EvaluationReason::VARIABLE_DISABLED,
                variable_key: variable_key,
                variable_value: variable_schema[:disabledValue],
                variable_schema: variable_schema,
                enabled: false
              }
            elsif variable_schema[:useDefaultWhenDisabled]
              # useDefaultWhenDisabled: true
              evaluation = {
                type: type,
                feature_key: feature_key,
                reason: Featurevisor::EvaluationReason::VARIABLE_DEFAULT,
                variable_key: variable_key,
                variable_value: variable_schema[:defaultValue],
                variable_schema: variable_schema,
                enabled: false
              }
            end
          end
        end

        # serve disabled variation value if feature is disabled (if explicitly specified)
        if type == "variation" && feature && feature[:disabledVariationValue]
          evaluation = {
            type: type,
            feature_key: feature_key,
            reason: Featurevisor::EvaluationReason::VARIATION_DISABLED,
            variation_value: feature[:disabledVariationValue],
            enabled: false
          }
        end

        logger.debug("feature is disabled", evaluation)

        return evaluation
      end
    end

    # Sticky
    if sticky && (sticky[feature_key] || sticky[feature_key.to_sym])
      sticky_feature = sticky[feature_key] || sticky[feature_key.to_sym]

      # flag
      if type == "flag" && sticky_feature.key?(:enabled)
        evaluation = {
          type: type,
          feature_key: feature_key,
          reason: Featurevisor::EvaluationReason::STICKY,
          sticky: sticky_feature,
          enabled: sticky_feature[:enabled]
        }

        logger.debug("using sticky enabled", evaluation)

        return evaluation
      end

      # variation
      if type == "variation"
        variation_value = sticky_feature[:variation]

        if variation_value
          evaluation = {
            type: type,
            feature_key: feature_key,
            reason: Featurevisor::EvaluationReason::STICKY,
            variation_value: variation_value
          }

          logger.debug("using sticky variation", evaluation)

          return evaluation
        end
      end

      # variable
      if type == "variable" && variable_key
        variables = sticky_feature[:variables]

        if variables && (variables[variable_key] || variables[variable_key.to_sym])
          variable_value = variables[variable_key] || variables[variable_key.to_sym]
          evaluation = {
            type: type,
            feature_key: feature_key,
            reason: Featurevisor::EvaluationReason::STICKY,
            variable_key: variable_key,
            variable_value: variable_value
          }

          logger.debug("using sticky variable", evaluation)

          return evaluation
        end
      end
    end

    # Feature
    feature = feature_key.is_a?(String) ? datafile_reader.get_feature(feature_key) : feature_key

    # feature: not found
    unless feature
      evaluation = {
        type: type,
        feature_key: feature_key,
        reason: Featurevisor::EvaluationReason::FEATURE_NOT_FOUND
      }

      logger.warn("feature not found", evaluation)

      return evaluation
    end

    # feature: deprecated
    if type == "flag" && feature[:deprecated]
      logger.warn("feature is deprecated", { feature_key: feature_key })
    end

    # variableSchema
    variable_schema = nil

    if variable_key
      if feature[:variablesSchema] && (feature[:variablesSchema][variable_key] || feature[:variablesSchema][variable_key.to_sym])
        variable_schema = feature[:variablesSchema][variable_key] || feature[:variablesSchema][variable_key.to_sym]
      end

      # variable schema not found
      unless variable_schema
        evaluation = {
          type: type,
          feature_key: feature_key,
          reason: Featurevisor::EvaluationReason::VARIABLE_NOT_FOUND,
          variable_key: variable_key
        }

        logger.warn("variable schema not found", evaluation)

        return evaluation
      end

      if variable_schema[:deprecated]
        logger.warn("variable is deprecated", {
          feature_key: feature_key,
          variable_key: variable_key
        })
      end
    end

    # variation: no variations
    if type == "variation" && (!feature[:variations] || feature[:variations].empty?)
      evaluation = {
        type: type,
        feature_key: feature_key,
        reason: Featurevisor::EvaluationReason::NO_VARIATIONS
      }

      logger.warn("no variations", evaluation)

      return evaluation
    end

    # Forced
    force_result = datafile_reader.get_matched_force(feature, context)
    force = force_result[:force]
    force_index = force_result[:forceIndex]

    if force
      # flag
      if type == "flag" && force.key?(:enabled)
        evaluation = {
          type: type,
          feature_key: feature_key,
          reason: Featurevisor::EvaluationReason::FORCED,
          force_index: force_index,
          force: force,
          enabled: force[:enabled]
        }

        logger.debug("forced enabled found", evaluation)

        return evaluation
      end

      # variation
      if type == "variation" && force[:variation] && feature[:variations]
        variation = feature[:variations].find { |v| v[:value] == force[:variation] }

        if variation
          evaluation = {
            type: type,
            feature_key: feature_key,
            reason: Featurevisor::EvaluationReason::FORCED,
            force_index: force_index,
            force: force,
            variation: variation
          }

          logger.debug("forced variation found", evaluation)

          return evaluation
        end
      end

      # variable
      if variable_key && force[:variables] && (force[:variables][variable_key] || force[:variables][variable_key.to_sym])
        variable_value = force[:variables][variable_key] || force[:variables][variable_key.to_sym]
        evaluation = {
          type: type,
          feature_key: feature_key,
          reason: Featurevisor::EvaluationReason::FORCED,
          force_index: force_index,
          force: force,
          variable_key: variable_key,
          variable_schema: variable_schema,
          variable_value: variable_value
        }

        logger.debug("forced variable", evaluation)

        return evaluation
      end
    end

    # Required
    if type == "flag" && feature[:required] && feature[:required].length > 0
      required_features_are_enabled = feature[:required].all? do |required|
        required_key = nil
        required_variation = nil

        if required.is_a?(String)
          required_key = required
        else
          required_key = required[:key]
          required_variation = required[:variation]
        end

        required_evaluation = evaluate(options.merge(type: "flag", feature_key: required_key))
        required_is_enabled = required_evaluation[:enabled]

        next false unless required_is_enabled

        if required_variation
          required_variation_evaluation = evaluate(options.merge(type: "variation", feature_key: required_key))

          required_variation_value = nil

          if required_variation_evaluation[:variation_value]
            required_variation_value = required_variation_evaluation[:variation_value]
          elsif required_variation_evaluation[:variation]
            required_variation_value = required_variation_evaluation[:variation][:value]
          end

          next required_variation_value == required_variation
        end

        true
      end

      unless required_features_are_enabled
        evaluation = {
          type: type,
          feature_key: feature_key,
          reason: Featurevisor::EvaluationReason::REQUIRED,
          required: feature[:required],
          enabled: required_features_are_enabled
        }

        logger.debug("required features not enabled", evaluation)

        return evaluation
      end
    end

    # Bucketing
    # bucketKey
    bucket_key = Featurevisor::Bucketer.get_bucket_key({
      feature_key: feature_key,
      bucket_by: feature[:bucketBy],
      context: context,
      logger: logger
    })

    # Run bucket key hooks
    bucket_key = hooks_manager.run_bucket_key_hooks({
      feature_key: feature_key,
      context: context,
      bucket_by: feature[:bucketBy],
      bucket_key: bucket_key
    })

    # bucketValue
    bucket_value = Featurevisor::Bucketer.get_bucketed_number(bucket_key)

    # Run bucket value hooks
    bucket_value = hooks_manager.run_bucket_value_hooks({
      feature_key: feature_key,
      bucket_key: bucket_key,
      context: context,
      bucket_value: bucket_value
    })

    matched_traffic = nil
    matched_allocation = nil

    if type != "flag"
      matched_traffic = datafile_reader.get_matched_traffic(feature[:traffic], context)

      if matched_traffic
        matched_allocation = datafile_reader.get_matched_allocation(matched_traffic, bucket_value)
      end
    else
      matched_traffic = datafile_reader.get_matched_traffic(feature[:traffic], context)
    end

    if matched_traffic
      # percentage: 0
      if matched_traffic[:percentage] == 0
        evaluation = {
          type: type,
          feature_key: feature_key,
          reason: Featurevisor::EvaluationReason::RULE,
          bucket_key: bucket_key,
          bucket_value: bucket_value,
          rule_key: matched_traffic[:key],
          traffic: matched_traffic,
          enabled: false
        }

        logger.debug("matched rule with 0 percentage", evaluation)

        return evaluation
      end

      # flag
      if type == "flag"
        # flag: check if mutually exclusive
        if feature[:ranges] && feature[:ranges].length > 0
          matched_range = feature[:ranges].find do |range|
            bucket_value >= range[0] && bucket_value < range[1]
          end

          # matched
          if matched_range
            evaluation = {
              type: type,
              feature_key: feature_key,
              reason: Featurevisor::EvaluationReason::ALLOCATED,
              bucket_key: bucket_key,
              bucket_value: bucket_value,
              rule_key: matched_traffic[:key],
              traffic: matched_traffic,
              enabled: matched_traffic[:enabled].nil? ? true : matched_traffic[:enabled]
            }

            logger.debug("matched", evaluation)

            return evaluation
          end

          # no match
          evaluation = {
            type: type,
            feature_key: feature_key,
            reason: Featurevisor::EvaluationReason::OUT_OF_RANGE,
            bucket_key: bucket_key,
            bucket_value: bucket_value,
            enabled: false
          }

          logger.debug("not matched", evaluation)

          return evaluation
        end

        # flag: override from rule
        if matched_traffic.key?(:enabled)
          evaluation = {
            type: type,
            feature_key: feature_key,
            reason: Featurevisor::EvaluationReason::RULE,
            bucket_key: bucket_key,
            bucket_value: bucket_value,
            rule_key: matched_traffic[:key],
            traffic: matched_traffic,
            enabled: matched_traffic[:enabled]
          }

          logger.debug("override from rule", evaluation)

          return evaluation
        end

        # treated as enabled because of matched traffic
        if bucket_value <= matched_traffic[:percentage]
          evaluation = {
            type: type,
            feature_key: feature_key,
            reason: Featurevisor::EvaluationReason::RULE,
            bucket_key: bucket_key,
            bucket_value: bucket_value,
            rule_key: matched_traffic[:key],
            traffic: matched_traffic,
            enabled: true
          }

          logger.debug("matched traffic", evaluation)

          return evaluation
        end
      end

      # variation
      if type == "variation" && feature[:variations]
        # override from rule
        if matched_traffic[:variation]
          variation = feature[:variations].find { |v| v[:value] == matched_traffic[:variation] }

          if variation
            evaluation = {
              type: type,
              feature_key: feature_key,
              reason: Featurevisor::EvaluationReason::RULE,
              bucket_key: bucket_key,
              bucket_value: bucket_value,
              rule_key: matched_traffic[:key],
              traffic: matched_traffic,
              variation: variation
            }

            logger.debug("override from rule", evaluation)

            return evaluation
          end
        end

        # regular allocation
        if matched_allocation && matched_allocation[:variation]
          variation = feature[:variations].find { |v| v[:value] == matched_allocation[:variation] }

          if variation
            evaluation = {
              type: type,
              feature_key: feature_key,
              reason: Featurevisor::EvaluationReason::ALLOCATED,
              bucket_key: bucket_key,
              bucket_value: bucket_value,
              rule_key: matched_traffic[:key],
              traffic: matched_traffic,
              variation: variation
            }

            logger.debug("allocated variation", evaluation)

            return evaluation
          end
        end
      end
    end

    # variable
    if type == "variable" && variable_key
      # override from rule
      if matched_traffic &&
         matched_traffic[:variables] &&
         (matched_traffic[:variables][variable_key] || matched_traffic[:variables][variable_key.to_sym])
        variable_value = matched_traffic[:variables][variable_key] || matched_traffic[:variables][variable_key.to_sym]
        evaluation = {
          type: type,
          feature_key: feature_key,
          reason: Featurevisor::EvaluationReason::RULE,
          bucket_key: bucket_key,
          bucket_value: bucket_value,
          rule_key: matched_traffic[:key],
          traffic: matched_traffic,
          variable_key: variable_key,
          variable_schema: variable_schema,
          variable_value: variable_value
        }

        logger.debug("override from rule", evaluation)

        return evaluation
      end

      # check variations
      variation_value = nil

      if force && force[:variation]
        variation_value = force[:variation]
      elsif matched_traffic && matched_traffic[:variation]
        variation_value = matched_traffic[:variation]
      elsif matched_allocation && matched_allocation[:variation]
        variation_value = matched_allocation[:variation]
      end

      if variation_value && feature[:variations].is_a?(Array)
        variation = feature[:variations].find { |v| v[:value] == variation_value }

        if variation && variation[:variableOverrides] && (variation[:variableOverrides][variable_key] || variation[:variableOverrides][variable_key.to_sym])
          overrides = variation[:variableOverrides][variable_key] || variation[:variableOverrides][variable_key.to_sym]

          logger.debug("checking variableOverrides", {
            feature_key: feature_key,
            variable_key: variable_key,
            overrides: overrides,
            context: context
          })

          override = overrides.find do |o|
            logger.debug("evaluating override", {
              feature_key: feature_key,
              variable_key: variable_key,
              override: o,
              context: context
            })

            result = if o[:conditions]
              matched = datafile_reader.all_conditions_are_matched(
                o[:conditions].is_a?(String) && o[:conditions] != "*" ?
                  JSON.parse(o[:conditions]) : o[:conditions],
                context
              )
              logger.debug("conditions match result", {
                feature_key: feature_key,
                variable_key: variable_key,
                conditions: o[:conditions],
                matched: matched
              })
              matched
            elsif o[:segments]
              segments = datafile_reader.parse_segments_if_stringified(o[:segments])
              matched = datafile_reader.all_segments_are_matched(segments, context)
              logger.debug("segments match result", {
                feature_key: feature_key,
                variable_key: variable_key,
                segments: o[:segments],
                parsed_segments: segments,
                matched: matched
              })
              matched
            else
              logger.debug("override has no conditions or segments", {
                feature_key: feature_key,
                variable_key: variable_key,
                override: o
              })
              false
            end

            logger.debug("override evaluation result", {
              feature_key: feature_key,
              variable_key: variable_key,
              result: result
            })

            result
          end

          if override
            evaluation = {
              type: type,
              feature_key: feature_key,
              reason: Featurevisor::EvaluationReason::VARIABLE_OVERRIDE,
              bucket_key: bucket_key,
              bucket_value: bucket_value,
              rule_key: matched_traffic&.[](:key),
              traffic: matched_traffic,
              variable_key: variable_key,
              variable_schema: variable_schema,
              variable_value: override[:value]
            }

            logger.debug("variable override", evaluation)

            return evaluation
          end
        end

        if variation &&
           variation[:variables] &&
           (variation[:variables][variable_key] || variation[:variables][variable_key.to_sym])
          variable_value = variation[:variables][variable_key] || variation[:variables][variable_key.to_sym]
          evaluation = {
            type: type,
            feature_key: feature_key,
            reason: Featurevisor::EvaluationReason::ALLOCATED,
            bucket_key: bucket_key,
            bucket_value: bucket_value,
            rule_key: matched_traffic&.[](:key),
            traffic: matched_traffic,
            variable_key: variable_key,
            variable_schema: variable_schema,
            variable_value: variable_value
          }

          logger.debug("allocated variable", evaluation)

          return evaluation
        end
      end
    end

    # Nothing matched
    if type == "variation"
      evaluation = {
        type: type,
        feature_key: feature_key,
        reason: Featurevisor::EvaluationReason::NO_MATCH,
        bucket_key: bucket_key,
        bucket_value: bucket_value
      }

      logger.debug("no matched variation", evaluation)

      return evaluation
    end

    if type == "variable"
      if variable_schema
        evaluation = {
          type: type,
          feature_key: feature_key,
          reason: Featurevisor::EvaluationReason::VARIABLE_DEFAULT,
          bucket_key: bucket_key,
          bucket_value: bucket_value,
          variable_key: variable_key,
          variable_schema: variable_schema,
          variable_value: variable_schema[:defaultValue]
        }

        logger.debug("using default value", evaluation)

        return evaluation
      end

      evaluation = {
        type: type,
        feature_key: feature_key,
        reason: Featurevisor::EvaluationReason::VARIABLE_NOT_FOUND,
        variable_key: variable_key,
        bucket_key: bucket_key,
        bucket_value: bucket_value
      }

      logger.debug("variable not found", evaluation)

      return evaluation
    end

    evaluation = {
      type: type,
      feature_key: feature_key,
      reason: Featurevisor::EvaluationReason::NO_MATCH,
      bucket_key: bucket_key,
      bucket_value: bucket_value,
      enabled: false
    }

    logger.debug("nothing matched", evaluation)

    evaluation
  rescue => e
    evaluation = {
      type: type,
      feature_key: feature_key,
      variable_key: variable_key,
      reason: Featurevisor::EvaluationReason::ERROR,
      error: e
    }

    logger.error("error", evaluation)

    evaluation
  end
end

.evaluate_with_hooks(options) ⇒ Hash

Evaluate with hooks

Parameters:

  • options (Hash)

    Evaluation options

Returns:

  • (Hash)

    Evaluation result



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
# File 'lib/featurevisor/evaluate.rb', line 41

def self.evaluate_with_hooks(options)
  begin
    hooks_manager = options[:hooks_manager]
    hooks = hooks_manager.get_all

    # Run before hooks
    result_options = options
    hooks.each do |hook|
      if hook.respond_to?(:call_before)
        result_options = hook.call_before(result_options)
      end
    end

    # Evaluate
    evaluation = evaluate(result_options)

    # Default: variation
    if options[:default_variation_value] &&
       evaluation[:type] == "variation" &&
       evaluation[:variation_value].nil?
      evaluation[:variation_value] = options[:default_variation_value]
    end

    # Default: variable
    if options[:default_variable_value] &&
       evaluation[:type] == "variable" &&
       evaluation[:variable_value].nil?
      evaluation[:variable_value] = options[:default_variable_value]
    end

    # Run after hooks
    hooks.each do |hook|
      if hook.respond_to?(:call_after)
        evaluation = hook.call_after(evaluation, result_options)
      end
    end

    evaluation
  rescue => e
    type = options[:type]
    feature_key = options[:feature_key]
    variable_key = options[:variable_key]
    logger = options[:logger]

    evaluation = {
      type: type,
      feature_key: feature_key,
      variable_key: variable_key,
      reason: Featurevisor::EvaluationReason::ERROR,
      error: e
    }

    logger.error("error during evaluation", evaluation)

    evaluation
  end
end