@@ -17,6 +17,29 @@ import type {
1717import { getAuthToken } from "./auth-token" ;
1818import { getChains } from "./chain" ;
1919
20+ export interface InsightChainStats {
21+ date : string ;
22+ chainId : string ;
23+ totalRequests : number ;
24+ }
25+
26+ export interface InsightStatusCodeStats {
27+ date : string ;
28+ httpStatusCode : number ;
29+ totalRequests : number ;
30+ }
31+
32+ export interface InsightEndpointStats {
33+ date : string ;
34+ endpoint : string ;
35+ totalRequests : number ;
36+ }
37+
38+ interface InsightUsageStats {
39+ date : string ;
40+ totalRequests : number ;
41+ }
42+
2043async function fetchAnalytics (
2144 input : string | URL ,
2245 init ?: RequestInit ,
@@ -424,3 +447,91 @@ export async function getEngineCloudMethodUsage(
424447 const json = await res . json ( ) ;
425448 return json . data as EngineCloudStats [ ] ;
426449}
450+
451+ export async function getInsightChainUsage (
452+ params : AnalyticsQueryParams ,
453+ ) : Promise < { data : InsightChainStats [ ] } | { errorMessage : string } > {
454+ const searchParams = buildSearchParams ( params ) ;
455+ const res = await fetchAnalytics (
456+ `v2/insight/usage/by-chain?${ searchParams . toString ( ) } ` ,
457+ {
458+ method : "GET" ,
459+ } ,
460+ ) ;
461+
462+ if ( res ?. status !== 200 ) {
463+ const reason = await res ?. text ( ) ;
464+ const errMsg = `Failed to fetch Insight chain usage: ${ res ?. status } - ${ res . statusText } - ${ reason } ` ;
465+ console . error ( errMsg ) ;
466+ return { errorMessage : errMsg } ;
467+ }
468+
469+ const json = await res . json ( ) ;
470+ return { data : json . data as InsightChainStats [ ] } ;
471+ }
472+
473+ export async function getInsightStatusCodeUsage (
474+ params : AnalyticsQueryParams ,
475+ ) : Promise < { data : InsightStatusCodeStats [ ] } | { errorMessage : string } > {
476+ const searchParams = buildSearchParams ( params ) ;
477+ const res = await fetchAnalytics (
478+ `v2/insight/usage/by-status-code?${ searchParams . toString ( ) } ` ,
479+ {
480+ method : "GET" ,
481+ } ,
482+ ) ;
483+
484+ if ( res ?. status !== 200 ) {
485+ const reason = await res ?. text ( ) ;
486+ const errMsg = `Failed to fetch Insight status code usage: ${ res ?. status } - ${ res . statusText } - ${ reason } ` ;
487+ console . error ( errMsg ) ;
488+ return { errorMessage : errMsg } ;
489+ }
490+
491+ const json = await res . json ( ) ;
492+ return { data : json . data as InsightStatusCodeStats [ ] } ;
493+ }
494+
495+ export async function getInsightEndpointUsage (
496+ params : AnalyticsQueryParams ,
497+ ) : Promise < { data : InsightEndpointStats [ ] } | { errorMessage : string } > {
498+ const searchParams = buildSearchParams ( params ) ;
499+ const res = await fetchAnalytics (
500+ `v2/insight/usage/by-endpoint?${ searchParams . toString ( ) } ` ,
501+ {
502+ method : "GET" ,
503+ } ,
504+ ) ;
505+
506+ if ( res ?. status !== 200 ) {
507+ const reason = await res ?. text ( ) ;
508+ const errMsg = `Failed to fetch Insight endpoint usage: ${ res ?. status } - ${ res . statusText } - ${ reason } ` ;
509+ console . error ( errMsg ) ;
510+ return { errorMessage : errMsg } ;
511+ }
512+
513+ const json = await res . json ( ) ;
514+ return { data : json . data as InsightEndpointStats [ ] } ;
515+ }
516+
517+ export async function getInsightUsage (
518+ params : AnalyticsQueryParams ,
519+ ) : Promise < { data : InsightUsageStats [ ] } | { errorMessage : string } > {
520+ const searchParams = buildSearchParams ( params ) ;
521+ const res = await fetchAnalytics (
522+ `v2/insight/usage?${ searchParams . toString ( ) } ` ,
523+ {
524+ method : "GET" ,
525+ } ,
526+ ) ;
527+
528+ if ( res ?. status !== 200 ) {
529+ const reason = await res ?. text ( ) ;
530+ const errMsg = `Failed to fetch Insight usage: ${ res ?. status } - ${ res . statusText } - ${ reason } ` ;
531+ console . error ( errMsg ) ;
532+ return { errorMessage : errMsg } ;
533+ }
534+
535+ const json = await res . json ( ) ;
536+ return { data : json . data as InsightUsageStats [ ] } ;
537+ }
0 commit comments