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: bugs
  • Loading branch information
scopsy committed Dec 22, 2024
commit 29457498aceff4f1e550b915185c0b0a62d85340
1,116 changes: 1,106 additions & 10 deletions apps/api/mixpanel-inbox-cache.json

Large diffs are not rendered by default.

48,868 changes: 48,767 additions & 101 deletions apps/api/mixpanel-insights-cache.json

Large diffs are not rendered by default.

78 changes: 41 additions & 37 deletions apps/api/src/app/insights/services/metrics-calculator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
IMixpanelResponse,
IMetricStats,
MixpanelSeriesNameEnum,
IChannelData,
} from '../types/usage-insights.types';

@Injectable()
Expand Down Expand Up @@ -59,65 +60,74 @@ export class MetricsCalculatorService {
Logger.debug(`Calculating inbox metrics for organization: ${orgId}`);
const getMetricStats = (
currentSeriesData: ISeriesData | undefined,
previousSeriesData: ISeriesData | undefined,
orgKey: string
previousSeriesData: ISeriesData | undefined
): IMetricStats => {
if (!currentSeriesData || !previousSeriesData) {
Logger.debug(`No series data available for ${orgKey}`);
Logger.debug(`No series data available for ${orgId}`);

return { current: 0, previous: 0, change: 0 };
}

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

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

return { current: 0, previous: 0, change: 0 };
}

const currentData = currentSeriesData[orgKey];
const previousData = previousSeriesData[orgKey];
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 ${orgKey}`);
Logger.debug(`No data available for ${orgId}`);

return { current: 0, previous: 0, change: 0 };
}

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

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

return { current, previous, change };
};

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

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

const getMetricStats = (
currentSeriesData: ISeriesData | undefined,
previousSeriesData: ISeriesData | undefined
currentSeriesData: IChannelData | undefined,
previousSeriesData: IChannelData | undefined
): IMetricStats => {
if (!currentSeriesData?.$overall || !previousSeriesData?.$overall) {
return { current: 0, previous: 0, change: 0 };
Expand All @@ -132,20 +142,20 @@ export class MetricsCalculatorService {

return {
sessionInitialized: getMetricStats(
inboxSeries[MixpanelSeriesNameEnum.INBOX_SESSION_INITIALIZED],
inboxTimeComparison[MixpanelSeriesNameEnum.INBOX_SESSION_INITIALIZED]
inboxSeries[MixpanelSeriesNameEnum.INBOX_SESSION_INITIALIZED][orgId],
inboxTimeComparison[MixpanelSeriesNameEnum.INBOX_SESSION_INITIALIZED][orgId]
),
updatePreferences: getMetricStats(
inboxSeries[MixpanelSeriesNameEnum.INBOX_UPDATE_PREFERENCES],
inboxTimeComparison[MixpanelSeriesNameEnum.INBOX_UPDATE_PREFERENCES]
inboxSeries[MixpanelSeriesNameEnum.INBOX_UPDATE_PREFERENCES][orgId],
inboxTimeComparison[MixpanelSeriesNameEnum.INBOX_UPDATE_PREFERENCES][orgId]
),
markNotification: getMetricStats(
inboxSeries[MixpanelSeriesNameEnum.INBOX_MARK_NOTIFICATION],
inboxTimeComparison[MixpanelSeriesNameEnum.INBOX_MARK_NOTIFICATION]
inboxSeries[MixpanelSeriesNameEnum.INBOX_MARK_NOTIFICATION][orgId],
inboxTimeComparison[MixpanelSeriesNameEnum.INBOX_MARK_NOTIFICATION][orgId]
),
updateAction: getMetricStats(
inboxSeries[MixpanelSeriesNameEnum.INBOX_UPDATE_ACTION],
inboxTimeComparison[MixpanelSeriesNameEnum.INBOX_UPDATE_ACTION]
inboxSeries[MixpanelSeriesNameEnum.INBOX_UPDATE_ACTION][orgId],
inboxTimeComparison[MixpanelSeriesNameEnum.INBOX_UPDATE_ACTION][orgId]
),
};
}
Expand Down Expand Up @@ -197,23 +207,17 @@ export class MetricsCalculatorService {
}

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

const firstOrgId = Object.keys(subscriberSeries).find((key) => key !== '$overall');
if (!firstOrgId) {
Logger.debug('No organization data found for workflow stats');

return workflowStats;
}

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

return workflowStats;
}
Expand Down
1 change: 1 addition & 0 deletions apps/api/src/app/insights/services/mixpanel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,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', {
params: {
project_id: '2667883',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class OrganizationNotificationService {
email: `dima+testing-${organization._id}@novu.co`,
},
payload: {
organizationName: organization.name,
period: {
current: dateRange.to_date,
previous: dateRange.from_date,
Expand All @@ -63,6 +64,8 @@ export class OrganizationNotificationService {
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,25 @@ export class UsageInsights {
return null;
}

const seriesDateRange = {
from_date: mixpanelData.time_comparison.date_range.from_date,
to_date: mixpanelData.date_range.to_date,
};

const workflowStats = this.metricsCalculator.calculateWorkflowStats(subscriberSeries, subscriberTimeComparison);
mixpanelData.workflowStats = workflowStats;

const defaultInboxMetrics: IInboxMetrics = {
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('Initializing inbox stats');
const inboxStats: IUsageInsightsResponse['inboxStats'] = {
byOrganization: {},
overall: inboxData?.series
? this.metricsCalculator.calculateOverallInboxMetrics(inboxData.series, inboxData.time_comparison.series)
: defaultInboxMetrics,
};

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

const workflowStats = this.metricsCalculator.calculateWorkflowStats(
orgId,
subscriberSeries,
subscriberTimeComparison
);
mixpanelData.workflowStats = workflowStats;

const defaultInboxMetrics: IInboxMetrics = {
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('Initializing inbox stats');
Logger.debug(`Processing metrics for organization: ${orgId}`);
const metrics = this.metricsCalculator.createOrganizationMetrics(
orgId,
Expand Down Expand Up @@ -103,25 +95,23 @@ export class UsageInsights {
inboxData.series,
inboxData.time_comparison.series,
orgId,
seriesDateRange
{
from_date: inboxData.time_comparison.date_range.from_date,
to_date: inboxData.date_range.from_date,
}
);
metrics.inboxMetrics = inboxMetrics;
inboxStats.byOrganization[orgId] = inboxMetrics;
} else {
Logger.debug(`Using default inbox metrics for organization: ${orgId}`);
metrics.inboxMetrics = defaultInboxMetrics;
inboxStats.byOrganization[orgId] = defaultInboxMetrics;
}

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

Logger.debug('UsageInsights execution completed successfully');

return {
series: mixpanelData.series,
workflowStats: mixpanelData.workflowStats,
inboxStats,
};
}
}
19 changes: 15 additions & 4 deletions libs/notifications/src/workflows/usage-insights/email.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ function ChannelBreakdown({ channels }: { channels: IUsageEmailData['channelBrea
</Column>
<Column className="p-2">
<MetricCard
title="SMS"
current={channels.sms.current}
previous={channels.sms.previous}
change={channels.sms.change}
title="Inbox"
current={channels.inApp.current}
previous={channels.inApp.previous}
change={channels.inApp.change}
/>
</Column>
<Column className="p-2 pr-0">
Expand All @@ -114,6 +114,17 @@ function ChannelBreakdown({ channels }: { channels: IUsageEmailData['channelBrea
}

function InboxMetrics({ metrics }: { metrics: IUsageEmailData['inboxMetrics'] }) {
if (
!metrics.markNotification.current &&
!metrics.markNotification.previous &&
!metrics.sessionInitialized.current &&
!metrics.sessionInitialized.previous &&
!metrics.updatePreferences.previous &&
!metrics.updatePreferences.current
) {
return null;
}

return (
<Section className="mt-6">
<SectionHeader title="Inbox Activity" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { workflow } from '@novu/framework';
import { z } from 'zod';
import UsageInsightsEmail from './email';
import { IUsageEmailData } from './types';
import { sampleUsageData } from './sample-data';

const marketingLinkSchema = z
.object({
Expand Down Expand Up @@ -75,30 +74,32 @@ export const usageInsightsWorkflow = workflow(
name: 'Usage Insights',
payloadSchema: z
.object({
organizationName: z.string().default(sampleUsageData.organizationName),
organizationName: z.string(),
period: z
.object({
current: z.string().default(sampleUsageData.period.current),
previous: z.string().default(sampleUsageData.period.previous),
current: z.string(),
previous: z.string(),
})
.required(),
subscriberNotifications: channelMetricsSchema.default(sampleUsageData.subscriberNotifications),
subscriberNotifications: channelMetricsSchema,
channelBreakdown: z
.object({
email: channelMetricsSchema.default(sampleUsageData.channelBreakdown.email),
sms: channelMetricsSchema.default(sampleUsageData.channelBreakdown.sms),
push: channelMetricsSchema.default(sampleUsageData.channelBreakdown.push),
email: channelMetricsSchema,
sms: channelMetricsSchema,
push: channelMetricsSchema,
inApp: channelMetricsSchema,
chat: channelMetricsSchema,
})
.required(),
inboxMetrics: z
.object({
sessionInitialized: channelMetricsSchema.default(sampleUsageData.inboxMetrics.sessionInitialized),
updatePreferences: channelMetricsSchema.default(sampleUsageData.inboxMetrics.updatePreferences),
markNotification: channelMetricsSchema.default(sampleUsageData.inboxMetrics.markNotification),
updateAction: channelMetricsSchema.default(sampleUsageData.inboxMetrics.updateAction),
sessionInitialized: channelMetricsSchema,
updatePreferences: channelMetricsSchema,
markNotification: channelMetricsSchema,
updateAction: channelMetricsSchema,
})
.required(),
workflowStats: z.record(z.string(), channelMetricsSchema).default(sampleUsageData.workflowStats),
workflowStats: z.record(z.string(), channelMetricsSchema),
})
.required(),
}
Expand Down
Loading