This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
feature.py
126 lines (102 loc) · 4.49 KB
/
feature.py
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
# The contents of this file are subject to the Common Public Attribution
# License Version 1.0. (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://code.reddit.com/LICENSE. The License is based on the Mozilla Public
# License Version 1.1, but Sections 14 and 15 have been added to cover use of
# software over a computer network and provide for limited attribution for the
# Original Developer. In addition, Exhibit A has been modified to be consistent
# with Exhibit B.
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
#
# The Original Code is reddit.
#
# The Original Developer is the Initial Developer. The Initial Developer of
# the Original Code is reddit Inc.
#
# All portions of the code written by reddit are Copyright (c) 2006-2015 reddit
# Inc. All Rights Reserved.
###############################################################################
from r2.config.feature.state import FeatureState
from r2.config.feature.world import World
from r2.lib.hooks import HookRegistrar
feature_hooks = HookRegistrar()
_world = World()
_featurestate_cache = {}
def is_enabled(name, user=None, subreddit=None):
"""Test and return whether a given feature is enabled for this request.
If `feature` is not found, returns False.
The optional arguments allow overriding that you generally don't want, but
is useful outside of request contexts - cron jobs and the like.
:param name string - a given feature name
:param user - (optional) an Account
:param subreddit - (optional) a Subreddit
:return bool
"""
if not user:
user = _world.current_user()
if not subreddit:
subreddit = _world.current_subreddit()
subdomain = _world.current_subdomain()
oauth_client = _world.current_oauth_client()
return _get_featurestate(name).is_enabled(
user=user,
subreddit=subreddit,
subdomain=subdomain,
oauth_client=oauth_client,
)
def variant(name, user=None):
"""Return which variant of an experiment a user is part of.
If the experiment is not found, has no variants, or the user is not part of
any of them (control), return None.
:param name string - an experiment (feature) name
:param user - (optional) an Account. Defaults to the currently signed in
user.
:return string, or None if not part of an experiment
"""
if not user:
user = _world.current_user()
return _get_featurestate(name).variant(user)
def all_enabled(user=None):
"""Return a list of enabled features and experiments for the user.
Provides the user's assigned variant and the experiment ID for experiments.
This does not trigger bucketing events, so it should not be used for
feature flagging purposes on the server. It is meant to let clients
condition features on experiment variants. Those clients should manually
send the appropriate bucketing events.
:param user - (optional) an Account. Defaults to None, for which we
determine logged-out features.
:return dict - a dictionary mapping enabled feature keys to True or to the
experiment/variant information
"""
features = FeatureState.get_all(_world)
# Get enabled features and experiments
active = {}
for feature in features:
experiment = feature.config.get('experiment')
if experiment:
# Get experiment names, ids, and assigned variants, leaving out
# experiments for which this user is excluded
variant = feature.variant(user)
if variant:
active[feature.name] = {
'experiment_id': experiment.get('experiment_id'),
'variant': variant
}
elif feature.is_enabled(user):
active[feature.name] = True
return active
@feature_hooks.on('worker.live_config.update')
def clear_featurestate_cache():
global _featurestate_cache
_featurestate_cache = {}
def _get_featurestate(name):
"""Get a FeatureState object for this feature, creating it if necessary.
:param name string - a given feature name
:return FeatureState
"""
if name not in _featurestate_cache:
_featurestate_cache[name] = FeatureState(name, _world)
return _featurestate_cache[name]