diff --git a/packages/server/src/app.ts b/packages/server/src/app.ts index 5538487d9..b89f4fe64 100644 --- a/packages/server/src/app.ts +++ b/packages/server/src/app.ts @@ -1,5 +1,4 @@ import { Framework } from '@botpress/messaging-framework' -import { BillingService } from './billing/service' import { ChannelService } from './channels/service' import { ConduitService } from './conduits/service' import { ConversationService } from './conversations/service' @@ -35,7 +34,6 @@ export class App extends Framework { syncs: SyncService health: HealthService sockets: SocketService - billing: BillingService metrics: MetricsService constructor() { @@ -94,7 +92,6 @@ export class App extends Framework { this.instances ) this.sockets = new SocketService(this.caching, this.users) - this.billing = new BillingService(this.logger, this.conversations, this.messages) this.metrics = new MetricsService(this.conversations, this.messages) } @@ -127,12 +124,10 @@ export class App extends Framework { await super.monitor() await this.syncs.setup() await this.instances.monitor() - await this.billing.setup() } async destroy() { await super.destroy() - await this.billing?.destroy() await this.instances?.destroy() await this.metrics?.destroy() } diff --git a/packages/server/src/billing/service.ts b/packages/server/src/billing/service.ts deleted file mode 100644 index 9fbd423f2..000000000 --- a/packages/server/src/billing/service.ts +++ /dev/null @@ -1,101 +0,0 @@ -import { Logger, LoggerService, Service } from '@botpress/messaging-engine' -import axios from 'axios' -import clc from 'cli-color' -import ms from 'ms' -import yn from 'yn' -import { ConversationService } from '../conversations/service' -import { MessageCreatedEvent, MessageEvents } from '../messages/events' -import { MessageService } from '../messages/service' - -export class BillingService extends Service { - private logger: Logger - private timeout?: NodeJS.Timeout - private stats: { [clientId: string]: { received: number; sent: number } } = {} - - constructor(loggers: LoggerService, private conversations: ConversationService, private messages: MessageService) { - super() - this.logger = loggers.root.sub('billing') - } - - async setup() { - if (process.env.BILLING_ENDPOINT?.length || yn(process.env.ENABLE_BILLING_STATS)) { - this.messages.events.on(MessageEvents.Created, this.handleMessageCreated.bind(this)) - - void this.tickBilling() - } - } - - async destroy() { - try { - if (this.timeout) { - clearTimeout(this.timeout) - } - - await this.flushBilling() - } catch (e) { - this.logger.error(e, 'Failed to destroy billing') - } - } - - private async tickBilling() { - try { - await this.flushBilling() - } catch (e) { - this.logger.error(e, 'Error occurred in billing') - } finally { - this.timeout = setTimeout(this.tickBilling.bind(this), ms('3s')) - } - } - - private async handleMessageCreated({ message }: MessageCreatedEvent) { - const conversation = await this.conversations.get(message.conversationId) - const stat = this.stats[conversation.clientId] || { sent: 0, received: 0 } - - if (message.authorId) { - stat.received++ - } else { - stat.sent++ - } - - this.stats[conversation.clientId] = stat - } - - private async flushBilling() { - const entries = [...Object.entries(this.stats)] - - for (const [clientId, stat] of entries) { - const sentStats = { ...stat } - const timestamp = new Date().toISOString() - - stat.sent = 0 - stat.received = 0 - - if (process.env.BILLING_ENDPOINT?.length) { - await axios.post(process.env.BILLING_ENDPOINT!, { - meta: { - timestamp, - sender: 'messaging', - type: 'messages_processed', - schema_version: '1.0.0' - }, - schema_version: '1.0.0', - records: [ - { - client_id: clientId, - messages: sentStats, - timestamp - } - ] - }) - } - - // it's possible that new stats have been entered while the post request - // was waiting since it's async - if (stat.received === 0 && stat.sent === 0) { - delete this.stats[clientId] - } - - this.logger.info(`${clc.blackBright(`[${clientId}]`)} ${clc.cyan('stats')}`, sentStats) - } - } -} diff --git a/packages/server/src/global.ts b/packages/server/src/global.ts index 44fa084a9..603c283ce 100644 --- a/packages/server/src/global.ts +++ b/packages/server/src/global.ts @@ -6,8 +6,6 @@ export type MessagingEnv = FrameworkEnv & { SPINNED_URL?: string NO_LAZY_LOADING?: string TWILIO_TESTING?: string - BILLING_ENDPOINT?: string - ENABLE_BILLING_STATS?: string DISABLE_SOCKETS?: string ENABLE_LEGACY_CHANNELS?: string METRICS_ENABLED?: string