snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import type { IUser } from '@rocket.chat/core-typings';
|
||||
import { Statistics } from '@rocket.chat/models';
|
||||
|
||||
import { hasPermissionAsync } from '../../authorization/hasPermission';
|
||||
import { statistics } from '../lib/statistics';
|
||||
|
||||
export async function getLastStatistics({ userId, refresh }: { userId: IUser['_id']; refresh?: boolean }) {
|
||||
if (!(await hasPermissionAsync(userId, 'view-statistics'))) {
|
||||
throw new Error('error-not-allowed');
|
||||
}
|
||||
|
||||
if (refresh) {
|
||||
return statistics.save();
|
||||
}
|
||||
return Statistics.findLast();
|
||||
}
|
||||
44
apps/meteor/server/lib/statistics/functions/getStatistics.ts
Normal file
44
apps/meteor/server/lib/statistics/functions/getStatistics.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import type { IStats } from '@rocket.chat/core-typings';
|
||||
import { Statistics } from '@rocket.chat/models';
|
||||
import type { FindOptions, SchemaMember } from 'mongodb';
|
||||
|
||||
import { hasPermissionAsync } from '../../authorization/hasPermission';
|
||||
|
||||
type GetStatisticsParams = {
|
||||
userId: string;
|
||||
query?: Record<string, any>;
|
||||
pagination: {
|
||||
offset: number;
|
||||
count?: number;
|
||||
sort?: FindOptions<IStats>['sort'];
|
||||
fields?: SchemaMember<IStats, number | boolean>;
|
||||
};
|
||||
};
|
||||
|
||||
type GetStatisticsReturn = { statistics: IStats[]; count: number; offset: number; total: number };
|
||||
|
||||
export async function getStatistics({
|
||||
userId,
|
||||
query = {},
|
||||
pagination: { offset, count, sort, fields },
|
||||
}: GetStatisticsParams): Promise<GetStatisticsReturn> {
|
||||
if (!(await hasPermissionAsync(userId, 'view-statistics'))) {
|
||||
throw new Error('error-not-allowed');
|
||||
}
|
||||
|
||||
const { cursor, totalCount } = Statistics.findPaginated(query, {
|
||||
sort: sort || { name: 1 },
|
||||
skip: offset,
|
||||
limit: count,
|
||||
projection: fields,
|
||||
});
|
||||
|
||||
const [statistics, total] = await Promise.all([cursor.toArray(), totalCount]);
|
||||
|
||||
return {
|
||||
statistics,
|
||||
count: statistics.length,
|
||||
offset,
|
||||
total,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
import { expect } from 'chai';
|
||||
import { describe, it, beforeEach, afterEach } from 'mocha';
|
||||
import proxyquire from 'proxyquire';
|
||||
import sinon from 'sinon';
|
||||
|
||||
const sandbox = sinon.createSandbox();
|
||||
|
||||
const mocks = {
|
||||
Statistics: {
|
||||
findLast: sandbox.stub(),
|
||||
updateOne: sandbox.stub(),
|
||||
},
|
||||
statistics: {
|
||||
save: sandbox.stub(),
|
||||
},
|
||||
serverFetch: sandbox.stub(),
|
||||
getWorkspaceAccessToken: sandbox.stub().resolves('workspace-token'),
|
||||
Meteor: {
|
||||
absoluteUrl: sandbox.stub().returns('http://localhost:3000/'),
|
||||
},
|
||||
logger: {
|
||||
error: sandbox.stub(),
|
||||
},
|
||||
Info: {
|
||||
version: '3.0.1',
|
||||
},
|
||||
};
|
||||
|
||||
const { sendUsageReport } = proxyquire.noCallThru().load('./sendUsageReport', {
|
||||
'@rocket.chat/models': { Statistics: mocks.Statistics },
|
||||
'@rocket.chat/server-fetch': { serverFetch: mocks.serverFetch },
|
||||
'../index': { statistics: mocks.statistics },
|
||||
'../../cloud': { getWorkspaceAccessToken: mocks.getWorkspaceAccessToken },
|
||||
'meteor/meteor': { Meteor: mocks.Meteor },
|
||||
'../../../../app/utils/rocketchat.info': { Info: mocks.Info },
|
||||
});
|
||||
|
||||
describe('sendUsageReport', () => {
|
||||
beforeEach(() => {
|
||||
sandbox.resetHistory();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
delete process.env.RC_DISABLE_STATISTICS_REPORTING;
|
||||
});
|
||||
|
||||
it('should save statistics locally and not send to collector when RC_DISABLE_STATISTICS_REPORTING is true', async () => {
|
||||
process.env.RC_DISABLE_STATISTICS_REPORTING = 'true';
|
||||
|
||||
const result = await sendUsageReport(mocks.logger);
|
||||
|
||||
expect(mocks.statistics.save.called).to.be.true;
|
||||
expect(mocks.serverFetch.called).to.be.false;
|
||||
expect(result).to.be.undefined;
|
||||
});
|
||||
|
||||
it('should save statistics locally and send to collector when RC_DISABLE_STATISTICS_REPORTING is false', async () => {
|
||||
process.env.RC_DISABLE_STATISTICS_REPORTING = 'false';
|
||||
|
||||
const result = await sendUsageReport(mocks.logger);
|
||||
|
||||
expect(mocks.statistics.save.called).to.be.true;
|
||||
expect(mocks.serverFetch.calledOnce).to.be.true;
|
||||
expect(mocks.serverFetch.calledWith('https://collector.rocket.chat/', sinon.match({ method: 'POST' }))).to.be.true;
|
||||
expect(result).to.be.undefined;
|
||||
});
|
||||
|
||||
it('should generate new statistics when version changes', async () => {
|
||||
mocks.Statistics.findLast.resolves({
|
||||
_id: 'stats-id',
|
||||
version: '2.9.0',
|
||||
createdAt: new Date(),
|
||||
});
|
||||
|
||||
mocks.statistics.save.resolves({ _id: 'new-stats-id' });
|
||||
|
||||
const result = await sendUsageReport(mocks.logger);
|
||||
|
||||
expect(mocks.statistics.save.calledOnce).to.be.true;
|
||||
expect(result).to.be.undefined;
|
||||
});
|
||||
|
||||
it('should NOT generate new statistics if last version equals current version', async () => {
|
||||
mocks.Statistics.findLast.resolves({
|
||||
_id: 'stats-id',
|
||||
version: '3.0.1',
|
||||
createdAt: new Date(),
|
||||
statsToken: 'token',
|
||||
});
|
||||
|
||||
const result = await sendUsageReport(mocks.logger);
|
||||
|
||||
expect(mocks.statistics.save.called).to.be.false;
|
||||
expect(result).to.equal('token');
|
||||
});
|
||||
|
||||
it('should generate new statistics when no previous stats exist', async () => {
|
||||
mocks.Statistics.findLast.resolves(undefined);
|
||||
mocks.statistics.save.resolves({ _id: 'new-stats-id' });
|
||||
|
||||
await sendUsageReport(mocks.logger);
|
||||
|
||||
expect(mocks.statistics.save.calledOnce).to.be.true;
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,79 @@
|
||||
import type { IStats } from '@rocket.chat/core-typings';
|
||||
import { License } from '@rocket.chat/license';
|
||||
import type { Logger } from '@rocket.chat/logger';
|
||||
import { Statistics } from '@rocket.chat/models';
|
||||
import { serverFetch as fetch } from '@rocket.chat/server-fetch';
|
||||
import { tracerSpan } from '@rocket.chat/tracing';
|
||||
import { Meteor } from 'meteor/meteor';
|
||||
|
||||
import { Info } from '../../../../app/utils/rocketchat.info';
|
||||
import { shouldReportStatistics } from '../../../cron/usageReport';
|
||||
import { getWorkspaceAccessToken } from '../../cloud';
|
||||
import { statistics } from '../index';
|
||||
|
||||
async function sendStats(logger: Logger, cronStatistics: IStats): Promise<string | undefined> {
|
||||
try {
|
||||
const token = await getWorkspaceAccessToken();
|
||||
const headers = { ...(token && { Authorization: `Bearer ${token}` }) };
|
||||
|
||||
const response = await fetch('https://collector.rocket.chat/', {
|
||||
method: 'POST',
|
||||
body: {
|
||||
...cronStatistics,
|
||||
host: Meteor.absoluteUrl(),
|
||||
},
|
||||
headers,
|
||||
// SECURITY: the URL is a default hardcoded value or an envvar/setting set by an admin. It's safe to disable this check.
|
||||
ignoreSsrfValidation: true,
|
||||
});
|
||||
|
||||
const { statsToken } = await response.json();
|
||||
|
||||
if (statsToken != null) {
|
||||
await Statistics.updateOne({ _id: cronStatistics._id }, { $set: { statsToken } });
|
||||
return statsToken;
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error({ msg: 'Failed to send usage report', err });
|
||||
}
|
||||
}
|
||||
|
||||
export async function sendUsageReport(logger: Logger): Promise<string | undefined> {
|
||||
// Even when disabled, we still generate statistics locally to avoid breaking
|
||||
// internal processes, such as restriction checks for air-gapped workspaces.
|
||||
// Evaluated at send time (not entry) so the verdict cannot go stale while the
|
||||
// statistics are being generated — e.g. an offline license applied during startup.
|
||||
const shouldSendToCollector = () => shouldReportStatistics() && !License.hasOfflineLicense();
|
||||
|
||||
return tracerSpan('generateStatistics', {}, async () => {
|
||||
const last = await Statistics.findLast();
|
||||
|
||||
const currentVersion = Info.version;
|
||||
|
||||
if (last && last.version === currentVersion) {
|
||||
const yesterday = new Date();
|
||||
yesterday.setDate(yesterday.getDate() - 1);
|
||||
|
||||
// if the last data we have has less than 24h and was not sent to yet, send it
|
||||
if (last.createdAt > yesterday) {
|
||||
// but if it has the confirmation token, we can skip
|
||||
if (last.statsToken) {
|
||||
return last.statsToken;
|
||||
}
|
||||
|
||||
// if it doesn't it means the request failed, so we try sending again with the same data
|
||||
if (shouldSendToCollector()) {
|
||||
return sendStats(logger, last);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// if our latest stats has more than 24h OR the statistics.version differs from the actual version, it is time to generate a new one and send it
|
||||
const cronStatistics = await statistics.save();
|
||||
if (shouldSendToCollector()) {
|
||||
return sendStats(logger, cronStatistics);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { updateCounter } from './updateStatsCounter';
|
||||
import telemetryEvent from '../lib/telemetryEvents';
|
||||
|
||||
type slashCommandsDataType = { command: string };
|
||||
|
||||
export function slashCommandsStats(data: slashCommandsDataType): void {
|
||||
if (data.command === 'jitsi') {
|
||||
updateCounter({ settingsId: 'Jitsi_Start_SlashCommands_Count' });
|
||||
}
|
||||
}
|
||||
|
||||
telemetryEvent.register('slashCommandsStats', slashCommandsStats);
|
||||
@@ -0,0 +1,17 @@
|
||||
import { Settings } from '@rocket.chat/models';
|
||||
|
||||
import { notifyOnSettingChanged } from '../../notifyListener';
|
||||
import telemetryEvent from '../lib/telemetryEvents';
|
||||
|
||||
type updateCounterDataType = { settingsId: string };
|
||||
|
||||
export function updateCounter(data: updateCounterDataType): void {
|
||||
void (async () => {
|
||||
const value = await Settings.incrementValueById(data.settingsId, 1, { returnDocument: 'after' });
|
||||
if (value) {
|
||||
void notifyOnSettingChanged(value);
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
telemetryEvent.register('updateCounter', updateCounter);
|
||||
Reference in New Issue
Block a user