Skip to content

Commit

Permalink
feat: As a user, I want to count unique attested subjects (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
alainncls authored Dec 15, 2023
1 parent ea703ab commit 082b63c
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
40 changes: 40 additions & 0 deletions sdk/examples/utils/countUniqueSubjects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import fs from "fs";
import path from "path";
import { VeraxSdk } from "../../src/VeraxSdk";

const BATCH_SIZE = 100000;

const fetchSubjectsFromFile = async (fileSuffix: number): Promise<string[]> => {
console.log(`Reading file #${fileSuffix}...`);
return JSON.parse(fs.readFileSync(path.resolve(__dirname, `../../allSubjects-${fileSuffix}.txt`), "utf8"));
};

async function main() {
const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_MAINNET);
const attestationsNumber = await veraxSdk.utils.getAttestationIdCounter();
const filesNumber = Math.ceil(Number(attestationsNumber) / BATCH_SIZE);

const allSubjects: string[][] = [];
const uniqueSubjects: Set<string> = new Set<string>();

for (let i = 0; i <= filesNumber; i++) {
allSubjects.push(await fetchSubjectsFromFile(i));
}

console.log(`Total subjects = ${allSubjects.reduce((total, subjects) => total + subjects.length, 0)}`);

for (const array of allSubjects) {
for (const subject of array) {
uniqueSubjects.add(subject);
}
}

console.log(`Unique subjects = ${uniqueSubjects.size}`);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
58 changes: 58 additions & 0 deletions sdk/examples/utils/getAllAttestations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { VeraxSdk } from "../../src/VeraxSdk";
import fs from "fs";
import path from "path";

const BATCH_SIZE = 100000;

const fetchAllAttestations = async (batchNumber: number, veraxSdk: VeraxSdk) => {
let skip = 0;
let hasMoreResults = true;
const batch: string[] = [];

while (hasMoreResults && batch.length <= BATCH_SIZE) {
console.log(`Query batch #${skip}`);
const matchingAttestations = await veraxSdk.attestation.findBy(1000, skip * 1000 + batchNumber * BATCH_SIZE);

if (matchingAttestations.length === 0) {
hasMoreResults = false;
continue;
}

const subjects = matchingAttestations.map((attestation) => attestation.subject);

batch.push(...subjects);
skip++;
}

return batch;
};

async function main() {
const veraxSdk = new VeraxSdk(VeraxSdk.DEFAULT_LINEA_MAINNET);
const attestationsNumber = await veraxSdk.utils.getAttestationIdCounter();
const batchesNumber = Math.ceil(Number(attestationsNumber) / BATCH_SIZE);

console.log(
`We expect ${batchesNumber} batches of ${BATCH_SIZE} attestations to get all ${attestationsNumber} attestations.`,
);

for (let i = 0; i <= batchesNumber; i++) {
console.log(`Attestations batch #${i}`);
const subjectsBatch = await fetchAllAttestations(i, veraxSdk);

fs.writeFile(path.resolve(__dirname, `../../allSubjects-${i}.txt`), JSON.stringify(subjectsBatch), function (err) {
if (err) {
return console.log(err);
}

console.log(`File #${i} was saved!`);
});
}
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});

0 comments on commit 082b63c

Please sign in to comment.