|
22 | 22 |
|
23 | 23 | import com.optimizely.ab.Optimizely;
|
24 | 24 | import com.optimizely.ab.UnknownEventTypeException;
|
| 25 | +import com.optimizely.ab.UnknownLiveVariableException; |
25 | 26 | import com.optimizely.ab.config.Experiment;
|
| 27 | +import com.optimizely.ab.config.LiveVariable; |
| 28 | +import com.optimizely.ab.config.LiveVariableUsageInstance; |
26 | 29 | import com.optimizely.ab.config.ProjectConfig;
|
27 | 30 | import com.optimizely.ab.config.Variation;
|
| 31 | +import com.optimizely.ab.error.NoOpErrorHandler; |
| 32 | +import com.optimizely.ab.error.RaiseExceptionErrorHandler; |
28 | 33 | import com.optimizely.ab.notification.NotificationCenter;
|
29 | 34 |
|
30 | 35 | import org.slf4j.Logger;
|
31 | 36 |
|
| 37 | +import java.util.Collections; |
32 | 38 | import java.util.HashMap;
|
33 | 39 | import java.util.List;
|
34 | 40 | import java.util.Map;
|
@@ -551,4 +557,237 @@ public NotificationCenter getNotificationCenter() {
|
551 | 557 |
|
552 | 558 | return null;
|
553 | 559 | }
|
| 560 | + |
| 561 | + //======== live variable getters ========// |
| 562 | + /** |
| 563 | + * Helper method to retrieve the {@link LiveVariable} for the given variable key. |
| 564 | + * If {@link RaiseExceptionErrorHandler} is provided, either a live variable is returned, or an exception is |
| 565 | + * thrown. |
| 566 | + * If {@link NoOpErrorHandler} is used, either a live variable or {@code null} is returned. |
| 567 | + * |
| 568 | + * @param projectConfig the current project config |
| 569 | + * @param variableKey the key for the live variable being retrieved from the current project config |
| 570 | + * @return the live variable to retrieve for the given variable key |
| 571 | + * |
| 572 | + */ |
| 573 | + private LiveVariable getLiveVariable(ProjectConfig projectConfig, String variableKey) { |
| 574 | + |
| 575 | + LiveVariable liveVariable = projectConfig |
| 576 | + .getLiveVariableKeyMapping() |
| 577 | + .get(variableKey); |
| 578 | + |
| 579 | + if (liveVariable == null) { |
| 580 | + String unknownLiveVariableKeyError = |
| 581 | + String.format("Live variable \"%s\" is not in the datafile.", variableKey); |
| 582 | + logger.error(unknownLiveVariableKeyError); |
| 583 | + return null; |
| 584 | + |
| 585 | + } |
| 586 | + |
| 587 | + return liveVariable; |
| 588 | + } |
| 589 | + |
| 590 | + /** |
| 591 | + * Get the value of a String live variable |
| 592 | + * @param variableKey the String key for the variable |
| 593 | + * @param userId the user ID |
| 594 | + * @param activateExperiment the flag denoting whether to activate an experiment or not |
| 595 | + * @return String value of the live variable |
| 596 | + */ |
| 597 | + @Deprecated |
| 598 | + public @Nullable |
| 599 | + String getVariableString(@NonNull String variableKey, |
| 600 | + @NonNull String userId, |
| 601 | + boolean activateExperiment) { |
| 602 | + return getVariableString(variableKey, userId, Collections.<String, String>emptyMap(), activateExperiment); |
| 603 | + } |
| 604 | + |
| 605 | + /** |
| 606 | + * Get the value of a String live variable |
| 607 | + * @param variableKey the String key for the variable |
| 608 | + * @param userId the user ID |
| 609 | + * @param attributes a map of attributes about the user |
| 610 | + * @param activateExperiment the flag denoting whether to activate an experiment or not |
| 611 | + * @return String value of the live variable |
| 612 | + */ |
| 613 | + @Deprecated |
| 614 | + public @Nullable |
| 615 | + String getVariableString(@NonNull String variableKey, |
| 616 | + @NonNull String userId, |
| 617 | + @NonNull Map<String, String> attributes, |
| 618 | + boolean activateExperiment) { |
| 619 | + |
| 620 | + if (!isValid()) { |
| 621 | + logger.warn("Optimizely is not initialized, could not get live variable {} " + |
| 622 | + "for user {}", variableKey, userId); |
| 623 | + return null; |
| 624 | + } |
| 625 | + |
| 626 | + if (variableKey == null || variableKey.isEmpty()) { |
| 627 | + logger.error("Invalid live variable key (null or empty) " + |
| 628 | + "for user {}", userId); |
| 629 | + return null; |
| 630 | + } |
| 631 | + |
| 632 | + if (userId == null || userId.isEmpty()) { |
| 633 | + logger.error("Invalid userId (null or empty) " + |
| 634 | + "for live variable {}", variableKey); |
| 635 | + return null; |
| 636 | + } |
| 637 | + |
| 638 | + LiveVariable variable = getLiveVariable(optimizely.getProjectConfig(), variableKey); |
| 639 | + if (variable == null) { |
| 640 | + return null; |
| 641 | + } |
| 642 | + |
| 643 | + List<Experiment> experimentsUsingLiveVariable = |
| 644 | + optimizely.getProjectConfig().getLiveVariableIdToExperimentsMapping().get(variable.getId()); |
| 645 | + Map<String, Map<String, LiveVariableUsageInstance>> variationToLiveVariableUsageInstanceMapping = |
| 646 | + optimizely.getProjectConfig().getVariationToLiveVariableUsageInstanceMapping(); |
| 647 | + |
| 648 | + if (experimentsUsingLiveVariable == null) { |
| 649 | + logger.warn("No experiment is using variable \"{}\".", variable.getKey()); |
| 650 | + return variable.getDefaultValue(); |
| 651 | + } |
| 652 | + |
| 653 | + for (Experiment experiment : experimentsUsingLiveVariable) { |
| 654 | + Variation variation; |
| 655 | + if (activateExperiment) { |
| 656 | + variation = activate(experiment.getKey(), userId, attributes); |
| 657 | + } else { |
| 658 | + variation = getVariation(experiment.getKey(), userId, attributes); |
| 659 | + } |
| 660 | + |
| 661 | + if (variation != null) { |
| 662 | + LiveVariableUsageInstance usageInstance = |
| 663 | + variationToLiveVariableUsageInstanceMapping.get(variation.getId()).get(variable.getId()); |
| 664 | + return usageInstance.getValue(); |
| 665 | + } |
| 666 | + } |
| 667 | + |
| 668 | + return variable.getDefaultValue(); |
| 669 | + } |
| 670 | + |
| 671 | + /** |
| 672 | + * Get the value of a Boolean live variable |
| 673 | + * @param variableKey the String key for the variable |
| 674 | + * @param userId the user ID |
| 675 | + * @param activateExperiment the flag denoting whether to activate an experiment or not |
| 676 | + * @return Boolean value of the live variable |
| 677 | + */ |
| 678 | + @Deprecated |
| 679 | + public @Nullable |
| 680 | + Boolean getVariableBoolean(@NonNull String variableKey, |
| 681 | + @NonNull String userId, |
| 682 | + boolean activateExperiment) { |
| 683 | + return getVariableBoolean(variableKey, userId, Collections.<String, String>emptyMap(), activateExperiment); |
| 684 | + } |
| 685 | + |
| 686 | + /** |
| 687 | + * Get the value of a Boolean live variable |
| 688 | + * @param variableKey the String key for the variable |
| 689 | + * @param userId the user ID |
| 690 | + * @param attributes a map of attributes about the user |
| 691 | + * @param activateExperiment the flag denoting whether to activate an experiment or not |
| 692 | + * @return Boolean value of the live variable |
| 693 | + */ |
| 694 | + @Deprecated |
| 695 | + public @Nullable |
| 696 | + Boolean getVariableBoolean(@NonNull String variableKey, |
| 697 | + @NonNull String userId, |
| 698 | + @NonNull Map<String, String> attributes, |
| 699 | + boolean activateExperiment) { |
| 700 | + |
| 701 | + String variableValueString = getVariableString(variableKey, userId, attributes, activateExperiment); |
| 702 | + if (variableValueString != null) { |
| 703 | + return Boolean.parseBoolean(variableValueString); |
| 704 | + } |
| 705 | + |
| 706 | + return null; |
| 707 | + } |
| 708 | + |
| 709 | + /** |
| 710 | + * Get the value of a Integer live variable |
| 711 | + * @param variableKey the String key for the variable |
| 712 | + * @param userId the user ID |
| 713 | + * @param activateExperiment the flag denoting whether to activate an experiment or not |
| 714 | + * @return Integer value of the live variable |
| 715 | + */ |
| 716 | + @Deprecated |
| 717 | + public @Nullable |
| 718 | + Integer getVariableInteger(@NonNull String variableKey, |
| 719 | + @NonNull String userId, |
| 720 | + boolean activateExperiment) { |
| 721 | + return getVariableInteger(variableKey, userId, Collections.<String, String>emptyMap(), activateExperiment); |
| 722 | + } |
| 723 | + |
| 724 | + /** |
| 725 | + * Get the value of a Integer live variable |
| 726 | + * @param variableKey the String key for the variable |
| 727 | + * @param userId the user ID |
| 728 | + * @param attributes a map of attributes about the user |
| 729 | + * @param activateExperiment the flag denoting whether to activate an experiment or not* @return Integer value of the live variable |
| 730 | + */ |
| 731 | + @Deprecated |
| 732 | + public @Nullable |
| 733 | + Integer getVariableInteger(@NonNull String variableKey, |
| 734 | + @NonNull String userId, |
| 735 | + @NonNull Map<String, String> attributes, |
| 736 | + boolean activateExperiment) { |
| 737 | + |
| 738 | + String variableValueString = getVariableString(variableKey, userId, attributes, activateExperiment); |
| 739 | + if (variableValueString != null) { |
| 740 | + try { |
| 741 | + return Integer.parseInt(variableValueString); |
| 742 | + } catch (NumberFormatException e) { |
| 743 | + logger.error("Variable value \"{}\" for live variable \"{}\" is not an integer.", variableValueString, |
| 744 | + variableKey); |
| 745 | + } |
| 746 | + } |
| 747 | + |
| 748 | + return null; |
| 749 | + } |
| 750 | + |
| 751 | + /** |
| 752 | + * Get the value of a Double live variable |
| 753 | + * @param variableKey the String key for the variable |
| 754 | + * @param userId the user ID |
| 755 | + * @param activateExperiment the flag denoting whether to activate an experiment or not |
| 756 | + * @return Double value of the live variable |
| 757 | + */ |
| 758 | + @Deprecated |
| 759 | + public @Nullable |
| 760 | + Double getVariableDouble(@NonNull String variableKey, |
| 761 | + @NonNull String userId, |
| 762 | + boolean activateExperiment) { |
| 763 | + return getVariableDouble(variableKey, userId, Collections.<String, String>emptyMap(), activateExperiment); |
| 764 | + } |
| 765 | + |
| 766 | + /** |
| 767 | + * Get the value of a Double live variable |
| 768 | + * @param variableKey the String key for the variable |
| 769 | + * @param userId the user ID |
| 770 | + * @param attributes a map of attributes about the user |
| 771 | + * @param activateExperiment the flag denoting whether to activate an experiment or not |
| 772 | + * @return Double value of the live variable |
| 773 | + */ |
| 774 | + @Deprecated |
| 775 | + public @Nullable |
| 776 | + Double getVariableDouble(@NonNull String variableKey, |
| 777 | + @NonNull String userId, |
| 778 | + @NonNull Map<String, String> attributes, |
| 779 | + boolean activateExperiment) { |
| 780 | + |
| 781 | + String variableValueString = getVariableString(variableKey, userId, attributes, activateExperiment); |
| 782 | + if (variableValueString != null) { |
| 783 | + try { |
| 784 | + return Double.parseDouble(variableValueString); |
| 785 | + } catch (NumberFormatException e) { |
| 786 | + logger.error("Variable value \"{}\" for live variable \"{}\" is not a double.", variableValueString, |
| 787 | + variableKey); |
| 788 | + } |
| 789 | + } |
| 790 | + |
| 791 | + return null; |
| 792 | + } |
554 | 793 | }
|
0 commit comments