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

Rebase and fix for Streaming Exports changes #1829

Merged
merged 16 commits into from
Oct 29, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add participant-votes.csv export.
  • Loading branch information
samskivert committed Sep 19, 2024

Verified

This commit was signed with the committer’s verified signature.
sunya-ch Sunyanan Choochotkaew
commit 9063469ef4a0f688522d7848e7baf29f9adc31c0
100 changes: 100 additions & 0 deletions server/src/routes/export.ts
Original file line number Diff line number Diff line change
@@ -200,6 +200,102 @@ async function sendVotesSummary(zid: number, res: Response) {
);
}

async function sendParticipantVotesSummary(zid: number, res: Response) {
// Load up the comment ids
const commentRows = (await pgQueryP_readOnly(
"SELECT tid, pid FROM comments WHERE zid = ($1) ORDER BY tid ASC, created ASC", // TODO: filter only active comments?
[zid]
)) as { tid: number; pid: number }[];
const commentIds = commentRows.map((row) => row.tid);
const participantCommentCounts = new Map<number, number>();
for (const row of commentRows) {
const count = participantCommentCounts.get(row.pid) || 0;
participantCommentCounts.set(row.pid, count + 1);
}

const pca = await getPca(zid);
const groupClusters: { id: number; members: number[] }[] | undefined =
pca?.asPOJO["group-clusters"];
function getGroupId(pid: number) {
if (groupClusters) {
for (const group of groupClusters) {
if (group.members.includes(pid)) {
return group.id;
}
}
}
return undefined;
}

res.setHeader("content-type", "text/csv");
res.write(
[
"participant",
"group-id",
"n-comments",
"n-votes",
"n-agree",
"n-disagree",
...commentIds,
].join(",") + sep
);

// Query the votes in participant order so that we can summarize them in a streaming pass
let currentParticipantId = -1;
const currentParticipantVotes = new Map<number, number>();
function sendCurrentParticipantRow() {
let agrees = 0;
let disagrees = 0;
for (const vote of currentParticipantVotes.values()) {
if (vote === 1) agrees += 1;
else if (vote === -1) disagrees += 1;
}
const values = [
currentParticipantId,
getGroupId(currentParticipantId),
participantCommentCounts.get(currentParticipantId) || 0,
currentParticipantVotes.size,
agrees,
disagrees,
...commentIds.map((tid) => currentParticipantVotes.get(tid)),
];
res.write(
values
.map((value) => (value === undefined ? "" : String(value)))
.join(",") + sep
);
}

stream_pgQueryP_readOnly(
"SELECT pid, tid, vote FROM votes WHERE zid = ($1) ORDER BY pid",
[zid],
(row) => {
const pid: number = row.pid;
if (pid != currentParticipantId) {
if (currentParticipantId != -1) {
sendCurrentParticipantRow();
}
currentParticipantId = pid;
currentParticipantVotes.clear();
}

const tid: number = row.tid;
const vote: number = row.vote;
currentParticipantVotes.set(tid, vote);
},
() => {
if (currentParticipantId != -1) {
sendCurrentParticipantRow();
}
res.end();
},
(error) => {
logger.error("polis_err_report_participant_votes", error);
fail(res, 500, "polis_err_data_export", error);
}
);
}

export async function handle_GET_reportExport(
req: {
p: { rid: string; report_type: string };
@@ -229,6 +325,10 @@ export async function handle_GET_reportExport(
await sendVotesSummary(zid, res);
break;

case "participant-votes.csv":
await sendParticipantVotesSummary(zid, res);
break;

default:
fail(res, 404, "polis_error_data_unknown_report");
break;