diff --git a/hooks/useStorageMonitor.ts b/hooks/useStorageMonitor.ts index 80a4761..b210a4c 100644 --- a/hooks/useStorageMonitor.ts +++ b/hooks/useStorageMonitor.ts @@ -86,44 +86,6 @@ export function useStorageMonitor(): UseStorageMonitorReturn { fetchStats(); }, [fetchStats]); - // Monitoramento automático para todos os usuários - useEffect(() => { - let intervalId: NodeJS.Timeout | null = null; - - const startMonitoring = async () => { - // Executar imediatamente - await fetchAndDisplayStats(); - - // Depois executar a cada 10 segundos - intervalId = setInterval(async () => { - await fetchAndDisplayStats(); - }, 10000); - }; - - const fetchAndDisplayStats = async () => { - try { - const response = await fetch("/api/storage-stats"); - const data = await response.json(); - - if (data.success) { - console.log( - `[Monitor] Total de produtos no cache: ${data.data.totalProducts} produtos` - ); - } - } catch (error) { - console.error("[Monitor] Erro ao buscar estatísticas:", error); - } - }; - - startMonitoring(); - - return () => { - if (intervalId) { - clearInterval(intervalId); - } - }; - }, []); - return { stats, isLoading, diff --git a/lib/jsonCacheManager.ts b/lib/jsonCacheManager.ts index fc23bdf..b60353f 100644 --- a/lib/jsonCacheManager.ts +++ b/lib/jsonCacheManager.ts @@ -235,33 +235,31 @@ export class JsonCacheManager { `[JSON Cache] Buscando: "${searchTerm}" (país: ${country || "todos"})` ); - // Filtrar produtos + // Filtrar produtos; a relevância é calculada uma vez por produto const filteredProducts = Object.values(this.cacheData.products) .filter((cachedProduct) => { - const product = this.convertToAPIProduct(cachedProduct); - // Verificar se corresponde à query const matchesQuery = - product.product_name?.toLowerCase().includes(searchTerm) || - product.brands?.toLowerCase().includes(searchTerm) || - product.categories?.toLowerCase().includes(searchTerm); + cachedProduct.product_name?.toLowerCase().includes(searchTerm) || + cachedProduct.brands?.toLowerCase().includes(searchTerm) || + cachedProduct.categories?.toLowerCase().includes(searchTerm); // Verificar país se especificado const matchesCountry = !country || - product.countries_tags?.includes(country) || - product.countries?.toLowerCase().includes(country.toLowerCase()); + cachedProduct.countries_tags?.includes(country) || + cachedProduct.countries?.toLowerCase().includes(country.toLowerCase()); return matchesQuery && matchesCountry; }) - .map((cachedProduct) => this.convertToAPIProduct(cachedProduct)) - // Ordenar por relevância - .sort((a, b) => { - const scoreA = this.calculateRelevanceScore(a, searchTerm); - const scoreB = this.calculateRelevanceScore(b, searchTerm); - return scoreB - scoreA; + .map((cachedProduct) => { + const product = this.convertToAPIProduct(cachedProduct); + return { product, score: this.calculateRelevanceScore(product, searchTerm) }; }) - .slice(0, limit); + // Ordenar por relevância + .sort((a, b) => b.score - a.score) + .slice(0, limit) + .map(({ product }) => product); console.log( `[JSON Cache] Encontrados ${filteredProducts.length} produtos`