1
1
import * as Sentry from '@sentry/node'
2
- import { json } from 'http-responders'
2
+ import { redirect } from 'http-responders'
3
3
4
4
import { getStatsWithFilterAndCaching } from './request-helpers.js'
5
5
@@ -22,18 +22,20 @@ import { handlePlatformRoutes } from './platform-routes.js'
22
22
23
23
/**
24
24
* @param {object } args
25
+ * @param {string } args.SPARK_API_BASE_URL
25
26
* @param {import('@filecoin-station/spark-stats-db').PgPools } args.pgPools
26
27
* @param {import('./typings.d.ts').Logger } args.logger
27
28
* @returns
28
29
*/
29
30
export const createHandler = ( {
31
+ SPARK_API_BASE_URL ,
30
32
pgPools,
31
33
logger
32
34
} ) => {
33
35
return ( req , res ) => {
34
36
const start = Date . now ( )
35
37
logger . request ( `${ req . method } ${ req . url } ...` )
36
- handler ( req , res , pgPools )
38
+ handler ( req , res , pgPools , SPARK_API_BASE_URL )
37
39
. catch ( err => errorHandler ( res , err , logger ) )
38
40
. then ( ( ) => {
39
41
logger . request ( `${ req . method } ${ req . url } ${ res . statusCode } (${ Date . now ( ) - start } ms)` )
@@ -73,8 +75,9 @@ const createRespondWithFetchFn =
73
75
* @param {import('node:http').IncomingMessage } req
74
76
* @param {import('node:http').ServerResponse } res
75
77
* @param {import('@filecoin-station/spark-stats-db').PgPools } pgPools
78
+ * @param {string } SPARK_API_BASE_URL
76
79
*/
77
- const handler = async ( req , res , pgPools ) => {
80
+ const handler = async ( req , res , pgPools , SPARK_API_BASE_URL ) => {
78
81
// Caveat! `new URL('//foo', 'http://127.0.0.1')` would produce "http://foo/" - not what we want!
79
82
const { pathname, searchParams } = new URL ( `http://127.0.0.1${ req . url } ` )
80
83
const segs = pathname . split ( '/' ) . filter ( Boolean )
@@ -102,11 +105,11 @@ const handler = async (req, res, pgPools) => {
102
105
} else if ( req . method === 'GET' && url === '/miners/retrieval-success-rate/summary' ) {
103
106
await respond ( fetchMinersRSRSummary )
104
107
} else if ( req . method === 'GET' && segs [ 0 ] === 'miner' && segs [ 1 ] && segs [ 2 ] === 'deals' && segs [ 3 ] === 'eligible' && segs [ 4 ] === 'summary' ) {
105
- await getRetrievableDealsForMiner ( req , res , pgPools . api , segs [ 1 ] )
108
+ redirectToSparkApi ( req , res , SPARK_API_BASE_URL )
106
109
} else if ( req . method === 'GET' && segs [ 0 ] === 'client' && segs [ 1 ] && segs [ 2 ] === 'deals' && segs [ 3 ] === 'eligible' && segs [ 4 ] === 'summary' ) {
107
- await getRetrievableDealsForClient ( req , res , pgPools . api , segs [ 1 ] )
110
+ redirectToSparkApi ( req , res , SPARK_API_BASE_URL )
108
111
} else if ( req . method === 'GET' && segs [ 0 ] === 'allocator' && segs [ 1 ] && segs [ 2 ] === 'deals' && segs [ 3 ] === 'eligible' && segs [ 4 ] === 'summary' ) {
109
- await getRetrievableDealsForAllocator ( req , res , pgPools . api , segs [ 1 ] )
112
+ redirectToSparkApi ( req , res , SPARK_API_BASE_URL )
110
113
} else if ( await handlePlatformRoutes ( req , res , pgPools ) ) {
111
114
// no-op, request was handled by handlePlatformRoute
112
115
} else if ( req . method === 'GET' && url === '/' ) {
@@ -141,86 +144,14 @@ const notFound = (res) => {
141
144
}
142
145
143
146
/**
144
- * @param {import('node:http').IncomingMessage } _req
147
+ * @param {import('node:http').IncomingMessage } req
145
148
* @param {import('node:http').ServerResponse } res
146
- * @param {PgPools['api'] } client
147
- * @param {string } minerId
149
+ * @param {string } SPARK_API_BASE_URL
148
150
*/
149
- const getRetrievableDealsForMiner = async ( _req , res , client , minerId ) => {
150
- /** @type {{rows: {client_id: string; deal_count: number}[]} } */
151
- const { rows } = await client . query ( `
152
- SELECT client_id, COUNT(cid)::INTEGER as deal_count FROM retrievable_deals
153
- WHERE miner_id = $1 AND expires_at > now()
154
- GROUP BY client_id
155
- ORDER BY deal_count DESC, client_id ASC
156
- ` , [
157
- minerId
158
- ] )
159
-
160
- // Cache the response for 6 hours
161
- res . setHeader ( 'cache-control' , `max-age=${ 6 * 3600 } ` )
162
-
163
- const body = {
164
- minerId,
165
- dealCount : rows . reduce ( ( sum , row ) => sum + row . deal_count , 0 ) ,
166
- clients :
167
- rows . map (
168
- // eslint-disable-next-line camelcase
169
- ( { client_id, deal_count } ) => ( { clientId : client_id , dealCount : deal_count } )
170
- )
171
- }
172
-
173
- json ( res , body )
174
- }
175
-
176
- const getRetrievableDealsForClient = async ( _req , res , client , clientId ) => {
177
- /** @type {{rows: {miner_id: string; deal_count: number}[]} } */
178
- const { rows } = await client . query ( `
179
- SELECT miner_id, COUNT(cid)::INTEGER as deal_count FROM retrievable_deals
180
- WHERE client_id = $1 AND expires_at > now()
181
- GROUP BY miner_id
182
- ORDER BY deal_count DESC, miner_id ASC
183
- ` , [
184
- clientId
185
- ] )
186
-
151
+ const redirectToSparkApi = ( req , res , SPARK_API_BASE_URL ) => {
187
152
// Cache the response for 6 hours
188
153
res . setHeader ( 'cache-control' , `max-age=${ 6 * 3600 } ` )
189
154
190
- const body = {
191
- clientId,
192
- dealCount : rows . reduce ( ( sum , row ) => sum + row . deal_count , 0 ) ,
193
- providers : rows . map (
194
- // eslint-disable-next-line camelcase
195
- ( { miner_id, deal_count } ) => ( { minerId : miner_id , dealCount : deal_count } )
196
- )
197
- }
198
- json ( res , body )
199
- }
200
-
201
- const getRetrievableDealsForAllocator = async ( _req , res , client , allocatorId ) => {
202
- /** @type {{rows: {client_id: string; deal_count: number}[]} } */
203
- const { rows } = await client . query ( `
204
- SELECT ac.client_id, COUNT(cid)::INTEGER as deal_count
205
- FROM allocator_clients ac
206
- LEFT JOIN retrievable_deals rd ON ac.client_id = rd.client_id
207
- WHERE ac.allocator_id = $1 AND expires_at > now()
208
- GROUP BY ac.client_id
209
- ORDER BY deal_count DESC, ac.client_id ASC
210
- ` , [
211
- allocatorId
212
- ] )
213
-
214
- // Cache the response for 6 hours
215
- res . setHeader ( 'cache-control' , `max-age=${ 6 * 3600 } ` )
216
-
217
- const body = {
218
- allocatorId,
219
- dealCount : rows . reduce ( ( sum , row ) => sum + row . deal_count , 0 ) ,
220
- clients : rows . map (
221
- // eslint-disable-next-line camelcase
222
- ( { client_id, deal_count } ) => ( { clientId : client_id , dealCount : deal_count } )
223
- )
224
- }
225
- json ( res , body )
155
+ const location = new URL ( req . url , SPARK_API_BASE_URL ) . toString ( )
156
+ redirect ( req , res , location , 302 )
226
157
}
0 commit comments