Merge pull request #633 from Hexastack/fix/hook-stats-entry-event

fix: hook stats entry event
This commit is contained in:
Yassine 2025-04-10 08:52:13 +01:00 committed by GitHub
commit 1f91618d84
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 52 additions and 16 deletions

View File

@ -93,8 +93,9 @@ export class BotStatsService extends BaseService<BotStats> {
) {
this.eventEmitter.emit(
'hook:stats:entry',
'retention',
BotStatsType.retention,
'Retentioned users',
subscriber,
);
}
}
@ -106,7 +107,11 @@ export class BotStatsService extends BaseService<BotStats> {
* @param name - The name or identifier of the statistics entry (e.g., a specific feature or component being tracked).
*/
@OnEvent('hook:stats:entry')
async handleStatEntry(type: BotStatsType, name: string): Promise<void> {
async handleStatEntry(
type: BotStatsType,
name: string,
_subscriber: Subscriber,
): Promise<void> {
const day = new Date();
day.setMilliseconds(0);
day.setSeconds(0);

View File

@ -16,6 +16,7 @@ import {
UpdateWithAggregationPipeline,
} from 'mongoose';
import { BotStatsType } from '@/analytics/schemas/bot-stats.schema';
import { BaseRepository } from '@/utils/generics/base-repository';
import { TFilterQuery } from '@/utils/types/filter.types';
@ -47,7 +48,7 @@ export class SubscriberRepository extends BaseRepository<
async postCreate(created: SubscriberDocument): Promise<void> {
this.eventEmitter.emit(
'hook:stats:entry',
'new_users',
BotStatsType.new_users,
'New users',
created,
);

View File

@ -243,8 +243,8 @@ describe('BlockService', () => {
await botService.startConversation(event, block);
expect(hasBotSpoken).toEqual(true);
expect(triggeredEvents).toEqual([
['popular', 'hasNextBlocks'],
['new_conversations', 'New conversations'],
['popular', 'hasNextBlocks', webSubscriber],
['new_conversations', 'New conversations', webSubscriber],
]);
clearMock.mockClear();
});
@ -301,7 +301,7 @@ describe('BlockService', () => {
const captured = await botService.processConversationMessage(event);
expect(captured).toBe(true);
expect(triggeredEvents).toEqual([
['existing_conversations', 'Existing conversations'],
['existing_conversations', 'Existing conversations', webSubscriber],
]);
clearMock.mockClear();
});

View File

@ -9,6 +9,7 @@
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { BotStatsType } from '@/analytics/schemas/bot-stats.schema';
import EventWrapper from '@/channel/lib/EventWrapper';
import { LoggerService } from '@/logger/logger.service';
import { SettingService } from '@/setting/services/setting.service';
@ -65,8 +66,18 @@ export class BotService {
.getHandler()
.sendMessage(event, envelope, options, context);
this.eventEmitter.emit('hook:stats:entry', 'outgoing', 'Outgoing');
this.eventEmitter.emit('hook:stats:entry', 'all_messages', 'All Messages');
this.eventEmitter.emit(
'hook:stats:entry',
BotStatsType.outgoing,
'Outgoing',
recipient,
);
this.eventEmitter.emit(
'hook:stats:entry',
BotStatsType.all_messages,
'All Messages',
recipient,
);
// Trigger sent message event
const sentMessage: MessageCreateDto = {
@ -293,7 +304,12 @@ export class BotService {
if (next) {
// Increment stats about popular blocks
this.eventEmitter.emit('hook:stats:entry', 'popular', next.name);
this.eventEmitter.emit(
'hook:stats:entry',
BotStatsType.popular,
next.name,
convo.sender,
);
// Go next!
this.logger.debug('Respond to nested conversion! Go next ', next.id);
try {
@ -352,8 +368,9 @@ export class BotService {
this.eventEmitter.emit(
'hook:stats:entry',
'existing_conversations',
BotStatsType.existing_conversations,
'Existing conversations',
subscriber,
);
this.logger.debug('Conversation has been captured! Responding ...');
return await this.handleIncomingMessage(conversation, event);
@ -373,10 +390,15 @@ export class BotService {
* @param block - Starting block
*/
async startConversation(event: EventWrapper<any, any>, block: BlockFull) {
// Increment popular stats
this.eventEmitter.emit('hook:stats:entry', 'popular', block.name);
// Launching a new conversation
const subscriber = event.getSender();
// Increment popular stats
this.eventEmitter.emit(
'hook:stats:entry',
BotStatsType.popular,
block.name,
subscriber,
);
try {
const convo = await this.conversationService.create({
@ -384,8 +406,9 @@ export class BotService {
});
this.eventEmitter.emit(
'hook:stats:entry',
'new_conversations',
BotStatsType.new_conversations,
'New conversations',
subscriber,
);
try {

View File

@ -11,6 +11,7 @@ import { EventEmitter2, OnEvent } from '@nestjs/event-emitter';
import mime from 'mime';
import { v4 as uuidv4 } from 'uuid';
import { BotStatsType } from '@/analytics/schemas/bot-stats.schema';
import { AttachmentService } from '@/attachment/services/attachment.service';
import {
AttachmentAccess,
@ -149,11 +150,17 @@ export class ChatService {
}
this.websocketGateway.broadcastMessageReceived(populatedMsg, subscriber);
this.eventEmitter.emit('hook:stats:entry', 'incoming', 'Incoming');
this.eventEmitter.emit(
'hook:stats:entry',
'all_messages',
BotStatsType.incoming,
'Incoming',
subscriber,
);
this.eventEmitter.emit(
'hook:stats:entry',
BotStatsType.all_messages,
'All Messages',
subscriber,
);
} catch (err) {
this.logger.error('Unable to log received message.', err, event);
@ -248,7 +255,7 @@ export class ChatService {
};
this.eventEmitter.emit('hook:chatbot:sent', sentMessage, event);
this.eventEmitter.emit('hook:stats:entry', 'echo', 'Echo');
this.eventEmitter.emit('hook:stats:entry', 'echo', 'Echo', recipient);
} catch (err) {
this.logger.error('Unable to log echo message', err, event);
}