Skip to content

Commit 2f6f3a0

Browse files
[Dashboard] Update analytics API integration and improve number formatting (#8023)
1 parent 4284b9d commit 2f6f3a0

File tree

10 files changed

+74
-41
lines changed

10 files changed

+74
-41
lines changed

apps/dashboard/src/@/lib/search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ async function fetchContractName(chainId: number, contractAddress: string) {
184184
return name;
185185
}
186186

187-
const formatNumber = (num: number) => {
187+
export const formatNumber = (num: number) => {
188188
if (num >= 1000000) {
189189
return `${(num / 1000000).toLocaleString(undefined, {
190190
maximumFractionDigits: 1,

apps/dashboard/src/app/(app)/(dashboard)/(chain)/[chain_id]/[contractAddress]/analytics/ContractAnalyticsPage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "@/components/analytics/date-range-selector";
1111
import { ThirdwebBarChart } from "@/components/blocks/charts/bar-chart";
1212
import { SkeletonContainer } from "@/components/ui/skeleton";
13+
import { formatNumber } from "@/lib/search";
1314
import {
1415
type AnalyticsQueryParams,
1516
type TotalQueryResult,
@@ -376,7 +377,7 @@ function AnalyticsStatUI(props: { label: string; data: number | undefined }) {
376377
<SkeletonContainer
377378
loadedData={props.data}
378379
render={(v) => {
379-
return <dd className="font-normal text-xl">{v.toLocaleString()}</dd>;
380+
return <dd className="font-normal text-xl">{formatNumber(v)}</dd>;
380381
}}
381382
skeletonData={10000}
382383
/>

apps/dashboard/src/app/(app)/(dashboard)/(chain)/[chain_id]/[contractAddress]/analytics/utils/contract-events.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export async function getContractEventAnalytics(params: {
6161

6262
if (!res.ok) {
6363
const errorText = await res.text();
64-
throw new Error(`Failed to fetch analytics data: ${errorText}`);
64+
throw new Error(`Failed to fetch events analytics data: ${errorText}`);
6565
}
6666

6767
const json = (await res.json()) as InsightResponse;

apps/dashboard/src/app/(app)/(dashboard)/(chain)/[chain_id]/[contractAddress]/analytics/utils/contract-function-breakdown.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,18 @@ export async function getContractFunctionBreakdown(params: {
2626
startDate?: Date;
2727
endDate?: Date;
2828
}): Promise<FunctionBreakdownEntry[]> {
29+
const daysDifference =
30+
params.startDate && params.endDate
31+
? Math.ceil(
32+
(params.endDate.getTime() - params.startDate.getTime()) /
33+
(1000 * 60 * 60 * 24),
34+
)
35+
: 30;
2936
const queryParams = [
3037
`chain=${params.chainId}`,
31-
"group_by=time",
38+
"group_by=day",
3239
"group_by=function_selector",
33-
"aggregate=toStartOfDay(toDate(block_timestamp)) as time",
34-
"aggregate=count(*) as count",
40+
`limit=${daysDifference * 10}`, // at most 10 functions per day
3541
params.startDate
3642
? `filter_block_timestamp_gte=${getUnixTime(params.startDate)}`
3743
: "",
@@ -68,17 +74,16 @@ export async function getContractFunctionBreakdown(params: {
6874
if (
6975
typeof value === "object" &&
7076
value !== null &&
71-
"time" in value &&
77+
"day" in value &&
7278
"count" in value &&
7379
"function_selector" in value &&
7480
typeof value.function_selector === "string" &&
75-
typeof value.time === "string" &&
76-
typeof value.count === "number"
81+
typeof value.day === "string"
7782
) {
7883
collectedAggregations.push({
79-
count: value.count,
84+
count: Number(value.count),
8085
function_selector: value.function_selector,
81-
time: value.time,
86+
time: value.day,
8287
});
8388
}
8489
}

apps/dashboard/src/app/(app)/(dashboard)/(chain)/[chain_id]/[contractAddress]/analytics/utils/contract-transactions.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@ export async function getContractTransactionAnalytics(params: {
3030
startDate?: Date;
3131
endDate?: Date;
3232
}): Promise<TransactionAnalyticsEntry[]> {
33+
const daysDifference =
34+
params.startDate && params.endDate
35+
? Math.ceil(
36+
(params.endDate.getTime() - params.startDate.getTime()) /
37+
(1000 * 60 * 60 * 24),
38+
)
39+
: 30;
3340
const queryParams = [
3441
`chain=${params.chainId}`,
35-
"group_by=time",
36-
"aggregate=toStartOfDay(toDate(block_timestamp)) as time",
37-
"aggregate=count(block_timestamp) as count",
42+
"group_by=day",
43+
`limit=${daysDifference}`,
3844
params.startDate
3945
? `filter_block_timestamp_gte=${getUnixTime(params.startDate)}`
4046
: "",
@@ -55,7 +61,8 @@ export async function getContractTransactionAnalytics(params: {
5561
);
5662

5763
if (!res.ok) {
58-
throw new Error("Failed to fetch analytics data");
64+
const errorText = await res.text();
65+
throw new Error(`Failed to fetch transaction analytics data: ${errorText}`);
5966
}
6067

6168
const json = (await res.json()) as InsightResponse;
@@ -67,14 +74,13 @@ export async function getContractTransactionAnalytics(params: {
6774
if (
6875
typeof tx === "object" &&
6976
tx !== null &&
70-
"time" in tx &&
77+
"day" in tx &&
7178
"count" in tx &&
72-
typeof tx.time === "string" &&
73-
typeof tx.count === "number"
79+
typeof tx.day === "string"
7480
) {
7581
returnValue.push({
76-
count: tx.count,
77-
time: new Date(tx.time),
82+
count: Number(tx.count),
83+
time: new Date(tx.day),
7884
});
7985
}
8086
}

apps/dashboard/src/app/(app)/(dashboard)/(chain)/[chain_id]/[contractAddress]/analytics/utils/contract-wallet-analytics.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,18 @@ export async function getContractUniqueWalletAnalytics(params: {
3030
startDate?: Date;
3131
endDate?: Date;
3232
}): Promise<TransactionAnalyticsEntry[]> {
33+
const daysDifference =
34+
params.startDate && params.endDate
35+
? Math.ceil(
36+
(params.endDate.getTime() - params.startDate.getTime()) /
37+
(1000 * 60 * 60 * 24),
38+
)
39+
: 30;
3340
const queryParams = [
3441
`chain=${params.chainId}`,
35-
"group_by=time",
36-
"aggregate=toStartOfDay(toDate(block_timestamp)) as time",
37-
"aggregate=count(distinct from_address) as count",
42+
"group_by=day",
43+
"aggregate=count(distinct from_address)",
44+
`limit=${daysDifference}`,
3845
params.startDate
3946
? `filter_block_timestamp_gte=${getUnixTime(params.startDate)}`
4047
: "",
@@ -55,10 +62,12 @@ export async function getContractUniqueWalletAnalytics(params: {
5562
);
5663

5764
if (!res.ok) {
58-
throw new Error("Failed to fetch analytics data");
65+
const errorText = await res.text();
66+
throw new Error(`Failed to fetch wallet analytics data: ${errorText}`);
5967
}
6068

6169
const json = (await res.json()) as InsightResponse;
70+
console.log("wallet analytics json", json);
6271
const aggregations = Object.values(json.aggregations[0]);
6372

6473
const returnValue: TransactionAnalyticsEntry[] = [];
@@ -67,14 +76,13 @@ export async function getContractUniqueWalletAnalytics(params: {
6776
if (
6877
typeof tx === "object" &&
6978
tx !== null &&
70-
"time" in tx &&
79+
"day" in tx &&
7180
"count" in tx &&
72-
typeof tx.time === "string" &&
73-
typeof tx.count === "number"
81+
typeof tx.day === "string"
7482
) {
7583
returnValue.push({
76-
count: tx.count,
77-
time: new Date(tx.time),
84+
count: Number(tx.count),
85+
time: new Date(tx.day),
7886
});
7987
}
8088
}

apps/dashboard/src/app/(app)/(dashboard)/(chain)/[chain_id]/[contractAddress]/analytics/utils/total-contract-events.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export async function getTotalContractEvents(params: {
3333
);
3434

3535
if (!res.ok) {
36-
throw new Error("Failed to fetch analytics data");
36+
const errorText = await res.text();
37+
throw new Error(`Failed to fetch events analytics data: ${errorText}`);
3738
}
3839

3940
const json = (await res.json()) as InsightResponse;

apps/dashboard/src/app/(app)/(dashboard)/(chain)/[chain_id]/[contractAddress]/analytics/utils/total-contract-transactions.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type InsightResponse = {
66
aggregations: [
77
{
88
0: {
9-
total: number;
9+
count: number;
1010
};
1111
},
1212
];
@@ -19,10 +19,9 @@ export async function getTotalContractTransactions(params: {
1919
contractAddress: string;
2020
chainId: number;
2121
}): Promise<{ count: number }> {
22-
const queryParams = [
23-
`chain=${params.chainId}`,
24-
"aggregate=count(block_number) as total",
25-
].join("&");
22+
const queryParams = [`chain=${params.chainId}`, "aggregate=count()"].join(
23+
"&",
24+
);
2625

2726
const res = await fetch(
2827
`https://insight.${thirdwebDomain}.com/v1/transactions/${params.contractAddress}?${queryParams}`,
@@ -34,12 +33,15 @@ export async function getTotalContractTransactions(params: {
3433
);
3534

3635
if (!res.ok) {
37-
throw new Error("Failed to fetch analytics data");
36+
const errorText = await res.text();
37+
throw new Error(
38+
`Failed to fetch transactions analytics data: ${errorText}`,
39+
);
3840
}
3941

4042
const json = (await res.json()) as InsightResponse;
4143

4244
return {
43-
count: json.aggregations[0][0].total,
45+
count: json.aggregations[0][0].count,
4446
};
4547
}

apps/dashboard/src/app/(app)/(dashboard)/(chain)/[chain_id]/[contractAddress]/analytics/utils/total-unique-wallets.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type InsightResponse = {
66
aggregations: [
77
{
88
0: {
9-
total: number;
9+
count: number;
1010
};
1111
},
1212
];
@@ -21,7 +21,7 @@ export async function getTotalContractUniqueWallets(params: {
2121
}): Promise<{ count: number }> {
2222
const queryParams = [
2323
`chain=${params.chainId}`,
24-
"aggregate=count(distinct from_address) as total",
24+
"aggregate=count(distinct from_address)",
2525
].join("&");
2626

2727
const res = await fetch(
@@ -34,12 +34,15 @@ export async function getTotalContractUniqueWallets(params: {
3434
);
3535

3636
if (!res.ok) {
37-
throw new Error("Failed to fetch analytics data");
37+
const errorText = await res.text();
38+
throw new Error(
39+
`Failed to fetch unique wallets analytics data: ${errorText}`,
40+
);
3841
}
3942

4043
const json = (await res.json()) as InsightResponse;
4144

4245
return {
43-
count: json.aggregations[0][0].total,
46+
count: json.aggregations[0][0].count,
4447
};
4548
}

apps/dashboard/src/app/(app)/team/[team_slug]/[project_slug]/(sidebar)/ai/components/TransactionsSection/TransactionsSection.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ export function TransactionsSection(props: { client: ThirdwebClient }) {
190190
}
191191

192192
const response = await fetch(url.toString());
193+
194+
if (!response.ok) {
195+
throw new Error(
196+
`Failed to fetch transactions: ${response.status} - ${await response.text()}`,
197+
);
198+
}
199+
193200
const json = (await response.json()) as {
194201
data?: WalletTransaction[];
195202
};

0 commit comments

Comments
 (0)