Skip to content

Commit 7bd178b

Browse files
[Dashboard] Update analytics API integration and add error logging
1 parent f789e76 commit 7bd178b

File tree

4 files changed

+63
-46
lines changed

4 files changed

+63
-46
lines changed

apps/dashboard/src/app/(app)/(dashboard)/(chain)/[chain_id]/[contractAddress]/_utils/isAnalyticsSupportedForChain.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ export async function isInsightSupportedForChain(
1919
);
2020

2121
if (!res.ok) {
22+
const errorText = await res.text();
23+
console.error(
24+
"failed to fetch chain services for chain",
25+
chainId,
26+
errorText,
27+
);
2228
return false;
2329
}
2430

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

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { NEXT_PUBLIC_DASHBOARD_CLIENT_ID } from "@/constants/public-envs";
33
import { getVercelEnv } from "@/utils/vercel";
44

55
type InsightAggregationEntry = {
6-
event_signature: string;
7-
time: string;
6+
topic_0: string;
7+
day: string;
88
count: number;
99
};
1010

@@ -26,12 +26,18 @@ export async function getContractEventBreakdown(params: {
2626
startDate?: Date;
2727
endDate?: Date;
2828
}): Promise<EventBreakdownEntry[]> {
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",
32-
"group_by=topic_0 as event_signature",
33-
"aggregate=toStartOfDay(toDate(block_timestamp)) as time",
34-
"aggregate=count(*) as count",
38+
"group_by=day",
39+
"group_by=topic_0",
40+
`limit=${daysDifference * 10}`, // at most 10 topics per day
3541
params.startDate
3642
? `filter_block_timestamp_gte=${getUnixTime(params.startDate)}`
3743
: "",
@@ -42,14 +48,14 @@ export async function getContractEventBreakdown(params: {
4248
.filter(Boolean)
4349
.join("&");
4450

45-
const res = await fetch(
46-
`https://insight.${thirdwebDomain}.com/v1/events/${params.contractAddress}?${queryParams}`,
47-
{
48-
headers: {
49-
"x-client-id": NEXT_PUBLIC_DASHBOARD_CLIENT_ID,
50-
},
51+
// const url = `https://insight.${thirdwebDomain}.com/v1/events/${params.contractAddress}?${queryParams}`;
52+
const url = `http://localhost:3123/v1/events/${params.contractAddress}?${queryParams}`;
53+
54+
const res = await fetch(url, {
55+
headers: {
56+
"x-client-id": NEXT_PUBLIC_DASHBOARD_CLIENT_ID,
5157
},
52-
);
58+
});
5359

5460
if (!res.ok) {
5561
throw new Error("Failed to fetch analytics data");
@@ -58,22 +64,23 @@ export async function getContractEventBreakdown(params: {
5864
const json = (await res.json()) as InsightResponse;
5965
const aggregations = Object.values(json.aggregations[0]);
6066

67+
console.log("getContractEventBreakdown aggregations", aggregations);
68+
6169
const collectedAggregations: InsightAggregationEntry[] = [];
6270
for (const value of aggregations) {
6371
if (
6472
typeof value === "object" &&
6573
value !== null &&
66-
"time" in value &&
74+
"day" in value &&
6775
"count" in value &&
68-
"event_signature" in value &&
69-
typeof value.event_signature === "string" &&
70-
typeof value.time === "string" &&
71-
typeof value.count === "number"
76+
"topic_0" in value &&
77+
typeof value.topic_0 === "string" &&
78+
typeof value.day === "string"
7279
) {
7380
collectedAggregations.push({
74-
count: value.count,
75-
event_signature: value.event_signature,
76-
time: value.time,
81+
count: Number(value.count),
82+
topic_0: value.topic_0,
83+
day: value.day,
7784
});
7885
}
7986
}
@@ -84,15 +91,15 @@ export async function getContractEventBreakdown(params: {
8491
> = new Map();
8592

8693
for (const value of collectedAggregations) {
87-
const mapKey = value.time;
94+
const mapKey = value.day;
8895
let valueForDay = dayToFunctionBreakdownMap.get(mapKey);
8996
if (!valueForDay) {
9097
valueForDay = {};
9198
dayToFunctionBreakdownMap.set(mapKey, valueForDay);
9299
}
93100

94-
valueForDay[value.event_signature] =
95-
(valueForDay[value.event_signature] || 0) + value.count;
101+
valueForDay[value.topic_0] =
102+
(valueForDay[value.topic_0] || 0) + value.count;
96103
}
97104

98105
const values: EventBreakdownEntry[] = [];

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

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,17 @@ export async function getContractEventAnalytics(params: {
3030
startDate?: Date;
3131
endDate?: Date;
3232
}): Promise<AnalyticsEntry[]> {
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
: "",
@@ -45,17 +51,17 @@ export async function getContractEventAnalytics(params: {
4551
.filter(Boolean)
4652
.join("&");
4753

48-
const res = await fetch(
49-
`https://insight.${thirdwebDomain}.com/v1/events/${params.contractAddress}?${queryParams}`,
50-
{
51-
headers: {
52-
"x-client-id": NEXT_PUBLIC_DASHBOARD_CLIENT_ID,
53-
},
54+
const url = `https://insight.${thirdwebDomain}.com/v1/events/${params.contractAddress}?${queryParams}`;
55+
56+
const res = await fetch(url, {
57+
headers: {
58+
"x-client-id": NEXT_PUBLIC_DASHBOARD_CLIENT_ID,
5459
},
55-
);
60+
});
5661

5762
if (!res.ok) {
58-
throw new Error("Failed to fetch analytics data");
63+
const errorText = await res.text();
64+
throw new Error(`Failed to fetch analytics data: ${errorText}`);
5965
}
6066

6167
const json = (await res.json()) as InsightResponse;
@@ -67,14 +73,13 @@ export async function getContractEventAnalytics(params: {
6773
if (
6874
typeof value === "object" &&
6975
value !== null &&
70-
"time" in value &&
76+
"day" in value &&
7177
"count" in value &&
72-
typeof value.time === "string" &&
73-
typeof value.count === "number"
78+
typeof value.day === "string"
7479
) {
7580
values.push({
76-
count: value.count,
77-
time: new Date(value.time),
81+
count: Number(value.count),
82+
time: new Date(value.day),
7883
});
7984
}
8085
}

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

Lines changed: 5 additions & 6 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 getTotalContractEvents(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/events/${params.contractAddress}?${queryParams}`,
@@ -40,6 +39,6 @@ export async function getTotalContractEvents(params: {
4039
const json = (await res.json()) as InsightResponse;
4140

4241
return {
43-
count: json.aggregations[0][0].total,
42+
count: json.aggregations[0][0].count,
4443
};
4544
}

0 commit comments

Comments
 (0)