From 038c57ff0fdf33b6cf493634abea4002e2820a29 Mon Sep 17 00:00:00 2001 From: DirkSchlossmacher <62424946+DirkSchlossmacher@users.noreply.github.com> Date: Mon, 13 Nov 2023 10:00:05 +0100 Subject: [PATCH] ioredis and redisClient --- app/utils/cloud/redisClient.ts | 34 ++++++++++++++++++++++++++++++++++ package.json | 3 ++- 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 app/utils/cloud/redisClient.ts diff --git a/app/utils/cloud/redisClient.ts b/app/utils/cloud/redisClient.ts new file mode 100644 index 000000000..dfdcd4931 --- /dev/null +++ b/app/utils/cloud/redisClient.ts @@ -0,0 +1,34 @@ +// redisClient.ts +import Redis from 'ioredis'; + +const redis = new Redis(process.env.UPSTASH_REDIS_URL); + +export const incrementSignInCount = async (email: string | undefined, dateKey: string) => { + if (!email) { + console.error('Email is undefined, cannot increment sign-in count.'); + return; + } + await redis.hincrby(`sign_ins:${email}`, dateKey, 1); +}; + +export const incrementSessionRefreshCount = async (email: string | undefined, dateKey: string) => { + if (!email) { + console.error('Email is undefined, cannot increment session refresh count.'); + return; + } + await redis.hincrby(`session_refreshes:${email}`, dateKey, 1); +}; + +export const incrementTokenCounts = async ( + email: string | undefined, + dateKey: string, + completionTokens: number, + promptTokens: number +) => { + if (!email) { + console.error('Email is undefined, cannot increment token counts.'); + return; + } + await redis.hincrby(`tokens:${email}`, `${dateKey}:completion_tokens`, completionTokens); + await redis.hincrby(`tokens:${email}`, `${dateKey}:prompt_tokens`, promptTokens); +}; diff --git a/package.json b/package.json index eed58ae95..88955f73f 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,8 @@ "sass": "^1.59.2", "spark-md5": "^3.0.2", "use-debounce": "^9.0.4", - "zustand": "^4.3.8" + "zustand": "^4.3.8", + "ioredis": "^5.3.2" }, "devDependencies": { "@tauri-apps/cli": "^1.4.0",