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
fix: working state
  • Loading branch information
scopsy committed Dec 22, 2024
commit 4c9bd12f54994f2f166f13125991349dbb40cbef
157 changes: 82 additions & 75 deletions apps/api/src/app/insights/services/metrics-calculator.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Injectable, Logger } from '@nestjs/common';
import { startOfDay, formatISO } from 'date-fns';
import { ChannelTypeEnum } from '@novu/shared';
import {
IDateRange,
ISeriesData,
IInboxResponse,
IMixpanelInboxResponse,
IInboxMetrics,
IOrganizationMetrics,
IMixpanelResponse,
IMixpanelTriggerResponse,
IMetricStats,
MixpanelSeriesNameEnum,
IChannelData,
MixpanelInboxSeriesNameEnum,
} from '../types/usage-insights.types';

@Injectable()
Expand Down Expand Up @@ -52,15 +53,24 @@ export class MetricsCalculatorService {
}

calculateInboxMetrics(
inboxSeries: IInboxResponse['series'],
inboxTimeComparison: IInboxResponse['time_comparison']['series'],
orgId: string,
dateRange: IDateRange
inboxSeries?: Record<MixpanelInboxSeriesNameEnum, IChannelData>,
inboxTimeComparison?: Record<MixpanelInboxSeriesNameEnum, IChannelData>,
orgId?: string,
dateRange?: IDateRange
): IInboxMetrics {
Logger.debug(`Calculating inbox metrics for organization: ${orgId}`);
if (!inboxSeries || !inboxTimeComparison || !orgId || !dateRange) {
return {
sessionInitialized: { current: 0, previous: 0, change: 0 },
updatePreferences: { current: 0, previous: 0, change: 0 },
markNotification: { current: 0, previous: 0, change: 0 },
updateAction: { current: 0, previous: 0, change: 0 },
};
}

Logger.debug(`Calculating inbox metrics for organization`);
const getMetricStats = (
currentSeriesData: ISeriesData | undefined,
previousSeriesData: ISeriesData | undefined
currentSeriesData: IChannelData | undefined,
previousSeriesData: IChannelData | undefined
): IMetricStats => {
if (!currentSeriesData || !previousSeriesData) {
Logger.debug(`No series data available for ${orgId}`);
Expand All @@ -80,9 +90,6 @@ export class MetricsCalculatorService {
const currentData = currentOrgData[this.roundToStartOfDay(dateRange.to_date)];
const previousData = previousOrgData[this.roundToStartOfDay(dateRange.from_date)];

console.log(currentOrgData, 'HIII', this.roundToStartOfDay(dateRange.to_date));
console.log(previousOrgData, 'HIII', this.roundToStartOfDay(dateRange.from_date));

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

Expand All @@ -100,28 +107,28 @@ export class MetricsCalculatorService {

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

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

Expand All @@ -142,94 +149,94 @@ export class MetricsCalculatorService {

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

createOrganizationMetrics(
orgId: string,
subscriberSeries: ISeriesData,
subscriberTimeComparison: ISeriesData,
workflowSeries: ISeriesData,
workflowTimeComparison: ISeriesData
): IOrganizationMetrics {
Logger.debug(`Creating organization metrics for: ${orgId}`);

const orgMetrics: IOrganizationMetrics = {
id: orgId,
name: '',
subscriberNotifications: {
current: Number(Object.values(subscriberSeries[orgId]?.$overall || {})[0] || 0),
previous: Number(Object.values(subscriberTimeComparison[orgId]?.$overall || {})[0] || 0),
change: 0,
},
channelBreakdown: {},
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 },
};

Logger.debug(`Subscriber notifications for ${orgId}:`, orgMetrics.subscriberNotifications);
const orgWorkflowData = workflowSeries;
const orgWorkflowPreviousData = workflowTimeComparison;

const orgWorkflowData = workflowSeries[orgId];
const orgWorkflowPreviousData = workflowTimeComparison[orgId];
if (orgWorkflowData && orgWorkflowPreviousData) {
Object.entries(orgWorkflowData).forEach(([channel, data]) => {
if (channel !== '$overall') {
const current = Number(Object.values(data.$overall || {})[0] || 0);
const previous = Number(Object.values(orgWorkflowPreviousData[channel]?.$overall || {})[0] || 0);

orgMetrics.channelBreakdown[channel] = {
current,
previous,
change: 0,
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,
};

Logger.debug(`Channel metrics for ${orgId}/${channel}: current=${current}, previous=${previous}`);
}
});
} else {
Logger.debug(`No workflow data available for organization: ${orgId}`);
Logger.debug(`No workflow data available for organization`);
}

return orgMetrics;
return channelBreakdown;
}

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

const orgData = subscriberSeries[orgId]?.undefined;
const orgPreviousData = subscriberTimeComparison[orgId]?.undefined;
if (!orgData || !orgPreviousData) {
Logger.debug(`No workflow data found for organization: ${orgId}`);
const currentWorkflowsData = triggerEventSeries.undefined;
const previousWorkflowsData = previousTriggerEventSeries.undefined;
if (!currentWorkflowsData || !previousWorkflowsData) {
Logger.debug(`No workflow data found for organization`);

return workflowStats;
}

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

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

Expand Down
10 changes: 5 additions & 5 deletions apps/api/src/app/insights/services/mixpanel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, Logger } from '@nestjs/common';
import axios from 'axios';
import { promises as fs } from 'fs';
import { join } from 'path';
import { IMixpanelResponse, IInboxResponse } from '../usecases/usage-insights/types/usage-insights.types';
import { IMixpanelInboxResponse, IMixpanelTriggerResponse } from '../types/usage-insights.types';

const USE_INSIGHTS_CACHE = process.env.USE_INSIGHTS_CACHE === 'true';

Expand Down Expand Up @@ -61,7 +61,7 @@ export class MixpanelService {
}
}

async fetchMixpanelInsights(): Promise<IMixpanelResponse | null> {
async fetchMixpanelInsights(): Promise<IMixpanelTriggerResponse | null> {
Logger.debug('Fetching Mixpanel insights');
const cachedData = await this.readCacheFile(this.CACHE_FILE);
if (cachedData) {
Expand All @@ -70,7 +70,7 @@ export class MixpanelService {

try {
Logger.debug('Making Mixpanel API request for insights');
const response = await axios.get<IMixpanelResponse>('https://mixpanel.com/api/2.0/insights', {
const response = await axios.get<IMixpanelTriggerResponse>('https://mixpanel.com/api/2.0/insights', {
params: {
project_id: '2667883',
bookmark_id: '68515975',
Expand All @@ -92,7 +92,7 @@ export class MixpanelService {
}
}

async fetchInboxInsights(): Promise<IInboxResponse | null> {
async fetchInboxInsights(): Promise<IMixpanelInboxResponse | null> {
Logger.debug('Fetching Inbox insights');
const cachedData = await this.readCacheFile(this.INBOX_CACHE_FILE);
if (cachedData) {
Expand All @@ -102,7 +102,7 @@ export class MixpanelService {
try {
Logger.debug('Making Mixpanel API request for inbox insights');

const response = await axios.get<IInboxResponse>('https://mixpanel.com/api/2.0/insights', {
const response = await axios.get<IMixpanelInboxResponse>('https://mixpanel.com/api/2.0/insights', {
params: {
project_id: '2667883',
bookmark_id: '68521376',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { CommunityOrganizationRepository } from '@novu/dal';
import { FeatureFlagsService } from '@novu/application-generic';
import { FeatureFlagsKeysEnum } from '@novu/shared';
import { usageInsightsWorkflow } from '@novu/notifications';

import { IDateRange, ICombinedMetrics, IMixpanelResponse } from '../usecases/usage-insights/types/usage-insights.types';
import { IDateRange, IOrganizationMetrics } from '../types/usage-insights.types';

@Injectable()
export class OrganizationNotificationService {
Expand All @@ -13,28 +12,18 @@ export class OrganizationNotificationService {
private featureFlagsService: FeatureFlagsService
) {}

async sendOrganizationNotification(
metrics: ICombinedMetrics,
workflowStats: IMixpanelResponse['workflowStats'],
dateRange: IDateRange
) {
Logger.debug(`Processing metrics for organization: ${metrics.id}`);
async sendOrganizationNotification(organizationId: string, metrics: IOrganizationMetrics, dateRange: IDateRange) {
Logger.debug(`Processing metrics for organization: ${organizationId}`);
try {
const organization = await this.organizationRepository.findById('675fe9bcab6a05bb6dcb7dab');

if (!organization) {
Logger.warn(`Organization not found in repository: ${metrics.id}`);
Logger.warn(`Organization not found in repository: ${organizationId}`);

return;
}

const enrichedMetrics = {
...metrics,
name: organization.name,
workflowStats: workflowStats.workflows,
};

Logger.debug(`Enriched metrics for ${organization.name}:`, enrichedMetrics);
Logger.debug(`Enriched metrics for ${organization.name}:`, metrics);

const isEnabled = await this.featureFlagsService.get(FeatureFlagsKeysEnum.IS_USAGE_INSIGHTS_ENABLED, false, {
environmentId: 'system',
Expand All @@ -43,42 +32,37 @@ export class OrganizationNotificationService {
});

if (!isEnabled) {
Logger.log('Skipping notification delivery - usage insights disabled by feature flag', enrichedMetrics);
Logger.log('Skipping notification delivery - usage insights disabled by feature flag', metrics);

return;
}

const payload = {
organizationName: organization.name,
period: {
current: dateRange.to_date,
previous: dateRange.from_date,
},
subscriberNotifications: metrics.eventTriggers,
channelBreakdown: metrics.channelBreakdown,
inboxMetrics: metrics.inboxMetrics,
workflowStats: metrics.workflowStats,
};

delete (payload.channelBreakdown as any).trigger;

Logger.debug(`Sending notification for ${organization.name} with payload:`, payload);

await usageInsightsWorkflow.trigger({
to: {
subscriberId: '675fe9bcab6a05bb6dcb7dab_11',
email: `dima+testing-${organization._id}@novu.co`,
},
payload: {
organizationName: organization.name,
period: {
current: dateRange.to_date,
previous: dateRange.from_date,
},
subscriberNotifications: metrics.subscriberNotifications,
channelBreakdown: {
email: metrics.channelBreakdown.email || { current: 0, previous: 0, change: 0 },
sms: metrics.channelBreakdown.sms || { current: 0, previous: 0, change: 0 },
push: metrics.channelBreakdown.push || { current: 0, previous: 0, change: 0 },
inApp: metrics.channelBreakdown.in_app || { current: 0, previous: 0, change: 0 },
chat: metrics.channelBreakdown.chat || { current: 0, previous: 0, change: 0 },
},
inboxMetrics: {
sessionInitialized: metrics.inboxMetrics?.sessionInitialized,
updatePreferences: metrics.inboxMetrics?.updatePreferences,
markNotification: metrics.inboxMetrics?.markNotification,
updateAction: metrics.inboxMetrics?.updateAction,
},
workflowStats: workflowStats.workflows,
},
payload,
secretKey: process.env.NOVU_INTERNAL_SECRET_KEY,
});
} catch (error) {
Logger.error(`Failed to process metrics for organization ${metrics.id}:`, error);
Logger.error(`Failed to process metrics for organization ${organizationId}:`, error);
}
}
}
Loading
Loading