|
| 1 | +/* |
| 2 | + * Sends recalculate request for all ab testing experiment variant cohorts |
| 3 | + * Server: countly |
| 4 | + * Path: $(countly dir)/bin/scripts/fix-data |
| 5 | + * Command: node recalculate_ab_test_cohorts.js |
| 6 | + */ |
| 7 | + |
| 8 | +// API key here with permission to update cohorts |
| 9 | +const API_KEY = ''; |
| 10 | +// Countly app id, if not specified will do nothing |
| 11 | +const APP_ID = ''; |
| 12 | +// ab test experiment id, will do nothing if not specified |
| 13 | +const EXPERIMENT_ID = ''; |
| 14 | +// countly instance public url, something like 'https://name.count.ly' |
| 15 | +const SERVER_URL = ''; |
| 16 | + |
| 17 | +const pluginManager = require('../../../plugins/pluginManager.js'); |
| 18 | +const request = require('countly-request')(pluginManager.getConfig('security')); |
| 19 | + |
| 20 | +if (API_KEY.length === 0) { |
| 21 | + console.warn('Please provide an API_KEY'); |
| 22 | + process.exit(1); |
| 23 | +} |
| 24 | + |
| 25 | +pluginManager.dbConnection('countly_out').then(async(db) => { |
| 26 | + let urlObj = {}; |
| 27 | + try { |
| 28 | + urlObj = new URL(SERVER_URL); |
| 29 | + } |
| 30 | + catch (err) { |
| 31 | + urlObj = new URL((process.env.COUNTLY_CONFIG_PROTOCOL || "http") + "://" + (process.env.COUNTLY_CONFIG_HOSTNAME || "localhost")); |
| 32 | + } |
| 33 | + urlObj.pathname = 'i/cohorts/recalculate'; |
| 34 | + urlObj.searchParams.append('api_key', API_KEY); |
| 35 | + urlObj.searchParams.append('app_id', APP_ID); |
| 36 | + |
| 37 | + console.log(`Finding ab test experiment ${EXPERIMENT_ID} in app ${APP_ID}`); |
| 38 | + |
| 39 | + const experimentCollectionName = `ab_testing_experiments${APP_ID}`; |
| 40 | + const experiment = await db.collection(experimentCollectionName).findOne({ _id: db.ObjectID(EXPERIMENT_ID) }); |
| 41 | + |
| 42 | + if (experiment?.variants?.length > 0) { |
| 43 | + for (let varIdx = 0; varIdx < experiment.variants.length; varIdx += 1) { |
| 44 | + const variant = experiment.variants[varIdx]; |
| 45 | + |
| 46 | + if (variant?.cohorts && Object.keys(variant.cohorts).length > 0) { |
| 47 | + for (let cohIdx = 0; cohIdx < Object.keys(variant.cohorts).length; cohIdx += 1) { |
| 48 | + const cohortId = variant.cohorts[Object.keys(variant.cohorts)[cohIdx]]; |
| 49 | + console.log(`Sending recalculate request for variant ${variant.name}, cohort ${cohortId}`); |
| 50 | + |
| 51 | + urlObj.searchParams.delete('cohort_id'); |
| 52 | + urlObj.searchParams.append('cohort_id', cohortId); |
| 53 | + |
| 54 | + await new Promise((resolve) => { |
| 55 | + request.get(urlObj.href, (err, _, body) => { |
| 56 | + if (err) { |
| 57 | + console.warn('Request failed ', JSON.stringify(cohortId), err); |
| 58 | + } |
| 59 | + else { |
| 60 | + console.log('Request finished ', JSON.stringify(cohortId), body); |
| 61 | + } |
| 62 | + resolve(); |
| 63 | + }); |
| 64 | + }); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + else { |
| 70 | + console.warn(`Experiments ${EXPERIMENT_ID} not found in app ${APP_ID}`); |
| 71 | + } |
| 72 | + |
| 73 | + db.close(); |
| 74 | +}); |
0 commit comments