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: hello world
  • Loading branch information
scopsy committed Dec 4, 2024
commit 6eb853e30cf6be2f32bacdb8a694588505299cbb
113 changes: 113 additions & 0 deletions apps/api/mixpanel-inbox-cache.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
"timestamp": 1733311977381,
"data": {
"headers": ["$event", "$distinct_id"],
"computed_at": "2024-12-04T11:32:56.526066+00:00",
"date_range": {
"from_date": "2024-11-25T00:00:00+02:00",
"to_date": "2024-12-04T13:32:54.570035+02:00"
},
"meta": {
"min_sampling_factor": 1,
"is_segmentation_limit_hit": false,
"report_sections": {
"group": [
{
"bookmark": {
"dataGroupId": "7400984862319914261",
"dataset": "$mixpanel",
"propertyType": "string",
"resourceType": "people",
"search": "",
"value": "$distinct_id"
}
}
],
"show": [
{
"metric_key": "A. Session Initialized - [Inbox] [Total Events]"
},
{
"metric_key": "B. Update Preferences - [Inbox] [Total Events]"
},
{
"metric_key": "C. Mark Notification As - [Inbox] [Total Events]"
},
{
"metric_key": "D. Update Notification Action - [Inbox] [Total Events]"
}
]
}
},
"series": {
"A. Session Initialized - [Inbox] [Total Events]": {
"$overall": {
"2024-11-25T00:00:00+02:00": 1110879,
"2024-12-02T00:00:00+02:00": 350009
},
"66bdd7144a759ebff0deb5e2": {
"2024-11-25T00:00:00+02:00": 1110879,
"2024-12-02T00:00:00+02:00": 350009
}
},
"B. Update Preferences - [Inbox] [Total Events]": {
"$overall": {
"2024-11-25T00:00:00+02:00": 2,
"2024-12-02T00:00:00+02:00": 0
},
"66bdd7144a759ebff0deb5e2": {
"2024-11-25T00:00:00+02:00": 2,
"2024-12-02T00:00:00+02:00": 0
}
},
"C. Mark Notification As - [Inbox] [Total Events]": {
"$overall": {
"2024-11-25T00:00:00+02:00": 249,
"2024-12-02T00:00:00+02:00": 74
},
"66bdd7144a759ebff0deb5e2": {
"2024-11-25T00:00:00+02:00": 249,
"2024-12-02T00:00:00+02:00": 74
}
}
},
"time_comparison": {
"date_range": {
"from_date": "2024-10-25T00:00:00+03:00",
"to_date": "2024-11-03T23:59:59.999000+02:00"
},
"series": {
"A. Session Initialized - [Inbox] [Total Events]": {
"$overall": {
"2024-10-21T00:00:00+03:00": 227100,
"2024-10-28T00:00:00+02:00": 474209
},
"66bdd7144a759ebff0deb5e2": {
"2024-10-21T00:00:00+03:00": 227100,
"2024-10-28T00:00:00+02:00": 474209
}
},
"B. Update Preferences - [Inbox] [Total Events]": {
"$overall": {
"2024-10-21T00:00:00+03:00": 6,
"2024-10-28T00:00:00+02:00": 2
},
"66bdd7144a759ebff0deb5e2": {
"2024-10-21T00:00:00+03:00": 6,
"2024-10-28T00:00:00+02:00": 2
}
},
"C. Mark Notification As - [Inbox] [Total Events]": {
"$overall": {
"2024-10-21T00:00:00+03:00": 94,
"2024-10-28T00:00:00+02:00": 162
},
"66bdd7144a759ebff0deb5e2": {
"2024-10-21T00:00:00+03:00": 94,
"2024-10-28T00:00:00+02:00": 162
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,68 @@ interface IMixpanelResponse {
workflowStats: {
workflows: {
[name: string]: {
current: {
total: number;
period: string;
};
previous: {
total: number;
period: string;
};
current: number;
previous: number;
change: number;
};
};
};
}

interface IInboxResponse {
series: {
'A. Session Initialized - [Inbox] [Total Events]': ISeriesData;
'B. Update Preferences - [Inbox] [Total Events]': ISeriesData;
'C. Mark Notification As - [Inbox] [Total Events]': ISeriesData;
'D. Update Notification Action - [Inbox] [Total Events]': ISeriesData;
};
}

interface IMetricStats {
current: number;
previous: number;
change: number;
}

interface IInboxMetrics {
sessionInitialized: IMetricStats;
updatePreferences: IMetricStats;
markNotification: IMetricStats;
updateAction: IMetricStats;
}

interface IUsageInsightsResponse {
series: IMixpanelResponse['series'];
workflowStats: IMixpanelResponse['workflowStats'];
inboxStats: {
byOrganization: {
[organizationId: string]: IInboxMetrics;
};
overall: IInboxMetrics;
};
}

interface ICombinedMetrics extends IOrganizationMetrics {
inboxMetrics?: IInboxMetrics;
}

@Injectable()
export class UsageInsights {
private readonly CACHE_FILE = join(process.cwd(), 'mixpanel-insights-cache.json');
private readonly INBOX_CACHE_FILE = join(process.cwd(), 'mixpanel-inbox-cache.json');
private readonly CACHE_TTL = 24 * 60 * 60 * 1000; // 24 hours in milliseconds

constructor(private organizationRepository: CommunityOrganizationRepository) {}

private async readCacheFile() {
private async readCacheFile(cacheFile: string) {
try {
const fileContent = await fs.readFile(this.CACHE_FILE, 'utf-8');
const fileContent = await fs.readFile(cacheFile, 'utf-8');
const cache = JSON.parse(fileContent);

if (cache.timestamp && Date.now() - cache.timestamp < this.CACHE_TTL) {
Logger.log('Using cached Mixpanel insights data');
Logger.log(`Using cached Mixpanel data from ${cacheFile}`);

return cache.data as IMixpanelResponse;
return cache.data;
}

Logger.log('Cache expired, fetching fresh data');
Expand All @@ -96,17 +128,17 @@ export class UsageInsights {
}
}

private async writeCacheFile(data: IMixpanelResponse) {
private async writeCacheFile(data: any, cacheFile: string) {
try {
const cache = {
timestamp: Date.now(),
data,
};

await fs.writeFile(this.CACHE_FILE, JSON.stringify(cache, null, 2));
Logger.log('Mixpanel insights data cached successfully');
await fs.writeFile(cacheFile, JSON.stringify(cache, null, 2));
Logger.log(`Mixpanel data cached successfully to ${cacheFile}`);
} catch (error) {
Logger.error('Failed to cache Mixpanel insights data:', error);
Logger.error('Failed to cache Mixpanel data:', error);
}
}

Expand All @@ -118,14 +150,13 @@ export class UsageInsights {
return Number(((current - previous) / previous) * 100);
}

private async fetchMixpanelInsights() {
const cachedData = await this.readCacheFile();
private async fetchMixpanelInsights(): Promise<IMixpanelResponse | null> {
const cachedData = await this.readCacheFile(this.CACHE_FILE);
if (cachedData) {
return cachedData;
}

try {
console.log(process.env.MIXPANEL_BASIC_AUTH_TOKEN);
const response = await axios.get<IMixpanelResponse>('https://mixpanel.com/api/2.0/insights', {
params: {
project_id: '2667883',
Expand All @@ -137,17 +168,78 @@ export class UsageInsights {
},
});

await this.writeCacheFile(response.data);
await this.writeCacheFile(response.data, this.CACHE_FILE);

return response.data;
} catch (error) {
Logger.log('a', error.response.data);
Logger.error('Error fetching Mixpanel insights:', error);

return null;
}
}

private async fetchInboxInsights(): Promise<IInboxResponse | null> {
const cachedData = await this.readCacheFile(this.INBOX_CACHE_FILE);
if (cachedData) {
return cachedData;
}

try {
const response = await axios.get<IInboxResponse>('https://mixpanel.com/api/2.0/insights', {
params: {
project_id: '2667883',
bookmark_id: '68521376',
},
headers: {
Authorization: `Basic ${process.env.MIXPANEL_BASIC_AUTH_TOKEN}`,
'Content-Type': 'application/json',
},
});

await this.writeCacheFile(response.data, this.INBOX_CACHE_FILE);

return response.data;
} catch (error) {
Logger.error('Error fetching Inbox insights:', error?.response?.data || error);

return null;
}
}

private calculateInboxMetrics(inboxSeries: IInboxResponse['series'], orgId: string): IInboxMetrics {
const getMetricStats = (seriesData: ISeriesData | undefined, orgKey: string): IMetricStats => {
const data = seriesData?.[orgKey];
if (!data) {
return {
current: 0,
previous: 0,
change: 0,
};
}

const current = data['2024-12-02T00:00:00+02:00'] || 0;
const previous = data['2024-11-25T00:00:00+02:00'] || 0;

return {
current,
previous,
change: this.calculateChange(current, previous),
};
};

// Always return a complete metrics object
return {
sessionInitialized: getMetricStats(inboxSeries['A. Session Initialized - [Inbox] [Total Events]'], orgId),
updatePreferences: getMetricStats(inboxSeries['B. Update Preferences - [Inbox] [Total Events]'], orgId),
markNotification: getMetricStats(inboxSeries['C. Mark Notification As - [Inbox] [Total Events]'], orgId),
updateAction: getMetricStats(inboxSeries['D. Update Notification Action - [Inbox] [Total Events]'], orgId),
};
}

private calculateOverallInboxMetrics(inboxSeries: IInboxResponse['series']): IInboxMetrics {
return this.calculateInboxMetrics(inboxSeries, '$overall');
}

private createOrganizationMetrics(
orgId: string,
subscriberSeries: ISeriesData,
Expand Down Expand Up @@ -181,10 +273,7 @@ export class UsageInsights {
return orgMetrics;
}

private async logOrganizationMetrics(
metrics: IOrganizationMetrics,
workflowStats: IMixpanelResponse['workflowStats']
) {
private async logOrganizationMetrics(metrics: ICombinedMetrics, workflowStats: IMixpanelResponse['workflowStats']) {
try {
const organization = await this.organizationRepository.findById(metrics.id);
if (!organization) {
Expand Down Expand Up @@ -235,8 +324,8 @@ export class UsageInsights {
}

@InstrumentUsecase()
async execute(command: UsageInsightsCommand): Promise<any> {
const mixpanelData = await this.fetchMixpanelInsights();
async execute(command: UsageInsightsCommand): Promise<IUsageInsightsResponse | null> {
const [mixpanelData, inboxData] = await Promise.all([this.fetchMixpanelInsights(), this.fetchInboxInsights()]);

if (!mixpanelData?.series) {
Logger.error('No Mixpanel data available');
Expand All @@ -257,11 +346,24 @@ export class UsageInsights {
const workflowStats = this.calculateWorkflowStats(subscriberSeries);
mixpanelData.workflowStats = workflowStats;

// Initialize inbox stats with default values
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 },
};

const inboxStats: IUsageInsightsResponse['inboxStats'] = {
byOrganization: {},
overall: inboxData?.series ? this.calculateOverallInboxMetrics(inboxData.series) : defaultInboxMetrics,
};

// Process each organization's data
for (const [orgId, orgData] of Object.entries(workflowSeries)) {
if (orgId === '$overall') continue;

const metrics = this.createOrganizationMetrics(orgId, subscriberSeries, workflowSeries);
const metrics = this.createOrganizationMetrics(orgId, subscriberSeries, workflowSeries) as ICombinedMetrics;

// Calculate subscriber notifications change
metrics.subscriberNotifications.change = this.calculateChange(
Expand All @@ -280,9 +382,23 @@ export class UsageInsights {
])
);

// Add inbox metrics if available
if (inboxData?.series) {
const inboxMetrics = this.calculateInboxMetrics(inboxData.series, orgId);
metrics.inboxMetrics = inboxMetrics;
inboxStats.byOrganization[orgId] = inboxMetrics;
} else {
metrics.inboxMetrics = defaultInboxMetrics;
inboxStats.byOrganization[orgId] = defaultInboxMetrics;
}

await this.logOrganizationMetrics(metrics, workflowStats);
}

return mixpanelData;
return {
series: mixpanelData.series,
workflowStats: mixpanelData.workflowStats,
inboxStats,
};
}
}
Loading