Skip to content

Commit 1405289

Browse files
authored
migrate analytics endpoints from v1 to v2 (#6326)
1 parent 95cc74e commit 1405289

File tree

15 files changed

+138
-56
lines changed

15 files changed

+138
-56
lines changed

apps/dashboard/src/@/api/analytics.ts

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { fetchAnalytics } from "data/analytics/fetch-analytics";
22
import type {
33
AnalyticsQueryParams,
4+
AnalyticsQueryParamsV2,
45
InAppWalletStats,
56
RpcMethodStats,
67
TransactionStats,
@@ -9,14 +10,28 @@ import type {
910
WalletUserStats,
1011
} from "types/analytics";
1112

12-
function buildSearchParams(params: AnalyticsQueryParams): URLSearchParams {
13+
function buildSearchParams(
14+
params: AnalyticsQueryParams | AnalyticsQueryParamsV2,
15+
): URLSearchParams {
1316
const searchParams = new URLSearchParams();
14-
if (params.clientId) {
17+
18+
// v1 params
19+
if ("clientId" in params && params.clientId) {
1520
searchParams.append("clientId", params.clientId);
1621
}
17-
if (params.accountId) {
22+
if ("accountId" in params && params.accountId) {
1823
searchParams.append("accountId", params.accountId);
1924
}
25+
26+
// v2 params
27+
if ("teamId" in params && params.teamId) {
28+
searchParams.append("teamId", params.teamId);
29+
}
30+
if ("projectId" in params && params.projectId) {
31+
searchParams.append("projectId", params.projectId);
32+
}
33+
34+
// shared params
2035
if (params.from) {
2136
searchParams.append("from", params.from.toISOString());
2237
}
@@ -30,16 +45,22 @@ function buildSearchParams(params: AnalyticsQueryParams): URLSearchParams {
3045
}
3146

3247
export async function getWalletConnections(
33-
params: AnalyticsQueryParams,
48+
params: AnalyticsQueryParamsV2,
3449
): Promise<WalletStats[]> {
3550
const searchParams = buildSearchParams(params);
36-
const res = await fetchAnalytics(`v1/wallets?${searchParams.toString()}`, {
37-
method: "GET",
38-
headers: { "Content-Type": "application/json" },
39-
});
51+
const res = await fetchAnalytics(
52+
`v2/sdk/wallet-connects?${searchParams.toString()}`,
53+
{
54+
method: "GET",
55+
headers: { "Content-Type": "application/json" },
56+
},
57+
);
4058

4159
if (res?.status !== 200) {
42-
console.error("Failed to fetch wallet connections");
60+
const reason = await res?.text();
61+
console.error(
62+
`Failed to fetch wallet connections, ${res?.status} - ${res.statusText} - ${reason}`,
63+
);
4364
return [];
4465
}
4566

@@ -48,19 +69,22 @@ export async function getWalletConnections(
4869
}
4970

5071
export async function getInAppWalletUsage(
51-
params: AnalyticsQueryParams,
72+
params: AnalyticsQueryParamsV2,
5273
): Promise<InAppWalletStats[]> {
5374
const searchParams = buildSearchParams(params);
5475
const res = await fetchAnalytics(
55-
`v1/wallets/in-app?${searchParams.toString()}`,
76+
`v2/wallet/connects?${searchParams.toString()}`,
5677
{
5778
method: "GET",
5879
headers: { "Content-Type": "application/json" },
5980
},
6081
);
6182

6283
if (res?.status !== 200) {
63-
console.error("Failed to fetch in-app wallet usage");
84+
const reason = await res?.text();
85+
console.error(
86+
`Failed to fetch in-app wallet usage, ${res?.status} - ${res.statusText} - ${reason}`,
87+
);
6488
return [];
6589
}
6690

@@ -90,11 +114,11 @@ export async function getUserOpUsage(
90114
}
91115

92116
export async function getClientTransactions(
93-
params: AnalyticsQueryParams,
117+
params: AnalyticsQueryParamsV2,
94118
): Promise<TransactionStats[]> {
95119
const searchParams = buildSearchParams(params);
96120
const res = await fetchAnalytics(
97-
`v1/transactions/client?${searchParams.toString()}`,
121+
`v2/sdk/contract-transactions?${searchParams.toString()}`,
98122
{
99123
method: "GET",
100124
headers: { "Content-Type": "application/json" },
@@ -147,7 +171,10 @@ export async function getWalletUsers(
147171
);
148172

149173
if (res?.status !== 200) {
150-
console.error("Failed to fetch wallet user stats");
174+
const reason = await res?.text();
175+
console.error(
176+
`Failed to fetch wallet user stats: ${res?.status} - ${res.statusText} - ${reason}`,
177+
);
151178
return [];
152179
}
153180

@@ -165,7 +192,10 @@ export async function isProjectActive(
165192
});
166193

167194
if (res?.status !== 200) {
168-
console.error("Failed to fetch project active status");
195+
const reason = await res?.text();
196+
console.error(
197+
`Failed to fetch project active status: ${res?.status} - ${res.statusText} - ${reason}`,
198+
);
169199
return false;
170200
}
171201

apps/dashboard/src/app/team/[team_slug]/(team)/page.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ async function getProjectsWithAnalytics(
4242
projects: Project[],
4343
): Promise<Array<ProjectWithAnalytics>> {
4444
return Promise.all(
45-
projects.map(async (p) => {
45+
projects.map(async (project) => {
4646
try {
4747
const today = new Date();
4848
const thirtyDaysAgo = subDays(today, 30);
4949

5050
const data = await getWalletConnections({
51-
clientId: p.publishableKey,
51+
teamId: project.teamId,
52+
projectId: project.id,
5253
period: "all",
5354
from: thirtyDaysAgo,
5455
to: today,
@@ -60,12 +61,12 @@ async function getProjectsWithAnalytics(
6061
}
6162

6263
return {
63-
...p,
64+
...project,
6465
monthlyActiveUsers: uniqueWalletsConnected,
6566
};
6667
} catch {
6768
return {
68-
...p,
69+
...project,
6970
monthlyActiveUsers: 0,
7071
};
7172
}

apps/dashboard/src/app/team/[team_slug]/(team)/~/analytics/page.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ import { Suspense } from "react";
3434
import { TotalSponsoredChartCardUI } from "../../_components/TotalSponsoredCard";
3535
import { TransactionsChartCardUI } from "../../_components/TransactionsCard";
3636

37-
// revalidate every 5 minutes
38-
export const maxDuration = 300;
39-
4037
type SearchParams = {
4138
usersChart?: string;
4239
from?: string;
@@ -77,6 +74,7 @@ export default async function TeamOverviewPage(props: {
7774
<div className="flex grow flex-col justify-between gap-10 md:container md:pt-8 md:pb-16">
7875
<Suspense fallback={<GenericLoadingPage />}>
7976
<OverviewPageContent
77+
teamId={team.id}
8078
account={account}
8179
range={range}
8280
interval={interval}
@@ -89,12 +87,13 @@ export default async function TeamOverviewPage(props: {
8987
}
9088

9189
async function OverviewPageContent(props: {
90+
teamId: string;
9291
account: Account;
9392
range: Range;
9493
interval: "day" | "week";
9594
searchParams: SearchParams;
9695
}) {
97-
const { account, range, interval, searchParams } = props;
96+
const { teamId, account, range, interval, searchParams } = props;
9897

9998
const [
10099
walletConnections,
@@ -107,7 +106,7 @@ async function OverviewPageContent(props: {
107106
] = await Promise.all([
108107
// Aggregated wallet connections
109108
getWalletConnections({
110-
accountId: account.id,
109+
teamId: teamId,
111110
from: range.from,
112111
to: range.to,
113112
period: "all",
@@ -121,7 +120,7 @@ async function OverviewPageContent(props: {
121120
}),
122121
// In-app wallet usage
123122
getInAppWalletUsage({
124-
accountId: account.id,
123+
teamId: teamId,
125124
from: range.from,
126125
to: range.to,
127126
period: "all",
@@ -141,13 +140,13 @@ async function OverviewPageContent(props: {
141140
}),
142141
// Client transactions
143142
getClientTransactions({
144-
accountId: account.id,
143+
teamId: teamId,
145144
from: range.from,
146145
to: range.to,
147146
period: interval,
148147
}),
149148
getClientTransactions({
150-
accountId: account.id,
149+
teamId: teamId,
151150
from: range.from,
152151
to: range.to,
153152
period: "all",

apps/dashboard/src/app/team/[team_slug]/(team)/~/ecosystem/[slug]/(active)/analytics/components/EcosystemAnalyticsPage.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import { EcosystemWalletsSummary } from "./Summary";
99

1010
export async function EcosystemAnalyticsPage({
1111
ecosystemSlug,
12+
teamId,
1213
interval,
1314
range,
1415
}: {
1516
ecosystemSlug: string;
17+
teamId: string;
1618
interval: "day" | "week";
1719
range?: Range;
1820
}) {
@@ -22,20 +24,23 @@ export async function EcosystemAnalyticsPage({
2224

2325
const allTimeStatsPromise = getEcosystemWalletUsage({
2426
ecosystemSlug,
27+
teamId,
2528
from: new Date(2022, 0, 1),
2629
to: new Date(),
2730
period: "all",
2831
});
2932

3033
const monthlyStatsPromise = getEcosystemWalletUsage({
3134
ecosystemSlug,
35+
teamId,
3236
from: new Date(new Date().getFullYear(), new Date().getMonth(), 1),
3337
to: new Date(),
3438
period: "month",
3539
});
3640

3741
const statsPromise = getEcosystemWalletUsage({
3842
ecosystemSlug,
43+
teamId,
3944
from: range.from,
4045
to: range.to,
4146
period: interval,

apps/dashboard/src/app/team/[team_slug]/(team)/~/ecosystem/[slug]/(active)/analytics/page.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Range } from "components/analytics/date-range-selector";
22
import { redirect } from "next/navigation";
3+
import { getTeamBySlug } from "../../../../../../../../../@/api/team";
34
import { getAuthToken } from "../../../../../../../../api/lib/getAuthToken";
45
import { fetchEcosystem } from "../../../utils/fetchEcosystem";
56
import { EcosystemAnalyticsPage } from "./components/EcosystemAnalyticsPage";
@@ -26,15 +27,23 @@ export default async function Page(props: {
2627
redirect(ecosystemLayoutPath);
2728
}
2829

29-
const ecosystem = await fetchEcosystem(params.slug, authToken);
30+
const [ecosystem, team] = await Promise.all([
31+
fetchEcosystem(params.slug, authToken),
32+
getTeamBySlug(params.team_slug),
33+
]);
3034

3135
if (!ecosystem) {
3236
redirect(ecosystemLayoutPath);
3337
}
3438

39+
if (!team) {
40+
redirect("/team");
41+
}
42+
3543
return (
3644
<EcosystemAnalyticsPage
3745
ecosystemSlug={ecosystem.slug}
46+
teamId={team.id}
3847
interval={searchParams.interval || "week"}
3948
range={searchParams.range}
4049
/>

apps/dashboard/src/app/team/[team_slug]/(team)/~/nebula/page.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { getTeamBySlug } from "@/api/team";
2-
import { getValidAccount } from "../../../../../account/settings/getAccount";
32
import { getAuthToken } from "../../../../../api/lib/getAuthToken";
43
import { loginRedirect } from "../../../../../login/loginRedirect";
54
import { NebulaAnalyticsPage } from "../../../[project_slug]/nebula/components/analytics/nebula-analytics-page";
@@ -20,8 +19,7 @@ export default async function Page(props: {
2019
props.searchParams,
2120
]);
2221

23-
const [account, authToken, team] = await Promise.all([
24-
getValidAccount(),
22+
const [authToken, team] = await Promise.all([
2523
getAuthToken(),
2624
getTeamBySlug(params.team_slug),
2725
]);
@@ -35,7 +33,7 @@ export default async function Page(props: {
3533
if (hasNebulaAccess) {
3634
return (
3735
<NebulaAnalyticsPage
38-
accountId={account.id}
36+
teamId={team.id}
3937
authToken={authToken}
4038
searchParams={searchParams}
4139
/>

apps/dashboard/src/app/team/[team_slug]/(team)/~/usage/overview/components/Usage.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,14 @@ export const Usage: React.FC<UsageProps> = ({
9292
return (
9393
<div className="flex grow flex-col gap-8">
9494
<InAppWalletUsersChartCard
95+
teamId={team.id}
9596
accountId={account.id}
9697
from={currentPeriodStart}
9798
to={currentPeriodEnd}
9899
/>
99100

100101
<TotalSponsoredCard
102+
teamId={team.id}
101103
accountId={account.id}
102104
from={currentPeriodStart}
103105
to={currentPeriodEnd}
@@ -138,6 +140,7 @@ export const Usage: React.FC<UsageProps> = ({
138140
type ChartCardProps = {
139141
from: Date;
140142
to: Date;
143+
teamId: string;
141144
accountId: string;
142145
};
143146

@@ -176,7 +179,7 @@ async function AsyncInAppWalletUsersChartCard(
176179
period: "day",
177180
from: props.from,
178181
to: props.to,
179-
accountId: props.accountId,
182+
teamId: props.teamId,
180183
}).catch(() => null);
181184

182185
return (

apps/dashboard/src/app/team/[team_slug]/[project_slug]/components/Transactions/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { LoadingChartState } from "components/analytics/empty-chart-state";
22
import { Suspense } from "react";
3-
import type { AnalyticsQueryParams } from "types/analytics";
3+
import type { AnalyticsQueryParamsV2 } from "types/analytics";
44
import { getClientTransactions } from "../../../../../../@/api/analytics";
55
import { TransactionsChartsUI } from "./TransactionCharts";
66

77
export function TransactionsCharts(
8-
props: AnalyticsQueryParams & {
8+
props: AnalyticsQueryParamsV2 & {
99
searchParams: { [key: string]: string | string[] | undefined };
1010
},
1111
) {
@@ -24,7 +24,7 @@ export function TransactionsCharts(
2424
}
2525

2626
async function TransactionsChartCardAsync(
27-
props: AnalyticsQueryParams & {
27+
props: AnalyticsQueryParamsV2 & {
2828
searchParams: { [key: string]: string | string[] | undefined };
2929
},
3030
) {

0 commit comments

Comments
 (0)