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

feat(api-service): Usage insights email #7346

Draft
wants to merge 34 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
9f7b9dc
feat: insights
scopsy Dec 4, 2024
5728669
feat: wip
scopsy Dec 4, 2024
6eb853e
fix: hello world
scopsy Dec 4, 2024
099fea9
fix: email
scopsy Dec 4, 2024
8fb84e3
fix: email style
scopsy Dec 4, 2024
60a85a3
fix: add marketing section
scopsy Dec 4, 2024
a49b625
fix: upload
scopsy Dec 4, 2024
d56c811
fix: logo
scopsy Dec 4, 2024
8a89c1b
Merge branch 'next' into insights-email
scopsy Dec 22, 2024
747dffd
fix: items
scopsy Dec 22, 2024
5ee4ad9
fix: workflows
scopsy Dec 22, 2024
6d7c867
fix: items
scopsy Dec 22, 2024
63ef939
fix: items
scopsy Dec 22, 2024
836c44f
fix: items
scopsy Dec 22, 2024
0dd5361
fix: refactor
scopsy Dec 22, 2024
9570eff
fix: review
scopsy Dec 22, 2024
2be2bf1
fix: items
scopsy Dec 22, 2024
5464b96
fix: items
scopsy Dec 22, 2024
2945749
fix: bugs
scopsy Dec 22, 2024
4c9bd12
fix: working state
scopsy Dec 22, 2024
3b4e5a9
feat: add controller
scopsy Dec 22, 2024
4a4f6bc
feat: add insights tester
scopsy Dec 22, 2024
88f0530
fix: mixpanel
scopsy Dec 22, 2024
510860a
fix: remove cache
scopsy Dec 22, 2024
efc2df7
fix: remove unused import
scopsy Dec 22, 2024
bd7e05f
fix: trigger
scopsy Dec 22, 2024
65c3d67
fix: empty state
scopsy Dec 22, 2024
9ea8c3c
fix: refactpr
scopsy Dec 22, 2024
69a10f8
fix: r emov unused
scopsy Dec 22, 2024
3173273
fix: validation
scopsy Dec 22, 2024
465427d
fix: remove pr info
scopsy Dec 22, 2024
2c39098
Merge branch 'next' into insights-email
scopsy Dec 22, 2024
ed1d443
Merge branch 'next' into insights-email
scopsy Dec 22, 2024
325762b
fix: import
scopsy Dec 22, 2024
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
Prev Previous commit
Next Next commit
feat: add controller
  • Loading branch information
scopsy committed Dec 22, 2024
commit 3b4e5a9c50826f1c1650b7946fa58ba9f520ed68
27 changes: 27 additions & 0 deletions apps/api/src/app/insights/insights.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Controller, Get, Query } from '@nestjs/common';
import { ApiOperation, ApiQuery } from '@nestjs/swagger';
import { UsageInsights } from './usecases/usage-insights/usage-insights.usecase';
import { UsageInsightsCommand } from './usecases/usage-insights/usage-insights.command';

@Controller({
path: 'insights',
})
export class InsightsController {
Comment on lines +8 to +11
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a temporary controller protected allowed to be triggered by novu admins (protected by LD) to trigger individual insight emails during testing period

constructor(private usageInsights: UsageInsights) {}

@Get('/execute')
@ApiOperation({
summary: 'Execute insights for a specific organization',
})
@ApiQuery({
name: 'organizationId',
type: String,
required: true,
description: 'The ID of the organization to execute insights for',
})
async executeInsights(@Query('organizationId') organizationId: string) {
const command = new UsageInsightsCommand({ organizationId });

return this.usageInsights.execute(command);
}
}
2 changes: 2 additions & 0 deletions apps/api/src/app/insights/insights.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { InsightsInitializerService } from './services/insights-initializer.serv
import { MixpanelService } from './services/mixpanel.service';
import { MetricsCalculatorService } from './services/metrics-calculator.service';
import { OrganizationNotificationService } from './services/organization-notification.service';
import { InsightsController } from './insights.controller';

@Module({
imports: [SharedModule],
Expand All @@ -27,6 +28,7 @@ import { OrganizationNotificationService } from './services/organization-notific
MetricsCalculatorService,
OrganizationNotificationService,
],
controllers: [InsightsController],
exports: [...USE_CASES],
})
export class InsightsModule {}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Injectable, OnApplicationBootstrap, Logger } from '@nestjs/common';
import { UsageInsights } from '../usecases/usage-insights/usage-insights.usecase';
import { UsageInsightsCommand } from '../usecases/usage-insights/usage-insights.command';

@Injectable()
export class InsightsInitializerService implements OnApplicationBootstrap {
Expand All @@ -9,11 +8,7 @@ export class InsightsInitializerService implements OnApplicationBootstrap {
async onApplicationBootstrap() {
try {
Logger.log('Initializing usage insights...');
const command = new UsageInsightsCommand();

setTimeout(() => {
this.usageInsights.execute(command);
}, 10000);
Logger.log('Usage insights initialization completed');
} catch (error) {
Logger.error('Failed to initialize insights:', error);
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
export class UsageInsightsCommand {}
export class UsageInsightsCommand {
organizationId?: string;

constructor(data: { organizationId?: string } = {}) {
this.organizationId = data.organizationId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { InstrumentUsecase } from '@novu/application-generic';

import {
IChannelData,
IMixpanelInboxResponse,
IMixpanelTriggerResponse,
IOrganizationMetrics,
MixpanelInboxSeriesNameEnum,
MixpanelTriggerEventNameEnum,
Expand Down Expand Up @@ -46,12 +48,38 @@ export class UsageInsights {
this.mixpanelService.fetchInboxInsights(),
]);

if (!mixpanelData?.series) {
if (!mixpanelData?.series || !inboxData?.series) {
Logger.error('Mixpanel data unavailable or invalid');

return;
}

Logger.debug('Processing organization data');

if (command.organizationId) {
await this.processOrganization(command.organizationId, mixpanelData, inboxData);
} else {
let counter = 0;
for (const [orgId, orgData] of Object.entries(
mixpanelData.series[MixpanelTriggerEventNameEnum.NOTIFICATION_SUBSCRIBER_EVENT]
)) {
if (orgId === '$overall') continue;

counter += 1;
if (counter > 5) break;

await this.processOrganization(orgId, mixpanelData, inboxData);
}
}

Logger.debug('Usage Insights execution completed successfully');
}

private async processOrganization(
orgId: string,
mixpanelData: IMixpanelTriggerResponse,
inboxData: IMixpanelInboxResponse
) {
Logger.debug('Processing Mixpanel and Inbox data');
// Trigger Workflow data
const triggerEventSeries = mixpanelData.series[MixpanelTriggerEventNameEnum.NOTIFICATION_SUBSCRIBER_EVENT];
Expand Down Expand Up @@ -88,70 +116,58 @@ export class UsageInsights {
return;
}

let counter = 0;
Logger.debug('Processing organization data');
for (const [orgId, orgData] of Object.entries(processWorkflowSeries)) {
if (orgId === '$overall') continue;

counter += 1;

if (counter > 5) break;

const organizationData: IOrganizationData = {
workflowTrigger: {
current: triggerEventSeries[orgId],
previous: triggerEventPreviousSeries[orgId],
const organizationData: IOrganizationData = {
workflowTrigger: {
current: triggerEventSeries[orgId],
previous: triggerEventPreviousSeries[orgId],
},
stepProcessing: {
current: processWorkflowSeries[orgId],
previous: previousProcessWorkflowSeries[orgId],
},
inbox: {
current: {
[MixpanelInboxSeriesNameEnum.INBOX_SESSION_INITIALIZED]: inboxSessionInitializedSeries[orgId],
[MixpanelInboxSeriesNameEnum.INBOX_UPDATE_PREFERENCES]: inboxUpdatePreferencesSeries[orgId],
[MixpanelInboxSeriesNameEnum.INBOX_MARK_NOTIFICATION]: inboxMarkNotificationSeries[orgId],
[MixpanelInboxSeriesNameEnum.INBOX_UPDATE_ACTION]: inboxUpdateActionSeries[orgId],
},
stepProcessing: {
current: processWorkflowSeries[orgId],
previous: previousProcessWorkflowSeries[orgId],
previous: {
[MixpanelInboxSeriesNameEnum.INBOX_SESSION_INITIALIZED]: previousInboxSessionInitializedSeries[orgId],
[MixpanelInboxSeriesNameEnum.INBOX_UPDATE_PREFERENCES]: previousInboxUpdatePreferencesSeries[orgId],
[MixpanelInboxSeriesNameEnum.INBOX_MARK_NOTIFICATION]: previousInboxMarkNotificationSeries[orgId],
[MixpanelInboxSeriesNameEnum.INBOX_UPDATE_ACTION]: previousInboxUpdateActionSeries[orgId],
},
inbox: {
current: {
[MixpanelInboxSeriesNameEnum.INBOX_SESSION_INITIALIZED]: inboxSessionInitializedSeries[orgId],
[MixpanelInboxSeriesNameEnum.INBOX_UPDATE_PREFERENCES]: inboxUpdatePreferencesSeries[orgId],
[MixpanelInboxSeriesNameEnum.INBOX_MARK_NOTIFICATION]: inboxMarkNotificationSeries[orgId],
[MixpanelInboxSeriesNameEnum.INBOX_UPDATE_ACTION]: inboxUpdateActionSeries[orgId],
},
previous: {
[MixpanelInboxSeriesNameEnum.INBOX_SESSION_INITIALIZED]: previousInboxSessionInitializedSeries[orgId],
[MixpanelInboxSeriesNameEnum.INBOX_UPDATE_PREFERENCES]: previousInboxUpdatePreferencesSeries[orgId],
[MixpanelInboxSeriesNameEnum.INBOX_MARK_NOTIFICATION]: previousInboxMarkNotificationSeries[orgId],
[MixpanelInboxSeriesNameEnum.INBOX_UPDATE_ACTION]: previousInboxUpdateActionSeries[orgId],
},
},
};

const metrics: IOrganizationMetrics = {
eventTriggers: this.metricsCalculator.calculateEventTriggersMetrics(
organizationData.workflowTrigger.current,
organizationData.workflowTrigger.previous
),
channelBreakdown: this.metricsCalculator.calculateChannelBreakdown(
organizationData.stepProcessing.current,
organizationData.stepProcessing.previous
),
workflowStats: this.metricsCalculator.calculateWorkflowStats(
organizationData.workflowTrigger.current,
organizationData.workflowTrigger.previous
),
inboxMetrics: this.metricsCalculator.calculateInboxMetrics(
organizationData.inbox?.current,
organizationData.inbox?.previous,
orgId,
{
from_date: inboxData!.time_comparison.date_range.from_date,
to_date: inboxData!.date_range.from_date,
}
),
};

await this.organizationNotification.sendOrganizationNotification(orgId, metrics, {
from_date: mixpanelData.time_comparison.date_range.from_date,
to_date: mixpanelData.date_range.to_date,
});
}

Logger.debug('Usage Insights execution completed successfully');
},
};

const metrics: IOrganizationMetrics = {
eventTriggers: this.metricsCalculator.calculateEventTriggersMetrics(
organizationData.workflowTrigger.current,
organizationData.workflowTrigger.previous
),
channelBreakdown: this.metricsCalculator.calculateChannelBreakdown(
organizationData.stepProcessing.current,
organizationData.stepProcessing.previous
),
workflowStats: this.metricsCalculator.calculateWorkflowStats(
organizationData.workflowTrigger.current,
organizationData.workflowTrigger.previous
),
inboxMetrics: this.metricsCalculator.calculateInboxMetrics(
organizationData.inbox?.current,
organizationData.inbox?.previous,
orgId,
{
from_date: inboxData!.time_comparison.date_range.from_date,
to_date: inboxData!.date_range.from_date,
}
),
};

await this.organizationNotification.sendOrganizationNotification(orgId, metrics, {
from_date: mixpanelData!.time_comparison.date_range.from_date,
to_date: mixpanelData!.date_range.to_date,
});
}
}
4 changes: 2 additions & 2 deletions libs/notifications/src/workflows/usage-insights/email.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,13 @@ export default function UsageInsightsEmail(props: IUsageEmailData & { marketingC
📊 Usage Insights for {props.organizationName} - {formatDate(props.period.current)}
</Preview>
<Tailwind>
<Body className="bg-gray-50 pt-10 font-sans">
<Body className="bg-gray-50 font-sans">
<Img
src={`https://dashboard.novu.co/static/images/novu-colored-text.png`}
width="119"
height="37"
alt="Novu Logo"
className="mx-auto"
className="mx-auto mb-10 mt-10"
/>
<Container className="mx-auto w-full max-w-[700px]">
<Section className="rounded-t-lg bg-gray-900 px-6 py-8">
Expand Down