11import { subHours } from "date-fns" ;
22import { AlertTriangleIcon , ClockIcon } from "lucide-react" ;
3+ import { toast } from "sonner" ;
34import {
45 getWebhookLatency ,
56 getWebhookRequests ,
@@ -12,9 +13,7 @@ import {
1213 type Range ,
1314} from "@/components/analytics/date-range-selector" ;
1415import { RangeSelector } from "@/components/analytics/range-selector" ;
15- import { ThirdwebBarChart } from "@/components/blocks/charts/bar-chart" ;
16- import { Card , CardContent } from "@/components/ui/card" ;
17- import type { ChartConfig } from "@/components/ui/chart" ;
16+ import { StatCard } from "@/components/analytics/stat" ;
1817import type {
1918 WebhookLatencyStats ,
2019 WebhookRequestStats ,
@@ -23,16 +22,12 @@ import type {
2322import { LatencyChart } from "./latency-chart" ;
2423import { StatusCodesChart } from "./status-codes-chart" ;
2524import { WebhookSelector } from "./webhook-selector" ;
26- import { toast } from "sonner" ;
27- import { StatCard } from "@/components/analytics/stat" ;
28-
29-
3025
3126type WebhookAnalyticsProps = {
3227 interval : "day" | "week" ;
3328 range : Range ;
3429 selectedWebhookId : string | null ;
35- webhooks : WebhookConfig [ ] ;
30+ webhooksConfigs : WebhookConfig [ ] ;
3631 requestStats : WebhookRequestStats [ ] ;
3732 latencyStats : WebhookLatencyStats [ ] ;
3833 summaryStats : WebhookSummaryStats [ ] ;
@@ -42,91 +37,67 @@ function WebhookAnalytics({
4237 interval,
4338 range,
4439 selectedWebhookId,
45- webhooks ,
40+ webhooksConfigs ,
4641 requestStats,
4742 latencyStats,
4843 summaryStats,
4944} : WebhookAnalyticsProps ) {
50-
5145 // Calculate overview metrics for the last 24 hours
52- const last24HoursSummary = summaryStats . find (
53- ( s ) => s . webhookId === selectedWebhookId ,
54- ) ;
55- const errorRate = 100 - ( last24HoursSummary ?. successRate || 0 ) ;
56- const avgLatency = last24HoursSummary ?. avgLatencyMs || 0 ;
46+ const errorRate = 100 - ( summaryStats [ 0 ] ?. successRate || 0 ) ;
47+ const avgLatency = summaryStats [ 0 ] ?. avgLatencyMs || 0 ;
5748
58- // Transform request data for combined chart
49+ // Transform request data for combined chart.
5950 const allRequestsData = requestStats
60- . filter (
61- ( stat ) => ! selectedWebhookId || stat . webhookId === selectedWebhookId ,
62- )
6351 . reduce ( ( acc , stat ) => {
52+ const statusCode = stat . httpStatusCode . toString ( ) ;
6453 const existingEntry = acc . find ( ( entry ) => entry . time === stat . date ) ;
6554 if ( existingEntry ) {
66- existingEntry . totalRequests += stat . totalRequests ;
67- existingEntry [ stat . httpStatusCode . toString ( ) ] =
68- ( existingEntry [ stat . httpStatusCode . toString ( ) ] || 0 ) +
69- stat . totalRequests ;
55+ existingEntry [ statusCode ] =
56+ ( existingEntry [ statusCode ] || 0 ) + stat . totalRequests ;
7057 } else {
7158 acc . push ( {
72- time : stat . date , // Changed from 'date' to 'time'
73- totalRequests : stat . totalRequests ,
74- [ stat . httpStatusCode . toString ( ) ] : stat . totalRequests ,
59+ time : stat . date ,
60+ [ statusCode ] : stat . totalRequests ,
7561 } ) ;
7662 }
7763 return acc ;
7864 } , [ ] as any [ ] )
7965 . sort ( ( a , b ) => new Date ( a . time ) . getTime ( ) - new Date ( b . time ) . getTime ( ) ) ;
8066
81- // Transform latency data for line chart
67+ // Transform latency data for line chart.
8268 const latencyData = latencyStats
83- . filter (
84- ( stat ) => ! selectedWebhookId || stat . webhookId === selectedWebhookId ,
85- )
86- . map ( ( stat ) => ( {
87- p50LatencyMs : stat . p50LatencyMs , // Changed from 'date' to 'time'
88- p90LatencyMs : stat . p90LatencyMs ,
89- p99LatencyMs : stat . p99LatencyMs ,
90- time : stat . date ,
91- } ) )
69+ . map ( ( stat ) => ( { ...stat , time : stat . date } ) )
9270 . sort ( ( a , b ) => new Date ( a . time ) . getTime ( ) - new Date ( b . time ) . getTime ( ) ) ;
9371
9472 return (
9573 < div className = "flex flex-col gap-6" >
96- { /* Webhook Selector */ }
9774 < WebhookSelector
9875 selectedWebhookId = { selectedWebhookId }
99- webhooks = { webhooks }
76+ webhooks = { webhooksConfigs }
10077 />
10178
102- { /* Overview Cards */ }
10379 < div className = "grid grid-cols-1 md:grid-cols-2 gap-6" >
10480 < StatCard
81+ formatter = { ( value ) => `${ value . toFixed ( 2 ) } %` }
10582 icon = { AlertTriangleIcon }
10683 isPending = { false }
10784 label = "Error Rate (24h)"
10885 value = { errorRate }
109- formatter = { ( value ) => `${ value . toFixed ( 2 ) } %` }
11086 />
111- < StatCard
87+ < StatCard
88+ formatter = { ( value ) => `${ value . toFixed ( 0 ) } ms` }
11289 icon = { ClockIcon }
11390 isPending = { false }
11491 label = "P50 Latency (24h)"
11592 value = { avgLatency }
116- formatter = { ( value ) => `${ value . toFixed ( 0 ) } ms` }
11793 />
11894 </ div >
11995
12096 < RangeSelector interval = { interval } range = { range } />
12197
12298 < div className = "flex flex-col gap-4 lg:gap-6" >
123- { selectedWebhookId && (
124- < StatusCodesChart data = { allRequestsData } isPending = { false } />
125- ) }
126-
127- { selectedWebhookId && (
128- < LatencyChart data = { latencyData } isPending = { false } />
129- ) }
99+ < StatusCodesChart data = { allRequestsData } isPending = { false } />
100+ < LatencyChart data = { latencyData } isPending = { false } />
130101 </ div >
131102 </ div >
132103 ) ;
@@ -146,80 +117,53 @@ export async function AnalyticsPageContent({
146117} ) {
147118 // Parse search params for filters
148119 const selectedWebhookId = searchParams ?. webhookId as string | undefined ;
149- const interval = ( searchParams ?. interval as "day" | "week" ) || DEFAULT_INTERVAL ;
120+ const interval =
121+ ( searchParams ?. interval as "day" | "week" ) || DEFAULT_INTERVAL ;
150122 const range = DEFAULT_RANGE ; // Could be enhanced to parse from search params
151123
152- // Fetch webhooks
153- const webhooksResponse = await getWebhookConfigs ( teamSlug , project . id ) ;
154- if ( "error" in webhooksResponse ) {
155- toast . error ( webhooksResponse . error ) ;
124+ // Get webhook configs.
125+ const webhooksConfigsResponse = await getWebhookConfigs ( teamSlug , project . id ) ;
126+ if ( "error" in webhooksConfigsResponse ) {
127+ toast . error ( webhooksConfigsResponse . error ) ;
156128 return null ;
157129 }
158130
159- const webhooks : WebhookConfig [ ] =
160- webhooksResponse . data . length > 0
161- ? webhooksResponse . data
162- : [
163- {
164- id : "8582b449-551e-429f-99c4-5359f253dce1" ,
165- description : "Webhook 2" ,
166- createdAt : new Date ( ) ,
167- updatedAt : new Date ( ) ,
168- deletedAt : null ,
169- teamId : 'team_clmb33q9w00gn1x0u2ri8z0k0' ,
170- projectId : 'prj_cm6ibyah500bgxag5q7d79fps' ,
171- destinationUrl : "https://example.com/webhook2" ,
172- pausedAt : null ,
173- webhookSecret : "secret" ,
174- } ,
175- ] ;
176-
177- // Fetch analytics data
131+ // Get webhook analytics.
178132 const [ requestStats , latencyStats , summaryStats ] = await Promise . all ( [
179133 getWebhookRequests ( {
180- teamId : 'team_clmb33q9w00gn1x0u2ri8z0k0' ,
181- projectId : 'prj_cm6ibyah500bgxag5q7d79fps' ,
182- // teamId: project.teamId,
183- // projectId: project.id,
134+ teamId : project . teamId ,
135+ projectId : project . id ,
184136 from : range . from ,
185- to : range . to ,
186137 period : interval ,
138+ to : range . to ,
187139 webhookId : selectedWebhookId || undefined ,
188140 } ) . catch ( ( ) => [ ] ) ,
189141 getWebhookLatency ( {
190- teamId : 'team_clmb33q9w00gn1x0u2ri8z0k0' ,
191- projectId : 'prj_cm6ibyah500bgxag5q7d79fps' ,
192- // teamId: project.teamId,
193- // projectId: project.id,
142+ teamId : project . teamId ,
143+ projectId : project . id ,
194144 from : range . from ,
195- to : range . to ,
196145 period : interval ,
146+ to : range . to ,
197147 webhookId : selectedWebhookId || undefined ,
198148 } ) . catch ( ( ) => [ ] ) ,
199149 getWebhookSummary ( {
200- teamId : 'team_clmb33q9w00gn1x0u2ri8z0k0' ,
201- projectId : 'prj_cm6ibyah500bgxag5q7d79fps' ,
202- // teamId: project.teamId,
203- // projectId: project.id,
150+ teamId : project . teamId ,
151+ projectId : project . id ,
204152 from : subHours ( new Date ( ) , 24 ) ,
205153 to : new Date ( ) ,
206154 webhookId : selectedWebhookId || undefined ,
207155 } ) . catch ( ( ) => [ ] ) ,
208156 ] ) ;
209157
210- console . log ( "requestStats" , requestStats ) ;
211- console . log ( "latencyStats" , latencyStats ) ;
212- console . log ( "summaryStats" , summaryStats ) ;
213-
214158 return (
215159 < WebhookAnalytics
216160 interval = { interval }
161+ latencyStats = { latencyStats }
217162 range = { range }
218- selectedWebhookId = { selectedWebhookId || null }
219- webhooks = { webhooks }
220163 requestStats = { requestStats }
221- latencyStats = { latencyStats }
164+ selectedWebhookId = { selectedWebhookId || null }
222165 summaryStats = { summaryStats }
166+ webhooksConfigs = { webhooksConfigsResponse . data }
223167 />
224168 ) ;
225169}
0 commit comments