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
80 lines
2.9 KiB
TypeScript
80 lines
2.9 KiB
TypeScript
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);
|
|
}
|
|
});
|
|
}
|