Skip to content
Snippets Groups Projects
Unverified Commit c4325e82 authored by Marcos Spessatto Defendi's avatar Marcos Spessatto Defendi Committed by GitHub
Browse files

feat: add statsToken to statistics and send it to the sync process (#30181)

parent 47303b52
No related branches found
No related tags found
No related merge requests found
......@@ -38,6 +38,7 @@ export type WorkspaceRegistrationData<T> = {
MAC: number;
// activeContactsBillingMonth: number;
// activeContactsYesterday: number;
statsToken?: string;
};
export async function buildWorkspaceRegistrationData<T extends string | undefined>(contactEmail: T): Promise<WorkspaceRegistrationData<T>> {
......@@ -92,5 +93,6 @@ export async function buildWorkspaceRegistrationData<T extends string | undefine
MAC: stats.omnichannelContactsBySource?.contactsCount ?? 0,
// activeContactsBillingMonth: stats.omnichannelContactsBySource.contactsCount,
// activeContactsYesterday: stats.uniqueContactsOfYesterday.contactsCount,
statsToken: stats.statsToken,
};
}
......@@ -598,7 +598,9 @@ export const statistics = {
async save(): Promise<IStats> {
const rcStatistics = await statistics.get();
rcStatistics.createdAt = new Date();
await Statistics.insertOne(rcStatistics);
const { insertedId } = await Statistics.insertOne(rcStatistics);
rcStatistics._id = insertedId;
return rcStatistics;
},
};
import { cronJobs } from '@rocket.chat/cron';
import type { Logger } from '@rocket.chat/logger';
import { Statistics } from '@rocket.chat/models';
import { serverFetch as fetch } from '@rocket.chat/server-fetch';
import { Meteor } from 'meteor/meteor';
......@@ -8,9 +9,7 @@ import { settings } from '../../app/settings/server';
import { statistics } from '../../app/statistics/server';
async function generateStatistics(logger: Logger): Promise<void> {
const cronStatistics: Record<string, any> = await statistics.save();
cronStatistics.host = Meteor.absoluteUrl();
const cronStatistics = await statistics.save();
if (!settings.get('Statistics_reporting')) {
return;
......@@ -20,11 +19,20 @@ async function generateStatistics(logger: Logger): Promise<void> {
const token = await getWorkspaceAccessToken();
const headers = { ...(token && { Authorization: `Bearer ${token}` }) };
await fetch('https://collector.rocket.chat/', {
const response = await fetch('https://collector.rocket.chat/', {
method: 'POST',
body: cronStatistics,
body: {
...cronStatistics,
host: Meteor.absoluteUrl(),
},
headers,
});
const { statsToken } = await response.json();
if (statsToken != null) {
await Statistics.updateOne({ _id: cronStatistics._id }, { $set: { statsToken } });
}
} catch (error) {
/* error*/
logger.warn('Failed to send usage report');
......
......@@ -227,4 +227,5 @@ export interface IStats {
webRTCEnabled: boolean;
webRTCEnabledForOmnichannel: boolean;
omnichannelWebRTCCalls: number;
statsToken?: string;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment