Report DAU and MAU by role (#19657)
parent
c056793c3e
commit
cd6bcbfd44
@ -0,0 +1,47 @@ |
||||
const order = [ |
||||
'admin', |
||||
'livechat-manager', |
||||
'livechat-monitor', |
||||
'livechat-agent', |
||||
'custom-role', |
||||
'user', |
||||
'app', |
||||
'bot', |
||||
'guest', |
||||
'anonymous', |
||||
]; |
||||
|
||||
const rolesToConsiderAsUser = [ |
||||
'auditor', |
||||
'auditor-log', |
||||
]; |
||||
|
||||
export function getMostImportantRole(roles = []) { |
||||
if (!roles.length) { |
||||
return 'no-role'; |
||||
} |
||||
|
||||
roles = roles.map((r) => (rolesToConsiderAsUser.includes(r) ? 'user' : r)); |
||||
|
||||
if (roles.length === 1) { |
||||
if (!order.includes(roles[0])) { |
||||
return 'custom-role'; |
||||
} |
||||
return roles[0]; |
||||
} |
||||
|
||||
const newRoles = []; |
||||
for (const role of roles) { |
||||
if (order.includes(role)) { |
||||
newRoles.push(role); |
||||
} else if (!newRoles.includes('custom-role')) { |
||||
newRoles.push('custom-role'); |
||||
} |
||||
} |
||||
|
||||
for (const item of order) { |
||||
if (newRoles.includes(item)) { |
||||
return item; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,45 @@ |
||||
/* eslint-env mocha */ |
||||
|
||||
import { expect } from 'chai'; |
||||
|
||||
import { getMostImportantRole } from './getMostImportantRole'; |
||||
|
||||
describe('getMostImportantRole', () => { |
||||
it('should return the same role if only one exists', () => { |
||||
expect(getMostImportantRole(['admin'])).to.be.eq('admin'); |
||||
expect(getMostImportantRole(['livechat-manager'])).to.be.eq('livechat-manager'); |
||||
expect(getMostImportantRole(['livechat-monitor'])).to.be.eq('livechat-monitor'); |
||||
expect(getMostImportantRole(['livechat-agent'])).to.be.eq('livechat-agent'); |
||||
expect(getMostImportantRole(['user'])).to.be.eq('user'); |
||||
expect(getMostImportantRole(['guest'])).to.be.eq('guest'); |
||||
expect(getMostImportantRole(['anonymous'])).to.be.eq('anonymous'); |
||||
expect(getMostImportantRole(['app'])).to.be.eq('app'); |
||||
expect(getMostImportantRole(['bot'])).to.be.eq('bot'); |
||||
}); |
||||
|
||||
it('should return custom roles as `custom-role`', () => { |
||||
expect(getMostImportantRole(['custom1'])).to.be.eq('custom-role'); |
||||
}); |
||||
|
||||
it('should return auditor, app and bot as `user`', () => { |
||||
expect(getMostImportantRole(['auditor'])).to.be.eq('user'); |
||||
expect(getMostImportantRole(['auditor-log'])).to.be.eq('user'); |
||||
}); |
||||
|
||||
it('should return `no-role` if no one exists', () => { |
||||
expect(getMostImportantRole([])).to.be.eq('no-role'); |
||||
expect(getMostImportantRole()).to.be.eq('no-role'); |
||||
}); |
||||
|
||||
it('should return correct role', () => { |
||||
expect(getMostImportantRole(['user', 'admin'])).to.be.eq('admin'); |
||||
expect(getMostImportantRole(['user', 'anonymous'])).to.be.eq('user'); |
||||
expect(getMostImportantRole(['user', 'guest'])).to.be.eq('user'); |
||||
expect(getMostImportantRole(['user', 'guest', 'livechat-monitor'])).to.be.eq('livechat-monitor'); |
||||
expect(getMostImportantRole(['user', 'custom1'])).to.be.eq('custom-role'); |
||||
expect(getMostImportantRole(['custom2', 'user', 'custom1'])).to.be.eq('custom-role'); |
||||
expect(getMostImportantRole(['custom2', 'admin', 'custom1'])).to.be.eq('admin'); |
||||
expect(getMostImportantRole(['custom2', 'app'])).to.be.eq('custom-role'); |
||||
expect(getMostImportantRole(['anonymous', 'app'])).to.be.eq('app'); |
||||
}); |
||||
}); |
@ -0,0 +1,41 @@ |
||||
import { Promise } from 'meteor/promise'; |
||||
|
||||
import { Migrations } from '../../../app/migrations'; |
||||
import { Sessions } from '../../../app/models/server/raw'; |
||||
import { getMostImportantRole } from '../../../app/statistics/server/lib/getMostImportantRole'; |
||||
|
||||
async function migrateSessions() { |
||||
const cursor = Sessions.col.aggregate([{ |
||||
$match: { $or: [{ mostImportantRole: { $exists: 0 } }, { mostImportantRole: null }] }, |
||||
}, { |
||||
$group: { |
||||
_id: '$userId', |
||||
}, |
||||
}, { |
||||
$lookup: { |
||||
from: 'users', |
||||
localField: '_id', |
||||
foreignField: '_id', |
||||
as: 'user', |
||||
}, |
||||
}, { |
||||
$unwind: '$user', |
||||
}, { |
||||
$project: { |
||||
'user.roles': 1, |
||||
}, |
||||
}, { |
||||
$match: { 'user.roles.0': { $exists: 1 } }, |
||||
}]); |
||||
|
||||
for await (const session of cursor) { |
||||
await Sessions.col.updateMany({ userId: session._id }, { $set: { mostImportantRole: getMostImportantRole(session.user.roles) } }); |
||||
} |
||||
} |
||||
|
||||
Migrations.add({ |
||||
version: 211, |
||||
up() { |
||||
Promise.await(migrateSessions()); |
||||
}, |
||||
}); |
Loading…
Reference in new issue