From 4dc6fb5ca46487b75d7a8d3cbbdb0e27caa05fab Mon Sep 17 00:00:00 2001 From: David McFadzean Date: Fri, 20 Dec 2024 10:55:51 -0500 Subject: [PATCH] feat: Added gatekeeper status interval to config (#500) * Added KC_GATEKEEPER_STATUS_INTERVAL * Refactored checkDIDs * Fixed unit test --- docker-compose.yml | 1 + packages/gatekeeper/src/gatekeeper-lib.js | 13 +++++++++++- sample.env | 1 + services/gatekeeper/server/src/config.js | 1 + .../gatekeeper/server/src/gatekeeper-api.js | 16 +++++++++------ tests/gatekeeper.test.js | 20 ++++++++++--------- 6 files changed, 36 insertions(+), 16 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index ed2e589b..6f1e86dd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -25,6 +25,7 @@ services: - KC_GATEKEEPER_DB=${KC_GATEKEEPER_DB} - KC_GATEKEEPER_REGISTRIES=${KC_GATEKEEPER_REGISTRIES} - KC_GATEKEEPER_GC_INTERVAL=${KC_GATEKEEPER_GC_INTERVAL} + - KC_GATEKEEPER_STATUS_INTERVAL=${KC_GATEKEEPER_STATUS_INTERVAL} - KC_MONGODB_URL=mongodb://mongodb:27017 - KC_REDIS_URL=redis://redis:6379 volumes: diff --git a/packages/gatekeeper/src/gatekeeper-lib.js b/packages/gatekeeper/src/gatekeeper-lib.js index f2471f34..1b305a98 100644 --- a/packages/gatekeeper/src/gatekeeper-lib.js +++ b/packages/gatekeeper/src/gatekeeper-lib.js @@ -125,6 +125,8 @@ export async function checkDIDs(options = {}) { const total = dids.length; let n = 0; + let agents = 0; + let assets = 0; let confirmed = 0; let unconfirmed = 0; let ephemeral = 0; @@ -140,6 +142,14 @@ export async function checkDIDs(options = {}) { console.log(`resolved ${n}/${total} ${did} OK`); } + if (doc.mdip.type === 'agent') { + agents += 1; + } + + if (doc.mdip.type === 'asset') { + assets += 1; + } + if (doc.didDocumentMetadata.confirmed) { confirmed += 1; } @@ -165,7 +175,8 @@ export async function checkDIDs(options = {}) { } } - return { total, confirmed, unconfirmed, ephemeral, invalid, byRegistry, byVersion }; + const byType = { agents, assets, confirmed, unconfirmed, ephemeral, invalid }; + return { total, byType, byRegistry, byVersion }; } export async function initRegistries(csvRegistries) { diff --git a/sample.env b/sample.env index 06c694a2..99bfa1f7 100644 --- a/sample.env +++ b/sample.env @@ -11,6 +11,7 @@ KC_GATEKEEPER_DB=json-cache KC_GATEKEEPER_REGISTRIES=hyperswarm,TBTC,TFTC KC_GATEKEEPER_PORT=4224 KC_GATEKEEPER_GC_INTERVAL=60 +KC_GATEKEEPER_STATUS_INTERVAL=5 # Keymaster KC_ENCRYPTED_PASSPHRASE= diff --git a/services/gatekeeper/server/src/config.js b/services/gatekeeper/server/src/config.js index 320c64fc..8517d0d4 100644 --- a/services/gatekeeper/server/src/config.js +++ b/services/gatekeeper/server/src/config.js @@ -7,6 +7,7 @@ const config = { db: process.env.KC_GATEKEEPER_DB || 'redis', registries: process.env.KC_GATEKEEPER_REGISTRIES, gcInterval: process.env.KC_GATEKEEPER_GC_INTERVAL ? parseInt(process.env.KC_GATEKEEPER_GC_INTERVAL) : 15, + statusInterval: process.env.KC_GATEKEEPER_STATUS_INTERVAL ? parseInt(process.env.KC_GATEKEEPER_STATUS_INTERVAL) : 5, }; export default config; diff --git a/services/gatekeeper/server/src/gatekeeper-api.js b/services/gatekeeper/server/src/gatekeeper-api.js index f57da561..82998a6f 100644 --- a/services/gatekeeper/server/src/gatekeeper-api.js +++ b/services/gatekeeper/server/src/gatekeeper-api.js @@ -295,6 +295,14 @@ async function reportStatus() { console.log(`DID Database (${config.db}):`); console.log(` Total: ${status.dids.total}`); + console.log(` By type:`); + console.log(` Agents: ${status.dids.byType.agents}`); + console.log(` Assets: ${status.dids.byType.assets}`); + console.log(` Confirmed: ${status.dids.byType.confirmed}`); + console.log(` Unconfirmed: ${status.dids.byType.unconfirmed}`); + console.log(` Ephemeral: ${status.dids.byType.ephemeral}`); + console.log(` Invalid: ${status.dids.byType.invalid}`); + console.log(` By registry:`); const registries = Object.keys(status.dids.byRegistry).sort(); for (let registry of registries) { @@ -310,10 +318,6 @@ async function reportStatus() { } 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(` RSS: ${formatBytes(status.memoryUsage.rss)} (Resident Set Size - total memory allocated for the process)`); @@ -384,9 +388,9 @@ function formatBytes(bytes) { } async function main() { - console.log(`Starting Gatekeeper with a db (${config.db}) check...`); + console.log(`Starting KeychainMDIP Gatekeeper with a db (${config.db}) check...`); await reportStatus(); - setInterval(reportStatus, 60 * 1000); + setInterval(reportStatus, config.statusInterval * 60 * 1000); console.log(`Starting DID garbage collection in ${config.gcInterval} minutes`); setTimeout(gcLoop, config.gcInterval * 60 * 1000); diff --git a/tests/gatekeeper.test.js b/tests/gatekeeper.test.js index 75abf531..ef57c894 100644 --- a/tests/gatekeeper.test.js +++ b/tests/gatekeeper.test.js @@ -2625,9 +2625,10 @@ 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.byType.agents).toBe(1); + expect(check.byType.assets).toBe(1); + expect(check.byType.ephemeral).toBe(1); + expect(check.byType.invalid).toBe(0); expect(check.byRegistry['local']).toBe(2); expect(check.byVersion[1]).toBe(2); }); @@ -2649,10 +2650,12 @@ describe('checkDIDs', () => { 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.byType.agents).toBe(1); + expect(check.byType.assets).toBe(1); + expect(check.byType.confirmed).toBe(1); + expect(check.byType.unconfirmed).toBe(1); + expect(check.byType.ephemeral).toBe(0); + expect(check.byType.invalid).toBe(0); expect(check.byRegistry['hyperswarm']).toBe(2); expect(check.byVersion[1]).toBe(1); expect(check.byVersion[2]).toBe(1); @@ -2673,8 +2676,7 @@ describe('checkDIDs', () => { const check = await gatekeeper.checkDIDs({ chatty: true, dids }); expect(check.total).toBe(3); - expect(check.ephemeral).toBe(0); - expect(check.invalid).toBe(1); + expect(check.byType.invalid).toBe(1); }); it('should reset a corrupted db', async () => {