Skip to content

Commit

Permalink
refactor getProposals API by removing unnecessary comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lealbrunocalhau committed Nov 14, 2024
1 parent 5b65680 commit 13c5411
Showing 1 changed file with 1 addition and 12 deletions.
13 changes: 1 addition & 12 deletions src/api/routes/v2-state/get_proposals/get_proposals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { estypes } from '@elastic/elasticsearch';
async function getProposals(fastify: FastifyInstance, request: FastifyRequest) {
const query: any = request.query;

// Pagination
let skip: number;
let limit: number;
skip = parseInt(query.skip, 10);
Expand All @@ -29,9 +28,8 @@ async function getProposals(fastify: FastifyInstance, request: FastifyRequest) {
};

let stateResult: any[];
const startTime = Date.now(); // Inicia o cronômetro para a consulta
const startTime = Date.now();

// Verifica se a consulta deve ser feita no MongoDB
if (fastify.manager.config.indexer.experimental_mongodb_state && fastify.manager.conn.mongodb && query.useMongo === 'true') {
const dbName = `${fastify.manager.conn.mongodb.database_prefix}_${fastify.manager.chain}`;
const collection = fastify.mongo.client.db(dbName).collection('proposals');
Expand Down Expand Up @@ -60,8 +58,6 @@ async function getProposals(fastify: FastifyInstance, request: FastifyRequest) {
mongoQuery["provided_approvals.actor"] = query.provided;
}

// Consultar MongoDB
console.log(`Consultar Mongo`)
const lastBlockResult = await fastify.mongo.client.db(dbName).collection('proposals').find().sort({ block_num: -1 }).limit(1).toArray();
response.last_indexed_block = lastBlockResult[0]?.block_num || 0;
response.last_indexed_block_time = lastBlockResult[0]?.block_time || '';
Expand All @@ -74,8 +70,6 @@ async function getProposals(fastify: FastifyInstance, request: FastifyRequest) {
.toArray();

} else {
// Caso não seja no MongoDB, faz a consulta no Elasticsearch
console.log(`Consultar Elastic`)
let queryStruct: any = { bool: { must: [] } };
if (query.account) {
const accounts = query.account.split(',');
Expand Down Expand Up @@ -110,7 +104,6 @@ async function getProposals(fastify: FastifyInstance, request: FastifyRequest) {
queryStruct = { match_all: {} };
}

// Consultar Elasticsearch
const esResult = await fastify.elastic.search<any>({
index: `${fastify.manager.chain}-table-proposals-*`,
from: skip || 0,
Expand All @@ -123,19 +116,15 @@ async function getProposals(fastify: FastifyInstance, request: FastifyRequest) {
stateResult = esResult.hits.hits.map((hit: any) => hit._source);
response.total = (esResult.hits.total as estypes.SearchTotalHits).value;

// Pegando o último bloco indexado
response.last_indexed_block = esResult.hits.hits.length > 0 ? esResult.hits.hits[0]._source.block_num : 0;
response.last_indexed_block_time = esResult.hits.hits.length > 0 ? esResult.hits.hits[0]._source.block_time : '';
}

// Formatar resposta com os dados obtidos
for (const proposal of stateResult) {
response.proposals.push(proposal);
}

// Calculando o tempo de consulta
response.query_time_ms = Date.now() - startTime;

return response;
}

Expand Down

0 comments on commit 13c5411

Please sign in to comment.