Skip to content

Commit

Permalink
fix: fix duplicate in votes CSV report (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e authored May 15, 2024
1 parent 9af8845 commit 19845e7
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions src/lib/votesReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ class VotesReport extends Cache {
};

fetchAllVotes = async () => {
let votes: Vote[] = [];
const votes = new Map<string, Vote>();
let page = 0;
let createdPivot = 0;
const pageSize = 1000;
let resultsSize = 0;
const maxPage = 5;

do {
let newVotes = await fetchVotes(this.id, {
const newVotes = await fetchVotes(this.id, {
first: pageSize,
skip: page * pageSize,
created_gte: createdPivot,
Expand All @@ -78,28 +78,21 @@ class VotesReport extends Cache {
});
resultsSize = newVotes.length;

if (page === 0 && createdPivot > 0) {
// Loosely assuming that there will never be more than 1000 duplicates
const existingIpfs = votes.slice(-pageSize).map(vote => vote.ipfs);

newVotes = newVotes.filter(vote => {
return !existingIpfs.includes(vote.ipfs);
});
}

if (page === maxPage) {
page = 0;
createdPivot = newVotes[newVotes.length - 1].created;
} else {
page++;
}

votes = votes.concat(newVotes);
for (const vote of newVotes) {
votes.set(vote.ipfs, vote);
}

this.generationProgress = +((votes.length / this.proposal!.votes) * 100).toFixed(2);
this.generationProgress = +((votes.size / this.proposal!.votes) * 100).toFixed(2);
} while (resultsSize === pageSize);

return votes;
return Array.from(votes.values());
};

toString() {
Expand Down

0 comments on commit 19845e7

Please sign in to comment.