RezinaLMS-BackEnd/internal/web_server/handlers/analytics_field_normalize.go
Yared Yemane 3b08a07c85 Add subscription behaviour analytics and clean profile breakdowns.
Extend the analytics dashboard with live subscription snapshots, renewal/resubscribe metrics, and field-options normalization so Unknown and invalid profile values aggregate as Other. Also include exam_prep in the learner progress summary.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-29 08:46:30 -07:00

97 lines
3.0 KiB
Go

package handlers
import (
"Yimaru-Backend/internal/domain"
"context"
"sort"
"strings"
)
const analyticsOtherLabel = "OTHER"
// analyticsProfileFieldKeys are user-profile breakdowns validated against field_options.
var analyticsProfileFieldKeys = []string{
domain.ProfileFieldAgeGroup,
domain.ProfileFieldEducationLevel,
domain.ProfileFieldOccupation,
domain.ProfileFieldLearningGoal,
domain.ProfileFieldLanguageChallange,
domain.ProfileFieldCountry,
domain.ProfileFieldEthiopiaRegions,
}
func (h *Handler) loadAnalyticsFieldOptionSets(ctx context.Context) (map[string][]domain.ProfileFieldOption, error) {
sets := make(map[string][]domain.ProfileFieldOption, len(analyticsProfileFieldKeys))
if h.profileFieldOptionSvc == nil {
return sets, nil
}
active := domain.ProfileFieldOptionStatusActive
for _, key := range analyticsProfileFieldKeys {
fieldKey := key
options, _, err := h.profileFieldOptionSvc.ListProfileFieldOptions(ctx, &fieldKey, &active, 500, 0)
if err != nil {
return nil, err
}
sets[key] = options
}
return sets, nil
}
func normalizeAnalyticsOptionKey(raw string) string {
normalized := strings.ToUpper(strings.TrimSpace(raw))
normalized = strings.ReplaceAll(normalized, " ", "_")
normalized = strings.ReplaceAll(normalized, "-", "_")
return normalized
}
// resolveAnalyticsFieldLabel maps a stored user value to a static field_options code.
// Empty / "unknown" / values not in the option list become OTHER.
// When options is empty, only empty/unknown are remapped (other values kept as-is).
func resolveAnalyticsFieldLabel(raw string, options []domain.ProfileFieldOption) string {
trimmed := strings.TrimSpace(raw)
if trimmed == "" || strings.EqualFold(trimmed, "unknown") {
return analyticsOtherLabel
}
if len(options) == 0 {
return trimmed
}
key := normalizeAnalyticsOptionKey(trimmed)
for _, opt := range options {
if strings.EqualFold(opt.Code, trimmed) || normalizeAnalyticsOptionKey(opt.Code) == key {
return opt.Code
}
if strings.EqualFold(strings.TrimSpace(opt.Label), trimmed) {
return opt.Code
}
}
return analyticsOtherLabel
}
func collapseAnalyticsLabelCounts(rows []domain.AnalyticsLabelCount, options []domain.ProfileFieldOption) []domain.AnalyticsLabelCount {
if len(rows) == 0 {
return rows
}
sums := make(map[string]int64, len(rows))
for _, row := range rows {
label := resolveAnalyticsFieldLabel(row.Label, options)
sums[label] += row.Count
}
out := make([]domain.AnalyticsLabelCount, 0, len(sums))
for label, count := range sums {
out = append(out, domain.AnalyticsLabelCount{Label: label, Count: count})
}
sort.Slice(out, func(i, j int) bool {
if out[i].Count == out[j].Count {
return out[i].Label < out[j].Label
}
return out[i].Count > out[j].Count
})
return out
}
// collapseUnknownToOther remaps empty/unknown labels when no field_options catalog exists.
func collapseUnknownToOther(rows []domain.AnalyticsLabelCount) []domain.AnalyticsLabelCount {
return collapseAnalyticsLabelCounts(rows, nil)
}