Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Statistics about language usage #20832

Merged
merged 7 commits into from
Feb 21, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the only find using this index was findLast and it sorts on reverse order

}

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

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

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

updateStatusText(_id, statusText) {
const update = {
$set: {
Expand Down
45 changes: 44 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,48 @@ const wizardFields = [
'Register_Server',
];

const getUserLanguages = (() => {
let first = true;
let cache;
let lastCache;
const cacheTime = 60 * 60 * 24 * 5 * 1000; // cache is valid for 5 days

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

const languages = {};

result.forEach(({ _id, total }) => {
const lng = _id || 'none';
languages[lng] = (languages[lng] || 0) + total;
});

return languages;
};

return () => {
// first run is tipically on server startup, since this can be very expensive operation, we'll return the last saved statistic (if any)
if (first) {
first = false;

const { userLanguages = {} } = Statistics.findLast() || {};

return userLanguages;
}

// if cache is still valid, return it
if (lastCache && Date.now() - lastCache < cacheTime) {
return cache;
}

// fill up cache and set lastCache to now
cache = getValues();
lastCache = Date.now();

return cache;
};
})();

export const statistics = {
get: function _getStatistics() {
const readPreference = readSecondaryPreferred(Uploads.model.rawDatabase());
Expand Down Expand Up @@ -74,6 +116,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();

// 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 });
},
});