-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from math-silva:feature/analytics-api
feat: add analytics functionality
- Loading branch information
Showing
10 changed files
with
285 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { dailyUsages, getOrInitializeDailyUsage } from '../../lib/kv'; | ||
import moment from 'moment-timezone'; | ||
|
||
moment.tz.setDefault('America/Sao_Paulo'); | ||
|
||
export async function GET() { | ||
const today = moment().format('YYYY-MM-DD'); | ||
const usage = await getOrInitializeDailyUsage(today); | ||
return Response.json({ usage }); | ||
} | ||
|
||
export async function POST(req: Request) { | ||
const { type, count, tokenInfo } = await req.json(); | ||
const today = moment().format('YYYY-MM-DD'); | ||
|
||
try { | ||
let usage = await getOrInitializeDailyUsage(today); | ||
|
||
await dailyUsages.hincrby(`daily_usage:${today}`, type, count); | ||
await dailyUsages.hincrby(`daily_usage:${today}`, 'usageCount', 1); | ||
await dailyUsages.hincrby(`daily_usage:${today}`, 'inputTokenCount', tokenInfo.inputTokenCount); | ||
await dailyUsages.hincrby(`daily_usage:${today}`, 'outputTokenCount', tokenInfo.outputTokenCount); | ||
await dailyUsages.hincrby(`daily_usage:${today}`, 'totalTokenCount', tokenInfo.totalTokenCount); | ||
await dailyUsages.hincrby('total_usage', type, count); | ||
await dailyUsages.hincrby('total_usage', 'usageCount', 1); | ||
usage = await getOrInitializeDailyUsage(today); | ||
return Response.json({ message: 'Usage updated', usage }); | ||
} catch (error) { | ||
return Response.json({ error: 'Failed to update usage' }, { status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { dailyUsages } from '../../../lib/kv'; | ||
|
||
export async function GET() { | ||
const totalUsage = await dailyUsages.hgetall('total_usage'); | ||
return Response.json({ totalUsage }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { createClient } from "@vercel/kv"; | ||
|
||
export interface DailyUsage { | ||
flashcardCount: number; | ||
summaryCount: number; | ||
exerciseCount: number; | ||
usageCount: number; | ||
inputTokenCount: number; | ||
outputTokenCount: number; | ||
totalTokenCount: number; | ||
} | ||
|
||
export const dailyUsages = createClient({ | ||
url: process.env.KV_REST_API_URL || "", | ||
token: process.env.KV_REST_API_TOKEN || "", | ||
}); | ||
|
||
export async function getOrInitializeDailyUsage(today: string): Promise<DailyUsage> { | ||
const usage = await dailyUsages.hgetall(`daily_usage:${today}`) as unknown as DailyUsage; | ||
|
||
if (!usage) { | ||
const defaultUsage: DailyUsage = { | ||
flashcardCount: 0, | ||
summaryCount: 0, | ||
exerciseCount: 0, | ||
usageCount: 0, | ||
inputTokenCount: 0, | ||
outputTokenCount: 0, | ||
totalTokenCount: 0, | ||
}; | ||
|
||
await dailyUsages.hset(`daily_usage:${today}`, { ...defaultUsage }); | ||
return defaultUsage; | ||
} | ||
|
||
return usage; | ||
} |
Oops, something went wrong.