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 all commits
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
6 changes: 4 additions & 2 deletions apps/api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ForwardReference } from '@nestjs/common/interfaces/modules/forward-refe
import { isClerkEnabled } from '@novu/shared';
import { SentryModule } from '@sentry/nestjs/setup';
import { ApiExcludeController } from '@nestjs/swagger';
import { usageLimitsWorkflow } from '@novu/notifications';
import { usageInsightsWorkflow, usageLimitsWorkflow } from '@novu/notifications';
import packageJson from '../package.json';
import { SharedModule } from './app/shared/shared.module';
import { UserModule } from './app/user/user.module';
Expand Down Expand Up @@ -49,6 +49,7 @@ import { WorkflowModule } from './app/workflows-v2/workflow.module';
import { WorkflowModuleV1 } from './app/workflows-v1/workflow-v1.module';
import { EnvironmentsModuleV1 } from './app/environments-v1/environments-v1.module';
import { EnvironmentsModule } from './app/environments-v2/environments.module';
import { InsightsModule } from './app/insights/insights.module';

const enterpriseImports = (): Array<Type | DynamicModule | Promise<DynamicModule> | ForwardReference> => {
const modules: Array<Type | DynamicModule | Promise<DynamicModule> | ForwardReference> = [];
Expand Down Expand Up @@ -115,6 +116,7 @@ const baseModules: Array<Type | DynamicModule | Promise<DynamicModule> | Forward
WorkflowModule,
EnvironmentsModule,
NovuModule,
InsightsModule,
];

const enterpriseModules = enterpriseImports();
Expand Down Expand Up @@ -165,7 +167,7 @@ modules.push(
process.env.NOVU_STRICT_AUTHENTICATION_ENABLED === 'true',
}),
controllerDecorators: [ApiExcludeController()],
workflows: [usageLimitsWorkflow],
workflows: [usageLimitsWorkflow, usageInsightsWorkflow],
})
);

Expand Down
46 changes: 46 additions & 0 deletions apps/api/src/app/insights/insights.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Controller, Get, Query, UnauthorizedException } from '@nestjs/common';
import { ApiOperation, ApiQuery } from '@nestjs/swagger';
import { FeatureFlagsService } from '@novu/application-generic';
import { FeatureFlagsKeysEnum } from '@novu/shared';
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,
private featureFlagsService: FeatureFlagsService
) {}

@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 isAllowedToTestInsights = await this.featureFlagsService.get(
FeatureFlagsKeysEnum.IS_ALLOWED_TO_TEST_INSIGHTS_ENABLED,
false,
{
organizationId,
userId: 'system',
environmentId: 'system',
}
);

if (!isAllowedToTestInsights) {
throw new UnauthorizedException('Organization is not allowed to test insights');
}

const command = new UsageInsightsCommand({ organizationId });

return this.usageInsights.execute(command);
}
}
32 changes: 32 additions & 0 deletions apps/api/src/app/insights/insights.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Module } from '@nestjs/common';
import {
OrganizationRepository,
MessageRepository,
NotificationRepository,
CommunityOrganizationRepository,
} from '@novu/dal';
import { USE_CASES } from './usecases';
import { SharedModule } from '../shared/shared.module';
import { InsightsInitializerService } from './services/insights-initializer.service';
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],
providers: [
...USE_CASES,
InsightsInitializerService,
OrganizationRepository,
MessageRepository,
NotificationRepository,
CommunityOrganizationRepository,
MixpanelService,
MetricsCalculatorService,
OrganizationNotificationService,
],
controllers: [InsightsController],
exports: [...USE_CASES],
})
export class InsightsModule {}
17 changes: 17 additions & 0 deletions apps/api/src/app/insights/services/insights-initializer.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Injectable, OnApplicationBootstrap, Logger } from '@nestjs/common';
import { UsageInsights } from '../usecases/usage-insights/usage-insights.usecase';

@Injectable()
export class InsightsInitializerService implements OnApplicationBootstrap {
constructor(private usageInsights: UsageInsights) {}

async onApplicationBootstrap() {
try {
Logger.log('Initializing usage insights...');

Logger.log('Usage insights initialization completed');
Comment on lines +10 to +12
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will be used in the future to handle monthly cron post testing period

} catch (error) {
Logger.error('Failed to initialize insights:', error);
}
}
}
241 changes: 241 additions & 0 deletions apps/api/src/app/insights/services/metrics-calculator.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
import { Injectable, Logger } from '@nestjs/common';
import { startOfDay, formatISO } from 'date-fns';
import { ChannelTypeEnum } from '@novu/shared';
import {
IDateRange,
IMixpanelInboxResponse,
IInboxMetrics,
IOrganizationMetrics,
IMixpanelTriggerResponse,
IMetricStats,
IChannelData,
MixpanelInboxSeriesNameEnum,
} from '../types/usage-insights.types';

@Injectable()
export class MetricsCalculatorService {
private roundToStartOfDay(date: string): string {
return formatISO(startOfDay(new Date(date)));
}

calculateChange(current: number, previous: number): number {
let change: number;

if (previous === 0) {
change = current > 0 ? 100 : 0;
} else {
change = Number(((current - previous) / previous) * 100);
}

Logger.debug(`Calculating change: current=${current}, previous=${previous}, change=${change}%`);

return change;
}

getSeriesDateRange(currentSeries: IChannelData, previousSeries: IChannelData): IDateRange {
const currentDates = Object.keys(currentSeries?.$overall || {});
const previousDates = Object.keys(previousSeries?.$overall || {});

if (!currentDates.length || !previousDates.length) {
return { from_date: '', to_date: '' };
}

return {
from_date: this.roundToStartOfDay(previousDates[0]),
to_date: this.roundToStartOfDay(currentDates[0]),
};
}

calculateInboxMetrics(
inboxSeries?: Record<MixpanelInboxSeriesNameEnum, IChannelData>,
inboxTimeComparison?: Record<MixpanelInboxSeriesNameEnum, IChannelData>,
orgId?: string,
dateRange?: IDateRange
): IInboxMetrics {
const emptyResponse = { current: 0, previous: 0, change: 0 };

if (!inboxSeries || !inboxTimeComparison || !orgId || !dateRange) {
return {
sessionInitialized: emptyResponse,
updatePreferences: emptyResponse,
markNotification: emptyResponse,
updateAction: emptyResponse,
};
}

Logger.debug(`Calculating inbox metrics for organization`);
const getMetricStats = (
currentSeriesData: IChannelData | undefined,
previousSeriesData: IChannelData | undefined
): IMetricStats => {
if (!currentSeriesData || !previousSeriesData) {
Logger.debug(`No series data available for ${orgId}`);

return emptyResponse;
}

const currentOrgData = currentSeriesData[orgId];
const previousOrgData = previousSeriesData[orgId];

if (!currentOrgData || !previousOrgData) {
Logger.debug(`No series data available for ${orgId}`);

return emptyResponse;
}

const currentData = currentOrgData[this.roundToStartOfDay(dateRange.to_date)];
const previousData = previousOrgData[this.roundToStartOfDay(dateRange.from_date)];

if (!currentData || !previousData) {
Logger.debug(`No data available for ${orgId}`);

return emptyResponse;
}

const current = Number(currentData || 0);
const previous = Number(previousData || 0);
const change = this.calculateChange(current, previous);

Logger.debug(`Metric stats for ${orgId}: current=${current}, previous=${previous}, change=${change}%`);

return { current, previous, change };
};

return {
sessionInitialized: getMetricStats(
inboxSeries[MixpanelInboxSeriesNameEnum.INBOX_SESSION_INITIALIZED],
inboxTimeComparison[MixpanelInboxSeriesNameEnum.INBOX_SESSION_INITIALIZED]
),
updatePreferences: getMetricStats(
inboxSeries[MixpanelInboxSeriesNameEnum.INBOX_UPDATE_PREFERENCES],
inboxTimeComparison[MixpanelInboxSeriesNameEnum.INBOX_UPDATE_PREFERENCES]
),
markNotification: getMetricStats(
inboxSeries[MixpanelInboxSeriesNameEnum.INBOX_MARK_NOTIFICATION],
inboxTimeComparison[MixpanelInboxSeriesNameEnum.INBOX_MARK_NOTIFICATION]
),
updateAction: getMetricStats(
inboxSeries[MixpanelInboxSeriesNameEnum.INBOX_UPDATE_ACTION],
inboxTimeComparison[MixpanelInboxSeriesNameEnum.INBOX_UPDATE_ACTION]
),
};
}

calculateOverallInboxMetrics(
orgId: string,
inboxSeries: IMixpanelInboxResponse['series'],
inboxTimeComparison: IMixpanelInboxResponse['time_comparison']['series']
): IInboxMetrics {
Logger.debug('Calculating overall inbox metrics');

const getMetricStats = (
currentSeriesData: IChannelData | undefined,
previousSeriesData: IChannelData | undefined
): IMetricStats => {
if (!currentSeriesData?.$overall || !previousSeriesData?.$overall) {
return { current: 0, previous: 0, change: 0 };
}

const current = Number(Object.values(currentSeriesData.$overall)[0] || 0);
const previous = Number(Object.values(previousSeriesData.$overall)[0] || 0);
const change = this.calculateChange(current, previous);

return { current, previous, change };
};

return {
sessionInitialized: getMetricStats(
inboxSeries[MixpanelInboxSeriesNameEnum.INBOX_SESSION_INITIALIZED][orgId],
inboxTimeComparison[MixpanelInboxSeriesNameEnum.INBOX_SESSION_INITIALIZED][orgId]
),
updatePreferences: getMetricStats(
inboxSeries[MixpanelInboxSeriesNameEnum.INBOX_UPDATE_PREFERENCES][orgId],
inboxTimeComparison[MixpanelInboxSeriesNameEnum.INBOX_UPDATE_PREFERENCES][orgId]
),
markNotification: getMetricStats(
inboxSeries[MixpanelInboxSeriesNameEnum.INBOX_MARK_NOTIFICATION][orgId],
inboxTimeComparison[MixpanelInboxSeriesNameEnum.INBOX_MARK_NOTIFICATION][orgId]
),
updateAction: getMetricStats(
inboxSeries[MixpanelInboxSeriesNameEnum.INBOX_UPDATE_ACTION][orgId],
inboxTimeComparison[MixpanelInboxSeriesNameEnum.INBOX_UPDATE_ACTION][orgId]
),
};
}

calculateEventTriggersMetrics(
subscriberSeries: IChannelData,
subscriberTimeComparison: IChannelData
): IOrganizationMetrics['eventTriggers'] {
const current = Number(Object.values(subscriberSeries?.$overall || {})[0] || 0);
const previous = Number(Object.values(subscriberTimeComparison?.$overall || {})[0] || 0);
const change = this.calculateChange(current, previous);

return { current, previous, change };
}

calculateChannelBreakdown(
workflowSeries: IChannelData,
workflowTimeComparison: IChannelData
): IOrganizationMetrics['channelBreakdown'] {
const channelBreakdown: IOrganizationMetrics['channelBreakdown'] = {
[ChannelTypeEnum.EMAIL]: { current: 0, previous: 0, change: 0 },
[ChannelTypeEnum.SMS]: { current: 0, previous: 0, change: 0 },
[ChannelTypeEnum.PUSH]: { current: 0, previous: 0, change: 0 },
[ChannelTypeEnum.IN_APP]: { current: 0, previous: 0, change: 0 },
[ChannelTypeEnum.CHAT]: { current: 0, previous: 0, change: 0 },
};

const orgWorkflowData = workflowSeries;
const orgWorkflowPreviousData = workflowTimeComparison;

if (orgWorkflowData && orgWorkflowPreviousData) {
Object.entries(orgWorkflowData).forEach(([channel, data]) => {
if (channel !== '$overall') {
const currentChannelData = Number(Object.values(data.$overall || {})[0] || 0);
const previousChannelData = Number(Object.values(orgWorkflowPreviousData[channel]?.$overall || {})[0] || 0);
const currentChannelChange = this.calculateChange(currentChannelData, previousChannelData);

channelBreakdown[channel] = {
current: currentChannelData,
previous: previousChannelData,
change: currentChannelChange,
};
}
});
} else {
Logger.debug(`No workflow data available for organization`);
}

return channelBreakdown;
}

calculateWorkflowStats(
triggerEventSeries: IChannelData,
previousTriggerEventSeries: IChannelData
): IMixpanelTriggerResponse['workflowStats']['workflows'] {
Logger.debug('Calculating workflow statistics');
const workflowStats: IMixpanelTriggerResponse['workflowStats']['workflows'] = {};

const currentWorkflowsData = triggerEventSeries.undefined;
const previousWorkflowsData = previousTriggerEventSeries.undefined;
if (!currentWorkflowsData || !previousWorkflowsData) {
Logger.debug(`No workflow data found for organization`);

return workflowStats;
}

Object.entries(currentWorkflowsData)
.filter(([name]) => name !== '$overall')
.forEach(([name, data]) => {
const current = Number(Object.values(data)[0] || 0);
const previous = Number(Object.values(previousWorkflowsData[name] || {})[0] || 0);
const change = this.calculateChange(current, previous);

workflowStats[name] = { current, previous, change };
Logger.debug(`Workflow stats for ${name}: current=${current}, previous=${previous}, change=${change}%`);
});

return workflowStats;
}
}
Loading
Loading