Skip to content

Commit

Permalink
[NEW] Statistics about language usage (#20832)
Browse files Browse the repository at this point in the history
Co-authored-by: Diego Sampaio <chinello@gmail.com>
  • Loading branch information
gabrieleiro and sampaiodiego authored Feb 21, 2021
1 parent 2d65486 commit 5e05e6a
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 15 deletions.
6 changes: 3 additions & 3 deletions app/cloud/server/functions/buildRegistrationData.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { settings } from '../../../settings';
import { Users } from '../../../models';
import { settings } from '../../../settings/server';
import { Users, Statistics } from '../../../models/server';
import { statistics } from '../../../statistics';
import { LICENSE_VERSION } from '../license';

export function buildWorkspaceRegistrationData() {
const stats = statistics.get();
const stats = Statistics.findLast() || statistics.get();

const address = settings.get('Site_Url');
const siteName = settings.get('Site_Name');
Expand Down
20 changes: 10 additions & 10 deletions app/metrics/server/lib/collectMetrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ const setPrometheusData = async () => {
metrics.ddpAuthenticatedSessions.set(authenticatedSessions.length);
metrics.ddpConnectedUsers.set(_.unique(authenticatedSessions.map((s) => s.userId)).length);

// Apps metrics
const { totalInstalled, totalActive, totalFailed } = getAppsStatistics();

metrics.totalAppsInstalled.set(totalInstalled || 0);
metrics.totalAppsEnabled.set(totalActive || 0);
metrics.totalAppsFailed.set(totalFailed || 0);

const oplogQueue = getOplogInfo().mongo._oplogHandle?._entryQueue?.length || 0;
metrics.oplogQueue.set(oplogQueue);

const statistics = Statistics.findLast();
if (!statistics) {
return;
Expand Down Expand Up @@ -63,16 +73,6 @@ const setPrometheusData = async () => {
metrics.totalDirectMessages.set(statistics.totalDirectMessages);
metrics.totalLivechatMessages.set(statistics.totalLivechatMessages);

// Apps metrics
const { totalInstalled, totalActive, totalFailed } = getAppsStatistics();

metrics.totalAppsInstalled.set(totalInstalled || 0);
metrics.totalAppsEnabled.set(totalActive || 0);
metrics.totalAppsFailed.set(totalFailed || 0);

const oplogQueue = getOplogInfo().mongo._oplogHandle?._entryQueue?.length || 0;
metrics.oplogQueue.set(oplogQueue);

metrics.pushQueue.set(statistics.pushQueue || 0);
};

Expand Down
2 changes: 1 addition & 1 deletion app/models/server/models/Statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export class Statistics extends Base {
constructor() {
super('statistics');

this.tryEnsureIndex({ createdAt: 1 });
this.tryEnsureIndex({ createdAt: -1 });
}

// FIND ONE
Expand Down
1 change: 1 addition & 0 deletions app/models/server/models/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class Users extends Base {
this.tryEnsureIndex({ 'services.saml.inResponseTo': 1 });
this.tryEnsureIndex({ openBusinessHours: 1 }, { sparse: true });
this.tryEnsureIndex({ statusLivechat: 1 }, { sparse: true });
this.tryEnsureIndex({ language: 1 }, { sparse: true });
}

getLoginTokensByUserId(userId) {
Expand Down
21 changes: 21 additions & 0 deletions app/models/server/raw/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,27 @@ export class UsersRaw extends BaseRaw {
return this.col.aggregate(params).toArray();
}

getUserLanguages() {
const pipeline = [
{
$match: {
language: {
$exists: true,
$ne: '',
},
},
},
{
$group: {
_id: '$language',
total: { $sum: 1 },
},
},
];

return this.col.aggregate(pipeline).toArray();
}

updateStatusText(_id, statusText) {
const update = {
$set: {
Expand Down
21 changes: 20 additions & 1 deletion app/statistics/server/lib/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { settings } from '../../../settings/server';
import { Info, getMongoInfo } from '../../../utils/server';
import { Migrations } from '../../../migrations/server';
import { getStatistics as federationGetStatistics } from '../../../federation/server/functions/dashboard';
import { NotificationQueue } from '../../../models/server/raw';
import { NotificationQueue, Users as UsersRaw } from '../../../models/server/raw';
import { readSecondaryPreferred } from '../../../../server/database/readSecondaryPreferred';
import { getAppsStatistics } from './getAppsStatistics';
import { getStatistics as getEnterpriseStatistics } from '../../../../ee/app/license/server';
Expand All @@ -35,6 +35,24 @@ const wizardFields = [
'Register_Server',
];

const getUserLanguages = (totalUsers) => {
const result = Promise.await(UsersRaw.getUserLanguages());

const languages = {
none: totalUsers,
};

result.forEach(({ _id, total }) => {
if (!_id) {
return;
}
languages[_id] = total;
languages.none -= total;
});

return languages;
};

export const statistics = {
get: function _getStatistics() {
const readPreference = readSecondaryPreferred(Uploads.model.rawDatabase());
Expand Down Expand Up @@ -74,6 +92,7 @@ export const statistics = {
statistics.busyUsers = Meteor.users.find({ status: 'busy' }).count();
statistics.totalConnectedUsers = statistics.onlineUsers + statistics.awayUsers;
statistics.offlineUsers = statistics.totalUsers - statistics.onlineUsers - statistics.awayUsers - statistics.busyUsers;
statistics.userLanguages = getUserLanguages(statistics.totalUsers);

// Room statistics
statistics.totalRooms = Rooms.find().count();
Expand Down
1 change: 1 addition & 0 deletions server/startup/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,5 @@ import './v214';
import './v215';
import './v216';
import './v217';
import './v218';
import './xrun';
9 changes: 9 additions & 0 deletions server/startup/migrations/v218.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Migrations } from '../../../app/migrations/server';
import { Statistics } from '../../../app/models/server';

Migrations.add({
version: 218,
up() {
Statistics.tryDropIndex({ createdAt: 1 });
},
});

0 comments on commit 5e05e6a

Please sign in to comment.