@@ -41,7 +41,8 @@ const logger = require('../utils/logger');
4141const { parseCreateTime, parseTimeToUnixEpoch} = require ( '../utils/time' ) ;
4242const { applyDeltaEncoding, decodeDeltaEncoding } = require ( '../utils/encoding' ) ;
4343const sqlite3 = require ( 'sqlite3' ) . verbose ( ) ;
44- const path = require ( "path" )
44+ const path = require ( "path" ) ;
45+ const { devNull } = require ( 'os' ) ;
4546
4647// Initialize SQLite database connection
4748const dbPath = path . join ( __dirname , '../cache/transactions.db' ) ;
@@ -124,64 +125,64 @@ async function getBlocks(req, res) {
124125 }
125126}
126127
127- /**
128- * Fetches block data from the EXPLORER_BASE_URL and sends it as a response.
129- *
130- * @async
131- * @function getBlocks
132- * @param {Object } req - The HTTP request object.
133- * @param {Object } req.query - The query parameters.
134- * @param {number } req.query.start - The starting block number.
135- * @param {number } req.query.end - The ending block number.
136- * @param {Object } res - The HTTP response object.
137- * @returns {Promise<void> } Sends the fetched data or an error response.
138- */
139- async function getEncodedBlocks ( req , res ) {
140- const start = parseInt ( req . query . start , 10 ) ;
141- const end = parseInt ( req . query . end , 10 ) ;
128+ // / **
129+ // * Fetches block data from the EXPLORER_BASE_URL and sends it as a response.
130+ // *
131+ // * @async
132+ // * @function getBlocks
133+ // * @param {Object } req - The HTTP request object.
134+ // * @param {Object } req.query - The query parameters.
135+ // * @param {number } req.query.start - The starting block number.
136+ // * @param {number } req.query.end - The ending block number.
137+ // * @param {Object } res - The HTTP response object.
138+ // * @returns {Promise<void> } Sends the fetched data or an error response.
139+ // */
140+ // async function getEncodedBlocks(req, res) {
141+ // const start = parseInt(req.query.start, 10);
142+ // const end = parseInt(req.query.end, 10);
142143
143- try {
144- logger . info ( `Attempting to fetch blocks from cache` ) ;
145- const cacheData = await getDataFromCache ( start , end ) ;
144+ // try {
145+ // logger.info(`Attempting to fetch blocks from cache`);
146+ // const cacheData = await getDataFromCache(start, end);
146147
147- if ( cacheData && cacheData . length > 0 ) {
148- logger . info ( `Cache hit for blocks, returned ${ cacheData . length } records` ) ;
149- let modifiedData = cacheData . map ( record => {
150- return {
151- epoch : parseTimeToUnixEpoch ( record . created_at ) ,
152- volume : record . volume || 0
153- } ;
154- } ) ;
148+ // if (cacheData && cacheData.length > 0) {
149+ // logger.info(`Cache hit for blocks, returned ${cacheData.length} records`);
150+ // let modifiedData = cacheData.map(record => {
151+ // return {
152+ // epoch: parseTimeToUnixEpoch(record.created_at),
153+ // volume: record.volume || 0
154+ // };
155+ // });
155156
156- modifiedData = applyDeltaEncoding ( modifiedData ) ;
157- return res . send ( {
158- isCached : true ,
159- ...modifiedData
160- } ) ;
161- }
162- logger . info ( `Cache miss for blocks ${ start } -${ end } , falling back to API` ) ;
157+ // modifiedData = applyDeltaEncoding(modifiedData);
158+ // return res.send({
159+ // isCached: true,
160+ // ...modifiedData
161+ // });
162+ // }
163+ // logger.info(`Cache miss for blocks ${start}-${end}, falling back to API`);
163164
164- } catch ( cacheError ) {
165- logger . error ( 'Error retrieving data from cache. Continue to API fallback' , cacheError ) ;
166- }
167- try {
168- const apiData = await getDataFromApi ( start , end ) ;
169- const modifiedData = applyDeltaEncoding ( apiData ) ;
170- return res . send ( modifiedData ) ;
171- } catch ( error ) {
172- logger . error ( 'Error fetching block data from API:' , error ) ;
173- return res . status ( 500 ) . send ( {
174- error : 'Failed to fetch block data' ,
175- details : error . message ,
176- } ) ;
177- }
178- }
165+ // } catch (cacheError) {
166+ // logger.error('Error retrieving data from cache. Continue to API fallback', cacheError);
167+ // }
168+ // try {
169+ // const apiData = await getDataFromApi(start, end);
170+ // const modifiedData = applyDeltaEncoding(apiData);
171+ // return res.send(modifiedData);
172+ // } catch (error) {
173+ // logger.error('Error fetching block data from API:', error);
174+ // return res.status(500).send({
175+ // error: 'Failed to fetch block data',
176+ // details: error.message,
177+ // });
178+ // }
179+ // }
179180// Using the function below for the graph, decoupled with pagination for the table
180181async function getAllEncodedBlocks ( req , res ) {
181182 try {
182183 logger . info ( `Fetching ALL blocks from cache for full graph rendering` ) ;
183184
184- const cacheData = await getDataFromCache ( ) ; // No start/end
185+ const cacheData = await getDataFromCache ( null , null ) ; // No start/end
185186
186187 if ( ! cacheData || cacheData . length === 0 ) {
187188 logger . warn ( 'No cache data available for full graph' ) ;
@@ -209,43 +210,51 @@ async function getAllEncodedBlocks(req, res) {
209210/**
210211 * Retrieves data from the SQLite cache
211212 *
212- * @param {number } start - Start block ID
213- * @param {number } end - End block ID
213+ * @param {number|null } start - Start block ID
214+ * @param {number|null } end - End block ID
214215 * @returns {Promise<Array> } - The cached block data
215216 */
216217function getDataFromCache ( start , end ) {
217218 return new Promise ( ( resolve , reject ) => {
218- let query ;
219- let params = [ ] ;
220-
221- // Only add WHERE clause if both start and end are valid numbers
222- if ( ! isNaN ( start ) && ! isNaN ( end ) ) {
223- query = `
224- SELECT block_id, volume, created_at
225- FROM transactions
226- WHERE block_id BETWEEN ? AND ?
227- ORDER BY block_id ASC
228- ` ;
229- params = [ start , end ] ;
219+ let query ;
220+ let params = [ ] ;
221+
222+ if ( typeof start === "number" && typeof end === "number" && ! isNaN ( start ) && ! isNaN ( end ) ) {
223+ // Used by table view
224+ query = `
225+ SELECT block_id, volume, created_at
226+ FROM transactions
227+ WHERE block_id BETWEEN ? AND ?
228+ ORDER BY block_id ASC
229+ ` ;
230+ params = [ start , end ] ;
231+ } else if ( start === null && end === null ) {
232+ // Used by full chart query
233+ query = `
234+ SELECT block_id, volume, created_at
235+ FROM transactions
236+ ORDER BY block_id ASC
237+ ` ;
238+ } else {
239+ // Fallback when parameters are missing or invalid
240+ query = `
241+ SELECT block_id, volume, created_at
242+ FROM transactions
243+ ORDER BY block_id ASC
244+ LIMIT 100
245+ ` ;
246+ }
247+
248+ db . all ( query , params , ( err , rows ) => {
249+ if ( err ) {
250+ reject ( err ) ;
230251 } else {
231- query = `
232- SELECT block_id, volume, created_at
233- FROM transactions
234- ORDER BY block_id ASC
235- LIMIT 100
236- ` ;
252+ resolve ( rows ) ;
237253 }
238-
239- db . all ( query , params , ( err , rows ) => {
240- if ( err ) {
241- reject ( err ) ;
242- } else {
243- resolve ( rows ) ;
244- }
245- } ) ;
254+ } ) ;
246255 } ) ;
247- }
248-
256+ }
257+
249258/**
250259 * Retrieves data from the API
251260 *
0 commit comments