1- import { serve } from " bun" ;
1+ import { serve } from ' bun' ;
22
3- const REFRESH = "5s" ;
4-
5- type Pool = {
6- hashrate5m : string ;
7- hashrate1h : string ;
8- shares : number ;
9- bestshare : number ;
10- } ;
3+ const REFRESH = '30s' ;
114
125const hashrateSuffix = ( value : string ) : { value : string ; unit : string } => {
136 const match = value . match ( / ^ ( [ \d . ] + ) ( [ K M G T P E Z Y ] ) $ / ) ;
14- if ( ! match ) return { value, unit : "" } ;
7+ if ( ! match ) return { value, unit : '' } ;
158 const [ , num , unit ] = match ;
169 return { value : num . toString ( ) , unit : `${ unit } h/s` } ;
1710} ;
1811
1912const abbreviateNumber = ( value : number ) : { value : string ; unit : string } => {
20- if ( value >= 1e12 ) return { value : ( value / 1e12 ) . toFixed ( 2 ) , unit : "T" } ;
21- if ( value >= 1e9 ) return { value : ( value / 1e9 ) . toFixed ( 2 ) , unit : "G" } ;
22- if ( value >= 1e6 ) return { value : ( value / 1e6 ) . toFixed ( 2 ) , unit : "M" } ;
23- if ( value >= 1e3 ) return { value : ( value / 1e3 ) . toFixed ( 2 ) , unit : "K" } ;
24- return { value : value . toFixed ( 2 ) , unit : "" } ;
13+ if ( value >= 1e12 ) return { value : ( value / 1e12 ) . toFixed ( 2 ) , unit : 'T' } ;
14+ if ( value >= 1e9 ) return { value : ( value / 1e9 ) . toFixed ( 2 ) , unit : 'G' } ;
15+ if ( value >= 1e6 ) return { value : ( value / 1e6 ) . toFixed ( 2 ) , unit : 'M' } ;
16+ if ( value >= 1e3 ) return { value : ( value / 1e3 ) . toFixed ( 2 ) , unit : 'K' } ;
17+ return { value : value . toFixed ( 2 ) , unit : '' } ;
2518} ;
2619
2720const errorResponse = ( ) =>
2821 Response . json ( {
29- type : " four-stats" ,
22+ type : ' four-stats' ,
3023 refresh : REFRESH ,
31- link : "" ,
24+ link : '' ,
3225 items : [
33- { title : " Hashrate" , text : "-" } ,
34- { title : " Workers" , text : "-" } ,
35- { title : " Best Share" , text : "-" } ,
36- { title : " Total Shares" , text : "-" } ,
26+ { title : ' Hashrate' , text : '-' } ,
27+ { title : ' Workers' , text : '-' } ,
28+ { title : ' Best Share' , text : '-' } ,
29+ { title : ' Total Shares' , text : '-' } ,
3730 ] ,
3831 } ) ;
3932
@@ -43,18 +36,19 @@ serve({
4336 const url = new URL ( req . url ) ;
4437 const apiUrl = process . env . BASSIN_API_POOL_URL ;
4538
46- if ( url . pathname !== " /widgets/pool" ) {
47- return new Response ( " Path not found" , { status : 404 } ) ;
39+ if ( url . pathname !== ' /widgets/pool' ) {
40+ return new Response ( ' Path not found' , { status : 404 } ) ;
4841 }
4942 if ( ! apiUrl ) {
50- return new Response ( " No API url provided" , { status : 403 } ) ;
43+ return new Response ( ' No API url provided' , { status : 403 } ) ;
5144 }
5245
5346 try {
5447 const response = await fetch ( `${ apiUrl } ?${ Date . now ( ) } ` ) ;
5548 if ( ! response . ok ) throw new Error ( `HTTP error: ${ response . status } ` ) ;
56- const lines = ( await response . text ( ) ) . trim ( ) . split ( "\n" ) . filter ( Boolean ) ;
57- if ( lines . length < 3 ) throw new Error ( "Malformed pool response" ) ;
49+
50+ const lines = ( await response . text ( ) ) . trim ( ) . split ( '\n' ) . filter ( Boolean ) ;
51+ if ( lines . length < 3 ) throw new Error ( 'Malformed pool response' ) ;
5852
5953 const [ pool , hashrate , shares ] = lines . map ( ( line ) => JSON . parse ( line ) ) ;
6054
@@ -64,18 +58,18 @@ serve({
6458 const accepted = abbreviateNumber ( shares . accepted ) ;
6559
6660 return Response . json ( {
67- type : " four-stats" ,
61+ type : ' four-stats' ,
6862 refresh : REFRESH ,
69- link : "" ,
63+ link : '' ,
7064 items : [
71- { title : " Hashrate" , text : hashrate5m . value , subtext : hashrate5m . unit } ,
72- { title : "Workers" , text : workers } ,
73- { title : " Best Share" , text : bestshare . value , subtext : bestshare . unit } ,
74- { title : " Total Shares" , text : accepted . value , subtext : accepted . unit } ,
65+ { title : ' Hashrate' , text : hashrate5m . value , subtext : hashrate5m . unit } ,
66+ { title : `Worker ${ workers === '1' ? '' : 's' } ` , text : workers } ,
67+ { title : ' Best Share' , text : bestshare . value , subtext : bestshare . unit } ,
68+ { title : ' Total Shares' , text : accepted . value , subtext : accepted . unit } ,
7569 ] ,
7670 } ) ;
7771 } catch ( error ) {
78- console . error ( " Error fetching data:" , error ) ;
72+ console . error ( ' Error fetching data' , error ) ;
7973 return errorResponse ( ) ;
8074 }
8175 } ,
0 commit comments