-
Notifications
You must be signed in to change notification settings - Fork 981
Expand file tree
/
Copy pathrecalculate_ab_test_cohorts.js
More file actions
74 lines (63 loc) · 2.83 KB
/
recalculate_ab_test_cohorts.js
File metadata and controls
74 lines (63 loc) · 2.83 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
/*
* Sends recalculate request for all ab testing experiment variant cohorts
* Server: countly
* Path: $(countly dir)/bin/scripts/fix-data
* Command: node recalculate_ab_test_cohorts.js
*/
// API key here with permission to update cohorts
const API_KEY = '';
// Countly app id, if not specified will do nothing
const APP_ID = '';
// ab test experiment id, will do nothing if not specified
const EXPERIMENT_ID = '';
// countly instance public url, something like 'https://name.count.ly'
const SERVER_URL = '';
const pluginManager = require('../../../plugins/pluginManager.js');
const request = require('countly-request')(pluginManager.getConfig('security'));
if (API_KEY.length === 0) {
console.warn('Please provide an API_KEY');
process.exit(1);
}
pluginManager.dbConnection('countly_out').then(async(db) => {
let urlObj = {};
try {
urlObj = new URL(SERVER_URL);
}
catch (err) {
urlObj = new URL((process.env.COUNTLY_CONFIG_PROTOCOL || "http") + "://" + (process.env.COUNTLY_CONFIG_HOSTNAME || "localhost"));
}
urlObj.pathname = 'i/cohorts/recalculate';
urlObj.searchParams.append('api_key', API_KEY);
urlObj.searchParams.append('app_id', APP_ID);
console.log(`Finding ab test experiment ${EXPERIMENT_ID} in app ${APP_ID}`);
const experimentCollectionName = `ab_testing_experiments${APP_ID}`;
const experiment = await db.collection(experimentCollectionName).findOne({ _id: db.ObjectID(EXPERIMENT_ID) });
if (experiment?.variants?.length > 0) {
for (let varIdx = 0; varIdx < experiment.variants.length; varIdx += 1) {
const variant = experiment.variants[varIdx];
if (variant?.cohorts && Object.keys(variant.cohorts).length > 0) {
for (let cohIdx = 0; cohIdx < Object.keys(variant.cohorts).length; cohIdx += 1) {
const cohortId = variant.cohorts[Object.keys(variant.cohorts)[cohIdx]];
console.log(`Sending recalculate request for variant ${variant.name}, cohort ${cohortId}`);
urlObj.searchParams.delete('cohort_id');
urlObj.searchParams.append('cohort_id', cohortId);
await new Promise((resolve) => {
request.get(urlObj.href, (err, _, body) => {
if (err) {
console.warn('Request failed ', JSON.stringify(cohortId), err);
}
else {
console.log('Request finished ', JSON.stringify(cohortId), body);
}
resolve();
});
});
}
}
}
}
else {
console.warn(`Experiments ${EXPERIMENT_ID} not found in app ${APP_ID}`);
}
db.close();
});