Skip to content

Commit

Permalink
add /:num endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
danvk committed Sep 22, 2024
1 parent e122045 commit ba8cd25
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions localturk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const staticDir = options['staticDir'] || path.dirname(templateFile);
type Task = {[key: string]: string};
let flash = ''; // this is used to show warnings in the web UI.

async function renderTemplate({task, numCompleted, numTotal}: TaskStats) {
async function renderTemplate({task, numCompleted, rowNumber, numTotal}: TaskStats) {
const template = await fs.readFile(templateFile, {encoding: 'utf8'});
const fullDict: Record<string, string> = {};
for (const k in task) {
Expand All @@ -92,6 +92,7 @@ async function renderTemplate({task, numCompleted, numTotal}: TaskStats) {
for (var [k, v] of Object.entries(options.var)) {
fullDict[k] = utils.htmlEntities(v as string);
}
fullDict['ROW_NUMBER'] = String(rowNumber);
const userHtml = utils.renderTemplate(template, fullDict);

const thisFlash = flash;
Expand Down Expand Up @@ -123,6 +124,7 @@ async function renderTemplate({task, numCompleted, numTotal}: TaskStats) {
el.click();
}
});
// This is row number ${rowNumber}; visit /${rowNumber} to come back to it.
</script>
</body>
</html>
Expand Down Expand Up @@ -156,6 +158,7 @@ async function checkTaskOutput(task: Task) {
interface TaskStats {
task?: Task;
numCompleted: number;
rowNumber: number;
numTotal: number;
}

Expand All @@ -173,20 +176,52 @@ async function getNextTask(): Promise<TaskStats> {
continue;
}

task.ROW_NUMBER = String(numTotal - 1);
if (sampler) {
sampler.pushSome(task);
} else {
nextTask = task;
}
}

const task = sampler ? sampler[0] : nextTask;
let rowNumber = 0;
if (task) {
rowNumber = Number(task.ROW_NUMBER);
delete task.ROW_NUMBER;
}
return {
task: sampler ? sampler[0] : nextTask,
task,
numCompleted: _.size(completedTasks),
rowNumber,
numTotal,
}
}

async function getTaskNum(n: number): Promise<TaskStats> {
const completedTasks = await readCompletedTasks(); // just getting the count.
let i = 0;
let numTotal = 0;
let taskN;;
for await (const task of csv.readRowObjects(tasksFile)) {
task.ROW_NUMBER = String(numTotal + 1);
numTotal++;
if (i === n) {
taskN = task;
}
i++;
}
if (taskN) {
return {
task: taskN,
numCompleted: _.size(completedTasks),
rowNumber: n,
numTotal,
}
}
throw new Error('Task not found');
}

const app = express();
app.use(errorhandler());
app.use(express.json({limit: "50mb"}));
Expand All @@ -205,6 +240,12 @@ app.get('/', utils.wrapPromise(async (req, res) => {
}
}));

app.get('/:num(\\d+)', utils.wrapPromise(async (req, res) => {
const task = await getTaskNum(parseInt(req.params.num));
const html = await renderTemplate(task);
res.send(html);
}));

app.post('/submit', utils.wrapPromise(async (req, res) => {
const task: Task = req.body;
await csv.appendRow(outputsFile, task);
Expand Down

0 comments on commit ba8cd25

Please sign in to comment.