From a6b9b05ce36624e65a2b006fbf434519cec00b94 Mon Sep 17 00:00:00 2001 From: ahaapple Date: Fri, 10 Jan 2025 20:47:09 +0800 Subject: [PATCH] Remove incSearchCount code --- frontend/lib/db.ts | 16 ++------- frontend/lib/tools/auto.ts | 5 --- frontend/lib/tools/chat.ts | 4 --- frontend/lib/tools/generate-ui.ts | 5 --- frontend/lib/tools/indie.ts | 5 --- frontend/lib/tools/knowledge-base.ts | 5 --- frontend/lib/tools/o1-answer.ts | 5 --- frontend/lib/tools/product.ts | 5 --- vector/redis.ts | 51 ++++------------------------ 9 files changed, 8 insertions(+), 93 deletions(-) diff --git a/frontend/lib/db.ts b/frontend/lib/db.ts index 14a8c97..bb98cc1 100644 --- a/frontend/lib/db.ts +++ b/frontend/lib/db.ts @@ -24,10 +24,6 @@ export const RATE_LIMIT_KEY = 'ratelimit'; export const URLS_KEY = 'urls:'; export const ERROR_URLS_KEY = 'error_urls:'; export const INDEX_COUNT_KEY = 'index_count:'; -export const TOTAL_INDEX_COUNT_KEY = 't_index_count:'; - -export const SEARCH_COUNT_KEY = 's_count:'; -export const TOTAL_SEARCH_COUNT_KEY = 't_s_count:'; export const redisDB = new Redis({ url: UPSTASH_REDIS_REST_URL!, @@ -62,13 +58,6 @@ export async function getUserIdByEmail(email: string) { } } -export async function incSearchCount(userId: string): Promise { - if (!userId) { - userId = 'guest'; - } - const result = await Promise.all([redisDB.incr(SEARCH_COUNT_KEY + userId), redisDB.incr(TOTAL_SEARCH_COUNT_KEY)]); -} - export async function removeUrlFromErrorUrls(userId: string, url: string) { const result = await redisDB.zrem(ERROR_URLS_KEY + userId, url); console.log('removeUrlFromErrorUrls:', result); @@ -124,13 +113,12 @@ export async function getUserIndexCount(userId: string): Promise { export async function addUrl(userId: string, url: string): Promise { const date = Date.now(); - const [zaddResult, incrIndexCountResult, incrTotalIndexCountResult] = await Promise.all([ + const [zaddResult, incrIndexCountResult] = await Promise.all([ redisDB.zadd(URLS_KEY + userId, { score: date, member: url }), redisDB.incr(INDEX_COUNT_KEY + userId), - redisDB.incr(TOTAL_INDEX_COUNT_KEY), ]); - console.log('Result of all operations:', zaddResult, incrIndexCountResult, incrTotalIndexCountResult); + console.log('Result of all operations:', zaddResult, incrIndexCountResult); return incrIndexCountResult; } diff --git a/frontend/lib/tools/auto.ts b/frontend/lib/tools/auto.ts index 5f2f30d..da9af69 100644 --- a/frontend/lib/tools/auto.ts +++ b/frontend/lib/tools/auto.ts @@ -1,6 +1,5 @@ import 'server-only'; -import { incSearchCount } from '@/lib/db'; import { convertToCoreMessages, getLLM, getMaxOutputToken } from '@/lib/llm/llm'; import { AutoAnswerPrompt } from '@/lib/llm/prompt'; import { getHistory, getHistoryMessages, streamResponse } from '@/lib/llm/utils'; @@ -271,10 +270,6 @@ export async function autoAnswer( await streamResponse({ title: title }, onStream); } - // incSearchCount(userId).catch((error) => { - // console.error(`Failed to increment search count for user ${userId}:`, error); - // }); - await saveMessages(userId, messages, fullAnswer, texts, images, videos, fullRelated, SearchCategory.ALL); indexMessage(userId, messages[0].title, messages[0].id, query + '\n\n' + fullAnswer).catch((error) => { console.error(`Failed to index message for user ${userId}:`, error); diff --git a/frontend/lib/tools/chat.ts b/frontend/lib/tools/chat.ts index 52aa0c7..bef929d 100644 --- a/frontend/lib/tools/chat.ts +++ b/frontend/lib/tools/chat.ts @@ -1,6 +1,5 @@ import 'server-only'; -import { incSearchCount } from '@/lib/db'; import { convertToCoreMessages, getLLM, getMaxOutputToken } from '@/lib/llm/llm'; import { ChatPrompt } from '@/lib/llm/prompt'; import { getHistoryMessages, streamResponse } from '@/lib/llm/utils'; @@ -71,9 +70,6 @@ export async function chat( await streamResponse({ title: title }, onStream); } - // incSearchCount(userId).catch((error) => { - // console.error(`Failed to increment search count for user ${userId}:`, error); - // }); await saveMessages(userId, messages, fullAnswer, [], [], [], '', SearchCategory.ALL); indexMessage(userId, messages[0].title, messages[0].id, query + '\n\n' + fullAnswer).catch((error) => { console.error(`Failed to index message for user ${userId}:`, error); diff --git a/frontend/lib/tools/generate-ui.ts b/frontend/lib/tools/generate-ui.ts index 4e97220..8491cf6 100644 --- a/frontend/lib/tools/generate-ui.ts +++ b/frontend/lib/tools/generate-ui.ts @@ -3,7 +3,6 @@ import { log, logError } from '@/lib/log'; import { streamText } from 'ai'; import { SearchCategory, Message as StoreMessage } from '@/lib/types'; import { Claude_35_Sonnet } from '@/lib/model'; -import { incSearchCount } from '@/lib/db'; import { extractErrorMessage, saveMessages } from '@/lib/server-utils'; import { getSearchEngine, TEXT_LIMIT } from '@/lib/search/search'; import util from 'util'; @@ -107,10 +106,6 @@ export async function generateUI( onStream?.(JSON.stringify({ answer: text })); } - // incSearchCount(userId).catch((error) => { - // console.error(`Failed to increment search count for user ${userId}:`, error); - // }); - await saveMessages(userId, messages, fullAnswer, [], [], [], '', SearchCategory.UI); } catch (error) { const errorMessage = extractErrorMessage(error); diff --git a/frontend/lib/tools/indie.ts b/frontend/lib/tools/indie.ts index c2c1bd2..3908d88 100644 --- a/frontend/lib/tools/indie.ts +++ b/frontend/lib/tools/indie.ts @@ -1,6 +1,5 @@ import 'server-only'; -import { incSearchCount } from '@/lib/db'; import { getLLM } from '@/lib/llm/llm'; import { getHistory, streamResponse } from '@/lib/llm/utils'; import { logError } from '@/lib/log'; @@ -102,10 +101,6 @@ export async function indieMakerSearch( ); }); - // incSearchCount(userId).catch((error) => { - // console.error(`Failed to increment search count for user ${userId}:`, error); - // }); - await saveMessages(userId, messages, fullAnswer, texts, images, videos, fullRelated); onStream?.(null, true); } catch (error) { diff --git a/frontend/lib/tools/knowledge-base.ts b/frontend/lib/tools/knowledge-base.ts index 6464442..d91c6f9 100644 --- a/frontend/lib/tools/knowledge-base.ts +++ b/frontend/lib/tools/knowledge-base.ts @@ -1,6 +1,5 @@ import 'server-only'; -import { incSearchCount } from '@/lib/db'; import { getLLM } from '@/lib/llm/llm'; import { getHistory, streamResponse } from '@/lib/llm/utils'; import { logError } from '@/lib/log'; @@ -66,10 +65,6 @@ export async function knowledgeBaseSearch(messages: StoreMessage[], isPro: boole return; } - // incSearchCount(userId).catch((error) => { - // console.error(`Failed to increment search count for user ${userId}:`, error); - // }); - await saveMessages(userId, messages, fullAnswer, texts, [], [], ''); onStream?.(null, true); } catch (error) { diff --git a/frontend/lib/tools/o1-answer.ts b/frontend/lib/tools/o1-answer.ts index 48bcc8b..9565f21 100644 --- a/frontend/lib/tools/o1-answer.ts +++ b/frontend/lib/tools/o1-answer.ts @@ -1,6 +1,5 @@ import 'server-only'; -import { incSearchCount } from '@/lib/db'; import { getLLM } from '@/lib/llm/llm'; import { DirectAnswerPrompt } from '@/lib/llm/prompt'; import { getHistoryMessages, streamResponse } from '@/lib/llm/utils'; @@ -89,10 +88,6 @@ export async function o1Answer( await streamResponse({ videos: videos }, onStream); } - incSearchCount(userId).catch((error) => { - console.error(`Failed to increment search count for user ${userId}:`, error); - }); - await saveMessages(userId, messages, fullAnswer, texts, images, videos, fullRelated); onStream?.(null, true); } catch (error) { diff --git a/frontend/lib/tools/product.ts b/frontend/lib/tools/product.ts index 80bfc9a..8b46102 100644 --- a/frontend/lib/tools/product.ts +++ b/frontend/lib/tools/product.ts @@ -1,6 +1,5 @@ import 'server-only'; -import { incSearchCount } from '@/lib/db'; import { getLLM } from '@/lib/llm/llm'; import { getHistory, streamResponse } from '@/lib/llm/utils'; import { logError } from '@/lib/log'; @@ -99,10 +98,6 @@ export async function productSearch( ); }); - // incSearchCount(userId).catch((error) => { - // console.error(`Failed to increment search count for user ${userId}:`, error); - // }); - await saveMessages(userId, messages, fullAnswer, texts, images, videos, fullRelated); onStream?.(null, true); } catch (error) { diff --git a/vector/redis.ts b/vector/redis.ts index 4cd23d6..d02f4e0 100644 --- a/vector/redis.ts +++ b/vector/redis.ts @@ -12,10 +12,6 @@ export const redis = new Redis({ export const URLS_KEY = "urls:"; export const ERROR_URLS_KEY = "error_urls:"; export const INDEX_COUNT_KEY = "index_count:"; -export const TOTAL_INDEX_COUNT_KEY = "t_index_count:"; - -export const SEARCH_COUNT_KEY = "s_count:"; -export const TOTAL_SEARCH_COUNT_KEY = "t_s_count:"; export const SEARCH_KEY = "search:"; export const USER_SEARCH_KEY = "user:search:"; @@ -54,19 +50,12 @@ export async function markUserFullIndexed(userId: string): Promise { export async function addUrl(userId: string, url: string): Promise { const date = Date.now(); - const [zaddResult, incrIndexCountResult, incrTotalIndexCountResult] = - await Promise.all([ - redis.zadd(URLS_KEY + userId, { score: date, member: url }), - redis.incr(INDEX_COUNT_KEY + userId), - redis.incr(TOTAL_INDEX_COUNT_KEY), - ]); - - console.log( - "Result of all operations:", - zaddResult, - incrIndexCountResult, - incrTotalIndexCountResult - ); + const [zaddResult, incrIndexCountResult] = await Promise.all([ + redis.zadd(URLS_KEY + userId, { score: date, member: url }), + redis.incr(INDEX_COUNT_KEY + userId), + ]); + + console.log("Result of all operations:", zaddResult, incrIndexCountResult); return incrIndexCountResult; } @@ -96,31 +85,3 @@ export async function urlExists(userId: string, url: string): Promise { export async function clearUrls(userId: string): Promise { await redis.del(URLS_KEY + userId); } - -async function getCount(key: string): Promise { - const value = await redis.get(key); - return parseInt((value as string) || "0", 10); -} - -export async function incIndexCount(userId: string): Promise { - const result = await Promise.all([ - redis.incr(INDEX_COUNT_KEY + userId), - redis.incr(TOTAL_INDEX_COUNT_KEY), - ]); -} - -export async function getIndexCount(userId: string): Promise { - return getCount(INDEX_COUNT_KEY + userId); -} - -export async function getTotalIndexCount(): Promise { - return getCount(TOTAL_INDEX_COUNT_KEY); -} - -export async function getSearchCount(userId: string): Promise { - return getCount(SEARCH_COUNT_KEY + userId); -} - -export async function getTotalSearchCount(): Promise { - return getCount(TOTAL_SEARCH_COUNT_KEY); -}