-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathhelpers.py
More file actions
executable file
·281 lines (254 loc) · 8.34 KB
/
helpers.py
File metadata and controls
executable file
·281 lines (254 loc) · 8.34 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
import json
import os
import time
from functools import cache
from pathlib import Path
import requests
from nimbus.models.base_dataclass import (
BaseExperimentApplications,
)
LOAD_DATA_RETRIES = 60
LOAD_DATA_RETRY_DELAY = 1.0
TARGETING_CONFIGS_PATH = (
Path(__file__).resolve().parents[1] / "fixtures" / "targeting_configs.json"
)
def load_graphql_data(query):
nginx_url = os.getenv("INTEGRATION_TEST_NGINX_URL", "https://nginx")
for retry in range(LOAD_DATA_RETRIES):
try:
return requests.post(
f"{nginx_url}/api/v5/graphql",
json=query,
verify=False,
).json()
except json.JSONDecodeError:
if retry + 1 >= LOAD_DATA_RETRIES:
raise
time.sleep(LOAD_DATA_RETRY_DELAY)
@cache
def load_config_data():
return load_graphql_data(
{
"operationName": "getConfig",
"variables": {},
"query": """
query getConfig {
nimbusConfig {
applications {
label
value
}
channels {
label
value
}
conclusionRecommendationsChoices {
label
value
}
applicationConfigs {
application
channels {
label
value
}
}
allFeatureConfigs {
id
name
slug
description
application
ownerEmail
schema
setsPrefs
enabled
}
firefoxVersions {
label
value
}
outcomes {
friendlyName
slug
application
description
isDefault
metrics {
slug
friendlyName
description
}
}
owners {
username
}
targetingConfigs {
label
value
description
applicationValues
stickyRequired
isFirstRunRequired
}
hypothesisDefault
documentationLink {
label
value
}
maxPrimaryOutcomes
locales {
id
code
name
}
countries {
id
code
name
}
languages {
id
code
name
}
projects {
id
name
}
takeaways {
label
value
}
types {
label
value
}
statusUpdateExemptFields {
all
experiments
rollouts
}
populationSizingData
}
}
""",
}
)["data"]["nimbusConfig"]
def load_targeting_configs(app=BaseExperimentApplications.FIREFOX_DESKTOP.value):
targeting_configs = json.loads(TARGETING_CONFIGS_PATH.read_text())
return [
item["value"]
for item in targeting_configs
if (
BaseExperimentApplications.FIREFOX_DESKTOP.value in app
and BaseExperimentApplications.FIREFOX_DESKTOP.value
in item["applicationValues"]
)
or (
BaseExperimentApplications.FIREFOX_DESKTOP.value not in app
and BaseExperimentApplications.FIREFOX_DESKTOP.value
not in item["applicationValues"]
)
]
def get_feature_id_as_string(slug, app):
config_data = load_config_data()["allFeatureConfigs"]
for f in config_data:
if f["slug"] == slug and f["application"] == app:
return str(f["id"])
def load_experiment_data(slug):
return load_graphql_data(
{
"operationName": "getExperiment",
"variables": {"slug": slug},
"query": """
query getExperiment($slug: String!) {
experimentBySlug(slug: $slug) {
id
jexlTargetingExpression
recipeJson
}
}
""",
}
)
def create_basic_experiment(name, app, targeting=None, languages=None, is_rollout=False):
config_data = load_config_data()
if languages is None:
languages = []
language_ids = [l["id"] for l in config_data["languages"] if l["code"] in languages]
if targeting is None:
targeting = load_targeting_configs()[0]
return load_graphql_data(
{
"operationName": "createExperiment",
"variables": {
"input": {
"name": name,
"hypothesis": "Test hypothesis",
"application": app,
"languages": language_ids,
"changelogMessage": "test changelog message",
"targetingConfigSlug": targeting,
"isRollout": is_rollout,
}
},
"query": """
mutation createExperiment($input: ExperimentInput!) {
createExperiment(input: $input) {
nimbusExperiment {
slug
}
}
}
""",
}
)
def update_experiment(slug, data):
experiment_id = load_experiment_data(slug)["data"]["experimentBySlug"]["id"]
data.update({"id": experiment_id})
return load_graphql_data(
{
"operationName": "updateExperiment",
"variables": {"input": data},
"query": """
mutation updateExperiment($input: ExperimentInput!) {
updateExperiment(input: $input) {
message
}
}
""",
}
)
def create_experiment(slug, app, data, targeting=None, is_rollout=False):
return (
create_basic_experiment(
slug,
app,
targeting=targeting,
is_rollout=is_rollout,
),
update_experiment(slug, data),
)
def end_experiment(slug):
experiment_id = load_experiment_data(slug)["data"]["experimentBySlug"]["id"]
data = {
"id": experiment_id,
"changelogMessage": "Update Experiment",
"publishStatus": "APPROVED",
"status": "LIVE",
"statusNext": "COMPLETE",
}
load_graphql_data(
{
"operationName": "updateExperiment",
"variables": {"input": data},
"query": """
mutation updateExperiment($input: ExperimentInput!) {
updateExperiment(input: $input) {
message
}
}
""",
}
)