[web] Expose metric for active users in SP (#20130)

* [web] Expose metric for active users in SP

* Removed redundant UserHandler.setupLoginData()

In the past this method was also calling
a now deleted notifyDomainLicence(), but now
this is just an alias for populateTeamInvites()

* Added migration for `lastActive`

* Added secondary read precedence to count active users

GitOrigin-RevId: 86d6db31e1ae74ae40c6599e6acb731d8c4a04bd
This commit is contained in:
Miguel Serrano
2024-10-14 10:57:28 +00:00
committed by Copybot
parent 8309671a8c
commit 3ff142d478
9 changed files with 142 additions and 21 deletions
@@ -4,6 +4,7 @@ const base = require('./settings.test.defaults')
module.exports = base.mergeWith({
defaultFeatures: ServerCEDefaults.defaultFeatures,
activeUserMetricInterval: 100,
})
module.exports.mergeWith = function (overrides) {
@@ -0,0 +1,34 @@
const { promisify } = require('util')
const { expect } = require('chai')
const Features = require('../../../app/src/infrastructure/Features')
const {
promises: { getMetric },
} = require('./helpers/metrics')
const User = require('./helpers/User').promises
const sleep = promisify(setTimeout)
async function getActiveUsersMetric() {
return getMetric(line => line.startsWith('num_active_users'))
}
describe('ActiveUsersMetricTests', function () {
before(async function () {
if (Features.hasFeature('saas')) {
this.skip()
}
})
it('updates "num_active_users" metric after a new user opens a project', async function () {
expect(await getActiveUsersMetric()).to.equal(0)
this.user = new User()
await this.user.login()
const projectId = await this.user.createProject('test project')
await this.user.openProject(projectId)
// settings.activeUserMetricInterval is configured to 100ms
await sleep(110)
expect(await getActiveUsersMetric()).to.equal(1)
})
})
@@ -94,7 +94,7 @@ describe('AuthenticationController', function () {
},
}),
'../User/UserHandler': (this.UserHandler = {
setupLoginData: sinon.stub(),
populateTeamInvites: sinon.stub(),
}),
'../Analytics/AnalyticsManager': (this.AnalyticsManager = {
recordEventForUserInBackground: sinon.stub(),
@@ -556,7 +556,7 @@ describe('AuthenticationController', function () {
})
it('should not setup the user data in the background', function () {
this.UserHandler.setupLoginData.called.should.equal(false)
this.UserHandler.populateTeamInvites.called.should.equal(false)
})
it('should record a failed login', function () {
@@ -1134,7 +1134,7 @@ describe('AuthenticationController', function () {
this.AuthenticationController._clearRedirectFromSession = sinon.stub()
this.AuthenticationController._redirectToReconfirmPage = sinon.stub()
this.UserSessionsManager.trackSession = sinon.stub()
this.UserHandler.setupLoginData = sinon.stub()
this.UserHandler.populateTeamInvites = sinon.stub()
this.LoginRateLimiter.recordSuccessfulLogin = sinon.stub()
this.AuthenticationController._recordSuccessfulLogin = sinon.stub()
this.AnalyticsManager.recordEvent = sinon.stub()
@@ -1461,7 +1461,9 @@ describe('AuthenticationController', function () {
})
it('should setup the user data in the background', function () {
this.UserHandler.setupLoginData.calledWith(this.user).should.equal(true)
this.UserHandler.populateTeamInvites
.calledWith(this.user)
.should.equal(true)
})
it('should set res.session.justLoggedIn', function () {
@@ -1,4 +1,5 @@
const sinon = require('sinon')
const { expect } = require('chai')
const modulePath = '../../../../app/src/Features/User/UserHandler.js'
const SandboxedModule = require('sandboxed-module')
@@ -14,9 +15,18 @@ describe('UserHandler', function () {
createTeamInvitesForLegacyInvitedEmail: sinon.stub().yields(),
}
this.db = {
users: {
find: sinon.stub().returns({
count: sinon.stub().resolves(2),
}),
},
}
this.UserHandler = SandboxedModule.require(modulePath, {
requires: {
'../Subscription/TeamInvitesHandler': this.TeamInvitesHandler,
'../../infrastructure/mongodb': { db: this.db },
},
})
})
@@ -32,4 +42,10 @@ describe('UserHandler', function () {
.should.eq(true)
})
})
describe('countActiveUsers', function () {
it('return user count from DB lookup', async function () {
expect(await this.UserHandler.promises.countActiveUsers()).to.equal(2)
})
})
})