Files
upstream-rocketchat/apps/meteor/server/services/omnichannel-analytics/ChartData.ts
upstream-mirror 30810eecd7
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
Code scanning - action / CodeQL-Build (push) Failing after 2s
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

155 lines
4.5 KiB
TypeScript

import type { IOmnichannelRoom } from '@rocket.chat/core-typings';
import type { ILivechatRoomsModel } from '@rocket.chat/model-typings';
import type { Filter } from 'mongodb';
/* eslint-disable new-cap */
type ChartDataValidActions =
| 'Total_conversations'
| 'Avg_chat_duration'
| 'Total_messages'
| 'Avg_first_response_time'
| 'Avg_reaction_time'
| 'Best_first_response_time'
| 'Avg_response_time';
type DateParam = {
gte: Date;
lte: Date;
};
export class ChartData {
constructor(private readonly roomsModel: ILivechatRoomsModel) {}
isActionAllowed(action: string | undefined): action is ChartDataValidActions {
if (!action) {
return false;
}
return [
'Total_conversations',
'Avg_chat_duration',
'Total_messages',
'Avg_first_response_time',
'Avg_reaction_time',
'Best_first_response_time',
'Avg_response_time',
].includes(action);
}
callAction<T extends ChartDataValidActions>(action: T, ...args: [DateParam, string?, Filter<IOmnichannelRoom>?]) {
switch (action) {
case 'Total_conversations':
return this.Total_conversations(...args);
case 'Avg_chat_duration':
return this.Avg_chat_duration(...args);
case 'Total_messages':
return this.Total_messages(...args);
case 'Avg_first_response_time':
return this.Avg_first_response_time(...args);
case 'Avg_reaction_time':
return this.Avg_reaction_time(...args);
case 'Best_first_response_time':
return this.Best_first_response_time(...args);
case 'Avg_response_time':
return this.Avg_response_time(...args);
default:
throw new Error('Invalid action');
}
}
async Total_conversations(date: DateParam, departmentId?: string, extraQuery: Filter<IOmnichannelRoom> = {}) {
// @ts-expect-error - Check extraquery usage on this func
return this.roomsModel.getTotalConversationsBetweenDate('l', date, { departmentId }, extraQuery);
}
async Avg_chat_duration(date: DateParam, departmentId?: string, extraQuery: Filter<IOmnichannelRoom> = {}) {
let total = 0;
let count = 0;
await this.roomsModel.getAnalyticsMetricsBetweenDate('l', date, { departmentId }, extraQuery).forEach(({ metrics }) => {
if (metrics?.chatDuration) {
total += metrics.chatDuration;
count++;
}
});
const avgCD = count ? total / count : 0;
return Math.round(avgCD * 100) / 100;
}
async Total_messages(date: DateParam, departmentId?: string, extraQuery: Filter<IOmnichannelRoom> = {}) {
let total = 0;
// we don't want to count visitor messages
const extraFilter = { $lte: ['$token', null] };
await this.roomsModel
.getAnalyticsMetricsBetweenDateWithMessages('l', date, { departmentId }, extraFilter, extraQuery)
.forEach(({ msgs }) => {
if (msgs) {
total += msgs;
}
});
return total;
}
async Avg_first_response_time(date: DateParam, departmentId?: string, extraQuery: Filter<IOmnichannelRoom> = {}) {
let frt = 0;
let count = 0;
await this.roomsModel.getAnalyticsMetricsBetweenDate('l', date, { departmentId }, extraQuery).forEach(({ metrics }) => {
if (metrics?.response?.ft) {
frt += metrics.response.ft;
count++;
}
});
const avgFrt = count ? frt / count : 0;
return Math.round(avgFrt * 100) / 100;
}
async Best_first_response_time(date: DateParam, departmentId?: string, extraQuery: Filter<IOmnichannelRoom> = {}) {
let maxFrt = 0;
await this.roomsModel.getAnalyticsMetricsBetweenDate('l', date, { departmentId }, extraQuery).forEach(({ metrics }) => {
if (metrics?.response?.ft) {
maxFrt = maxFrt ? Math.min(maxFrt, metrics.response.ft) : metrics.response.ft;
}
});
if (!maxFrt) {
maxFrt = 0;
}
return Math.round(maxFrt * 100) / 100;
}
async Avg_response_time(date: DateParam, departmentId?: string, extraQuery: Filter<IOmnichannelRoom> = {}) {
let art = 0;
let count = 0;
await this.roomsModel.getAnalyticsMetricsBetweenDate('l', date, { departmentId }, extraQuery).forEach(({ metrics }) => {
if (metrics?.response?.avg) {
art += metrics.response.avg;
count++;
}
});
const avgArt = count ? art / count : 0;
return Math.round(avgArt * 100) / 100;
}
async Avg_reaction_time(date: DateParam, departmentId?: string, extraQuery: Filter<IOmnichannelRoom> = {}) {
let arnt = 0;
let count = 0;
await this.roomsModel.getAnalyticsMetricsBetweenDate('l', date, { departmentId }, extraQuery).forEach(({ metrics }) => {
if (metrics?.reaction?.ft) {
arnt += metrics.reaction.ft;
count++;
}
});
const avgArnt = count ? arnt / count : 0;
return Math.round(avgArnt * 100) / 100;
}
}