feat: filtered goal types - count specific training types
Deploy Development / deploy (push) Successful in 49s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 14s

NEW FEATURE: Filter conditions for goal types
Enables counting/aggregating specific subsets of data.

Example use case: Count only strength training sessions per week
- Create goal type with filter: {"training_type": "strength"}
- count_7d now counts only strength training, not all activities

Implementation:
- Migration 026: filter_conditions JSONB column
- Backend: Dynamic WHERE clause building from JSON filters
- Supports single value: {"training_type": "strength"}
- Supports multiple values: {"training_type": ["strength", "hiit"]}
- Works with all 8 aggregation methods (count, avg, sum, min, max)
- Frontend: JSON textarea with example + validation
- Pydantic models: filter_conditions field added

Technical details:
- SQL injection safe (parameterized queries)
- Graceful degradation (invalid JSON ignored with warning)
- Backward compatible (NULL filters = no filtering)

Answers user question: 'Kann ich Trainingstypen wie Krafttraining separat zählen?'
Answer: YES! 🎯
This commit is contained in:
Lars committed 2026-03-27 08:14:22 +01:00
1 parent 2c978bf948
commit 2303c04123
4 files changed
+143 -24

No files matched your search

+33 -2
View File
@@ -20,6 +20,7 @@ export default function AdminGoalTypesPage() {
source_table: '',
source_column: '',
aggregation_method: 'latest',
filter_conditions: '',
description: ''
})
@@ -75,6 +76,7 @@ export default function AdminGoalTypesPage() {
source_table: '',
source_column: '',
aggregation_method: 'latest',
filter_conditions: '',
description: ''
})
setShowForm(true)
@@ -91,6 +93,7 @@ export default function AdminGoalTypesPage() {
source_table: type.source_table || '',
source_column: type.source_column || '',
aggregation_method: type.aggregation_method || 'latest',
filter_conditions: type.filter_conditions ? JSON.stringify(type.filter_conditions, null, 2) : '',
description: type.description || ''
})
setShowForm(true)
@@ -102,16 +105,29 @@ export default function AdminGoalTypesPage() {
return
}
// Parse filter_conditions from string to JSON
let payload = { ...formData }
if (formData.filter_conditions && formData.filter_conditions.trim()) {
try {
payload.filter_conditions = JSON.parse(formData.filter_conditions)
} catch (e) {
setError('Ungültiges JSON in Filter-Bedingungen')
return
}
} else {
payload.filter_conditions = null
}
try {
if (editingType) {
await api.updateGoalType(editingType, formData)
await api.updateGoalType(editingType, payload)
showToast('✓ Goal Type aktualisiert')
} else {
if (!formData.type_key) {
setError('Bitte eindeutigen Key angeben (z.B. meditation_minutes)')
return
}
await api.createGoalType(formData)
await api.createGoalType(payload)
showToast('✓ Goal Type erstellt')
}
@@ -452,6 +468,21 @@ export default function AdminGoalTypesPage() {
</select>
</div>
{/* Filter Conditions */}
<div>
<label className="form-label">Filter (optional, JSON)</label>
<textarea
className="form-input"
style={{ width: '100%', minHeight: 80, fontFamily: 'monospace', fontSize: 13 }}
value={formData.filter_conditions}
onChange={e => setFormData(f => ({ ...f, filter_conditions: e.target.value }))}
placeholder={'Beispiel:\n{\n "training_type": "strength"\n}\n\nOder mehrere Werte:\n{\n "training_type": ["strength", "hiit"]\n}'}
/>
<div style={{ fontSize: 12, color: 'var(--text2)', marginTop: 4 }}>
💡 Filtert Einträge nach Spalten. Beispiel: <code>{`{"training_type": "strength"}`}</code> zählt nur Krafttraining
</div>
</div>
{/* Description */}
<div>
<label className="form-label">Beschreibung (optional)</label>