From 0e57b66b7cce31fe25bf20e0ad451b8ff6a9b8d9 Mon Sep 17 00:00:00 2001 From: animetosho Date: Wed, 24 Jan 2024 21:00:35 +1000 Subject: [PATCH] Track and display NNTP connections' total connected/idle times --- cli/progressmgr.js | 2 ++ lib/nntp.js | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/cli/progressmgr.js b/cli/progressmgr.js index 7bea189..c38f342 100644 --- a/cli/progressmgr.js +++ b/cli/progressmgr.js @@ -37,12 +37,14 @@ var writeState = function(uploader, startTime, conn, debug) { host = 'unix:' + c.opts.connect.path; else host = c.opts.connect.host + ':' + c.opts.connect.port; + var timeStats = c.timeStats(now); conn.write([ ' Host: ' + host, ' State: ' + c.getCurrentActivity() + (c.lastActivity ? ' for ' + ((now - c.lastActivity)/1000) + 's' : ''), ' Transfer: ' + cliUtil.friendlySize(c.bytesRecv) + ' down / ' + cliUtil.friendlySize(c.bytesSent) + ' up', ' Requests: ' + c.numRequestsDetail(), ' Connects: ' + c.numConnects, + ' Time Idle: ' + (timeStats.idle / 1000) + 's (' + toPercent(timeStats.connected ?timeStats.idle/timeStats.connected : 0) + ')', ' Errors: ' + c.numErrorsDetail(), '', '' ].join('\r\n')); diff --git a/lib/nntp.js b/lib/nntp.js index d16ee48..3ee7aee 100644 --- a/lib/nntp.js +++ b/lib/nntp.js @@ -159,6 +159,10 @@ NNTP.prototype = { numErrors: null, numRequests: null, lastActivity: 0, + lastConnectedStart: null, + lastIdleStart: null, + connectedTime: 0, + idleTime: 0, // for tracking raw upload speed rawSpeedTracker: null, reqBytesSent: 0, @@ -990,6 +994,9 @@ NNTP.prototype = { var nr = this._requests[0]; this._requestSetTimer(nr.type, nr.ts + this.opts.timeout - Date.now()); } + if(!this._requests.length && this.state == 'connected') { + this.lastIdleStart = Date.now(); + } return req; }, _write: function(data, rtnLen) { @@ -1046,14 +1053,32 @@ NNTP.prototype = { return 'requesting (' + this._requests[0].type + ')'; }, _setState: function(state, cause) { + this.lastActivity = Date.now(); + if(state == 'connected') { + this.lastConnectedStart = this.lastActivity; + if(!this._requests.length) + this.lastIdleStart = this.lastActivity; + } + else if(this.state == 'connected') { + if(this.lastConnectedStart) // should always be set + this.connectedTime += this.lastActivity - this.lastConnectedStart; + this.lastConnectedStart = null; + if(this.lastIdleStart) { + this.idleTime += this.lastActivity - this.lastIdleStart; + this.lastIdleStart = null; + } + } this.state = state; this.stateCause = cause; - this.lastActivity = Date.now(); }, _startRequest: function(req) { this._requests.push(req); this.lastActivity = (req.ts = Date.now()); if(this.rawSpeedTracker) this.rawSpeedTracker.start(req.ts); + if(this.lastIdleStart) { + this.idleTime += this.lastActivity - this.lastIdleStart; + this.lastIdleStart = null; + } }, _endRequest: function(req) { this.lastActivity = Date.now(); @@ -1105,6 +1130,17 @@ NNTP.prototype = { } if(ret.length) return ret.join(', '); return '-'; + }, + timeStats: function(now) { + var stats = {connected: this.connectedTime, idle: this.idleTime}; + if(!now) now = Date.now(); + if(this.state == 'connected') { + if(this.lastConnectedStart) // should always be set + stats.connected += now - this.lastConnectedStart; + if(this.lastIdleStart && !this._requests.length) + stats.idle += now - this.lastIdleStart; + } + return stats; } };