|
| 1 | +# AI Weekly Summary Queries Documentation |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This document explains the behavior of the three database queries used by the AI Weekly Summary feature. These queries provide different perspectives on karma transactions and leaderboards for generating weekly summaries. |
| 6 | + |
| 7 | +## Query Functions |
| 8 | + |
| 9 | +### 1. `getLastWeekTransactions()` |
| 10 | + |
| 11 | +**Purpose:** Returns all transactions (points given or taken) from the last 7 days. |
| 12 | + |
| 13 | +**Description:** |
| 14 | +- Returns individual transaction records from the **most recent 7 days** (recent activity) |
| 15 | +- Includes full transaction details: message, amount, timestamp, running total, and user names |
| 16 | +- Used to show what happened in the current week |
| 17 | + |
| 18 | +**SQL Logic:** |
| 19 | +```sql |
| 20 | +WHERE t.timestamp >= datetime('now', '-7 days') |
| 21 | +``` |
| 22 | +- Gets transactions where timestamp is **greater than or equal to** 7 days ago |
| 23 | +- This means: transactions from the last 7 days (recent) |
| 24 | + |
| 25 | +**Returns:** |
| 26 | +- Array of `LastWeekTransaction` objects |
| 27 | +- Each transaction includes: |
| 28 | + - `message`: The transaction message |
| 29 | + - `amount`: Karma amount (positive or negative) |
| 30 | + - `timestamp`: When the transaction occurred |
| 31 | + - `newTotal`: Running total after this transaction |
| 32 | + - `fromName`: Real name of the user who gave/took karma |
| 33 | + - `toName`: Real name of the user who received/lost karma |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +### 2. `getLastWeekLeaderboard()` |
| 38 | + |
| 39 | +**Purpose:** Retrieves the leaderboard snapshot from 7 days ago. |
| 40 | + |
| 41 | +**Description:** |
| 42 | +- Returns the leaderboard state **as it was exactly 7 days ago** (historical snapshot) |
| 43 | +- Shows who was leading 7 days ago, before the current week's activity |
| 44 | +- Used to compare "where we were" vs "where we are now" |
| 45 | + |
| 46 | +**SQL Logic:** |
| 47 | +```sql |
| 48 | +WHERE t.timestamp <= datetime('now', '-7 days') |
| 49 | +``` |
| 50 | +- Gets transactions where timestamp is **less than or equal to** 7 days ago |
| 51 | +- This means: all transactions up to 7 days ago (historical data) |
| 52 | +- Calculates totals and ranks based only on transactions that existed 7 days ago |
| 53 | + |
| 54 | +**Returns:** |
| 55 | +- Array of `LeaderboardEntry` objects |
| 56 | +- Each entry includes: |
| 57 | + - `toRealName`: User's real name |
| 58 | + - `totalReceived`: Total karma received (up to 7 days ago) |
| 59 | + - `rank`: User's rank in the leaderboard (1 = highest) |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +### 3. `getTodayLeaderboard()` |
| 64 | + |
| 65 | +**Purpose:** Shows the current leaderboard as of today. |
| 66 | + |
| 67 | +**Description:** |
| 68 | +- Returns the **all-time leaderboard** up to and including today |
| 69 | +- Shows the current state of who has the most karma overall |
| 70 | +- Used to show "where we are now" |
| 71 | + |
| 72 | +**SQL Logic:** |
| 73 | +```sql |
| 74 | +WHERE date(t.timestamp) <= date('now') |
| 75 | +``` |
| 76 | +- Gets all transactions up to and including today |
| 77 | +- This means: all historical transactions (no time limit) |
| 78 | +- Calculates totals and ranks based on all transactions ever |
| 79 | + |
| 80 | +**Returns:** |
| 81 | +- Array of `LeaderboardEntry` objects |
| 82 | +- Each entry includes: |
| 83 | + - `toRealName`: User's real name |
| 84 | + - `totalReceived`: Total karma received (all-time) |
| 85 | + - `rank`: User's current rank in the leaderboard (1 = highest) |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +## Key Differences |
| 90 | + |
| 91 | +| Query | Time Range | Purpose | Use Case | |
| 92 | +|-------|-----------|---------|----------| |
| 93 | +| `getLastWeekTransactions` | **Last 7 days** (recent) | Show recent activity | "What happened this week?" | |
| 94 | +| `getLastWeekLeaderboard` | **Up to 7 days ago** (historical) | Show past state | "Where were we last week?" | |
| 95 | +| `getTodayLeaderboard` | **All time up to today** | Show current state | "Where are we now?" | |
| 96 | + |
| 97 | +## Visual Timeline Example |
| 98 | + |
| 99 | +``` |
| 100 | +Timeline: [14 days ago] [7 days ago] [Today] |
| 101 | + |--------------|--------------| |
| 102 | + |
| 103 | +getLastWeekLeaderboard: [============] (up to 7 days ago) |
| 104 | +getLastWeekTransactions: [========] (last 7 days) |
| 105 | +getTodayLeaderboard: [========================] (all time) |
| 106 | +``` |
| 107 | + |
| 108 | +## Related Files |
| 109 | + |
| 110 | +- `app/_lib/_db/index.ts`: Query implementations |
| 111 | +- `app/_lib/_agent/agent.ts`: Query usage in AI agent |
| 112 | +- `tests/e2e/aiWeeklySummary/aiWeeklySummary.spec.ts`: Test suite |
0 commit comments