// Adds N synthetic responses to the pain survey via LimeSurvey JSON-RPC and records their IDs. // Usage: node fake_responses.mjs add [N=50] | node fake_responses.mjs remove // Env: LS_URL (default http://:8080), LS_SID (218863), LS_USER/LS_PASSWORD or LS_ENV_FILE (default ~/.config/kelademot/limesurvey.env) import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const here = path.dirname(fileURLToPath(import.meta.url)); const envFile = process.env.LS_ENV_FILE || path.join(os.homedir(), '.config/kelademot/limesurvey.env'); const env = fs.existsSync(envFile) ? Object.fromEntries(fs.readFileSync(envFile, 'utf8').split('\n').filter(Boolean).map(l => l.split(/=(.*)/s).slice(0, 2))) : {}; const URL = process.env.LS_URL || 'http://:8080', SID = +(process.env.LS_SID || 218863); const USER = process.env.LS_USER || env.ADMIN_USER, PASS = process.env.LS_PASSWORD || env.ADMIN_PASSWORD; const IDS = path.join(here, 'fake_response_ids.json'); const rpc = async (m, p) => { const r = await (await fetch(URL + '/index.php/admin/remotecontrol', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ method: m, params: p, id: 1 }) })).json(); if (r.error) throw new Error(m + ': ' + JSON.stringify(r.error)); return r.result; }; // --- seeded RNG so the dataset is reproducible let seed = 20261118; const rnd = () => { seed |= 0; seed = seed + 0x6D2B79F5 | 0; let t = Math.imul(seed ^ seed >>> 15, 1 | seed); t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t; return ((t ^ t >>> 14) >>> 0) / 4294967296; }; const norm = () => { let u = 0, v = 0; while (!u) u = rnd(); v = rnd(); return Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v); }; const clamp = (x, a, b) => Math.max(a, Math.min(b, x)); const pick = (pairs) => { const tot = pairs.reduce((s, p) => s + p[1], 0); let r = rnd() * tot; for (const [v, w] of pairs) { if ((r -= w) <= 0) return v; } return pairs[0][0]; }; const REGION_P = { FHEAD: .38, FNECK: .30, FRSHO: .32, FLSHO: .30, FCHEST: .42, FABDO: .40, FPELV: .08, FRARM: .07, FLARM: .07, FRFORE: .06, FLFORE: .06, FRHAND: .10, FLHAND: .09, FRTHIGH: .04, FLTHIGH: .04, FRKNEE: .04, FLKNEE: .04, FRSHIN: .03, FLSHIN: .03, FRFOOT: .03, FLFOOT: .03, BHEAD: .20, BNECK: .42, BRSHO: .30, BLSHO: .30, BUPPER: .46, BLOWER: .34, BGLUTE: .06, BRARM: .05, BLARM: .05, BRTHIGH: .04, BLTHIGH: .04, BRCALF: .03, BLCALF: .03, BRHEEL: .03, BLHEEL: .03 }; const QUAL = { THROB: 1.1, SHOOT: .6, STAB: .5, SHARP: .8, CRAMP: 1.0, BURN: .8, ACHE: 1.3, HEAVY: 1.6, TENDER: .9, TIRING: 1.9, FEAR: 1.1 }; const RELIEF_P = { HANDS: .55, TALK: .5, GUIDE: .45, VALID: .5, COFFEE: .35, REST: .3 }; const WORDS = { fi: ['Ukkospilvi, joka ei koskaan tule perille.', 'Sumu: ei näe eteensä mutta kävellä täytyy.', 'Tyyni mutta paineinen ilma ennen myrskyä.', 'Kevyt tihkusade, ärsyttävä mutta ei vaarallinen.', 'Syysmyrsky, joka kaataa vanhoja puita.', 'Aurinkoinen päivä, jonka jälkeen tulee pakkanen.', 'Ei mitään erityistä, lähinnä uteliaisuutta.', 'Rankka kuuro, joka menee nopeasti ohi.', 'Hiljainen lumisade: kaunista, mutta peittää polut.', 'Kesäsää, jossa kaikki on mahdollista.'], en: ['A thunderstorm that never quite arrives.', 'Fog: you cannot see ahead, but you still have to walk.', 'The still, heavy air before a storm.', 'A light drizzle, annoying but harmless.', 'An autumn gale that fells old trees.', 'A sunny day followed by a hard frost.', 'Nothing special, mostly curiosity.', 'A heavy shower that passes quickly.', 'Quiet snowfall: beautiful, but it covers the paths.', 'A summer day when anything is possible.'] }; async function add(N) { const k = await rpc('get_session_key', [USER, PASS]); const fm = await rpc('get_fieldmap', [k, SID]); const F = {}; // F[title][aid||''] = fieldname for (const f of Object.values(fm)) if (f.title) { (F[f.title] ||= {})[f.aid || ''] = f.fieldname; } const ids = fs.existsSync(IDS) ? JSON.parse(fs.readFileSync(IDS, 'utf8')) : []; for (let i = 0; i < N; i++) { const r = {}; const lang = rnd() < .6 ? 'fi' : 'en'; const use = pick([['NEVER', 1], ['FEW', 2], ['WEEK', 3], ['DAILY', 3], ['BUILD', 1.2]]); const exp = pick([['A', 1], ['B', 2], ['C', 2.2], ['D', 2], ['E', 1.2]]); const role = pick([['QUAL', 22], ['QUANT', 20], ['METH', 20], ['DATA', 15], ['STUD', 10], ['MGR', 8]]); // latent distress: more experience and less hands-on use => higher let d = norm() * 0.9 + ({ NEVER: .6, FEW: .3, WEEK: 0, DAILY: -.3, BUILD: -.6 })[use] + ({ A: -.1, B: 0, C: .1, D: .25, E: .35 })[exp] + (role === 'STUD' ? -.2 : 0) + .15; const none = rnd() < clamp(.2 - .09 * d, .03, .45); // body map const bm = F.BODYMAP; if (none) { r[bm.NOSENS] = 'Y'; } else { const m = clamp(.65 + .35 * d, .25, 1.6); let n = 0; for (const [c, p] of Object.entries(REGION_P)) if (rnd() < clamp(p * m, 0, .9)) { r[bm[c]] = 'Y'; n++; } if (!n) r[bm[pick([['FCHEST', 2], ['FHEAD', 2], ['BNECK', 2], ['FABDO', 1]])]] = 'Y'; } // intensity const now = none ? Math.round(clamp(Math.abs(norm()) * .5, 0, 2)) : Math.round(clamp(3.4 + 1.6 * d + norm() * .9, 0, 10)); const avg = Math.round(clamp(now + norm() * 1.2 + (none ? 0 : 0.3), 0, 10)); const worst = Math.round(clamp(Math.max(now, avg) + (none ? Math.abs(norm()) : 1 + Math.abs(norm()) * 1.6), 0, 10)); r[F.NRS.NOW] = String(now); r[F.NRS.AVG] = String(avg); r[F.NRS.WORST] = String(worst); if (!none) { for (const [c, mu] of Object.entries(QUAL)) r[F.QUALITY[c]] = String(Math.round(clamp(mu + .45 * d + norm() * .8, 0, 3))); const base = avg * .55; for (const [c, off] of Object.entries({ ACTIV: 0, MOOD: .4, CONC: .8, SLEEP: -.6, ENJOY: .2 })) r[F.INTERF[c]] = String(Math.round(clamp(base + off + norm() * 1.4, 0, 10))); } // catastrophizing 0-4 const pc = { RUMIN: .2, MAGNI: -.3, HELPL: -.1 }; for (const [c, off] of Object.entries(pc)) r[F.PCS[c]] = String(Math.round(clamp(1.6 + .75 * d + off + norm() * .7 - (use === 'BUILD' ? .3 : 0), 0, 4))); // relief if (rnd() < (use === 'NEVER' ? .22 : .07)) r[F.RELIEF.NOTHING] = 'Y'; else { let any = false; for (const [c, p] of Object.entries(RELIEF_P)) { const q = clamp(p + (c === 'HANDS' ? ({ NEVER: -.3, FEW: -.1, WEEK: .1, DAILY: .2, BUILD: .3 })[use] : 0), .05, .95); if (rnd() < q) { r[F.RELIEF[c]] = 'Y'; any = true; } } if (!any) r[F.RELIEF.TALK] = 'Y'; } if (rnd() < .6) r[F.WORDS['']] = WORDS[lang][Math.floor(rnd() * WORDS[lang].length)]; r[F.ROLE['']] = role; r[F.EXP['']] = exp; r[F.USE['']] = use; // meta: spread over the last 3 days const ts = new Date(Date.now() - rnd() * 3 * 864e5); const fmt = d => d.toISOString().slice(0, 19).replace('T', ' '); r.submitdate = fmt(ts); r.startdate = fmt(new Date(ts - (150 + rnd() * 300) * 1000)); r.datestamp = r.submitdate; r.startlanguage = lang; r.lastpage = 5; const id = await rpc('add_response', [k, SID, r]); ids.push(+id); } fs.writeFileSync(IDS, JSON.stringify(ids)); console.log(`added ${N}; tracked ids: ${ids.length}`); await rpc('release_session_key', [k]); } async function remove() { const ids = fs.existsSync(IDS) ? JSON.parse(fs.readFileSync(IDS, 'utf8')) : []; const k = await rpc('get_session_key', [USER, PASS]); for (const id of ids) await rpc('delete_response', [k, SID, id]); fs.unlinkSync(IDS); console.log(`removed ${ids.length} fake responses`); await rpc('release_session_key', [k]); } const [cmd, n] = process.argv.slice(2); if (cmd === 'add') await add(+(n || 50)); else if (cmd === 'remove') await remove(); else console.log('usage: add [N] | remove');