feat: learnable activity type mapping system (DB-based, auto-learning)
Replaces hardcoded mappings with database-driven, self-learning system. Backend: - Migration 007: activity_type_mappings table - Supports global and user-specific mappings - Seeded with 40+ default mappings (German + English) - Unique constraint: (activity_type, profile_id) - Refactored: get_training_type_for_activity() queries DB - Priority: user-specific → global → NULL - Bulk categorization now saves mapping automatically - Source: 'bulk' for learned mappings - admin_activity_mappings.py: Full CRUD endpoints - List, Get, Create, Update, Delete - Coverage stats endpoint - CSV import uses DB mappings (no hardcoded logic) Frontend: - AdminActivityMappingsPage: Full mapping management UI - Coverage stats (% mapped, unmapped count) - Filter: All / Global - Create/Edit/Delete mappings - Tip: System learns from bulk categorization - Added route + admin link - API methods: adminList/Get/Create/Update/DeleteActivityMapping Benefits: - No code changes needed for new activity types - System learns from user bulk categorizations - User-specific mappings override global defaults - Admin can manage all mappings via UI - Migration pre-populates 40+ common German/English types Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
a4bd738e6f
commit
829edecbdc
8 files changed
+772
-67
No files matched your search
@@ -0,0 +1,121 @@
|
||||
-- Migration 007: Activity Type Mappings (Learnable System)
|
||||
-- Replaces hardcoded mappings with DB-based configurable system
|
||||
-- Created: 2026-03-21
|
||||
|
||||
-- ========================================
|
||||
-- 1. Create activity_type_mappings table
|
||||
-- ========================================
|
||||
CREATE TABLE IF NOT EXISTS activity_type_mappings (
|
||||
id SERIAL PRIMARY KEY,
|
||||
activity_type VARCHAR(100) NOT NULL,
|
||||
training_type_id INTEGER NOT NULL REFERENCES training_types(id) ON DELETE CASCADE,
|
||||
profile_id VARCHAR(36), -- NULL = global mapping, otherwise user-specific
|
||||
source VARCHAR(20) DEFAULT 'manual', -- 'manual', 'bulk', 'admin', 'default'
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
CONSTRAINT unique_activity_type_per_profile UNIQUE(activity_type, profile_id)
|
||||
);
|
||||
|
||||
-- ========================================
|
||||
-- 2. Create indexes
|
||||
-- ========================================
|
||||
CREATE INDEX IF NOT EXISTS idx_activity_type_mappings_type ON activity_type_mappings(activity_type);
|
||||
CREATE INDEX IF NOT EXISTS idx_activity_type_mappings_profile ON activity_type_mappings(profile_id);
|
||||
|
||||
-- ========================================
|
||||
-- 3. Seed default mappings (global)
|
||||
-- ========================================
|
||||
-- Note: These are the German Apple Health workout types
|
||||
-- training_type_id references are based on existing training_types data
|
||||
|
||||
-- Helper function to get training_type_id by subcategory
|
||||
DO $$
|
||||
DECLARE
|
||||
v_running_id INTEGER;
|
||||
v_walk_id INTEGER;
|
||||
v_cycling_id INTEGER;
|
||||
v_swimming_id INTEGER;
|
||||
v_hypertrophy_id INTEGER;
|
||||
v_functional_id INTEGER;
|
||||
v_hiit_id INTEGER;
|
||||
v_yoga_id INTEGER;
|
||||
v_technique_id INTEGER;
|
||||
v_sparring_id INTEGER;
|
||||
v_rowing_id INTEGER;
|
||||
v_dance_id INTEGER;
|
||||
v_static_id INTEGER;
|
||||
v_regeneration_id INTEGER;
|
||||
v_meditation_id INTEGER;
|
||||
v_mindfulness_id INTEGER;
|
||||
BEGIN
|
||||
-- Get training_type IDs
|
||||
SELECT id INTO v_running_id FROM training_types WHERE subcategory = 'running' LIMIT 1;
|
||||
SELECT id INTO v_walk_id FROM training_types WHERE subcategory = 'walk' LIMIT 1;
|
||||
SELECT id INTO v_cycling_id FROM training_types WHERE subcategory = 'cycling' LIMIT 1;
|
||||
SELECT id INTO v_swimming_id FROM training_types WHERE subcategory = 'swimming' LIMIT 1;
|
||||
SELECT id INTO v_hypertrophy_id FROM training_types WHERE subcategory = 'hypertrophy' LIMIT 1;
|
||||
SELECT id INTO v_functional_id FROM training_types WHERE subcategory = 'functional' LIMIT 1;
|
||||
SELECT id INTO v_hiit_id FROM training_types WHERE subcategory = 'hiit' LIMIT 1;
|
||||
SELECT id INTO v_yoga_id FROM training_types WHERE subcategory = 'yoga' LIMIT 1;
|
||||
SELECT id INTO v_technique_id FROM training_types WHERE subcategory = 'technique' LIMIT 1;
|
||||
SELECT id INTO v_sparring_id FROM training_types WHERE subcategory = 'sparring' LIMIT 1;
|
||||
SELECT id INTO v_rowing_id FROM training_types WHERE subcategory = 'rowing' LIMIT 1;
|
||||
SELECT id INTO v_dance_id FROM training_types WHERE subcategory = 'dance' LIMIT 1;
|
||||
SELECT id INTO v_static_id FROM training_types WHERE subcategory = 'static' LIMIT 1;
|
||||
SELECT id INTO v_regeneration_id FROM training_types WHERE subcategory = 'regeneration' LIMIT 1;
|
||||
SELECT id INTO v_meditation_id FROM training_types WHERE subcategory = 'meditation' LIMIT 1;
|
||||
SELECT id INTO v_mindfulness_id FROM training_types WHERE subcategory = 'mindfulness' LIMIT 1;
|
||||
|
||||
-- Insert default mappings (German Apple Health names)
|
||||
INSERT INTO activity_type_mappings (activity_type, training_type_id, profile_id, source) VALUES
|
||||
-- German workout types
|
||||
('Laufen', v_running_id, NULL, 'default'),
|
||||
('Gehen', v_walk_id, NULL, 'default'),
|
||||
('Wandern', v_walk_id, NULL, 'default'),
|
||||
('Outdoor Spaziergang', v_walk_id, NULL, 'default'),
|
||||
('Innenräume Spaziergang', v_walk_id, NULL, 'default'),
|
||||
('Spaziergang', v_walk_id, NULL, 'default'),
|
||||
('Radfahren', v_cycling_id, NULL, 'default'),
|
||||
('Schwimmen', v_swimming_id, NULL, 'default'),
|
||||
('Traditionelles Krafttraining', v_hypertrophy_id, NULL, 'default'),
|
||||
('Funktionelles Krafttraining', v_functional_id, NULL, 'default'),
|
||||
('Hochintensives Intervalltraining', v_hiit_id, NULL, 'default'),
|
||||
('Yoga', v_yoga_id, NULL, 'default'),
|
||||
('Kampfsport', v_technique_id, NULL, 'default'),
|
||||
('Matrial Arts', v_technique_id, NULL, 'default'), -- Common typo
|
||||
('Boxen', v_sparring_id, NULL, 'default'),
|
||||
('Rudern', v_rowing_id, NULL, 'default'),
|
||||
('Tanzen', v_dance_id, NULL, 'default'),
|
||||
('Cardio Dance', v_dance_id, NULL, 'default'),
|
||||
('Flexibilität', v_static_id, NULL, 'default'),
|
||||
('Abwärmen', v_regeneration_id, NULL, 'default'),
|
||||
('Cooldown', v_regeneration_id, NULL, 'default'),
|
||||
('Meditation', v_meditation_id, NULL, 'default'),
|
||||
('Achtsamkeit', v_mindfulness_id, NULL, 'default'),
|
||||
('Geist & Körper', v_yoga_id, NULL, 'default')
|
||||
ON CONFLICT (activity_type, profile_id) DO NOTHING;
|
||||
|
||||
-- English workout types (for compatibility)
|
||||
INSERT INTO activity_type_mappings (activity_type, training_type_id, profile_id, source) VALUES
|
||||
('Running', v_running_id, NULL, 'default'),
|
||||
('Walking', v_walk_id, NULL, 'default'),
|
||||
('Hiking', v_walk_id, NULL, 'default'),
|
||||
('Cycling', v_cycling_id, NULL, 'default'),
|
||||
('Swimming', v_swimming_id, NULL, 'default'),
|
||||
('Traditional Strength Training', v_hypertrophy_id, NULL, 'default'),
|
||||
('Functional Strength Training', v_functional_id, NULL, 'default'),
|
||||
('High Intensity Interval Training', v_hiit_id, NULL, 'default'),
|
||||
('Martial Arts', v_technique_id, NULL, 'default'),
|
||||
('Boxing', v_sparring_id, NULL, 'default'),
|
||||
('Rowing', v_rowing_id, NULL, 'default'),
|
||||
('Dance', v_dance_id, NULL, 'default'),
|
||||
('Core Training', v_functional_id, NULL, 'default'),
|
||||
('Flexibility', v_static_id, NULL, 'default'),
|
||||
('Mindfulness', v_mindfulness_id, NULL, 'default')
|
||||
ON CONFLICT (activity_type, profile_id) DO NOTHING;
|
||||
END $$;
|
||||
|
||||
-- ========================================
|
||||
-- 4. Add comment
|
||||
-- ========================================
|
||||
COMMENT ON TABLE activity_type_mappings IS 'v9d Phase 1b: Learnable activity type to training type mappings. Replaces hardcoded mappings.';
|
||||
Reference in new issue
Block a user