|
| 1 | +# Simple Coding Time Tracker - How It Works |
| 2 | + |
| 3 | +This document explains how the time tracking mechanism works in Simple Coding Time Tracker (SCTT). It's written for both developers who want to understand the code and users who want to know what's happening behind the scenes. |
| 4 | + |
| 5 | +## Core Concepts |
| 6 | + |
| 7 | +### Time Units and Storage |
| 8 | +- Time is tracked in **minutes** |
| 9 | +- Data is stored as `TimeEntry` objects with: |
| 10 | + - Date: When the coding session occurred |
| 11 | + - Project: Which project was being worked on |
| 12 | + - Branch: Which git branch was active |
| 13 | + - TimeSpent: Duration in minutes |
| 14 | + |
| 15 | +### Key Variables That Control Tracking |
| 16 | + |
| 17 | +```typescript |
| 18 | +// These are the main timing control variables |
| 19 | +saveIntervalSeconds = 5 // How often to save time entries |
| 20 | +inactivityTimeoutSeconds = 300 // Stop tracking after 5 mins of inactivity |
| 21 | +focusTimeoutSeconds = 60 // Continue tracking for 1 min after losing focus |
| 22 | +``` |
| 23 | + |
| 24 | +## How Time Tracking Works |
| 25 | + |
| 26 | +### 1. Starting a Tracking Session |
| 27 | + |
| 28 | +A tracking session starts when: |
| 29 | +- You start typing/moving cursor |
| 30 | +- VS Code window gains focus |
| 31 | +- You switch to a different file |
| 32 | + |
| 33 | +The tracker records: |
| 34 | +- Start time (`startTime`) |
| 35 | +- Current project (`currentProject`) |
| 36 | +- Current git branch (`currentBranch`) |
| 37 | + |
| 38 | +### 2. During Active Tracking |
| 39 | + |
| 40 | +The tracker maintains three important intervals: |
| 41 | + |
| 42 | +1. **Update Interval (1 second)** |
| 43 | + - Runs every second |
| 44 | + - Updates internal state |
| 45 | + - Used for real-time UI updates |
| 46 | + |
| 47 | +2. **Save Interval (5 seconds by default)** |
| 48 | + - Saves your current session to database |
| 49 | + - Creates a new time entry |
| 50 | + - Resets the start time |
| 51 | + |
| 52 | +3. **Activity Monitoring** |
| 53 | + - Tracks cursor movements, typing, file changes |
| 54 | + - Updates `lastCursorActivity` timestamp |
| 55 | + - Used to detect inactivity |
| 56 | + |
| 57 | +### 3. Project & Branch Tracking |
| 58 | + |
| 59 | +The tracker automatically handles: |
| 60 | + |
| 61 | +- **Project Changes** |
| 62 | + - Detects when you switch between projects |
| 63 | + - Saves time separately for each project |
| 64 | + - Multi-root workspace aware |
| 65 | + |
| 66 | +- **Branch Changes** |
| 67 | + - Monitors git branch changes |
| 68 | + - Creates separate time entries per branch |
| 69 | + - Perfect for tracking time across feature branches |
| 70 | + |
| 71 | +### 4. When Tracking Stops |
| 72 | + |
| 73 | +Tracking stops in these scenarios: |
| 74 | + |
| 75 | +1. **Inactivity** (after 5 minutes by default) |
| 76 | + - No cursor movement |
| 77 | + - No typing |
| 78 | + - No file changes |
| 79 | + |
| 80 | +2. **Lost Focus** (after 1 minute by default) |
| 81 | + - Switched to another application |
| 82 | + - Can be configured to wait longer |
| 83 | + |
| 84 | +3. **Manual Stop** |
| 85 | + - VS Code is closed |
| 86 | + - Extension is disabled |
| 87 | + |
| 88 | +### Database Structure |
| 89 | + |
| 90 | +Time entries are stored with this structure: |
| 91 | +```typescript |
| 92 | +interface TimeEntry { |
| 93 | + date: string; // ISO date string (YYYY-MM-DD) |
| 94 | + project: string; // Project/workspace name |
| 95 | + timeSpent: number; // Duration in minutes |
| 96 | + branch: string; // Git branch name |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +## Configuration Options |
| 101 | + |
| 102 | +You can customize tracking behavior: |
| 103 | + |
| 104 | +1. **Save Interval** (`simpleCodingTimeTracker.saveInterval`) |
| 105 | + - How often to save time entries |
| 106 | + - Default: 5 seconds |
| 107 | + - Lower = More accurate but more writes |
| 108 | + |
| 109 | +2. **Inactivity Timeout** (`simpleCodingTimeTracker.inactivityTimeout`) |
| 110 | + - How long to wait before stopping due to inactivity |
| 111 | + - Default: 300 seconds (5 minutes) |
| 112 | + - Higher = More lenient with breaks |
| 113 | + |
| 114 | +3. **Focus Timeout** (`simpleCodingTimeTracker.focusTimeout`) |
| 115 | + - How long to continue tracking after losing window focus |
| 116 | + - Default: 60 seconds (1 minute) |
| 117 | + - Higher = Better for quick app switches |
| 118 | + |
| 119 | +## Tips for Accurate Tracking |
| 120 | + |
| 121 | +1. **Keep VS Code Focused** |
| 122 | + - Tracking is most accurate when VS Code remains your active window |
| 123 | + - Use the focus timeout setting if you frequently switch apps |
| 124 | + |
| 125 | +2. **Branch Awareness** |
| 126 | + - Create branches for different features |
| 127 | + - Time is tracked separately per branch |
| 128 | + - Great for client billing |
| 129 | + |
| 130 | +3. **Project Organization** |
| 131 | + - Use separate VS Code windows for different projects |
| 132 | + - Or use multi-root workspaces for clean separation |
| 133 | + |
| 134 | +## Common Scenarios |
| 135 | + |
| 136 | +1. **Multiple Projects Open** |
| 137 | + ``` |
| 138 | + Project A (main) → 30 mins |
| 139 | + Project B (feature) → 45 mins |
| 140 | + ``` |
| 141 | + Each gets tracked separately |
| 142 | + |
| 143 | +2. **Branch Switching** |
| 144 | + ``` |
| 145 | + main → 1 hour |
| 146 | + feature/xyz → 30 mins |
| 147 | + ``` |
| 148 | + Time is split accurately per branch |
| 149 | + |
| 150 | +3. **Taking Breaks** |
| 151 | + ``` |
| 152 | + 10:00 - Start coding |
| 153 | + 10:30 - Take break (no activity) |
| 154 | + 10:35 - Tracking auto-stops |
| 155 | + 10:40 - Resume coding (auto-starts) |
| 156 | + ``` |
| 157 | + |
| 158 | +## Behind-the-Scenes Logic |
| 159 | + |
| 160 | +1. **Activity Detection** |
| 161 | + - Monitors VS Code events |
| 162 | + - Cursor movements |
| 163 | + - Text changes |
| 164 | + - File switches |
| 165 | + - Git operations |
| 166 | + |
| 167 | +2. **Time Calculation** |
| 168 | + ```typescript |
| 169 | + duration = (currentTime - startTime) / 60000 // Convert ms to minutes |
| 170 | + ``` |
| 171 | + |
| 172 | +3. **Session Management** |
| 173 | + - Small sessions are combined |
| 174 | + - Inactive periods are excluded |
| 175 | + - Branch/project switches create new sessions |
| 176 | + |
| 177 | +## Developer Notes |
| 178 | + |
| 179 | +Key files and their roles: |
| 180 | +- `timeTracker.ts`: Core tracking logic |
| 181 | +- `database.ts`: Time entry storage |
| 182 | +- `statusBar.ts`: Real-time UI updates |
| 183 | +- `summaryView.ts`: Time statistics and reports |
0 commit comments