-
Notifications
You must be signed in to change notification settings - Fork 221
/
experiment.rs
412 lines (389 loc) · 16.2 KB
/
experiment.rs
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use nimbus::error::Result;
#[cfg(feature = "stateful")]
fn main() -> Result<()> {
const DEFAULT_BASE_URL: &str = "https://firefox.settings.services.mozilla.com";
const DEFAULT_COLLECTION_NAME: &str = "messaging-experiments";
use clap::{App, Arg, SubCommand};
use env_logger::Env;
use nimbus::{
metrics::{
EnrollmentStatusExtraDef, FeatureExposureExtraDef, MalformedFeatureConfigExtraDef,
MetricsHandler,
},
AppContext, AvailableRandomizationUnits, EnrollmentStatus, NimbusClient,
NimbusTargetingHelper, RemoteSettingsConfig,
};
use std::collections::HashMap;
use std::io::prelude::*;
pub struct NoopMetricsHandler;
impl MetricsHandler for NoopMetricsHandler {
fn record_enrollment_statuses(&self, _: Vec<EnrollmentStatusExtraDef>) {
// do nothing
}
fn record_feature_activation(&self, _activation_event: FeatureExposureExtraDef) {
// do nothing
}
fn record_feature_exposure(&self, _exposure_event: FeatureExposureExtraDef) {
// do nothing
}
fn record_malformed_feature_config(&self, _event: MalformedFeatureConfigExtraDef) {
// do nothing
}
}
// We set the logging level to be `warn` here, meaning that only
// logs of `warn` or higher will be actually be shown, any other
// error will be omitted
// To manually set the log level, you can set the `RUST_LOG` environment variable
// Possible values are "info", "debug", "warn" and "error"
// Check [`env_logger`](https://docs.rs/env_logger/) for more details
env_logger::from_env(Env::default().default_filter_or("info")).init();
viaduct_reqwest::use_reqwest_backend();
// Initiate the matches for the command line arguments
let matches = App::new("Nimbus SDK")
.author("Tarik E. <teshaq@mozilla.com>")
.about("A demo for the Nimbus SDK")
.arg(
Arg::with_name("config")
.short("c")
.long("config")
.value_name("FILE")
.help("Sets a custom File configuration")
.required(true)
.takes_value(true),
)
.arg(
Arg::with_name("collection")
.short("n")
.long("collection")
.value_name("COLLECTION")
.help("Sets a custom collection name")
.takes_value(true),
)
.arg(
Arg::with_name("server")
.short("s")
.long("server")
.value_name("SERVER_URL")
.help("Specifies the server to use")
.takes_value(true),
)
.arg(
Arg::with_name("db-path")
.long("db-path")
.value_name("PATH")
.help("The path where the database will be created")
.takes_value(true),
)
.subcommand(
SubCommand::with_name("show-experiments")
.about("Show all experiments, followed by the enrolled experiments"),
)
.subcommand(
SubCommand::with_name("fetch-experiments")
.about("Fetch experiments from the server. Subsequent calls to apply-pending-experiments will change enrolments."),
)
.subcommand(
SubCommand::with_name("apply-pending-experiments")
.about("Updates enrollments with the experiments last fetched from the server with fetch-experiments"),
)
.subcommand(
SubCommand::with_name("update-experiments")
.about("Equivalent to fetch-experiments and apply-pending-experiments together"),
)
.subcommand(
SubCommand::with_name("opt-in")
.about("Opts in to an experiment and branch")
.arg(
Arg::with_name("experiment")
.long("experiment")
.value_name("EXPERIMENT_ID")
.help("The ID of the experiment to opt in to")
.required(true)
.takes_value(true)
)
.arg(
Arg::with_name("branch")
.long("branch")
.value_name("BRANCH_ID")
.help("The ID of the branch to opt in to")
.required(true)
.takes_value(true)
)
)
.subcommand(
SubCommand::with_name("opt-out")
.about("Opts out of an experiment")
.arg(
Arg::with_name("experiment")
.long("experiment")
.value_name("EXPERIMENT_ID")
.help("The ID of the experiment to opt out of")
.required(true)
.takes_value(true)
)
)
.subcommand(
SubCommand::with_name("opt-out-all")
.about("Opts out of all experiments")
)
.subcommand(
SubCommand::with_name("gen-uuid")
.about("Generate a uuid that can get enrolled in experiments")
.arg(
Arg::with_name("number")
.default_value("1")
.help("The number of experiments the uuid generated should be able to enroll in, WARNING: This can end in an infinite loop if the number is too high")
)
.arg(
Arg::with_name("set")
.long("set")
.help("Sets the UUID in the database when complete.")
)
)
.subcommand(
SubCommand::with_name("brute-force")
.about("Brute-force an experiment a number of times, showing enrollment results")
.arg(
Arg::with_name("experiment")
.long("experiment")
.value_name("EXPERIMENT_ID")
.help("The ID of the experiment to reset")
.required(true)
.takes_value(true)
)
.arg(
Arg::with_name("num")
.long("num")
.short("n")
.default_value("10000")
.help("The number of times to generate a UUID and attempt enrollment.")
)
)
.get_matches();
// Read command line arguments, or set default values
let mut config_file = std::fs::File::open(matches.value_of("config").unwrap())
.expect("Config file does not exist");
let mut config = String::new();
config_file.read_to_string(&mut config).unwrap();
let config = serde_json::from_str::<serde_json::Value>(&config).unwrap();
let context = config.get("context").unwrap();
let context = serde_json::from_value::<AppContext>(context.clone()).unwrap();
let server_url = matches
.value_of("server")
.unwrap_or_else(|| match config.get("server_url") {
Some(v) => v.as_str().unwrap(),
_ => DEFAULT_BASE_URL,
});
log::info!("Server url is {}", server_url);
let client_id = config
.get("client_id")
.map(|v| v.to_string())
.unwrap_or_else(|| "no-client-id-specified".to_string());
log::info!("Client ID is {}", client_id);
let collection_name =
matches
.value_of("collection")
.unwrap_or_else(|| match config.get("collection_name") {
Some(v) => v.as_str().unwrap(),
_ => DEFAULT_COLLECTION_NAME,
});
log::info!("Collection name is {}", collection_name);
let temp_dir = std::env::temp_dir();
let db_path_default = temp_dir.to_str().unwrap();
let db_path = matches
.value_of("db-path")
.unwrap_or_else(|| match config.get("db_path") {
Some(v) => v.as_str().unwrap(),
_ => db_path_default,
});
log::info!("Database directory is {}", db_path);
// initiate the optional config
let config = RemoteSettingsConfig {
server_url: Some(server_url.to_string()),
bucket_name: None,
collection_name: collection_name.to_string(),
};
let aru = AvailableRandomizationUnits::with_client_id(&client_id);
// Here we initialize our main `NimbusClient` struct
let nimbus_client = NimbusClient::new(
context.clone(),
Default::default(),
db_path,
Some(config),
aru,
Box::new(NoopMetricsHandler),
)?;
log::info!("Nimbus ID is {}", nimbus_client.nimbus_id()?);
// Explicitly update experiments at least once for init purposes
nimbus_client.fetch_experiments()?;
nimbus_client.apply_pending_experiments()?;
// We match against the subcommands
match matches.subcommand() {
// show_enrolled shows only the enrolled experiments and the chosen branches
("show-experiments", _) => {
println!("======================================");
println!("Printing all experiments (regardless of enrollment)");
nimbus_client
.get_all_experiments()?
.iter()
.for_each(|e| println!("Experiment: {}", e.slug));
println!("======================================");
println!("Printing only enrolled experiments");
nimbus_client
.get_active_experiments()?
.iter()
.for_each(|e| {
println!(
"Enrolled in experiment: {}, in branch: {}",
e.slug, e.branch_slug
)
});
}
("fetch-experiments", _) => {
println!("======================================");
println!("Fetching experiments");
nimbus_client.fetch_experiments()?;
}
("apply-pending-experiments", _) => {
println!("======================================");
println!("Applying pending experiments");
nimbus_client.apply_pending_experiments()?;
}
("update-experiments", _) => {
println!("======================================");
println!("Fetching and applying experiments");
nimbus_client.fetch_experiments()?;
nimbus_client.apply_pending_experiments()?;
}
("opt-in", Some(matches)) => {
println!("======================================");
let experiment = matches.value_of("experiment").unwrap();
let branch = matches.value_of("branch").unwrap();
println!(
"Opting in to experiment '{}', branch '{}'",
experiment, branch
);
nimbus_client.opt_in_with_branch(experiment.to_string(), branch.to_string())?;
}
("opt-out", Some(matches)) => {
println!("======================================");
let experiment = matches.value_of("experiment").unwrap();
println!("Opting out of experiment '{}'", experiment);
nimbus_client.opt_out(experiment.to_string())?;
}
("opt-out-all", _) => {
println!("======================================");
println!("Opting out of ALL experiments:");
let experiments = nimbus_client.get_all_experiments().unwrap();
for experiment in experiments {
println!("\t'{}'", &experiment.slug);
nimbus_client.opt_out(experiment.slug)?;
}
}
// gen_uuid will generate a UUID that gets enrolled in a given number of
// experiments, optionally settting the generated ID in the database.
("gen-uuid", Some(matches)) => {
let num = matches
.value_of("number")
.unwrap()
.parse::<usize>()
.expect("the number parameter should be a number");
let all_experiments = nimbus_client.get_all_experiments()?;
// XXX - this check below isn't good enough - we need to know how
// many of those experiments we are actually eligible for!
if all_experiments.len() < num {
println!(
"Can't try to enroll in {} experiments - only {} exist",
num,
all_experiments.len(),
);
std::process::exit(1);
}
let mut num_tries = 0;
let aru = AvailableRandomizationUnits::with_client_id(&client_id);
'outer: loop {
let uuid = uuid::Uuid::new_v4();
let aru = aru.apply_nimbus_id(&uuid);
let mut num_of_experiments_enrolled = 0;
let event_store = nimbus_client.event_store();
let th = NimbusTargetingHelper::new(&context, event_store.clone());
for exp in &all_experiments {
let enr = nimbus::evaluate_enrollment(&aru, exp, &th)?;
if enr.status.is_enrolled() {
num_of_experiments_enrolled += 1;
if num_of_experiments_enrolled >= num {
println!("======================================");
println!("Generated UUID is: {}", uuid);
println!("(it took {} goes to find it)", num_tries);
// ideally we'd
if matches.is_present("set") {
println!("Setting uuid in the database...");
nimbus_client.set_nimbus_id(&uuid)?;
}
break 'outer;
}
}
}
num_tries += 1;
if num_tries % 5000 == 0 {
println!(
"Made {} attempts so far; it's not looking good...",
num_tries
);
}
}
}
("brute-force", Some(matches)) => {
let experiment_id = matches.value_of("experiment").unwrap();
let num = matches
.value_of("num")
.unwrap()
.parse::<usize>()
.expect("the number of iterations to brute-force");
println!("Brute-forcing experiment '{}' {} times", experiment_id, num);
// *sob* no way currently to get by id.
let find_exp = || {
for exp in nimbus_client
.get_all_experiments()
.expect("can't fetch experiments!?")
{
if exp.slug == experiment_id {
return exp;
}
}
panic!("No such experiment");
};
let exp = find_exp();
let mut results = HashMap::new();
let event_store = nimbus_client.event_store();
for _i in 0..num {
// Rather than inspecting what randomization unit is specified
// by the experiment just generate a new uuid for all possible
// options.
let uuid = uuid::Uuid::new_v4();
let aru =
AvailableRandomizationUnits::with_client_id(&client_id).apply_nimbus_id(&uuid);
let th = NimbusTargetingHelper::new(&context, event_store.clone());
let enrollment = nimbus::evaluate_enrollment(&aru, &exp, &th)?;
let key = match enrollment.status.clone() {
EnrollmentStatus::Enrolled { .. } => "Enrolled",
EnrollmentStatus::NotEnrolled { .. } => "NotEnrolled",
EnrollmentStatus::Disqualified { .. } => "Disqualified",
EnrollmentStatus::WasEnrolled { .. } => "WasEnrolled",
EnrollmentStatus::Error { .. } => "Error",
};
results.insert(key, results.get(&key).unwrap_or(&0) + 1);
}
println!("Results: {:#?}", results);
}
(&_, _) => println!("Invalid subcommand"),
};
Ok(())
}
#[cfg(not(feature = "stateful"))]
fn main() -> Result<()> {
Ok(())
}