-
Notifications
You must be signed in to change notification settings - Fork 13
Time Tracking
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.
The following features are implemented in this codebase:
-
Automatic Coding Time Tracking
- Tracks active coding time in minutes, per project and per git branch.
- Detects activity (typing, cursor movement, file changes) and inactivity.
- Handles project and branch switching automatically.
- Multi-root workspace aware.
-
Customizable Tracking Behavior
- Configurable save interval, inactivity timeout, and focus timeout via settings.
- Accurate time tracking even with frequent app switching.
-
Persistent Data Storage
- Stores time entries persistently using VS Code's global state.
- Data is structured as
TimeEntry
objects (date, project, branch, time spent). - Automatic migration for new data fields (e.g., branch support).
-
Summary and Reporting
- Provides a summary view with daily, project, and branch breakdowns.
- Aggregates total time, and allows filtering by date, project, or branch.
- Real-time status bar updates showing current tracked time and context.
-
User Interface Integration
- Status bar item for quick access and live tracking status.
- Commands to start/stop tracking, show summary, and clear all data.
- Webview or panel for detailed time statistics and reports.
-
Robust Error Handling
- All storage and tracking operations are wrapped in error handling.
- User notifications for errors or important actions (e.g., data deletion).
-
Developer-Friendly Structure
- Modular codebase with clear separation of concerns:
-
timeTracker.ts
: Core tracking logic and activity detection -
database.ts
: Persistent storage and summary aggregation -
statusBar.ts
: Status bar UI integration -
summaryView.ts
: Summary/reporting UI -
utils.ts
: Helper functions for formatting and common tasks
-
- Well-documented code and architecture for easy maintenance and extension.
- Modular codebase with clear separation of concerns:
- Time is tracked in minutes
- Data is stored as
TimeEntry
objects with:- Date: When the coding session occurred
- Project: Which project was being worked on
- Branch: Which git branch was active
- TimeSpent: Duration in minutes
// These are the main timing control variables
saveIntervalSeconds = 5 // How often to save time entries
inactivityTimeoutSeconds = 300 // Stop tracking after 5 mins of inactivity
focusTimeoutSeconds = 180 // Continue tracking for 3 mins after losing focus
A tracking session starts when:
- You start typing/moving cursor
- VS Code window gains focus
- You switch to a different file
The tracker records:
- Start time (
startTime
) - Current project (
currentProject
) - Current git branch (
currentBranch
)
The tracker maintains three important intervals and time validation:
-
Update Interval (1 second)
- Runs every second
- Updates internal state and validates time continuity
- Detects system sleep/wake by checking time gaps
- Used for real-time UI updates
-
Save Interval (5 seconds by default)
- Saves your current session to database
- Creates a new time entry
- Resets the start time
-
Activity Monitoring
- Tracks cursor movements, typing, file changes
- Updates
lastCursorActivity
timestamp - Used to detect inactivity
The tracker automatically handles:
-
Project Changes
- Detects when you switch between projects
- Saves time separately for each project
- Multi-root workspace aware
-
Branch Changes
- Monitors git branch changes
- Creates separate time entries per branch
- Perfect for tracking time across feature branches
Tracking stops in these scenarios:
-
Inactivity (after 5 minutes by default)
- No cursor movement
- No typing
- No file changes
- Abnormal time gaps (system sleep/hibernate detected)
-
Lost Focus (after 1 minute by default)
- Switched to another application
- Can be configured to wait longer
-
Manual Stop
- VS Code is closed
- Extension is disabled
Time entries are stored with this structure:
interface TimeEntry {
date: string; // ISO date string (YYYY-MM-DD)
project: string; // Project/workspace name
timeSpent: number; // Duration in minutes
branch: string; // Git branch name
}
You can customize tracking behavior:
-
Save Interval (
simpleCodingTimeTracker.saveInterval
)- How often to save time entries
- Default: 5 seconds
- Lower = More accurate but more writes
-
Inactivity Timeout (
simpleCodingTimeTracker.inactivityTimeout
)- How long to wait before stopping due to inactivity
- Default: 300 seconds (5 minutes)
- Higher = More lenient with breaks
-
Focus Timeout (
simpleCodingTimeTracker.focusTimeout
)- How long to continue tracking after losing window focus
- Default: 180 seconds (3 minutes)
- Higher = Better for quick app switches
-
Keep VS Code Focused
- Tracking is most accurate when VS Code remains your active window
- Use the focus timeout setting if you frequently switch apps
-
Branch Awareness
- Create branches for different features
- Time is tracked separately per branch
- Great for client billing
-
Project Organization
- Use separate VS Code windows for different projects
- Or use multi-root workspaces for clean separation
-
Multiple Projects Open
Project A (main) → 30 mins Project B (feature) → 45 mins
Each gets tracked separately
-
Branch Switching
main → 1 hour feature/xyz → 30 mins
Time is split accurately per branch
-
Taking Breaks
10:00 - Start coding 10:30 - Take break (no activity) 10:35 - Tracking auto-stops 10:40 - Resume coding (auto-starts)
-
Activity Detection
- Monitors VS Code events
- Cursor movements
- Text changes
- File switches
- Git operations
-
Time Calculation
duration = (currentTime - startTime) / 60000 // Convert ms to minutes
-
Session Management
- Small sessions are combined
- Inactive periods are excluded
- Branch/project switches create new sessions
Key files and their roles:
-
timeTracker.ts
: Core tracking logic -
database.ts
: Time entry storage -
statusBar.ts
: Real-time UI updates -
summaryView.ts
: Time statistics and reports
Next: Learn about Health Notifications or explore the Test Scenarios.