import { Meteor } from 'meteor/meteor'; import { cronJobs } from '@rocket.chat/cron'; import { serverFetch as fetch } from '@rocket.chat/server-fetch'; import { getWorkspaceAccessToken } from '../../app/cloud/server'; import { statistics } from '../../app/statistics/server'; import { settings } from '../../app/settings/server'; import type { Logger } from '../lib/logger/Logger'; async function generateStatistics(logger: Logger): Promise { const cronStatistics: Record = await statistics.save(); cronStatistics.host = Meteor.absoluteUrl(); if (!settings.get('Statistics_reporting')) { return; } try { const token = await getWorkspaceAccessToken(); const headers = { ...(token && { Authorization: `Bearer ${token}` }) }; await fetch('https://collector.rocket.chat/', { method: 'POST', body: cronStatistics, headers, }); } catch (error) { /* error*/ logger.warn('Failed to send usage report'); } } export async function statsCron(logger: Logger): Promise { if (settings.get('Troubleshoot_Disable_Statistics_Generator')) { return; } const name = 'Generate and save statistics'; let previousValue: boolean; settings.watch('Troubleshoot_Disable_Statistics_Generator', async (value) => { if (value === previousValue) { return; } previousValue = value; if (value) { await cronJobs.remove(name); return; } await generateStatistics(logger); const now = new Date(); await cronJobs.add(name, `12 ${now.getHours()} * * *`, async () => generateStatistics(logger)); }); }