Revert "fix: throttle Tiny API usage across backfills"

This reverts commit 10d7048497.
This commit is contained in:
Cauê Faleiros
2026-06-22 16:31:36 -03:00
parent ae41ae4a55
commit 1d24831aa7
6 changed files with 25 additions and 159 deletions

View File

@@ -2,7 +2,6 @@ import axios from 'axios';
import dotenv from 'dotenv';
import fs from 'fs';
import path from 'path';
import { tinyPost } from '../services/tiny-api.service';
dotenv.config();
@@ -58,11 +57,9 @@ const START_DATE = process.env.GRAPHS_BACKFILL_START_DATE || '13/02/2025';
const END_DATE = process.env.GRAPHS_BACKFILL_END_DATE || getTodayDate();
const MAX_DAYS = Number(process.env.GRAPHS_BACKFILL_MAX_DAYS || 0);
const MAX_ORDERS = Number(process.env.GRAPHS_BACKFILL_MAX_ORDERS || 0);
const TINY_MIN_REQUEST_INTERVAL_MS = Number(process.env.TINY_MIN_REQUEST_INTERVAL_MS || 0);
const USE_SHARED_RATE_LIMIT = TINY_MIN_REQUEST_INTERVAL_MS > 0;
const TINY_SEARCH_DELAY_MS = USE_SHARED_RATE_LIMIT ? 0 : Number(process.env.TINY_SEARCH_DELAY_MS || 1000);
const TINY_DETAIL_DELAY_MS = USE_SHARED_RATE_LIMIT ? 0 : Number(process.env.TINY_DETAIL_DELAY_MS || 1500);
const TINY_CONTACT_DELAY_MS = USE_SHARED_RATE_LIMIT ? 0 : Number(process.env.TINY_CONTACT_DELAY_MS || TINY_DETAIL_DELAY_MS);
const TINY_SEARCH_DELAY_MS = Number(process.env.TINY_SEARCH_DELAY_MS || 1000);
const TINY_DETAIL_DELAY_MS = Number(process.env.TINY_DETAIL_DELAY_MS || 1500);
const TINY_CONTACT_DELAY_MS = Number(process.env.TINY_CONTACT_DELAY_MS || TINY_DETAIL_DELAY_MS);
const STATE_FILE = process.env.GRAPHS_BACKFILL_STATE_FILE || path.join(process.cwd(), 'graphs_backfill_state.json');
const contactCache = new Map<string, ContactInfo | null>();
@@ -170,6 +167,12 @@ function pickFirst(...values: unknown[]) {
return '';
}
async function tinyPost(servicePhp: string, params: URLSearchParams) {
return axios.post(`https://api.tiny.com.br/api2/${servicePhp}`, params, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
}
async function fetchOrdersForDate(dateStr: string, page = 1): Promise<TinyOrderSummary[]> {
console.log(`[Tiny] Searching orders for ${dateStr}, page ${page}`);
@@ -376,12 +379,6 @@ async function runBackfill() {
console.log(`[config] End date: ${END_DATE}`);
console.log(`[config] Dry run: ${DRY_RUN ? 'yes' : 'no'}`);
console.log(`[config] Contact fallback: ${CONTACT_FALLBACK ? 'yes' : 'no'}`);
if (USE_SHARED_RATE_LIMIT) {
const requestsPerMinute = Math.floor(60000 / TINY_MIN_REQUEST_INTERVAL_MS);
console.log(`[config] Shared Tiny rate limit: 1 request every ${TINY_MIN_REQUEST_INTERVAL_MS}ms (~${requestsPerMinute}/minute)`);
} else {
console.log('[config] Shared Tiny rate limit: disabled');
}
console.log(`[config] State file: ${STATE_FILE}`);
while (!isAfter(currentDate, END_DATE)) {

View File

@@ -2,7 +2,6 @@ import axios from 'axios';
import dotenv from 'dotenv';
import fs from 'fs';
import path from 'path';
import { tinyPost } from '../services/tiny-api.service';
dotenv.config();
@@ -51,7 +50,9 @@ const fetchOrdersForDate = async (dateStr: string, page: number = 1): Promise<an
params.append('pagina', page.toString());
try {
const response = await tinyPost('pedidos.pesquisa.php', params);
const response = await axios.post('https://api.tiny.com.br/api2/pedidos.pesquisa.php', params, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
await sleep(1000); // 1 second delay
@@ -91,7 +92,9 @@ const fetchOrderDetails = async (orderId: string) => {
params.append('formato', 'JSON');
try {
const response = await tinyPost('pedido.obter.php', params);
const response = await axios.post('https://api.tiny.com.br/api2/pedido.obter.php', params, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
});
await sleep(1500); // 1.5 second delay. This is the heavy part.
@@ -181,4 +184,4 @@ const runBackfill = async () => {
console.log('\n✅ BACKFILL COMPLETE! Caught up to today.');
};
runBackfill();
runBackfill();