-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsessions.js
243 lines (226 loc) · 6.78 KB
/
sessions.js
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
const { program } = require("commander")
const axios = require("axios")
require("dotenv").config()
const otreeIPs = process.env.OTREE_IPS.split(",")
const otreeRestKey = process.env.OTREE_REST_KEY
// program
// .option("-s, --sessionConfigName <type>", "session config name.")
// .option("-n, --numParticipants <int>", "number of participants.")
//
program
.command("list <name>")
.description("Show an session urls.")
.option("--count", "Count urls.")
.option("--sessions", "List session names urls.")
.option("--urls", "List only urls.")
.action(async (name, options) => {
const results = [...(await getOtreeUrls(otreeIPs, otreeRestKey))].filter(
(r) => {
return r.experimentName == name
},
)
if (options.urls) {
const urls = results.map((r) => {
return r.experimentUrl
})
console.log(urls)
return
}
if (options.sessions) {
const sessions = [
...new Set(
results.map((r) => {
return r.sessionCode
}),
),
]
const sessionCounts = sessions.map((s) => {
const count = results.reduce((a, r) => {
if (r.sessionCode == s) {
a += 1
}
return a
}, 0)
return { name: s, count: count }
})
console.log(sessionCounts)
return
}
if (options.count) {
console.log(`Number of urls for ${name}: ${results.length}.`)
return
}
// console.log(`Showing item: ${name}`);
console.log(results)
})
program
.command("create <name>")
.description("Create session.")
.option("--num <size>", "Number of participants for session.", parseInt)
.action(async (name, options) => {
if (options.num) {
const num = options.num
console.log(`Creating ${num} of URLs for experiment ${name}.`)
try {
const results = await createSession(otreeIPs, otreeRestKey, name, num)
console.log(results)
} catch (err) {
console.error(err)
}
} else {
console.log(`Missing argument.`)
}
})
program.parse(process.argv)
const options = program.opts()
function getOrSetValue(obj, key, defaultValue) {
if (!(key in obj)) {
obj[key] = defaultValue
}
return obj[key]
}
function createSession(
otreeIPs,
otreeRestKey,
sessionConfigName,
numParticipants,
) {
return new Promise((resolve, reject) => {
const config = {
headers: {
"otree-rest-key": otreeRestKey,
},
}
const payload = {
session_config_name: sessionConfigName,
num_participants: numParticipants,
}
const results = []
// Get a map of promises for every REST call to the servers
// then we can wait on all promises to resolve with Promise.all
const outerPromises = otreeIPs.map((s) => {
const apiUrl = `http://${s}/api/sessions`
// console.log(`Calling ${apiUrl}`)
return axios
.post(apiUrl, payload, config)
.then((res) => {
// console.log(res.data)
results.push(res.data)
})
.catch((err) => {
console.error(err)
}) //axios
}) //outerPromises
Promise.all(outerPromises)
.then(() => {
// console.log(`${JSON.stringify(results,null,2)}`)
resolve(results)
})
.catch((error) => {
reject(error)
})
}) // Promise
}
function getOtreeUrls(otreeIPs, otreeRestKey) {
return new Promise((resolve, reject) => {
const config = {
headers: {
"otree-rest-key": otreeRestKey,
},
}
const results = []
// Get a map of promises for every REST call to the servers
// then we can wait on all promises to resolve with Promise.all
const outerPromises = otreeIPs.map((s) => {
const apiUrl = `http://${s}/api/sessions`
//console.log(`Calling ${apiUrl}`)
return axios.get(apiUrl, config).then(async (res) => {
// For every session on the server we call back to the server
// to get more participant info. We do the same with promises
// and wait on these internal promises resolve.
const innerPromises = res.data.map((session) => {
const code = session.code
const experimentName = session.config_name
const sessionUrl = apiUrl + "/" + code
return axios.get(sessionUrl, config).then((res) => {
res.data.participants.forEach((p) => {
const experimentUrl = `http://${s}/InitializeParticipant/${p.code}`
results.push({
server: s,
sessionCode: code,
experimentName: experimentName,
experimentUrl: experimentUrl,
})
})
})
}) //innerPromises
// Wait for promises in the inner loop (map) to resolve before
// moving to the next outer loop.
try {
await Promise.all(innerPromises)
} catch (error) {
reject(error)
}
})
})
Promise.all(outerPromises)
.then(() => {
//console.log(`${JSON.stringify(results,null,2)}`)
resolve(results)
})
.catch((error) => {
reject(error)
})
})
}
function lastElement(arr) {
return arr[arr.length - 1]
}
async function getExperimentUrls(experiments) {
const expToEnable = config.experiments.map((e) => e.name)
const usedUrlsFromDb = usersDb.getUsedUrls()
// clear existing URLs first:
for (const [_, val] of Object.entries(experiments)) {
// console.log(JSON.stringify(val, null, 4))
for (const [_, arr] of Object.entries(val.servers)) {
arr.splice(0, arr.length)
}
}
try {
// Get oTree experiment URLs from servers
otreeData = await getOtreeUrls(otreeIPs, otreeRestKey)
// Build experiments object
otreeData.forEach((r) => {
const exp = getOrSetValue(experiments, r.experimentName, {
name: r.experimentName,
enabled: expToEnable.includes(r.experimentName),
servers: {},
})
const serverPlusSession = `${r.server}#${r.sessionCode}`
// const expUrls = getOrSetValue(exp.servers, r.server, [])
const expUrls = getOrSetValue(exp.servers, serverPlusSession, [])
//console.log(`expUrls ${expUrls} oTreeUrls: ${r.experimentUrl}`)
if (
expUrls.includes(r.experimentUrl) ||
usedUrls.has(r.experimentUrl) ||
usedUrlsFromDb.includes(r.experimentUrl)
) {
return
}
expUrls.push(r.experimentUrl)
})
} catch (error) {
console.log(
`[ERROR] Failed to retrieve data from oTree server/s reason: ${error.message}`,
)
}
}
async function main() {
// Get current server URLs
// const results = await getOtreeUrls(otreeIPs, otreeRestKey)
// console.log(results)
// test create sessions
// await createSession(otreeIPs, otreeRestKey, "DropOutTest", 12)
// end test
}
main()