Skip to content

Commit

Permalink
adding queue info url path and fix bug with user stuck in redirected …
Browse files Browse the repository at this point in the history
…state
  • Loading branch information
recap committed Jul 12, 2024
1 parent 3f5b2df commit 5a3c941
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 11 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
{
"name": "DropOutTest",
"enabled": true,
"agreementTimeout": 100,
"agreementTimeout": 10000,
"scheduler": {
"type": "GatScheduler",
"params": {
Expand Down
115 changes: 105 additions & 10 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const User = require("./user.js")
const Agreement = require("./agreement.js")
const ClassLoader = require("./class-loader.js")
const UserDb = require("./UserSqliteDb.js")
const { argv0 } = require("process")

require("dotenv").config()
const otreeIPs = process.env.OTREE_IPS.split(",")
Expand Down Expand Up @@ -257,7 +258,9 @@ const validateHmac = (req, res, next) => {
// }

async function getExperimentUrls(experiments) {
const expToEnable = config.experiments.map((e) => e.name)
const expToEnable = config.experiments
.filter((e) => e.enabled)
.map((e) => e.name)
const usedUrlsFromDb = usersDb.getUsedUrls()

// clear existing URLs first:
Expand Down Expand Up @@ -429,7 +432,9 @@ async function main() {
// Load URLs from oTree servers
await getExperimentUrls(experiments)

const expToEnable = config.experiments.map((e) => e.name)
const expToEnable = config.experiments
.filter((e) => e.enabled)
.map((e) => e.name)
// Load schedulers from directory
// initialize returns a new ClassLoader
const SchedulerPlugins = await ClassLoader.initialize("./schedulers")
Expand Down Expand Up @@ -583,7 +588,83 @@ async function main() {
res.status(201).json(experiments)
})

app.get("/info/:experimentId", validatePass, async (req, res) => {
app.get("/info/queues/:experimentId?", validatePass, async (req, res) => {
const experimentId = req.params.experimentId
const experiment = experiments[experimentId]
let htmlString = `<!doctype html><html><title>Queues</title>
<style>th {text-align: left;}</style>
<body><h1>Queues info</h1><table border="1">`
const headerTable = `<tr>
<th>Queue name</th>
<th>No. Of users on Start</th>
<th>No. Of users in Queue</th>
<th>No. of users in Waiting Agreement</th>
<th>No. of users that Agreed</th>
<th>No. of users that Redirected</th>
</tr>`
htmlString += headerTable
let total = 0
let totalQueue = 0
let totalWait = 0
let totalRedirected = 0
let totalAgreed = 0
let totalStart = 0
// res.status(200).send(JSON.stringify(experiments))
// return
const usersFromDb = Array.from(usersDb.values())
Object.values(experiments)
.filter((e) => {
return e.enabled
})
.forEach((e) => {
const usersWaiting = usersFromDb
.filter(
(u) => (u.experimentId == e.name) & (u.state == "waitAgreement"),
)
.map((u) => u.userId)
const usersRedirected = usersFromDb
.filter(
(u) => (u.experimentId == e.name) & (u.state == "inoTreePages"),
)
.map((u) => u.userId)
const usersAgreed = usersFromDb
.filter((u) => (u.experimentId == e.name) & (u.state == "agreed"))
.map((u) => u.userId)
const usersConnected = usersFromDb
.filter(
(u) => (u.experimentId == e.name) & (u.state == "startedPage"),
)
.map((u) => u.userId)
const q = e.scheduler.queue.getQueue()
totalQueue += q.length
totalWait += usersWaiting.length
totalRedirected += usersRedirected.length
totalAgreed += usersAgreed.length
totalStart += usersConnected.length
let row = `<tr>
<td>${e.name}</td>
<td>${usersConnected.length}</td>
<td>${q.length}</td>
<td>${usersWaiting.length}</td>
<td>${usersAgreed.length}</td>
<td>${usersRedirected.length}</td>
</tr>`
htmlString += row
})
let lastRow = `<tr>
<td>TOTALS</td>
<td>${totalStart}</td>
<td>${totalQueue}</td>
<td>${totalWait}</td>
<td>${totalAgreed}</td>
<td>${totalRedirected}</td>
</tr>`
htmlString += `${lastRow}</table></body></html>`

res.status(201).send(htmlString)
})

app.get("/info/sessions/:experimentId", validatePass, async (req, res) => {
const experimentId = req.params.experimentId
const experiment = experiments[experimentId]
if (!experiment) {
Expand Down Expand Up @@ -772,8 +853,18 @@ async function main() {
// console.log(`${userId} in state ${user.state}.`)
return
}
if (user.state == "redirected") {
// Something went wrong here
// Try starting ganme with one user
// user.experimentUrl = expUrl
// user.groupId = agreementId
// user.redirectedUrl = `${expUrl}?participant_label=${user.userId}`
// user.server = server
startGame([user], [user.experimentUrl], null, user.groupId, user.server)

return
}
if (user.state === "inoTreePages") {
//console.log(`User ${userId} already already redirected`)
socket.emit("gameStart", { room: user.redirectedUrl.toString() })
return
}
Expand Down Expand Up @@ -862,8 +953,12 @@ async function main() {
// const compoundKey = `${userId}:${experimentId}`
const compoundKey = userId
const user = usersDb.get(compoundKey)
user.changeState("redirected")
const expUrl = new URL(urls[i])
user.experimentUrl = expUrl
user.groupId = agreementId
user.redirectedUrl = `${expUrl}?participant_label=${user.userId}`
user.server = server
user.changeState("redirected")
// Set user variables on oTree server
// Vars in oTree experiment template are accessed using
// the syntax {{ player.participant.vars.age }} where age is a var
Expand All @@ -872,12 +967,12 @@ async function main() {
function redirect() {
const sock = user.webSocket
// Emit a custom event with the game room URL
user.experimentUrl = expUrl
user.groupId = agreementId
user.redirectedUrl = `${expUrl}?participant_label=${user.userId}`
user.server = server
user.changeState("inoTreePages")
// user.experimentUrl = expUrl
// user.groupId = agreementId
// user.redirectedUrl = `${expUrl}?participant_label=${user.userId}`
// user.server = server
sock.emit("gameStart", { room: user.redirectedUrl })
user.changeState("inoTreePages")
usersDb.upsert(user)
console.log(`Redirecting user ${user.userId} to ${user.redirectedUrl}`)
}
Expand Down

0 comments on commit 5a3c941

Please sign in to comment.