11 KiB
11 KiB
Workflow State - mnemo_cards_backend
Last Updated: 2025-11-08
PLAN - 🔥 STATISTICS SYSTEM UPGRADE (BACKEND)
Phase: Backend Statistics Implementation
Goal: Extend backend models, API, and automatic tracking for comprehensive user statistics
Plan Document: STATISTICS_TASKS.md (35-47 hours total)
Status: 🟡 PLANNING COMPLETE - READY TO START
CURRENT STATUS
Project State:
- ✅ API v2 Auth working (Google OAuth, JWT, refresh tokens)
- ✅ Core API structure in place
- ✅ Isar database operational
- ✅ Dependency injection with GetIt/Injectable
- ⬜ Statistics system (old, needs upgrade)
Recent Work:
- ✅ Replaced dynamic user handling with typed UserModel
- ✅ Added RefreshTokenModel for token management
- ✅ Improved JWT implementation with crypto
- ✅ Added access control policies
NEXT_ACTIONS
Phase 1: Models and DTOs ✅ COMPLETED (8 hours)
- ✅ Create PackProgressDto in mnemo_cards_common
- ✅ Create AchievementDto and AchievementType enum
- ✅ Create DetailedWordStatisticsDto extending WordStatisticsDto
- ✅ Create StudySessionDto for session tracking
- ✅ Extend UserDataDto with new statistics fields
- ✅ Create Isar models (PackProgressModel, AchievementModel, StudySessionModel)
- ✅ Update UserDataModel with new embedded relations
- ✅ Run codegen (manual .g.dart creation due to build_runner issues)
- ⬜ Write unit tests for all models
Phase 2: Statistics Calculator ✅ COMPLETED (5 hours)
- ✅ Create StatisticsCalculator service with @lazySingleton
- ✅ Implement calculatePackProgress method with DTO conversion
- ✅ Implement calculateStreak (consecutive days logic with date normalization)
- ✅ Implement findDifficultWords method with difficulty scoring
- ✅ Implement calculateAccuracy method for word statistics
- ✅ Implement calculateStudyTime methods (total and daily aggregation)
- ✅ Implement getTimelineStatistics method with period filtering
- ✅ Add StatisticsCalculator to DI (GetIt/Injectable auto-registration)
- ✅ Write comprehensive unit tests (23 tests, all passing)
Phase 3: API Endpoints ✅ COMPLETED (7 hours)
- ✅ Add getDetailedStatistics endpoint to UsersApiV2 (/api/v2/users/me/statistics/detailed)
- ✅ Add getPacksStatistics endpoint with packId filter (/api/v2/users/me/statistics/packs)
- ✅ Add getWordsStatistics endpoint with pagination and filters (/api/v2/users/me/statistics/words)
- ✅ Add getTimelineStatistics endpoint with period filtering (/api/v2/users/me/statistics/timeline)
- ✅ Add recordStudySession endpoint for session tracking (/api/v2/users/me/sessions)
- ✅ Add getAchievements endpoint (/api/v2/users/me/achievements)
- ✅ Update UserManager with statistics methods (toDto extension)
- ✅ Write integration tests (9 tests, all passing)
- ⬜ Update OpenAPI specification
Phase 4: Automatic Tracking (AFTER - 9-12 hours)
- ✅ Create SessionTracker service (COMPLETED - 2 hours)
- ✅ Add session tracking middleware (COMPLETED - 1 hour)
- ✅ Add hooks in test completion flow (COMPLETED - 2 hours)
- ✅ Create AchievementManager (COMPLETED - 3 hours)
- ✅ Define all achievement types (COMPLETED - included)
- ✅ Implement achievement checking logic (COMPLETED - included)
- ✅ Add achievement hooks to user actions (COMPLETED - 1 hour)
- ✅ Write comprehensive tests (COMPLETED - 2 hours)
Phase 5: Testing and Docs (FINAL - 5-7 hours)
- ⬜ Complete all unit tests
- ⬜ Complete all integration tests
- ⬜ Update OpenAPI specification
- ⬜ Update PROGRESS.md
- ⬜ Update TODO.md
- ⬜ Create STATISTICS_API.md documentation
ASSUMPTIONS
Technical:
- Isar database can handle new models without migration issues
- Statistics calculations can be done synchronously (fast enough)
- Streak calculation runs daily via cron job
- Session tracking uses in-memory cache with persistence
- Achievement checking is asynchronous (won't block requests)
Business Logic:
- Streak counts consecutive calendar days (user's timezone)
- Difficulty score based on incorrect/correct ratio
- Study session timeout is 30 minutes
- Achievements are retroactive (can be unlocked for past data)
- Statistics are cached for 5 minutes
Architecture:
- Use existing UserManager for user operations
- Create separate StatisticsCalculator for stats logic
- SessionTracker runs as middleware
- AchievementManager is triggered by events
- All stats endpoints require authentication
PROGRESS_LOG
2025-11-08: Statistics Planning Complete ✅
Analysis:
- Reviewed current UserModel, UserDataModel structures
- Identified fields needed: streaks, pack progress, achievements, sessions
- Analyzed existing statistics collection (TestStatisticsDto, WordStatisticsDto)
- Examined UserManager methods for statistics updates
Planning:
- Created STATISTICS_TASKS.md with detailed breakdown
- 5 phases: Models (6-9h), Calculator (4-5h), API (6-8h), Tracking (9-12h), Testing (5-7h)
- Total: 35-47 hours estimated
- Clear dependencies: Models → Calculator → API → Tracking → Testing
Key Components Planned:
New DTOs:
- PackProgressDto (pack stats per user)
- AchievementDto (achievements with unlock dates)
- DetailedWordStatisticsDto (extended word stats)
- StudySessionDto (session tracking)
New Services:
- StatisticsCalculator (all calculation logic)
- SessionTracker (automatic session tracking)
- AchievementManager (achievement checking and awarding)
New Endpoints:
- GET /api/v2/users/me/statistics/detailed
- GET /api/v2/users/me/statistics/packs
- GET /api/v2/users/me/statistics/words (with pagination)
- GET /api/v2/users/me/statistics/timeline
- POST /api/v2/users/me/sessions
- GET /api/v2/users/me/achievements
Next Step: Create new DTOs in mnemo_cards_common
Previous Work:
Recent Updates:
- ✅ Fixed access control to use typed UserModel
- ✅ Added refresh token tests
- ✅ Improved Packs API v2 test coverage
- ✅ Added optional authentication handling
API v2 Implementation:
- ✅ JWT Service with proper crypto
- ✅ Authentication API (Google OAuth, tokens)
- ✅ Packs API (list, details, cards, images)
- ✅ Tests API (details, results, history)
- ✅ Games API (list, assets)
- ✅ Purchases API (create, verify)
OPEN_ISSUES
Statistics Feature:
- ⬜ Decide on difficulty scoring formula (incorrect/(correct+incorrect)?)
- ⬜ Determine achievement unlock criteria precisely
- ⬜ Plan database migration for new Isar models
- ⬜ Decide on caching strategy (in-memory? Redis?)
- ⬜ Handle timezone for streak calculations (use user's timezone from request?)
- ⬜ Define session timeout behavior (auto-end after 30 min inactivity?)
- ⬜ Plan for performance with large datasets (indexes needed?)
- ⬜ Decide on pagination defaults (50 items per page?)
General:
- ⬜ Need to run all existing tests after model changes
- ⬜ Consider adding rate limiting for statistics endpoints
- ⬜ Plan for data export (GDPR compliance)
- ⬜ Consider adding admin endpoints for statistics (analytics)
DEPENDENCIES
External:
- mnemo_cards_common (shared DTOs) - will be modified
- mnemo_cards_common_backend (shared models) - will be modified
- Isar database - will add new collections
- Shelf HTTP framework - existing
- GetIt/Injectable - existing
Internal:
- Phase 1 (Models) must complete before Phase 2 (Calculator)
- Phase 2 (Calculator) must complete before Phase 3 (API)
- Phase 3 (API) must complete before Phase 4 (Tracking)
- Phase 5 (Testing) runs parallel to implementation
TESTING STRATEGY
Unit Tests:
- All DTO serialization/deserialization
- All model conversions (toDto/fromDto)
- All StatisticsCalculator methods
- Streak calculation logic
- Difficulty scoring logic
- Achievement checking logic
Integration Tests:
- All new API endpoints
- End-to-end statistics flow
- Session tracking
- Achievement awarding
Test Coverage Goal:
- 80%+ coverage for new code
- 100% coverage for business logic (streaks, achievements)
ACCEPTANCE CRITERIA
Phase 1 - Models:
- All new DTOs created and working
- Codegen runs without errors
- Models convert to/from DTOs correctly
- Unit tests pass
Phase 2 - Calculator:
- StatisticsCalculator service created
- All calculation methods implemented
- Unit tests cover all methods
- Calculations are accurate
Phase 3 - API:
- All 6 new endpoints working
- Request validation implemented
- Error handling proper
- Integration tests pass
- OpenAPI spec updated
Phase 4 - Tracking:
- SessionTracker middleware working
- Test completion updates statistics
- AchievementManager awards achievements
- All tests pass
Phase 5 - Final:
- All tests pass
- Documentation updated
- Code reviewed
- Ready for frontend integration
WORK STRATEGY
Development Approach:
- Start with DTOs: Foundation for everything
- Test Models: Ensure serialization works
- Build Calculator: Pure logic, easy to test
- Add Endpoints: Connect calculator to API
- Implement Tracking: Make it automatic
- Test Everything: Comprehensive testing
Quality:
- Write tests alongside code
- Run
./codegen.shafter model changes - Run
flutter testfrequently - Check
dart analyzebefore committing - Keep methods small and focused
Documentation:
- Update workflow_state.md daily
- Update PROGRESS.md after each phase
- Update TODO.md as tasks complete
- Create STATISTICS_API.md for API docs
FILES TO TRACK
Models & DTOs:
mnemo_cards_common/lib/src/dtos/user/data/pack_progress_dto.dartmnemo_cards_common/lib/src/dtos/user/achievement_dto.dartmnemo_cards_common/lib/src/dtos/user/data/detailed_word_statistics_dto.dartmnemo_cards_common/lib/src/dtos/user/study_session_dto.dartmnemo_cards_common/lib/src/dtos/user/data/user_data_dto.dartmnemo_cards_common_backend/lib/src/models/pack_progress_model.dartmnemo_cards_common_backend/lib/src/models/achievement_model.dartmnemo_cards_common_backend/lib/src/models/study_session_model.dart
Services:
lib/statistics/statistics_calculator.dartlib/statistics/session_tracker.dartlib/statistics/achievement_manager.dart
API:
lib/api/v2/users_api_v2.dartlib/user/user_manager.dartpublic/open_api.yaml
Tests:
test/statistics/statistics_calculator_test.darttest/statistics/achievement_manager_test.darttest/api/v2/users_api_v2_statistics_test.dart
Documentation:
PROGRESS.mdTODO.mdSTATISTICS_API.md(to be created)
COMMANDS REFERENCE
# Code generation
./codegen.sh
# Run all tests
flutter test
# Run specific test file
flutter test test/statistics/statistics_calculator_test.dart
# Analyze code
dart analyze
# Format code
dart format lib/ test/
# Build (for verification)
flutter build
# Run dev server
./run_dev.sh
# Restart dev server
./restart_dev.sh
Status: Ready to begin implementation
Next Action: Create PackProgressDto in mnemo_cards_common
Estimated Time for Next Phase: 6-9 hours
Total Remaining: 35-47 hours