-
Notifications
You must be signed in to change notification settings - Fork 26
added analytics feature Fixes #107 #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1a2d6fb
added analytics feature
mohanish28 4294447
Enhance analytics API with improved error handling, validation for di…
mohanish28 8d13818
Add createdAt index to StudyRecord model and enhance error handling i…
mohanish28 d5db0d7
Refactor configuration and enhance error handling for Clerk and payme…
mohanish28 3f697a6
added analytics feature
mohanish28 66b80e8
added analytics feature
mohanish28 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,53 +1,17 @@ | ||
| # ================================ | ||
| # APPLICATION SETTINGS | ||
| # ================================ | ||
| NEXT_PUBLIC_APP_URL='http://localhost:3000' | ||
| NODE_ENV=development | ||
| APP_NAME="Flash Fathom AI" | ||
| APP_DESCRIPTION="Your AI-powered flashcard generator" | ||
| APP_VERSION="1.0.0" | ||
| APP_HOST=localhost | ||
| APP_PORT=3000 | ||
|
|
||
| # ================================ | ||
| # POSTGRESQL CONFIGURATION | ||
| # ================================ | ||
| POSTGRES_DB=flashfathom | ||
| POSTGRES_USER=postgres | ||
| POSTGRES_PASSWORD=your_secure_password_here | ||
| POSTGRES_PORT=5432 | ||
|
|
||
| # ================================ | ||
| # CLERK AUTHENTICATION | ||
| # ================================ | ||
| NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY="your nextjs publishable key" | ||
| CLERK_SECRET_KEY="your clerk secret key" | ||
| # Clerk | ||
| NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY= | ||
| CLERK_SECRET_KEY= | ||
|
|
||
| # Clerk URLs | ||
| NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in | ||
| NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up | ||
| NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL=/generate | ||
| NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/ | ||
| # Razorpay | ||
| NEXT_PUBLIC_RAZORPAY_KEY_ID= | ||
| RAZORPAY_KEY_ID= | ||
| RAZORPAY_KEY_SECRET= | ||
|
|
||
| # ================================ | ||
| # AI APIS | ||
| # ================================ | ||
| GEMINI_API_KEY="" | ||
| # Database | ||
| DATABASE_URL= | ||
|
|
||
| # ================================ | ||
| # EMAIL CONFIGURATION | ||
| # ================================ | ||
| EMAIL_USER= | ||
| EMAIL_PASS= | ||
| # Gemini API | ||
| GEMINI_API_KEY= | ||
|
|
||
| # ================================ | ||
| # DATABASE URL (Updated for Docker) | ||
| # ================================ | ||
| DATABASE_URL="postgresql://postgres:your_secure_password_here@postgres:5432/flashfathom" | ||
|
|
||
| # ================================ | ||
| # RAZORPAY CONFIGURATION | ||
| # ================================ | ||
| RAZORPAY_KEY_ID="your_razorpay_key_id" | ||
| RAZORPAY_KEY_SECRET="your_razorpay_key_secret" | ||
| NEXT_PUBLIC_RAZORPAY_KEY_ID="your_razorpay_key_id" | ||
| # App Port | ||
| APP_PORT=3000 |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -36,3 +36,6 @@ yarn-error.log* | |
| # typescript | ||
| *.tsbuildinfo | ||
| next-env.d.ts | ||
|
|
||
| # clerk configuration (can include secrets) | ||
| /.clerk/ | ||
This file contains hidden or 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 hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,46 @@ | ||
| import { currentUser } from "@clerk/nextjs/server"; | ||
| import { redirect } from "next/navigation"; | ||
| import { Filters } from "@/components/analytics/Filters"; | ||
| import { Suspense } from "react"; | ||
| import { LoadingSpinner } from "@/components/common/LoadingSpinner"; | ||
| import { RetentionCurve } from "@/components/analytics/RetentionCurve"; | ||
| import { SessionHeatmap } from "@/components/analytics/SessionHeatmap"; | ||
| import { SubjectProgress } from "@/components/analytics/SubjectProgress"; | ||
|
|
||
| export default async function AnalyticsPage({ | ||
| searchParams, | ||
| }: { | ||
| searchParams: { [key: string]: string | string[] | undefined }; | ||
| }) { | ||
| const user = await currentUser(); | ||
| if (!user) { | ||
| redirect("/sign-in"); | ||
| } | ||
|
|
||
| return ( | ||
| <div className="container mx-auto px-4 py-8"> | ||
| <h1 className="text-3xl font-bold mb-6">In-depth Performance Analytics</h1> | ||
|
|
||
| <Filters /> | ||
|
|
||
| <div className="grid grid-cols-1 lg:grid-cols-2 gap-8 mt-6"> | ||
|
|
||
| <div className="col-span-1"> | ||
| <Suspense fallback={<LoadingSpinner />}> | ||
| <SubjectProgress /> | ||
| </Suspense> | ||
| </div> | ||
| <div className="col-span-1"> | ||
| <Suspense fallback={<LoadingSpinner />}> | ||
| <RetentionCurve /> | ||
| </Suspense> | ||
| </div> | ||
| <div className="col-span-1 lg:col-span-2"> | ||
| <Suspense fallback={<LoadingSpinner />}> | ||
| <SessionHeatmap /> | ||
| </Suspense> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or 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,166 @@ | ||
|
|
||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { prisma } from "@/lib/database"; | ||
| import { currentUser } from "@clerk/nextjs/server"; | ||
| import { Difficulty } from "@prisma/client"; | ||
|
|
||
| // Helper function to safely map and validate difficulty parameter | ||
| function mapOrValidateDifficulty(difficultyParam: string | null): Difficulty | null { | ||
| if (!difficultyParam) return null; | ||
|
|
||
| const upperDifficulty = difficultyParam.toUpperCase(); | ||
| if (upperDifficulty === "EASY") return Difficulty.EASY; | ||
| if (upperDifficulty === "MEDIUM") return Difficulty.MEDIUM; | ||
| if (upperDifficulty === "HARD") return Difficulty.HARD; | ||
|
|
||
| return null; // Invalid difficulty | ||
| } | ||
mohanish28 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export async function GET(req: NextRequest) { | ||
| const user = await currentUser(); | ||
| if (!user) { | ||
| return NextResponse.json({ error: "Unauthorized" }, { | ||
| status: 401, | ||
| headers: { | ||
| "Cache-Control": "no-cache, no-store, must-revalidate" | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| const { searchParams } = new URL(req.url); | ||
| const subject = searchParams.get("subject")?.trim(); | ||
| const dateRange = searchParams.get("dateRange"); | ||
| const difficulty = searchParams.get("difficulty"); | ||
|
|
||
mohanish28 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Validate and normalize difficulty parameter | ||
| const safeDifficulty = mapOrValidateDifficulty(difficulty); | ||
| if (difficulty && !safeDifficulty) { | ||
| return NextResponse.json( | ||
| { | ||
| error: "Invalid difficulty value. Must be one of: EASY, MEDIUM, HARD" | ||
| }, | ||
| { | ||
| status: 400, | ||
| headers: { | ||
| "Cache-Control": "no-cache, no-store, must-revalidate" | ||
| } | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| // Parse and validate dateRange parameter | ||
| let dateFilter: { gte?: Date; lte?: Date } | undefined = undefined; | ||
| if (dateRange) { | ||
| try { | ||
| // Check if it's a single date or date range (comma-separated) | ||
| const dates = dateRange.split(',').map(d => d.trim()); | ||
|
|
||
| if (dates.length === 1) { | ||
| // Single date - use as gte | ||
| const date = new Date(dates[0]); | ||
| if (isNaN(date.getTime())) { | ||
| throw new Error("Invalid date format"); | ||
| } | ||
| dateFilter = { gte: date }; | ||
| } else if (dates.length === 2) { | ||
| // Date range - use first as gte, second as lte | ||
| const startDate = new Date(dates[0]); | ||
| const endDate = new Date(dates[1]); | ||
|
|
||
| if (isNaN(startDate.getTime()) || isNaN(endDate.getTime())) { | ||
| throw new Error("Invalid date format"); | ||
| } | ||
|
|
||
| if (startDate > endDate) { | ||
| return NextResponse.json( | ||
| { | ||
| error: "Start date must be before or equal to end date" | ||
| }, | ||
| { | ||
| status: 400, | ||
| headers: { | ||
| "Cache-Control": "no-cache, no-store, must-revalidate" | ||
| } | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| dateFilter = { gte: startDate, lte: endDate }; | ||
| } else { | ||
| throw new Error("Invalid date range format"); | ||
| } | ||
| } catch (error) { | ||
| return NextResponse.json( | ||
| { | ||
| error: "Invalid date format. Use ISO date format (YYYY-MM-DD) or comma-separated range (YYYY-MM-DD,YYYY-MM-DD)" | ||
| }, | ||
| { | ||
| status: 400, | ||
| headers: { | ||
| "Cache-Control": "no-cache, no-store, must-revalidate" | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| const studyRecords = await prisma.studyRecord.findMany({ | ||
| where: { | ||
| flashcard: { | ||
| userId: user.id, | ||
| deck: subject ? { | ||
| is: { | ||
| name: { | ||
| equals: subject, | ||
| mode: "insensitive" | ||
| } | ||
| } | ||
| } : undefined, | ||
| difficulty: safeDifficulty ? { equals: safeDifficulty } : undefined, | ||
| }, | ||
| createdAt: dateFilter, | ||
| }, | ||
| orderBy: { createdAt: "asc" }, | ||
| select: { | ||
| createdAt: true, | ||
| isCorrect: true, | ||
| }, | ||
| }); | ||
mohanish28 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const retentionData = studyRecords.reduce((acc: { [key: string]: { correct: number; total: number } }, record: { createdAt: Date; isCorrect: boolean }) => { | ||
| const date = new Date(record.createdAt).toLocaleDateString(); | ||
| if (!acc[date]) { | ||
| acc[date] = { correct: 0, total: 0 }; | ||
| } | ||
| acc[date].total++; | ||
| if (record.isCorrect) { | ||
| acc[date].correct++; | ||
| } | ||
| return acc; | ||
| }, {} as { [key: string]: { correct: number; total: number } }); | ||
|
|
||
| const labels = Object.keys(retentionData); | ||
| const retention = Object.values(retentionData).map((d: { correct: number; total: number }) => (d.correct / d.total) * 100); | ||
|
|
||
| return NextResponse.json({ labels, retention }, { | ||
| status: 200, | ||
| headers: { | ||
| "Cache-Control": "no-cache, no-store, must-revalidate" | ||
| }, | ||
| }); | ||
| } catch (error) { | ||
| console.error("Failed to compute retention curve:", error); | ||
| return NextResponse.json( | ||
| { | ||
| error: "Failed to compute retention", | ||
| details: error instanceof Error ? error.message : "Unknown error" | ||
| }, | ||
| { | ||
| status: 500, | ||
| headers: { | ||
| "Cache-Control": "no-cache, no-store, must-revalidate" | ||
| }, | ||
| } | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.