Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hook stats entry event #633

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions api/src/analytics/services/bot-stats.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@@ -98,8 +98,9 @@ export class BotStatsService extends BaseService<BotStats> {
) {
this.eventEmitter.emit(
'hook:stats:entry',
'retention',
BotStatsType.retention,
'Retentioned users',
subscriber,
);
}
}
@@ -111,7 +112,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);
3 changes: 2 additions & 1 deletion api/src/chat/repositories/subscriber.repository.ts
Original file line number Diff line number Diff line change
@@ -17,6 +17,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';

@@ -51,7 +52,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,
);
8 changes: 4 additions & 4 deletions api/src/chat/services/bot.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@@ -242,8 +242,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();
});
@@ -299,7 +299,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();
});
39 changes: 31 additions & 8 deletions api/src/chat/services/bot.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2024 Hexastack. All rights reserved.
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
@@ -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';
@@ -81,8 +82,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 = {
@@ -265,7 +276,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 {
@@ -329,8 +345,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);
@@ -350,19 +367,25 @@ 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({
sender: subscriber.id,
});
this.eventEmitter.emit(
'hook:stats:entry',
'new_conversations',
BotStatsType.new_conversations,
'New conversations',
subscriber,
);

try {
13 changes: 10 additions & 3 deletions api/src/chat/services/chat.service.ts
Original file line number Diff line number Diff line change
@@ -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,
@@ -133,11 +134,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);
@@ -232,7 +239,7 @@ export class ChatService {
};

this.eventEmitter.emit('hook:chatbot:sent', sentMessage);
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);
}