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

feat: Added db and versions to status #497

Merged
merged 4 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
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
23 changes: 16 additions & 7 deletions packages/gatekeeper/src/gatekeeper-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,12 @@ export async function checkDIDs(options = {}) {

const total = dids.length;
let n = 0;
let confirmed = 0;
let unconfirmed = 0;
let ephemeral = 0;
let invalid = 0;
const byRegistry = {};
const byVersion = {};

for (const did of dids) {
n += 1;
Expand All @@ -137,16 +140,22 @@ export async function checkDIDs(options = {}) {
console.log(`resolved ${n}/${total} ${did} OK`);
}

if (doc.didDocumentMetadata.confirmed) {
confirmed += 1;
}
else {
unconfirmed += 1;
}

if (doc.mdip.validUntil) {
ephemeral += 1;
}

if (byRegistry[doc.mdip.registry]) {
byRegistry[doc.mdip.registry] += 1;
}
else {
byRegistry[doc.mdip.registry] = 1;
}
const registry = doc.mdip.registry;
byRegistry[registry] = (byRegistry[registry] || 0) + 1;

const version = doc.didDocumentMetadata.version;
byVersion[version] = (byVersion[version] || 0) + 1;
}
catch (error) {
invalid += 1;
Expand All @@ -156,7 +165,7 @@ export async function checkDIDs(options = {}) {
}
}

return { total, ephemeral, invalid, byRegistry };
return { total, confirmed, unconfirmed, ephemeral, invalid, byRegistry, byVersion };
}

export async function initRegistries(csvRegistries) {
Expand Down
21 changes: 18 additions & 3 deletions services/gatekeeper/server/src/gatekeeper-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ async function gcLoop() {
try {
const response = await gatekeeper.verifyDb();
console.log(`DID garbage collection: ${JSON.stringify(response)} waiting ${config.gcInterval} minutes...`);
await checkDids();
}
catch (error) {
console.error(`Error in DID garbage collection: ${error}`);
Expand Down Expand Up @@ -291,16 +292,30 @@ async function reportStatus() {

console.log('Status -----------------------------');

console.log('DID Database:');
console.log(`DID Database (${config.db}):`);
console.log(` Total: ${status.dids.total}`);

console.log(` By registry:`);
for (let registry in status.dids.byRegistry) {
const registries = Object.keys(status.dids.byRegistry).sort();
for (let registry of registries) {
console.log(` ${registry}: ${status.dids.byRegistry[registry]}`);
}

console.log(` By version:`);
let count = 0;
for (let version of [1, 2, 3, 4, 5]) {
const num = status.dids.byVersion[version];
console.log(` ${version}: ${num}`);
count += num;
}
console.log(` 6+: ${status.dids.total - count}`);

console.log(` Confirmed: ${status.dids.confirmed}`);
console.log(` Unconfirmed: ${status.dids.unconfirmed}`);
console.log(` Ephemeral: ${status.dids.ephemeral}`);
console.log(` Invalid: ${status.dids.invalid}`);

console.log('Memory Usage Report:');
console.log(`Memory Usage Report:`);
console.log(` RSS: ${formatBytes(status.memoryUsage.rss)} (Resident Set Size - total memory allocated for the process)`);
console.log(` Heap Total: ${formatBytes(status.memoryUsage.heapTotal)} (Total heap allocated)`);
console.log(` Heap Used: ${formatBytes(status.memoryUsage.heapUsed)} (Heap actually used)`);
Expand Down
31 changes: 30 additions & 1 deletion tests/gatekeeper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2613,7 +2613,7 @@ describe('checkDIDs', () => {
mockFs.restore();
});

it('should check all DIDs in db', async () => {
it('should check all DIDs', async () => {
mockFs({});

const keypair = cipher.generateRandomJwk();
Expand All @@ -2625,8 +2625,37 @@ describe('checkDIDs', () => {
const check = await gatekeeper.checkDIDs({ chatty: true });

expect(check.total).toBe(2);
expect(check.confirmed).toBe(2);
expect(check.ephemeral).toBe(1);
expect(check.invalid).toBe(0);
expect(check.byRegistry['local']).toBe(2);
expect(check.byVersion[1]).toBe(2);
});

it('should report unconfirmed DIDs', async () => {
mockFs({});

const keypair = cipher.generateRandomJwk();
const agentOp = await createAgentOp(keypair, 1, 'hyperswarm');
const agentDID = await gatekeeper.createDID(agentOp);
const assetOp = await createAssetOp(agentDID, keypair, 'hyperswarm');
const assetDID = await gatekeeper.createDID(assetOp);
const doc = await gatekeeper.resolveDID(assetDID);
doc.didDocumentData = { mock: 1 };
const updateOp = await createUpdateOp(keypair, assetDID, doc);
const ok = await gatekeeper.updateDID(updateOp);

const check = await gatekeeper.checkDIDs({ chatty: true });

expect(ok).toBe(true);
expect(check.total).toBe(2);
expect(check.confirmed).toBe(1);
expect(check.unconfirmed).toBe(1);
expect(check.ephemeral).toBe(0);
expect(check.invalid).toBe(0);
expect(check.byRegistry['hyperswarm']).toBe(2);
expect(check.byVersion[1]).toBe(1);
expect(check.byVersion[2]).toBe(1);
});

it('should report invalid DIDs', async () => {
Expand Down
Loading