Skip to content

Commit

Permalink
[NEW] New stats rewrite (#25078)
Browse files Browse the repository at this point in the history
Co-authored-by: matheusbsilva137 <matheus_barbosa137@hotmail.com>
Co-authored-by: Diego Sampaio <chinello@gmail.com>
  • Loading branch information
3 people authored May 19, 2022
1 parent 42442d2 commit b893ffd
Show file tree
Hide file tree
Showing 25 changed files with 255 additions and 118 deletions.
5 changes: 3 additions & 2 deletions apps/meteor/app/importer-csv/server/importer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Random } from 'meteor/random';

import { Base, ProgressStep, ImporterWebsocket } from '../../importer/server';
import { Users } from '../../models/server';
import { Users, Settings as SettingsRaw } from '../../models/server';

export class CsvImporter extends Base {
constructor(info, importRecord) {
Expand Down Expand Up @@ -119,7 +119,8 @@ export class CsvImporter extends Base {
});
}

super.updateRecord({ 'count.users': parsedUsers.length });
SettingsRaw.incrementValueById('CSV_Importer_Count', usersCount);
super.updateRecord({ 'count.users': usersCount });
return increaseProgressCount();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fs from 'fs';
import { Meteor } from 'meteor/meteor';

import { Base, ProgressStep } from '../../importer/server';
import { Settings as SettingsRaw } from '../../models/server';

export class HipChatEnterpriseImporter extends Base {
constructor(info, importRecord) {
Expand Down Expand Up @@ -52,6 +53,7 @@ export class HipChatEnterpriseImporter extends Base {
this.converter.addUser(newUser);
}

SettingsRaw.incrementValueById('Hipchat_Enterprise_Importer_Count', count);
super.updateRecord({ 'count.users': count });
super.addCountToTotal(count);
}
Expand Down
2 changes: 2 additions & 0 deletions apps/meteor/app/importer-slack-users/server/importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Random } from 'meteor/random';
import { RawImports, Base, ProgressStep, Selection, SelectionUser } from '../../importer/server';
import { RocketChatFile } from '../../file';
import { Users } from '../../models';
import { Settings as SettingsRaw } from '../../models/server';

export class SlackUsersImporter extends Base {
constructor(info, importRecord) {
Expand Down Expand Up @@ -164,6 +165,7 @@ export class SlackUsersImporter extends Base {
});
}

SettingsRaw.incrementValueById('Slack_Users_Importer_Count', this.users.users.length);
super.updateProgress(ProgressStep.FINISHING);
super.updateProgress(ProgressStep.DONE);
} catch (e) {
Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/app/importer-slack/server/importer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from 'underscore';

import { Base, ProgressStep, ImporterWebsocket } from '../../importer/server';
import { Messages, ImportData } from '../../models/server';
import { Messages, ImportData, Settings as SettingsRaw } from '../../models/server';
import { settings } from '../../settings/server';
import { MentionsParser } from '../../mentions/lib/MentionsParser';
import { getUserAvatarURL } from '../../utils/lib/getUserAvatarURL';
Expand Down Expand Up @@ -155,6 +155,7 @@ export class SlackImporter extends Base {
}

this.converter.addUser(newUser);
SettingsRaw.incrementValueById('Slack_Importer_Count');
}
}

Expand Down
6 changes: 5 additions & 1 deletion apps/meteor/app/lib/server/methods/sendInvitationEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { check } from 'meteor/check';
import * as Mailer from '../../../mailer';
import { hasPermission } from '../../../authorization';
import { settings } from '../../../settings';
import { Settings as SettingsRaw } from '../../../models/server';

let html = '';
Meteor.startup(() => {
Expand Down Expand Up @@ -37,7 +38,7 @@ Meteor.methods({

return validEmails.filter((email) => {
try {
return Mailer.send({
const mailerResult = Mailer.send({
to: email,
from: settings.get('From_Email'),
subject,
Expand All @@ -46,6 +47,9 @@ Meteor.methods({
email,
},
});

SettingsRaw.incrementValueById('Invitation_Email_Count');
return mailerResult;
} catch ({ message }) {
throw new Meteor.Error('error-email-send-failed', `Error trying to send email: ${message}`, {
method: 'sendInvitationEmail',
Expand Down
5 changes: 5 additions & 0 deletions apps/meteor/app/lib/server/startup/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,11 @@ settingsRegistry.addGroup('Email', function () {
);
});

this.add('Invitation_Email_Count', 0, {
type: 'int',
hidden: true,
});

this.section('Forgot_password_section', function () {
this.add('Forgot_Password_Email_Subject', '{Forgot_Password_Email_Subject}', {
type: 'string',
Expand Down
20 changes: 20 additions & 0 deletions apps/meteor/app/lib/server/startup/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,26 @@ settingsRegistry.addGroup('Accounts', function () {
type: 'string',
hidden: true,
});
this.add('Manual_Entry_User_Count', 0, {
type: 'int',
hidden: true,
});
this.add('CSV_Importer_Count', 0, {
type: 'int',
hidden: true,
});
this.add('Hipchat_Enterprise_Importer_Count', 0, {
type: 'int',
hidden: true,
});
this.add('Slack_Importer_Count', 0, {
type: 'int',
hidden: true,
});
this.add('Slack_Users_Importer_Count', 0, {
type: 'int',
hidden: true,
});
this.add('Accounts_UseDefaultBlockedDomainsList', true, {
type: 'boolean',
});
Expand Down
1 change: 1 addition & 0 deletions apps/meteor/app/models/server/models/Rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class Rooms extends Base {
// field used for DMs only
this.tryEnsureIndex({ uids: 1 }, { sparse: true });
this.tryEnsureIndex({ createdOTR: 1 }, { sparse: true });
this.tryEnsureIndex({ encrypted: 1 }, { sparse: true }); // used on statistics

this.tryEnsureIndex(
{
Expand Down
4 changes: 2 additions & 2 deletions apps/meteor/app/models/server/models/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ export class Settings extends Base {
return this.update(query, update);
}

incrementValueById(_id) {
incrementValueById(_id, value = 1) {
const query = {
blocked: { $ne: true },
_id,
};

const update = {
$inc: {
value: 1,
value,
},
};

Expand Down
3 changes: 3 additions & 0 deletions apps/meteor/app/models/server/models/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export class Users extends Base {
this.tryEnsureIndex({ extension: 1 }, { sparse: true, unique: true });
this.tryEnsureIndex({ language: 1 }, { sparse: true });

this.tryEnsureIndex({ 'active': 1, 'services.email2fa.enabled': 1 }, { sparse: true }); // used by statistics
this.tryEnsureIndex({ 'active': 1, 'services.totp.enabled': 1 }, { sparse: true }); // used by statistics

const collectionObj = this.model.rawCollection();
this.findAndModify = Meteor.wrapAsync(collectionObj.findAndModify, collectionObj);
}
Expand Down
8 changes: 8 additions & 0 deletions apps/meteor/app/models/server/raw/Invites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ export class InvitesRaw extends BaseRaw<T> {
},
);
}

async countUses(): Promise<number> {
const [result] = await this.col
.aggregate<{ totalUses: number } | undefined>([{ $group: { _id: null, totalUses: { $sum: '$uses' } } }])
.toArray();

return result?.totalUses || 0;
}
}
42 changes: 42 additions & 0 deletions apps/meteor/app/models/server/raw/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,46 @@ export class MessagesRaw extends BaseRaw {
options,
);
}

async countRoomsWithStarredMessages(options) {
const [queryResult] = await this.col
.aggregate(
[{ $match: { 'starred._id': { $exists: true } } }, { $group: { _id: '$rid' } }, { $group: { _id: null, total: { $sum: 1 } } }],
options,
)
.toArray();

return queryResult?.total || 0;
}

async countRoomsWithPinnedMessages(options) {
const [queryResult] = await this.col
.aggregate([{ $match: { pinned: true } }, { $group: { _id: '$rid' } }, { $group: { _id: null, total: { $sum: 1 } } }], options)
.toArray();

return queryResult?.total || 0;
}

async countE2EEMessages(options) {
return this.find({ t: 'e2e' }, options).count();
}

findPinned(options) {
const query = {
t: { $ne: 'rm' },
_hidden: { $ne: true },
pinned: true,
};

return this.find(query, options);
}

findStarred(options) {
const query = {
'_hidden': { $ne: true },
'starred._id': { $exists: true },
};

return this.find(query, options);
}
}
17 changes: 17 additions & 0 deletions apps/meteor/app/models/server/raw/Rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,4 +460,21 @@ export class RoomsRaw extends BaseRaw {
},
]);
}

findByE2E(options) {
return this.find(
{
encrypted: true,
},
options,
);
}

findRoomsInsideTeams(autoJoin = false) {
return this.find({
teamId: { $exists: true },
teamMain: { $exists: false },
...(autoJoin && { teamDefault: true }),
});
}
}
16 changes: 16 additions & 0 deletions apps/meteor/app/models/server/raw/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -999,4 +999,20 @@ export class UsersRaw extends BaseRaw {

return this.find(query, options);
}

findActiveUsersTOTPEnable(options) {
const query = {
'active': true,
'services.totp.enabled': true,
};
return this.find(query, options);
}

findActiveUsersEmail2faEnable(options) {
const query = {
'active': true,
'services.email2fa.enabled': true,
};
return this.find(query, options);
}
}
10 changes: 10 additions & 0 deletions apps/meteor/app/statistics/server/lib/getImporterStatistics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { settings } from '../../../settings/server';

export function getImporterStatistics(): Record<string, unknown> {
return {
totalCSVImportedUsers: settings.get('CSV_Importer_Count'),
totalHipchatEnterpriseImportedUsers: settings.get('Hipchat_Enterprise_Importer_Count'),
totalSlackImportedUsers: settings.get('Slack_Importer_Count'),
totalSlackUsersImportedUsers: settings.get('Slack_Users_Importer_Count'),
};
}
Loading

0 comments on commit b893ffd

Please sign in to comment.