"use strict";async function decompressAsync(e){const t=await gunzip(e);return JSON.parse(t.toString())}function getIntervalMs(e){return{"1m":6e4,"3m":18e4,"5m":3e5,"15m":9e5,"30m":18e5,"1h":36e5,"2h":72e5,"4h":144e5,"6h":216e5,"8h":288e5,"12h":432e5,"1d":864e5,"3d":2592e5,"1w":6048e5}[e]||6e4}function countGaps(e,t){if(!Array.isArray(e)||e.length<2)return 0;let r=0;for(let a=1;a<e.length;a++){const s=e[a-1][0]+t;e[a][0]>s+t&&r++}return r}async function fileExists(e){try{await fs_1.promises.access(e);return!0}catch(e){return!1}}async function processInterval(e,t){const r=path_1.default.join(e,`${t}.json.gz`);if(!await fileExists(r))return{interval:t,data:null,size:0};try{const[e,a]=await Promise.all([fs_1.promises.stat(r),fs_1.promises.readFile(r)]),s=await decompressAsync(a);if(Array.isArray(s)&&s.length>0){const r=getIntervalMs(t);return{interval:t,data:{candleCount:s.length,fileSize:e.size,oldestCandle:s[0][0],newestCandle:s[s.length-1][0],gaps:countGaps(s,r)},size:e.size}}return{interval:t,data:null,size:0}}catch(e){return{interval:t,data:{candleCount:0,fileSize:0,oldestCandle:null,newestCandle:null,gaps:0,error:"Failed to read cache file"},size:0}}}async function processMarket(e){const t=`${e.currency}/${e.pair}`,r=path_1.default.join(cacheDirPath,e.currency,e.pair),a=await Promise.all(INTERVALS.map(e=>processInterval(r,e))),s={};let i=0;for(const e of a)if(e.data){s[e.interval]=e.data;i+=e.size}return{marketStat:{id:e.id,symbol:t,currency:e.currency,pair:e.pair,status:e.status,intervals:s},cacheSize:i}}var __importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0});exports.metadata=void 0;const fs_1=require("fs"),path_1=__importDefault(require("path")),zlib_1=__importDefault(require("zlib")),util_1=require("util"),db_1=require("@b/db"),query_1=require("@b/utils/query"),error_1=require("@b/utils/error"),gunzip=(0,util_1.promisify)(zlib_1.default.gunzip),cacheDirPath=path_1.default.resolve(process.cwd(),"data","chart");exports.metadata={summary:"Get chart data statistics for all markets",operationId:"getChartStatistics",tags:["Admin","Exchange","Chart"],responses:{200:{description:"Chart statistics for all markets",content:{"application/json":{schema:{type:"object",properties:{markets:{type:"array",items:{type:"object",properties:{symbol:{type:"string"},currency:{type:"string"},pair:{type:"string"},status:{type:"boolean"},intervals:{type:"object",additionalProperties:{type:"object",properties:{candleCount:{type:"number"},fileSize:{type:"number"},oldestCandle:{type:"number"},newestCandle:{type:"number"},gaps:{type:"number"}}}}}}},totalMarkets:{type:"number"},totalCacheSize:{type:"number"}}}}}},401:query_1.unauthorizedResponse,500:query_1.serverErrorResponse},requiresAuth:!0,permission:"view.exchange.chart"};const INTERVALS=["1m","5m","15m","30m","1h","2h","4h","6h","12h","1d","3d","1w"];exports.default=async()=>{try{const e=await db_1.models.exchangeMarket.findAll({where:{status:!0},attributes:["id","currency","pair","status"],raw:!0}),t=await Promise.all(e.map(processMarket)),r=t.map(e=>e.marketStat),a=t.reduce((e,t)=>e+t.cacheSize,0);return{markets:r,totalMarkets:e.length,totalCacheSize:a,intervals:INTERVALS}}catch(e){throw(0,error_1.createError)({statusCode:500,message:`Failed to get chart statistics: ${e.message}`})}};