-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Voice Calls data to statistics (#34948)
Co-authored-by: Marcos Spessatto Defendi <15324204+MarcosSpessatto@users.noreply.github.com>
- Loading branch information
1 parent
c8e8518
commit 8942b00
Showing
8 changed files
with
188 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'@rocket.chat/model-typings': minor | ||
'@rocket.chat/core-typings': minor | ||
'@rocket.chat/freeswitch': minor | ||
'@rocket.chat/models': minor | ||
'@rocket.chat/meteor': minor | ||
--- | ||
|
||
Adds voice calls data to statistics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
apps/meteor/app/statistics/server/lib/getVoIPStatistics.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { log } from 'console'; | ||
|
||
import type { IStats, IVoIPPeriodStats } from '@rocket.chat/core-typings'; | ||
import { FreeSwitchCall } from '@rocket.chat/models'; | ||
import { MongoInternals } from 'meteor/mongo'; | ||
|
||
import { readSecondaryPreferred } from '../../../../server/database/readSecondaryPreferred'; | ||
|
||
const { db } = MongoInternals.defaultRemoteCollectionDriver().mongo; | ||
|
||
const getMinDate = (days?: number): Date | undefined => { | ||
if (!days) { | ||
return; | ||
} | ||
|
||
const date = new Date(); | ||
date.setDate(date.getDate() - days); | ||
|
||
return date; | ||
}; | ||
|
||
async function getVoIPStatisticsForPeriod(days?: number): Promise<IVoIPPeriodStats> { | ||
const promises: Array<Promise<number | void>> = []; | ||
const options = { | ||
readPreference: readSecondaryPreferred(db), | ||
}; | ||
|
||
const minDate = getMinDate(days); | ||
|
||
const statistics: IVoIPPeriodStats = {}; | ||
|
||
promises.push( | ||
FreeSwitchCall.countCallsByDirection('internal', minDate, options).then((count) => { | ||
statistics.internalCalls = count; | ||
}), | ||
); | ||
promises.push( | ||
FreeSwitchCall.countCallsByDirection('external_inbound', minDate, options).then((count) => { | ||
statistics.externalInboundCalls = count; | ||
}), | ||
); | ||
promises.push( | ||
FreeSwitchCall.countCallsByDirection('external_outbound', minDate, options).then((count) => { | ||
statistics.externalOutboundCalls = count; | ||
}), | ||
); | ||
|
||
promises.push( | ||
FreeSwitchCall.sumCallsDuration(minDate, options).then((callsDuration) => { | ||
statistics.callsDuration = callsDuration; | ||
}), | ||
); | ||
|
||
promises.push( | ||
FreeSwitchCall.countCallsBySuccessState(true, minDate, options).then((count) => { | ||
statistics.successfulCalls = count; | ||
}), | ||
); | ||
|
||
promises.push( | ||
FreeSwitchCall.countCallsBySuccessState(false, minDate, options).then((count) => { | ||
statistics.failedCalls = count; | ||
}), | ||
); | ||
|
||
await Promise.allSettled(promises).catch(log); | ||
|
||
statistics.externalCalls = (statistics.externalInboundCalls || 0) + (statistics.externalOutboundCalls || 0); | ||
statistics.calls = (statistics.successfulCalls || 0) + (statistics.failedCalls || 0); | ||
|
||
return statistics; | ||
} | ||
|
||
export async function getVoIPStatistics(): Promise<IStats['enterprise']['voip']> { | ||
const statistics: IStats['enterprise']['voip'] = {}; | ||
|
||
const promises = [ | ||
getVoIPStatisticsForPeriod().then((total) => { | ||
statistics.total = total; | ||
}), | ||
getVoIPStatisticsForPeriod(30).then((month) => { | ||
statistics.lastMonth = month; | ||
}), | ||
getVoIPStatisticsForPeriod(7).then((week) => { | ||
statistics.lastWeek = week; | ||
}), | ||
getVoIPStatisticsForPeriod(1).then((day) => { | ||
statistics.lastDay = day; | ||
}), | ||
]; | ||
|
||
await Promise.allSettled(promises).catch(log); | ||
|
||
return statistics; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
import type { IFreeSwitchCall } from '@rocket.chat/core-typings'; | ||
import type { FindCursor, FindOptions, WithoutId } from 'mongodb'; | ||
import type { AggregateOptions, CountDocumentsOptions, FindCursor, FindOptions, WithoutId } from 'mongodb'; | ||
|
||
import type { IBaseModel, InsertionModel } from './IBaseModel'; | ||
|
||
export interface IFreeSwitchCallModel extends IBaseModel<IFreeSwitchCall> { | ||
registerCall(call: WithoutId<InsertionModel<IFreeSwitchCall>>): Promise<void>; | ||
findAllByChannelUniqueIds<T extends IFreeSwitchCall>(uniqueIds: string[], options?: FindOptions<IFreeSwitchCall>): FindCursor<T>; | ||
countCallsByDirection(direction: IFreeSwitchCall['direction'], minDate?: Date, options?: CountDocumentsOptions): Promise<number>; | ||
sumCallsDuration(minDate?: Date, options?: AggregateOptions): Promise<number>; | ||
countCallsBySuccessState(success: boolean, minDate?: Date, options?: CountDocumentsOptions): Promise<number>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters