11const { query } = require ( "./db" ) ;
22const { getNowInBrasiliaISO } = require ( "../utils/dateTime" ) ;
33
4+ // Converte o JSON salvo no banco para um array padrao usado pelos services.
45function parsePayload ( payloadJson ) {
56 if ( Array . isArray ( payloadJson ) ) {
67 return payloadJson ;
@@ -18,6 +19,7 @@ function parsePayload(payloadJson) {
1819 }
1920}
2021
22+ // Mapeia uma linha do banco para o formato de snapshot usado na aplicacao.
2123function mapSnapshotRow ( row ) {
2224 if ( ! row ) return null ;
2325
@@ -34,6 +36,7 @@ function mapSnapshotRow(row) {
3436 } ;
3537}
3638
39+ // Salva um novo snapshot no historico e atualiza a tabela com a ultima coleta.
3740async function saveSnapshot ( {
3841 source,
3942 payload,
@@ -85,6 +88,7 @@ async function saveSnapshot({
8588 } ;
8689}
8790
91+ // Busca o snapshot mais recente de uma fonte especifica.
8892async function getLatestSnapshot ( source ) {
8993 const result = await query (
9094 `
@@ -99,6 +103,7 @@ async function getLatestSnapshot(source) {
99103 return mapSnapshotRow ( row ) ;
100104}
101105
106+ // Lista snapshots mais recentes, com ou sem filtro por fonte.
102107async function listSnapshots ( { source = null , limit = 50 } ) {
103108 const parsedLimit = Number ( limit ) ;
104109 const safeLimit = Number . isFinite ( parsedLimit ) ? Math . min ( Math . max ( parsedLimit , 1 ) , 500 ) : 50 ;
@@ -127,8 +132,48 @@ async function listSnapshots({ source = null, limit = 50 }) {
127132 return result . rows . map ( mapSnapshotRow ) ;
128133}
129134
135+ // Lista snapshots dentro de um periodo para montar consultas historicas.
136+ async function listSnapshotsByPeriod ( { source = null , startDate = null , endDate = null , limit = 500 } ) {
137+ const parsedLimit = Number ( limit ) ;
138+ const safeLimit = Number . isFinite ( parsedLimit ) ? Math . min ( Math . max ( parsedLimit , 1 ) , 2000 ) : 500 ;
139+ const values = [ ] ;
140+ const conditions = [ ] ;
141+
142+ if ( source ) {
143+ values . push ( source ) ;
144+ conditions . push ( `fonte = $${ values . length } ` ) ;
145+ }
146+
147+ if ( startDate ) {
148+ values . push ( startDate ) ;
149+ conditions . push ( `coletado_em >= $${ values . length } ` ) ;
150+ }
151+
152+ if ( endDate ) {
153+ values . push ( endDate ) ;
154+ conditions . push ( `coletado_em <= $${ values . length } ` ) ;
155+ }
156+
157+ values . push ( safeLimit ) ;
158+
159+ const whereClause = conditions . length > 0 ? `WHERE ${ conditions . join ( " AND " ) } ` : "" ;
160+ const result = await query (
161+ `
162+ SELECT id, fonte, dados_json, quantidade_itens, coletado_em, janela_horario, tipo_disparo, criado_em
163+ FROM cotacoes_historico
164+ ${ whereClause }
165+ ORDER BY coletado_em ASC, id ASC
166+ LIMIT $${ values . length }
167+ ` ,
168+ values
169+ ) ;
170+
171+ return result . rows . map ( mapSnapshotRow ) ;
172+ }
173+
130174module . exports = {
131175 getLatestSnapshot,
132176 listSnapshots,
177+ listSnapshotsByPeriod,
133178 saveSnapshot,
134179} ;
0 commit comments