forked from danny-avila/LibreChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser-stats.js
55 lines (45 loc) · 1.43 KB
/
user-stats.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const path = require('path');
require('module-alias')({ base: path.resolve(__dirname, '..', 'api') });
const { silentExit } = require('./helpers');
const Conversation = require('~/models/schema/convoSchema');
const Message = require('~/models/schema/messageSchema');
const User = require('~/models/User');
const connect = require('./connect');
(async () => {
await connect();
/**
* Show the welcome / help menu
*/
console.purple('-----------------------------');
console.purple('Show the stats of all users');
console.purple('-----------------------------');
let users = await User.find({});
let userData = [];
for (const user of users) {
let conversationsCount = (await Conversation.countDocuments({ user: user._id })) ?? 0;
let messagesCount = (await Message.countDocuments({ user: user._id })) ?? 0;
userData.push({
User: user.name,
Email: user.email,
Conversations: conversationsCount,
Messages: messagesCount,
});
}
userData.sort((a, b) => {
if (a.Conversations !== b.Conversations) {
return b.Conversations - a.Conversations;
}
return b.Messages - a.Messages;
});
console.table(userData);
silentExit(0);
})();
process.on('uncaughtException', (err) => {
if (!err.message.includes('fetch failed')) {
console.error('There was an uncaught error:');
console.error(err);
}
if (!err.message.includes('fetch failed')) {
process.exit(1);
}
});