206 lines
6.1 KiB
Go
206 lines
6.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"Rezina-Backend/internal/domain"
|
|
"context"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
const analyticsOtherLabel = "OTHER"
|
|
|
|
// Default codes when a stored profile value is empty, OTHER, unknown, or not in field_options.
|
|
var analyticsFieldDefaults = map[string]string{
|
|
domain.ProfileFieldAgeGroup: "25_34",
|
|
domain.ProfileFieldOccupation: "STUDENTS",
|
|
domain.ProfileFieldEthiopiaRegions: "ADDIS_ABABA",
|
|
domain.ProfileFieldEducationLevel: "HIGH_SCHOOL",
|
|
domain.ProfileFieldLearningGoal: "EVERYDAY_CONVERSATION",
|
|
domain.ProfileFieldLanguageChallange: "PRONUNCIATION",
|
|
domain.ProfileFieldCountry: "ET",
|
|
}
|
|
|
|
const analyticsKnowledgeLevelDefault = "BEGINNER"
|
|
|
|
// 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
|
|
}
|
|
|
|
func isAnalyticsCatchAllLabel(raw string) bool {
|
|
key := normalizeAnalyticsOptionKey(raw)
|
|
switch key {
|
|
case "", analyticsOtherLabel, "UNKNOWN", "UNASSIGNED", "N_A", "NA":
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func analyticsDefaultForField(fieldKey string) string {
|
|
if code, ok := analyticsFieldDefaults[fieldKey]; ok && code != "" {
|
|
return code
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// resolveAnalyticsFieldLabel maps a stored user value to a static field_options code.
|
|
// Empty / "unknown" / OTHER / values not in the option list become the field default
|
|
// (never OTHER — analytics should not surface an Other bucket).
|
|
func resolveAnalyticsFieldLabel(raw string, options []domain.ProfileFieldOption, defaultCode string) string {
|
|
fallback := strings.TrimSpace(defaultCode)
|
|
if fallback == "" {
|
|
fallback = analyticsOtherLabel
|
|
}
|
|
|
|
trimmed := strings.TrimSpace(raw)
|
|
if isAnalyticsCatchAllLabel(trimmed) {
|
|
return fallback
|
|
}
|
|
if len(options) == 0 {
|
|
return trimmed
|
|
}
|
|
|
|
key := normalizeAnalyticsOptionKey(trimmed)
|
|
for _, opt := range options {
|
|
code := strings.TrimSpace(opt.Code)
|
|
if strings.EqualFold(code, analyticsOtherLabel) {
|
|
continue
|
|
}
|
|
if strings.EqualFold(code, trimmed) || normalizeAnalyticsOptionKey(code) == key {
|
|
return code
|
|
}
|
|
if strings.EqualFold(strings.TrimSpace(opt.Label), trimmed) {
|
|
return code
|
|
}
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func collapseAnalyticsLabelCounts(
|
|
rows []domain.AnalyticsLabelCount,
|
|
options []domain.ProfileFieldOption,
|
|
defaultCode string,
|
|
) []domain.AnalyticsLabelCount {
|
|
if len(rows) == 0 {
|
|
return rows
|
|
}
|
|
sums := make(map[string]int64, len(rows))
|
|
for _, row := range rows {
|
|
label := resolveAnalyticsFieldLabel(row.Label, options, defaultCode)
|
|
if strings.EqualFold(label, analyticsOtherLabel) {
|
|
if defaultCode != "" {
|
|
label = defaultCode
|
|
} else {
|
|
continue
|
|
}
|
|
}
|
|
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
|
|
}
|
|
|
|
func collapseAnalyticsField(
|
|
rows []domain.AnalyticsLabelCount,
|
|
fieldKey string,
|
|
fieldOptions map[string][]domain.ProfileFieldOption,
|
|
) []domain.AnalyticsLabelCount {
|
|
return collapseAnalyticsLabelCounts(rows, fieldOptions[fieldKey], analyticsDefaultForField(fieldKey))
|
|
}
|
|
|
|
// collapseUnknownToDefault remaps empty/unknown/OTHER when no field_options catalog exists.
|
|
func collapseUnknownToDefault(rows []domain.AnalyticsLabelCount, defaultCode string) []domain.AnalyticsLabelCount {
|
|
return collapseAnalyticsLabelCounts(rows, nil, defaultCode)
|
|
}
|
|
|
|
// Deprecated alias kept for call-site clarity during transition.
|
|
func collapseUnknownToOther(rows []domain.AnalyticsLabelCount) []domain.AnalyticsLabelCount {
|
|
return collapseUnknownToDefault(rows, analyticsKnowledgeLevelDefault)
|
|
}
|
|
|
|
const (
|
|
analyticsGenderLabelMale = "Male"
|
|
analyticsGenderLabelFemale = "Female"
|
|
analyticsGenderLabelUnassigned = "unassigned"
|
|
)
|
|
|
|
// resolveAnalyticsGenderLabel maps stored gender to Male, Female, or unassigned.
|
|
func resolveAnalyticsGenderLabel(raw string) string {
|
|
trimmed := strings.TrimSpace(raw)
|
|
if trimmed == "" || strings.EqualFold(trimmed, "unknown") ||
|
|
strings.EqualFold(trimmed, analyticsOtherLabel) ||
|
|
strings.EqualFold(trimmed, analyticsGenderLabelUnassigned) {
|
|
return analyticsGenderLabelUnassigned
|
|
}
|
|
switch normalizeAnalyticsOptionKey(trimmed) {
|
|
case "MALE":
|
|
return analyticsGenderLabelMale
|
|
case "FEMALE":
|
|
return analyticsGenderLabelFemale
|
|
default:
|
|
return analyticsGenderLabelUnassigned
|
|
}
|
|
}
|
|
|
|
func collapseGenderAnalytics(rows []domain.AnalyticsLabelCount) []domain.AnalyticsLabelCount {
|
|
if len(rows) == 0 {
|
|
return rows
|
|
}
|
|
sums := make(map[string]int64, len(rows))
|
|
for _, row := range rows {
|
|
label := resolveAnalyticsGenderLabel(row.Label)
|
|
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
|
|
}
|