From 53a205db7d16721926d0372d0cf388e6c64faba7 Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Fri, 10 Jan 2025 19:05:47 -0500 Subject: [PATCH] wip --- app.js | 2 ++ lib/call-session.js | 6 ++++-- lib/middleware.js | 11 +++++++---- lib/utils.js | 19 ++++++++++++++++--- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/app.js b/app.js index c653e63..2549129 100644 --- a/app.js +++ b/app.js @@ -68,6 +68,7 @@ const { const { client: redisClient, addKey, + deleteKey, retrieveKey, createSet, retrieveSet, @@ -111,6 +112,7 @@ srf.locals = {...srf.locals, }, realtimeDbHelpers: { addKey, + deleteKey, retrieveKey, createSet, incrKey, diff --git a/lib/call-session.js b/lib/call-session.js index 8f59818..dc2d9a3 100644 --- a/lib/call-session.js +++ b/lib/call-session.js @@ -454,13 +454,15 @@ class CallSession extends Emitter { if (process.env.JAMBONES_HOSTING || trackingOn) { const {writeCallCount, writeCallCountSP, writeCallCountApp} = this.req.srf.locals; if (!this.req.locals.callCountNudged) { + const {callId} = this.req.locals; this.req.locals.callCountNudged = true; this.logger.debug('decrementing call count at end of call'); await nudgeCallCounts(this.logger, { service_provider_sid: this.service_provider_sid, account_sid: this.account_sid, - application_sid: this.application_sid - }, this.decrKey, {writeCallCountSP, writeCallCount, writeCallCountApp}) + application_sid: this.application_sid, + callId + }, this.decrKey, {writeCallCountSP, writeCallCount, writeCallCountApp}, -1) .catch((err) => this.logger.error(err, 'Error decrementing call counts')); } } diff --git a/lib/middleware.js b/lib/middleware.js index 2eb877d..1ff9837 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -248,6 +248,7 @@ module.exports = function(srf, logger) { }; const checkLimits = async(req, res, next) => { + const {callId} = req.locals; const trackingOn = process.env.JAMBONES_TRACK_ACCOUNT_CALLS || process.env.JAMBONES_TRACK_SP_CALLS || process.env.JAMBONES_TRACK_APP_CALLS; @@ -267,8 +268,9 @@ module.exports = function(srf, logger) { nudgeCallCounts(logger, { service_provider_sid, account_sid, - application_sid - }, decrKey, {writeCallCountSP, writeCallCount, writeCallCountApp}) + application_sid, + callId + }, decrKey, {writeCallCountSP, writeCallCount, writeCallCountApp}, -1) .catch((err) => logger.error(err, 'Error decrementing call counts')); } }); @@ -278,8 +280,9 @@ module.exports = function(srf, logger) { const {callsSP, calls} = await nudgeCallCounts(logger, { service_provider_sid, account_sid, - application_sid - }, incrKey, {writeCallCountSP, writeCallCount, writeCallCountApp}); + application_sid, + callId + }, incrKey, {writeCallCountSP, writeCallCount, writeCallCountApp}, 1); /* compare to account's limit, though avoid db hit when call count is low */ const minLimit = process.env.MIN_CALL_LIMIT ? diff --git a/lib/utils.js b/lib/utils.js index d1e34d6..8298a57 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -117,8 +117,8 @@ const createHealthCheckApp = (port, logger) => { }); }; -const nudgeCallCounts = async(logger, sids, nudgeOperator, writers) => { - const { service_provider_sid, account_sid, application_sid } = sids; +const nudgeCallCounts = async(logger, sids, nudgeOperator, writers, direction) => { + const { service_provider_sid, account_sid, callId, application_sid } = sids; const { writeCallCount, writeCallCountSP, writeCallCountApp } = writers; const nudges = []; const writes = []; @@ -152,7 +152,20 @@ const nudgeCallCounts = async(logger, sids, nudgeOperator, writers) => { logger.debug({ calls, callsSP, callsApp, service_provider_sid, account_sid, application_sid - }, 'call counts after adjustment'); + }, `call counts after ${direction > 0 ? 'increment' : 'decrement'}`); + + if (process.env.JAMBONES_DEBUG_CALL_COUNTS) { + const {srf} = require('..'); + const {addKey, deleteKey} = srf.locals.realtimeDbHelpers; + + if (direction > 0) { + await addKey(`debug:incalls:${account_sid}:${callId}`, Date.now()); + } + else { + await deleteKey(`debug:incalls:${account_sid}:${callId}`); + } + } + if (process.env.JAMBONES_TRACK_SP_CALLS) { writes.push(writeCallCountSP({ service_provider_sid, calls_in_progress: callsSP })); }