feat(analytics): add clickhouse (#11174)
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { AnalyticsService } from 'src/engine/core-modules/analytics/analytics.service';
|
||||
import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
|
||||
import { AnalyticsService } from 'src/engine/core-modules/analytics/services/analytics.service';
|
||||
import { MONITORING_EVENT } from 'src/engine/core-modules/analytics/utils/events/track/monitoring/monitoring';
|
||||
|
||||
type MessagingTelemetryTrackInput = {
|
||||
eventName: string;
|
||||
@ -14,10 +14,7 @@ type MessagingTelemetryTrackInput = {
|
||||
|
||||
@Injectable()
|
||||
export class MessagingTelemetryService {
|
||||
constructor(
|
||||
private readonly analyticsService: AnalyticsService,
|
||||
private readonly twentyConfigService: TwentyConfigService,
|
||||
) {}
|
||||
constructor(private readonly analyticsService: AnalyticsService) {}
|
||||
|
||||
public async track({
|
||||
eventName,
|
||||
@ -27,20 +24,16 @@ export class MessagingTelemetryService {
|
||||
messageChannelId,
|
||||
message,
|
||||
}: MessagingTelemetryTrackInput): Promise<void> {
|
||||
await this.analyticsService.create(
|
||||
{
|
||||
action: 'monitoring',
|
||||
payload: {
|
||||
eventName: `messaging.${eventName}`,
|
||||
workspaceId,
|
||||
userId,
|
||||
connectedAccountId,
|
||||
messageChannelId,
|
||||
message,
|
||||
},
|
||||
},
|
||||
userId,
|
||||
workspaceId,
|
||||
);
|
||||
await this.analyticsService
|
||||
.createAnalyticsContext({
|
||||
userId,
|
||||
workspaceId,
|
||||
})
|
||||
.track(MONITORING_EVENT, {
|
||||
eventName: `messaging.${eventName}`,
|
||||
connectedAccountId,
|
||||
messageChannelId,
|
||||
message,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,6 +8,10 @@ import { AuditLogRepository } from 'src/modules/timeline/repositiories/audit-log
|
||||
import { AuditLogWorkspaceEntity } from 'src/modules/timeline/standard-objects/audit-log.workspace-entity';
|
||||
import { WorkspaceMemberRepository } from 'src/modules/workspace-member/repositories/workspace-member.repository';
|
||||
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
|
||||
import { AnalyticsService } from 'src/engine/core-modules/analytics/services/analytics.service';
|
||||
import { OBJECT_RECORD_UPDATED_EVENT } from 'src/engine/core-modules/analytics/utils/events/track/object-record/object-record-updated';
|
||||
import { OBJECT_RECORD_CREATED_EVENT } from 'src/engine/core-modules/analytics/utils/events/track/object-record/object-record-created';
|
||||
import { OBJECT_RECORD_DELETED_EVENT } from 'src/engine/core-modules/analytics/utils/events/track/object-record/object-record-delete';
|
||||
|
||||
@Processor(MessageQueue.entityEventsToDbQueue)
|
||||
export class CreateAuditLogFromInternalEvent {
|
||||
@ -16,6 +20,7 @@ export class CreateAuditLogFromInternalEvent {
|
||||
private readonly workspaceMemberService: WorkspaceMemberRepository,
|
||||
@InjectObjectMetadataRepository(AuditLogWorkspaceEntity)
|
||||
private readonly auditLogRepository: AuditLogRepository,
|
||||
private readonly analyticsService: AnalyticsService,
|
||||
) {}
|
||||
|
||||
@Process(CreateAuditLogFromInternalEvent.name)
|
||||
@ -48,6 +53,19 @@ export class CreateAuditLogFromInternalEvent {
|
||||
eventData.recordId,
|
||||
workspaceEventBatch.workspaceId,
|
||||
);
|
||||
|
||||
const analytics = this.analyticsService.createAnalyticsContext({
|
||||
workspaceId: workspaceEventBatch.workspaceId,
|
||||
userId: eventData.userId,
|
||||
});
|
||||
|
||||
if (workspaceEventBatch.name.endsWith('.updated')) {
|
||||
analytics.track(OBJECT_RECORD_UPDATED_EVENT, eventData.properties);
|
||||
} else if (workspaceEventBatch.name.endsWith('.created')) {
|
||||
analytics.track(OBJECT_RECORD_CREATED_EVENT, eventData.properties);
|
||||
} else if (workspaceEventBatch.name.endsWith('.deleted')) {
|
||||
analytics.track(OBJECT_RECORD_DELETED_EVENT, eventData.properties);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ import { UpsertTimelineActivityFromInternalEvent } from 'src/modules/timeline/jo
|
||||
import { AuditLogWorkspaceEntity } from 'src/modules/timeline/standard-objects/audit-log.workspace-entity';
|
||||
import { TimelineActivityModule } from 'src/modules/timeline/timeline-activity.module';
|
||||
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
|
||||
import { AnalyticsModule } from 'src/engine/core-modules/analytics/analytics.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@ -14,6 +15,7 @@ import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/sta
|
||||
AuditLogWorkspaceEntity,
|
||||
]),
|
||||
TimelineActivityModule,
|
||||
AnalyticsModule,
|
||||
],
|
||||
providers: [
|
||||
CreateAuditLogFromInternalEvent,
|
||||
|
||||
@ -3,10 +3,11 @@ import { Logger } from '@nestjs/common';
|
||||
|
||||
import crypto from 'crypto';
|
||||
|
||||
import { AnalyticsService } from 'src/engine/core-modules/analytics/analytics.service';
|
||||
import { AnalyticsService } from 'src/engine/core-modules/analytics/services/analytics.service';
|
||||
import { Process } from 'src/engine/core-modules/message-queue/decorators/process.decorator';
|
||||
import { Processor } from 'src/engine/core-modules/message-queue/decorators/processor.decorator';
|
||||
import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';
|
||||
import { WEBHOOK_RESPONSE_EVENT } from 'src/engine/core-modules/analytics/utils/events/track/webhook/webhook-response';
|
||||
|
||||
export type CallWebhookJobData = {
|
||||
targetUrl: string;
|
||||
@ -46,6 +47,9 @@ export class CallWebhookJob {
|
||||
webhookId: data.webhookId,
|
||||
eventName: data.eventName,
|
||||
};
|
||||
const analytics = this.analyticsService.createAnalyticsContext({
|
||||
workspaceId: data.workspaceId,
|
||||
});
|
||||
|
||||
try {
|
||||
const headers: Record<string, string> = {
|
||||
@ -73,27 +77,18 @@ export class CallWebhookJob {
|
||||
);
|
||||
|
||||
const success = response.status >= 200 && response.status < 300;
|
||||
const eventInput = {
|
||||
action: 'webhook.response',
|
||||
payload: {
|
||||
status: response.status,
|
||||
success,
|
||||
...commonPayload,
|
||||
},
|
||||
};
|
||||
|
||||
this.analyticsService.create(eventInput, 'webhook', data.workspaceId);
|
||||
analytics.track(WEBHOOK_RESPONSE_EVENT, {
|
||||
status: response.status,
|
||||
success,
|
||||
...commonPayload,
|
||||
});
|
||||
} catch (err) {
|
||||
const eventInput = {
|
||||
action: 'webhook.response',
|
||||
payload: {
|
||||
success: false,
|
||||
...commonPayload,
|
||||
...(err.response && { status: err.response.status }),
|
||||
},
|
||||
};
|
||||
|
||||
this.analyticsService.create(eventInput, 'webhook', data.workspaceId);
|
||||
analytics.track(WEBHOOK_RESPONSE_EVENT, {
|
||||
success: false,
|
||||
...commonPayload,
|
||||
...(err.response && { status: err.response.status }),
|
||||
});
|
||||
this.logger.error(
|
||||
`Error calling webhook on targetUrl '${data.targetUrl}': ${err}`,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user