-
Notifications
You must be signed in to change notification settings - Fork 685
Expand file tree
/
Copy pathget-year-in-review.ts
More file actions
182 lines (167 loc) · 4.85 KB
/
Copy pathget-year-in-review.ts
File metadata and controls
182 lines (167 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import "server-only";
import {
getAggregateUserOpUsage,
getEOAAndInAppWalletConnections,
getRpcUsageByType,
} from "@/api/analytics";
type YearInReviewStats = {
totalRpcRequests: number;
totalWalletConnections: number;
totalMainnetSponsoredTransactions: number;
year: number;
};
/**
* Get year-in-review statistics for the current user across all their teams
* Hardcoded to 2025 (Jan 1, 2025 - Dec 31, 2025)
*/
export async function getYearInReview(
authToken: string,
teamIds: string[],
): Promise<YearInReviewStats> {
const year = 2025;
if (!authToken || teamIds.length === 0) {
return {
totalRpcRequests: 0,
totalWalletConnections: 0,
totalMainnetSponsoredTransactions: 0,
year,
};
}
// Hardcoded to 2025: Jan 1, 2025 - Dec 31, 2025
const yearStart = new Date(2025, 0, 1);
const yearEnd = new Date(2025, 11, 31, 23, 59, 59, 999);
// Fetch all data in parallel across all teams
const [rpcRequests, walletConnections, sponsoredTxs] = await Promise.all([
// Get total RPC requests across all teams
getTotalRpcRequests(teamIds, authToken, yearStart, yearEnd),
// Get total wallet connections across all teams
getTotalWalletConnections(teamIds, authToken, yearStart, yearEnd),
// Get total mainnet sponsored transactions across all teams
getTotalMainnetSponsoredTransactions(
teamIds,
authToken,
yearStart,
yearEnd,
),
]);
return {
totalRpcRequests: rpcRequests,
totalWalletConnections: walletConnections,
totalMainnetSponsoredTransactions: sponsoredTxs,
year,
};
}
async function getTotalRpcRequests(
teamIds: string[],
authToken: string,
from: Date,
to: Date,
): Promise<number> {
try {
// Aggregate RPC requests across all teams using the same API as analytics
const requests = await Promise.all(
teamIds.map(async (teamId) => {
try {
// Use getRpcUsageByType without projectId to get team-level data
// This matches the format used in the analytics pages
const usageData = await getRpcUsageByType(
{
teamId,
from,
to,
period: "all",
},
authToken,
);
// Sum up all counts from the usage data
return usageData.reduce((sum, item) => sum + (item.count || 0), 0);
} catch (error) {
console.error(`Failed to fetch RPC usage for team ${teamId}:`, error);
return 0;
}
}),
);
return requests.reduce((sum, count) => sum + count, 0);
} catch (error) {
console.error("Failed to fetch RPC requests:", error);
return 0;
}
}
async function getTotalWalletConnections(
teamIds: string[],
authToken: string,
from: Date,
to: Date,
): Promise<number> {
try {
// Aggregate wallet connections across all teams
const connections = await Promise.all(
teamIds.map(async (teamId) => {
try {
const walletStats = await getEOAAndInAppWalletConnections(
{
teamId,
from,
to,
period: "all",
},
authToken,
);
// Sum unique wallets connected (for "onboarded users" metric)
// Note: With period: "all", this should be a single aggregated stat,
// but we sum in case there are multiple stats (e.g., by wallet type)
return walletStats.reduce(
(sum, stat) => sum + (stat.uniqueWalletsConnected || 0),
0,
);
} catch (error) {
console.error(
`Failed to fetch wallet connections for team ${teamId}:`,
error,
);
return 0;
}
}),
);
return connections.reduce((sum, count) => sum + count, 0);
} catch (error) {
console.error("Failed to fetch wallet connections:", error);
return 0;
}
}
async function getTotalMainnetSponsoredTransactions(
teamIds: string[],
authToken: string,
from: Date,
to: Date,
): Promise<number> {
try {
// Aggregate mainnet sponsored transactions across all teams
// getAggregateUserOpUsage filters out testnets automatically
const transactions = await Promise.all(
teamIds.map(async (teamId) => {
try {
const aggregateStats = await getAggregateUserOpUsage(
{
teamId,
from,
to,
},
authToken,
);
return aggregateStats.successful || 0;
} catch (error) {
console.error(
`Failed to fetch mainnet sponsored transactions for team ${teamId}:`,
error,
);
return 0;
}
}),
);
return transactions.reduce((sum, count) => sum + count, 0);
} catch (error) {
console.error("Failed to fetch mainnet sponsored transactions:", error);
return 0;
}
}