Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const mocks = {
logger: {
error: sandbox.stub(),
},
Info: {
version: '3.0.1',
},
};

const { sendUsageReport } = proxyquire.noCallThru().load('./sendUsageReport', {
Expand All @@ -29,6 +32,7 @@ const { sendUsageReport } = proxyquire.noCallThru().load('./sendUsageReport', {
'..': { statistics: mocks.statistics },
'../../../cloud/server': { getWorkspaceAccessToken: mocks.getWorkspaceAccessToken },
'meteor/meteor': { Meteor: mocks.Meteor },
'../../../utils/rocketchat.info': { Info: mocks.Info },
});

describe('sendUsageReport', () => {
Expand Down Expand Up @@ -60,4 +64,42 @@ describe('sendUsageReport', () => {
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;
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Meteor } from 'meteor/meteor';
import { statistics } from '..';
import { shouldReportStatistics } from '../../../../server/cron/usageReport';
import { getWorkspaceAccessToken } from '../../../cloud/server';
import { Info } from '../../../utils/rocketchat.info';

async function sendStats(logger: Logger, cronStatistics: IStats): Promise<string | undefined> {
try {
Expand Down Expand Up @@ -41,7 +42,10 @@ export async function sendUsageReport(logger: Logger): Promise<string | undefine

return tracerSpan('generateStatistics', {}, async () => {
const last = await Statistics.findLast();
if (last) {

const currentVersion = Info.version;

if (last && last.version === currentVersion) {
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);

Expand All @@ -61,7 +65,7 @@ export async function sendUsageReport(logger: Logger): Promise<string | undefine
}
}

// if our latest stats has more than 24h, it is time to generate a new one and send it
// 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);
Expand Down
55 changes: 0 additions & 55 deletions apps/meteor/app/statistics/server/lib/statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import { getImporterStatistics } from './getImporterStatistics';
import { getServicesStatistics } from './getServicesStatistics';
import { readSecondaryPreferred } from '../../../../server/database/readSecondaryPreferred';
import { isRunningMs } from '../../../../server/lib/isRunningMs';
import { SystemLogger } from '../../../../server/lib/logger/system';
import { getControl } from '../../../../server/lib/migrations';
import { getSettingsStatistics } from '../../../../server/lib/statistics/getSettingsStatistics';
import { getMatrixFederationStatistics } from '../../../../server/services/federation/infrastructure/rocket-chat/adapters/Statistics';
Expand Down Expand Up @@ -600,58 +599,4 @@ export const statistics = {

return rcStatistics;
},
async updateDeploymentData(): Promise<void> {
try {
const lastStatistics = await Statistics.findLast();

if (!lastStatistics) {
return;
}

const { mongoVersion, mongoStorageEngine } = await getMongoInfo();

const deploymentInfo: Partial<IStats> = {
os: {
type: os.type(),
platform: os.platform(),
arch: os.arch(),
release: os.release(),
uptime: os.uptime(),
loadavg: os.loadavg(),
totalmem: os.totalmem(),
freemem: os.freemem(),
cpus: os.cpus(),
},
process: {
nodeVersion: process.version,
pid: process.pid,
uptime: process.uptime(),
},
deploy: {
method: process.env.DEPLOY_METHOD || 'tar',
platform: process.env.DEPLOY_PLATFORM || 'selfinstall',
},
msEnabled: isRunningMs(),
mongoVersion,
mongoStorageEngine: mongoStorageEngine || '',
migration: await getControl(),
};

if (Info) {
deploymentInfo.version = Info.version;
}

const { _id, ...baseStatistics } = lastStatistics;

const newStatistics: Omit<IStats, '_id'> = {
...baseStatistics,
...deploymentInfo,
createdAt: new Date(),
};

await Statistics.insertOne(newStatistics);
} catch (error) {
SystemLogger.error({ msg: 'Error saving statistics with new deployment data', err: error });
}
},
};
2 changes: 0 additions & 2 deletions apps/meteor/server/startup/migrations/xrun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { UpdateResult } from 'mongodb';

import { upsertPermissions } from '../../../app/authorization/server/functions/upsertPermissions';
import { settings } from '../../../app/settings/server';
import { statistics } from '../../../app/statistics/server/lib/statistics';
import { migrateDatabase, onServerVersionChange } from '../../lib/migrations';
import { ensureCloudWorkspaceRegistered } from '../cloudRegistration';

Expand Down Expand Up @@ -63,6 +62,5 @@ export const performMigrationProcedure = async (): Promise<void> => {
await upsertPermissions();
await ensureCloudWorkspaceRegistered();
await moveRetentionSetting();
await statistics.updateDeploymentData();
});
};
Loading