Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /:num endpoint #46

Merged
merged 2 commits into from
Sep 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 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,51 @@ 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)) {
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 +239,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