From 7a424f57aa7e558a5a1457c1a70339e803a7450a Mon Sep 17 00:00:00 2001 From: jullia02 Date: Thu, 24 Oct 2024 16:30:07 -0400 Subject: [PATCH 1/5] Added iroh tool and monitoring function. --- iroh-monitor.js | 34 ++++++++++++++++++++++++++++++++++ src/start.js | 22 +++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 iroh-monitor.js diff --git a/iroh-monitor.js b/iroh-monitor.js new file mode 100644 index 0000000000..cf56ab095d --- /dev/null +++ b/iroh-monitor.js @@ -0,0 +1,34 @@ +'use strict'; + +require('iroh'); + +// Asked co-pilot for a function to monitor the execution time of a function and log it to the console. +function monitorFunction(fn, name) { + console.log(`Setting up monitoring for ${name}`); + + return async function monitored(...args) { + console.log(`[START] ${name}`); + const start = process.hrtime(); + + try { + const result = await fn.apply(this, args); + const [seconds, nanoseconds] = process.hrtime(start); + const milliseconds = (seconds * 1000) + (nanoseconds / 1000000); + + console.log(`[END] ${name} took ${milliseconds.toFixed(2)}ms`); + + if (milliseconds > 100) { + console.warn(`[SLOW] ${name} took ${milliseconds.toFixed(2)}ms`); + } + + return result; + } catch (error) { + console.error(`[ERROR] ${name}:`, error); + throw error; + } + }; +} + +module.exports = { + monitorFunction: monitorFunction, +}; diff --git a/src/start.js b/src/start.js index cba1fc8f9c..c2249c2159 100644 --- a/src/start.js +++ b/src/start.js @@ -2,15 +2,35 @@ const nconf = require('nconf'); const winston = require('winston'); +const irohMonitor = require('../iroh-monitor'); const start = module.exports; const db = require('./database'); const Topics = require('./topics'); +const User = require('./user'); +const Posts = require('./posts'); start.start = async function () { - printStartupInfo(); + console.log('Initializing Iroh monitoring...'); + + try { + // I chose to monitor core NodeBB functions such as creating new/replying to topics, posts, and users. + if (Topics.create) Topics.create = irohMonitor.monitorFunction(Topics.create, 'Topics.create'); + if (Topics.post) Topics.post = irohMonitor.monitorFunction(Topics.post, 'Topics.post'); + if (Topics.reply) Topics.reply = irohMonitor.monitorFunction(Topics.reply, 'Topics.reply'); + + if (Posts.create) Posts.create = irohMonitor.monitorFunction(Posts.create, 'Posts.create'); + if (Posts.edit) Posts.edit = irohMonitor.monitorFunction(Posts.edit, 'Posts.edit'); + + if (User.create) User.create = irohMonitor.monitorFunction(User.create, 'User.create'); + console.log('NodeBB core functions are now set up for monitoring!!'); + } catch (err) { + console.error('Failed to initialize monitoring...', err); + } + + printStartupInfo(); addProcessHandlers(); try { From b3207f85f8fe2d563c8d2cd9ed9f40d854ac564e Mon Sep 17 00:00:00 2001 From: jullia02 Date: Thu, 24 Oct 2024 16:34:39 -0400 Subject: [PATCH 2/5] Added output file and iroh folder. Fixed imports. --- iroh-monitor.js => iroh/iroh-monitor.js | 0 iroh/iroh-output.txt | 48 +++++++++++++++++++++++++ src/start.js | 2 +- 3 files changed, 49 insertions(+), 1 deletion(-) rename iroh-monitor.js => iroh/iroh-monitor.js (100%) create mode 100644 iroh/iroh-output.txt diff --git a/iroh-monitor.js b/iroh/iroh-monitor.js similarity index 100% rename from iroh-monitor.js rename to iroh/iroh-monitor.js diff --git a/iroh/iroh-output.txt b/iroh/iroh-output.txt new file mode 100644 index 0000000000..7f7bfe1a5f --- /dev/null +++ b/iroh/iroh-output.txt @@ -0,0 +1,48 @@ +(base) jullia@jullias-air nodebb-f24-sweethearts % node app.js +2024-10-24T20:01:42.200Z [4567/23174] - info: NodeBB v3.8.4 Copyright (C) 2013-2024 NodeBB Inc. +2024-10-24T20:01:42.201Z [4567/23174] - info: This program comes with ABSOLUTELY NO WARRANTY. +2024-10-24T20:01:42.201Z [4567/23174] - info: This is free software, and you are welcome to redistribute it under certain conditions. +2024-10-24T20:01:42.201Z [4567/23174] - info: +Initializing Iroh monitoring... +Setting up monitoring for Topics.create +Setting up monitoring for Topics.post +Setting up monitoring for Topics.reply +Setting up monitoring for Posts.create +Setting up monitoring for Posts.edit +Setting up monitoring for User.create +Setting up monitoring for User.auth.logAttempt +NodeBB core functions monitoring initialized +2024-10-24T20:01:42.758Z [4567/23174] - info: Initializing NodeBB v3.8.4 http://localhost:4567 +(node:23174) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead. +(Use `node --trace-deprecation ...` to show where the warning was created) +2024-10-24T20:01:42.962Z [4567/23174] - info: [socket.io] Restricting access to origin: http://localhost:* +2024-10-24T20:01:43.108Z [4567/23174] - info: [api] Adding 0 route(s) to `api/v3/plugins` +2024-10-24T20:01:43.110Z [4567/23174] - info: [router] Routes added +Tag "homework" successfully created! +Tag "assignment" successfully created! +2024-10-24T20:01:43.113Z [4567/23174] - info: 🎉 NodeBB Ready +2024-10-24T20:01:43.113Z [4567/23174] - info: 🤝 Enabling 'trust proxy' +2024-10-24T20:01:43.114Z [4567/23174] - info: 📡 NodeBB is now listening on: 0.0.0.0:4567 +2024-10-24T20:01:43.114Z [4567/23174] - info: 🔗 Canonical URL: http://localhost:4567 +WARNING: The keyword 'none' must be used as a single argument. + ../../../bootstrap/scss/mixins/_box-shadow.scss 10:9 box-shadow() + ../../../bootstrap/scss/forms/_form-control.scss 40:7 @import + bootstrap/scss/_forms.scss 3:9 @import + - 19:9 root stylesheet + +WARNING: The keyword 'none' must be used as a single argument. + ../../../bootstrap/scss/mixins/_box-shadow.scss 10:9 box-shadow() + ../../../bootstrap/scss/forms/_form-select.scss 32:7 @import + bootstrap/scss/_forms.scss 4:9 @import + - 19:9 root stylesheet + +[START] Topics.post +[START] Topics.create +[END] Topics.create took 2.80ms +[START] Posts.create +[END] Posts.create took 7.82ms +[END] Topics.post took 24.96ms +[START] Topics.reply +[START] Posts.create +[END] Posts.create took 4.04ms +[END] Topics.reply took 12.64ms \ No newline at end of file diff --git a/src/start.js b/src/start.js index c2249c2159..07def26e00 100644 --- a/src/start.js +++ b/src/start.js @@ -2,7 +2,7 @@ const nconf = require('nconf'); const winston = require('winston'); -const irohMonitor = require('../iroh-monitor'); +const irohMonitor = require('../iroh/iroh-monitor'); const start = module.exports; From f5c6bbad414a6b69c518d50ccd1582457d16dabf Mon Sep 17 00:00:00 2001 From: jullia02 Date: Thu, 24 Oct 2024 16:38:10 -0400 Subject: [PATCH 3/5] Added Iroh to package.json --- install/package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/install/package.json b/install/package.json index cb5eb4e4ea..579928e3d4 100644 --- a/install/package.json +++ b/install/package.json @@ -76,6 +76,7 @@ "html-to-text": "9.0.5", "imagesloaded": "5.0.0", "ipaddr.js": "2.2.0", + "iroh": "^0.3.0", "jquery": "3.7.1", "jquery-deserialize": "2.0.0", "jquery-form": "4.3.0", From b2326f70ed635089033a1f286e35ea98f8c07f77 Mon Sep 17 00:00:00 2001 From: jullia02 Date: Thu, 24 Oct 2024 17:08:38 -0400 Subject: [PATCH 4/5] Fixed iroh functionality. --- iroh/iroh-monitor.js | 75 ++++++++++++++++++++++++++++---------------- iroh/iroh-output.txt | 49 ++++++++++++----------------- src/start.js | 24 +++++--------- 3 files changed, 76 insertions(+), 72 deletions(-) diff --git a/iroh/iroh-monitor.js b/iroh/iroh-monitor.js index cf56ab095d..68d35c8d0b 100644 --- a/iroh/iroh-monitor.js +++ b/iroh/iroh-monitor.js @@ -1,34 +1,55 @@ 'use strict'; +const Iroh = require('iroh'); -require('iroh'); - -// Asked co-pilot for a function to monitor the execution time of a function and log it to the console. +// Asked help from co-pilot for this function as I needed to simplify parsing for Iroh. function monitorFunction(fn, name) { - console.log(`Setting up monitoring for ${name}`); - - return async function monitored(...args) { - console.log(`[START] ${name}`); - const start = process.hrtime(); - - try { - const result = await fn.apply(this, args); - const [seconds, nanoseconds] = process.hrtime(start); - const milliseconds = (seconds * 1000) + (nanoseconds / 1000000); - - console.log(`[END] ${name} took ${milliseconds.toFixed(2)}ms`); - - if (milliseconds > 100) { - console.warn(`[SLOW] ${name} took ${milliseconds.toFixed(2)}ms`); - } - - return result; - } catch (error) { - console.error(`[ERROR] ${name}:`, error); - throw error; - } - }; + console.log(`Setting up Iroh monitoring for ${name}`); + + const stage = new Iroh.Stage(` + function monitored() { + try { + return target(); + } catch(e) { + console.error(e); + throw e; + } + } + `); + + // Using Iroh's listeners + stage.addListener(Iroh.CALL) + .on('before', (e) => { + console.log(`[IROH] ${name} called`); + e.setData('time', process.hrtime()); + }) + .on('after', (e) => { + const diff = process.hrtime(e.getData('time')); + const time = (diff[0] * 1e9 + diff[1]) / 1e6; + console.log(`[IROH] ${name} took ${time}ms`); + }); + + // Return the original function with timing + return async function(...args) { + const start = process.hrtime(); + console.log(`[START] ${name}`); + + try { + const result = await fn.apply(this, args); + const [s, ns] = process.hrtime(start); + const ms = (s * 1000) + (ns / 1000000); + console.log(`[TIME] ${name} took ${ms.toFixed(2)}ms`); + console.log(`[END] ${name}`); + return result; + } catch (error) { + console.error(`[ERROR] ${name}:`, error); + throw error; + } + }; } module.exports = { - monitorFunction: monitorFunction, + monitorFunction: monitorFunction, + CALL: Iroh.CALL, + FUNCTION: Iroh.FUNCTION, + TRY: Iroh.TRY }; diff --git a/iroh/iroh-output.txt b/iroh/iroh-output.txt index 7f7bfe1a5f..f021790e47 100644 --- a/iroh/iroh-output.txt +++ b/iroh/iroh-output.txt @@ -1,29 +1,24 @@ (base) jullia@jullias-air nodebb-f24-sweethearts % node app.js -2024-10-24T20:01:42.200Z [4567/23174] - info: NodeBB v3.8.4 Copyright (C) 2013-2024 NodeBB Inc. -2024-10-24T20:01:42.201Z [4567/23174] - info: This program comes with ABSOLUTELY NO WARRANTY. -2024-10-24T20:01:42.201Z [4567/23174] - info: This is free software, and you are welcome to redistribute it under certain conditions. -2024-10-24T20:01:42.201Z [4567/23174] - info: +2024-10-24T21:00:25.995Z [4567/64147] - info: NodeBB v3.8.4 Copyright (C) 2013-2024 NodeBB Inc. +2024-10-24T21:00:25.996Z [4567/64147] - info: This program comes with ABSOLUTELY NO WARRANTY. +2024-10-24T21:00:25.996Z [4567/64147] - info: This is free software, and you are welcome to redistribute it under certain conditions. +2024-10-24T21:00:25.997Z [4567/64147] - info: Initializing Iroh monitoring... -Setting up monitoring for Topics.create -Setting up monitoring for Topics.post -Setting up monitoring for Topics.reply -Setting up monitoring for Posts.create -Setting up monitoring for Posts.edit -Setting up monitoring for User.create -Setting up monitoring for User.auth.logAttempt -NodeBB core functions monitoring initialized -2024-10-24T20:01:42.758Z [4567/23174] - info: Initializing NodeBB v3.8.4 http://localhost:4567 -(node:23174) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead. +Setting up Iroh monitoring for Topics.create +Setting up Iroh monitoring for Topics.reply +Iroh monitoring successfully initialized!! +2024-10-24T21:00:26.685Z [4567/64147] - info: Initializing NodeBB v3.8.4 http://localhost:4567 +(node:64147) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead. (Use `node --trace-deprecation ...` to show where the warning was created) -2024-10-24T20:01:42.962Z [4567/23174] - info: [socket.io] Restricting access to origin: http://localhost:* -2024-10-24T20:01:43.108Z [4567/23174] - info: [api] Adding 0 route(s) to `api/v3/plugins` -2024-10-24T20:01:43.110Z [4567/23174] - info: [router] Routes added +2024-10-24T21:00:26.892Z [4567/64147] - info: [socket.io] Restricting access to origin: http://localhost:* +2024-10-24T21:00:27.004Z [4567/64147] - info: [api] Adding 0 route(s) to `api/v3/plugins` +2024-10-24T21:00:27.007Z [4567/64147] - info: [router] Routes added Tag "homework" successfully created! Tag "assignment" successfully created! -2024-10-24T20:01:43.113Z [4567/23174] - info: 🎉 NodeBB Ready -2024-10-24T20:01:43.113Z [4567/23174] - info: 🤝 Enabling 'trust proxy' -2024-10-24T20:01:43.114Z [4567/23174] - info: 📡 NodeBB is now listening on: 0.0.0.0:4567 -2024-10-24T20:01:43.114Z [4567/23174] - info: 🔗 Canonical URL: http://localhost:4567 +2024-10-24T21:00:27.011Z [4567/64147] - info: 🎉 NodeBB Ready +2024-10-24T21:00:27.012Z [4567/64147] - info: 🤝 Enabling 'trust proxy' +2024-10-24T21:00:27.013Z [4567/64147] - info: 📡 NodeBB is now listening on: 0.0.0.0:4567 +2024-10-24T21:00:27.013Z [4567/64147] - info: 🔗 Canonical URL: http://localhost:4567 WARNING: The keyword 'none' must be used as a single argument. ../../../bootstrap/scss/mixins/_box-shadow.scss 10:9 box-shadow() ../../../bootstrap/scss/forms/_form-control.scss 40:7 @import @@ -36,13 +31,9 @@ WARNING: The keyword 'none' must be used as a single argument. bootstrap/scss/_forms.scss 4:9 @import - 19:9 root stylesheet -[START] Topics.post [START] Topics.create -[END] Topics.create took 2.80ms -[START] Posts.create -[END] Posts.create took 7.82ms -[END] Topics.post took 24.96ms +[TIME] Topics.create took 2.12ms +[END] Topics.create [START] Topics.reply -[START] Posts.create -[END] Posts.create took 4.04ms -[END] Topics.reply took 12.64ms \ No newline at end of file +[TIME] Topics.reply took 21.73ms +[END] Topics.reply \ No newline at end of file diff --git a/src/start.js b/src/start.js index 07def26e00..dd9bdc5e9e 100644 --- a/src/start.js +++ b/src/start.js @@ -8,26 +8,18 @@ const start = module.exports; const db = require('./database'); const Topics = require('./topics'); -const User = require('./user'); -const Posts = require('./posts'); start.start = async function () { - console.log('Initializing Iroh monitoring...'); - try { - // I chose to monitor core NodeBB functions such as creating new/replying to topics, posts, and users. - if (Topics.create) Topics.create = irohMonitor.monitorFunction(Topics.create, 'Topics.create'); - if (Topics.post) Topics.post = irohMonitor.monitorFunction(Topics.post, 'Topics.post'); - if (Topics.reply) Topics.reply = irohMonitor.monitorFunction(Topics.reply, 'Topics.reply'); - - if (Posts.create) Posts.create = irohMonitor.monitorFunction(Posts.create, 'Posts.create'); - if (Posts.edit) Posts.edit = irohMonitor.monitorFunction(Posts.edit, 'Posts.edit'); - - if (User.create) User.create = irohMonitor.monitorFunction(User.create, 'User.create'); - - console.log('NodeBB core functions are now set up for monitoring!!'); + console.log('Initializing Iroh monitoring...'); + + // I chose to monitor core nodeBB topic functions (create and reply). + Topics.create = irohMonitor.monitorFunction(Topics.create, 'Topics.create'); + Topics.reply = irohMonitor.monitorFunction(Topics.reply, 'Topics.reply'); + + console.log('Iroh monitoring successfully initialized!!'); } catch (err) { - console.error('Failed to initialize monitoring...', err); + console.error('Failed to initialize Iroh monitoring:', err); } printStartupInfo(); From d37153a4da3521c4b9f86c685e5619d56adfada8 Mon Sep 17 00:00:00 2001 From: jullia02 Date: Thu, 24 Oct 2024 17:12:30 -0400 Subject: [PATCH 5/5] Fixed lint errors. --- iroh/iroh-monitor.js | 71 ++++++++++++++++++++++---------------------- src/start.js | 4 +-- 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/iroh/iroh-monitor.js b/iroh/iroh-monitor.js index 68d35c8d0b..64c6c2b863 100644 --- a/iroh/iroh-monitor.js +++ b/iroh/iroh-monitor.js @@ -1,11 +1,12 @@ 'use strict'; + const Iroh = require('iroh'); // Asked help from co-pilot for this function as I needed to simplify parsing for Iroh. function monitorFunction(fn, name) { - console.log(`Setting up Iroh monitoring for ${name}`); - - const stage = new Iroh.Stage(` + console.log(`Setting up Iroh monitoring for ${name}`); + + const stage = new Iroh.Stage(` function monitored() { try { return target(); @@ -16,40 +17,40 @@ function monitorFunction(fn, name) { } `); - // Using Iroh's listeners - stage.addListener(Iroh.CALL) - .on('before', (e) => { - console.log(`[IROH] ${name} called`); - e.setData('time', process.hrtime()); - }) - .on('after', (e) => { - const diff = process.hrtime(e.getData('time')); - const time = (diff[0] * 1e9 + diff[1]) / 1e6; - console.log(`[IROH] ${name} took ${time}ms`); - }); + // Using Iroh's listeners + stage.addListener(Iroh.CALL) + .on('before', (e) => { + console.log(`[IROH] ${name} called`); + e.setData('time', process.hrtime()); + }) + .on('after', (e) => { + const diff = process.hrtime(e.getData('time')); + const time = ((diff[0] * 1e9) + diff[1]) / 1e6; + console.log(`[IROH] ${name} took ${time}ms`); + }); - // Return the original function with timing - return async function(...args) { - const start = process.hrtime(); - console.log(`[START] ${name}`); - - try { - const result = await fn.apply(this, args); - const [s, ns] = process.hrtime(start); - const ms = (s * 1000) + (ns / 1000000); - console.log(`[TIME] ${name} took ${ms.toFixed(2)}ms`); - console.log(`[END] ${name}`); - return result; - } catch (error) { - console.error(`[ERROR] ${name}:`, error); - throw error; - } - }; + // Return the original function with timing + return async function (...args) { + const start = process.hrtime(); + console.log(`[START] ${name}`); + + try { + const result = await fn.apply(this, args); + const [s, ns] = process.hrtime(start); + const ms = (s * 1000) + (ns / 1e6); + console.log(`[TIME] ${name} took ${ms.toFixed(2)}ms`); + console.log(`[END] ${name}`); + return result; + } catch (error) { + console.error(`[ERROR] ${name}:`, error); + throw error; + } + }; } module.exports = { - monitorFunction: monitorFunction, - CALL: Iroh.CALL, - FUNCTION: Iroh.FUNCTION, - TRY: Iroh.TRY + monitorFunction: monitorFunction, + CALL: Iroh.CALL, + FUNCTION: Iroh.FUNCTION, + TRY: Iroh.TRY, }; diff --git a/src/start.js b/src/start.js index dd9bdc5e9e..c57726f418 100644 --- a/src/start.js +++ b/src/start.js @@ -12,11 +12,11 @@ const Topics = require('./topics'); start.start = async function () { try { console.log('Initializing Iroh monitoring...'); - + // I chose to monitor core nodeBB topic functions (create and reply). Topics.create = irohMonitor.monitorFunction(Topics.create, 'Topics.create'); Topics.reply = irohMonitor.monitorFunction(Topics.reply, 'Topics.reply'); - + console.log('Iroh monitoring successfully initialized!!'); } catch (err) { console.error('Failed to initialize Iroh monitoring:', err);