Skip to content

Commit

Permalink
feat: add statsToken to statistics and send it to the sync process (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcosSpessatto authored Oct 23, 2023
1 parent 47303b5 commit c4325e8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>> {
Expand Down Expand Up @@ -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,
};
}
4 changes: 3 additions & 1 deletion apps/meteor/app/statistics/server/lib/statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
};
18 changes: 13 additions & 5 deletions apps/meteor/server/cron/statistics.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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';

Expand All @@ -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;
Expand All @@ -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');
Expand Down
1 change: 1 addition & 0 deletions packages/core-typings/src/IStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,5 @@ export interface IStats {
webRTCEnabled: boolean;
webRTCEnabledForOmnichannel: boolean;
omnichannelWebRTCCalls: number;
statsToken?: string;
}

0 comments on commit c4325e8

Please sign in to comment.