Map invalid profile analytics values to field defaults instead of Other.
Empty, unknown, and out-of-catalog values fold into per-field defaults so demographics charts no longer surface an Other bucket. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
78b60b8a52
commit
644a781cbb
|
|
@ -9,6 +9,19 @@ import (
|
|||
|
||||
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,
|
||||
|
|
@ -44,13 +57,35 @@ func normalizeAnalyticsOptionKey(raw string) string {
|
|||
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" / 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 {
|
||||
// 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 trimmed == "" || strings.EqualFold(trimmed, "unknown") {
|
||||
return analyticsOtherLabel
|
||||
if isAnalyticsCatchAllLabel(trimmed) {
|
||||
return fallback
|
||||
}
|
||||
if len(options) == 0 {
|
||||
return trimmed
|
||||
|
|
@ -58,23 +93,38 @@ func resolveAnalyticsFieldLabel(raw string, options []domain.ProfileFieldOption)
|
|||
|
||||
key := normalizeAnalyticsOptionKey(trimmed)
|
||||
for _, opt := range options {
|
||||
if strings.EqualFold(opt.Code, trimmed) || normalizeAnalyticsOptionKey(opt.Code) == key {
|
||||
return opt.Code
|
||||
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 opt.Code
|
||||
return code
|
||||
}
|
||||
}
|
||||
return analyticsOtherLabel
|
||||
return fallback
|
||||
}
|
||||
|
||||
func collapseAnalyticsLabelCounts(rows []domain.AnalyticsLabelCount, options []domain.ProfileFieldOption) []domain.AnalyticsLabelCount {
|
||||
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)
|
||||
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))
|
||||
|
|
@ -90,9 +140,22 @@ func collapseAnalyticsLabelCounts(rows []domain.AnalyticsLabelCount, options []d
|
|||
return out
|
||||
}
|
||||
|
||||
// collapseUnknownToOther remaps empty/unknown labels when no field_options catalog exists.
|
||||
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 collapseAnalyticsLabelCounts(rows, nil)
|
||||
return collapseUnknownToDefault(rows, analyticsKnowledgeLevelDefault)
|
||||
}
|
||||
|
||||
const (
|
||||
|
|
|
|||
|
|
@ -12,28 +12,29 @@ func TestResolveAnalyticsFieldLabel(t *testing.T) {
|
|||
{Code: "OTHER", Label: "Other"},
|
||||
{Code: "ADDIS_ABABA", Label: "Addis Ababa"},
|
||||
}
|
||||
defaultCode := "ET"
|
||||
|
||||
cases := []struct {
|
||||
raw string
|
||||
want string
|
||||
}{
|
||||
{"", "OTHER"},
|
||||
{"unknown", "OTHER"},
|
||||
{"Unknown", "OTHER"},
|
||||
{"", "ET"},
|
||||
{"unknown", "ET"},
|
||||
{"Unknown", "ET"},
|
||||
{"OTHER", "ET"},
|
||||
{"ET", "ET"},
|
||||
{"et", "ET"},
|
||||
{"Ethiopia", "ET"},
|
||||
{"EVERYDAY_CONVERSATION", "EVERYDAY_CONVERSATION"},
|
||||
{"Everyday conversation", "EVERYDAY_CONVERSATION"},
|
||||
{"Learn to Speak English", "OTHER"},
|
||||
{"Nurse", "OTHER"},
|
||||
{"addis abeba", "OTHER"},
|
||||
{"Learn to Speak English", "ET"},
|
||||
{"Nurse", "ET"},
|
||||
{"addis abeba", "ET"},
|
||||
{"Addis Ababa", "ADDIS_ABABA"},
|
||||
{"OTHER", "OTHER"},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
got := resolveAnalyticsFieldLabel(tc.raw, options)
|
||||
got := resolveAnalyticsFieldLabel(tc.raw, options, defaultCode)
|
||||
if got != tc.want {
|
||||
t.Fatalf("resolveAnalyticsFieldLabel(%q) = %q, want %q", tc.raw, got, tc.want)
|
||||
}
|
||||
|
|
@ -55,11 +56,10 @@ func TestCollapseAnalyticsLabelCounts(t *testing.T) {
|
|||
{Label: "OTHER", Count: 4},
|
||||
}
|
||||
|
||||
got := collapseAnalyticsLabelCounts(rows, options)
|
||||
got := collapseAnalyticsLabelCounts(rows, options, "PRONUNCIATION")
|
||||
want := map[string]int64{
|
||||
"ET": 12,
|
||||
"PRONUNCIATION": 3,
|
||||
"OTHER": 10, // 5 unknown + 1 Grammar + 4 OTHER
|
||||
"PRONUNCIATION": 13, // 3 Pronunciation + 5 unknown + 1 Grammar + 4 OTHER
|
||||
}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("got %d rows, want %d: %+v", len(got), len(want), got)
|
||||
|
|
@ -68,16 +68,24 @@ func TestCollapseAnalyticsLabelCounts(t *testing.T) {
|
|||
if want[row.Label] != row.Count {
|
||||
t.Fatalf("label %q count = %d, want %d", row.Label, row.Count, want[row.Label])
|
||||
}
|
||||
if stringsEqualFoldOther(row.Label) {
|
||||
t.Fatalf("unexpected OTHER bucket in result: %+v", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollapseUnknownToOtherWithoutOptions(t *testing.T) {
|
||||
func stringsEqualFoldOther(s string) bool {
|
||||
return normalizeAnalyticsOptionKey(s) == analyticsOtherLabel
|
||||
}
|
||||
|
||||
func TestCollapseUnknownToDefaultWithoutOptions(t *testing.T) {
|
||||
rows := []domain.AnalyticsLabelCount{
|
||||
{Label: "BEGINNER", Count: 3},
|
||||
{Label: "unknown", Count: 2},
|
||||
{Label: "OTHER", Count: 1},
|
||||
}
|
||||
got := collapseUnknownToOther(rows)
|
||||
want := map[string]int64{"BEGINNER": 3, "OTHER": 2}
|
||||
got := collapseUnknownToDefault(rows, "BEGINNER")
|
||||
want := map[string]int64{"BEGINNER": 6}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("got %+v", got)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -398,18 +398,19 @@ func mapUsersSection(
|
|||
NewMonth: summary.NewThisMonth,
|
||||
ByRole: roles,
|
||||
ByStatus: statuses,
|
||||
ByAgeGroup: collapseAnalyticsLabelCounts(ages, fieldOptions[domain.ProfileFieldAgeGroup]),
|
||||
ByAgeGroup: collapseAnalyticsField(ages, domain.ProfileFieldAgeGroup, fieldOptions),
|
||||
ByGender: collapseGenderAnalytics(genders),
|
||||
ByEducationLevel: collapseAnalyticsLabelCounts(education, fieldOptions[domain.ProfileFieldEducationLevel]),
|
||||
ByOccupation: collapseAnalyticsLabelCounts(occupations, fieldOptions[domain.ProfileFieldOccupation]),
|
||||
ByLearningGoal: collapseAnalyticsLabelCounts(learningGoals, fieldOptions[domain.ProfileFieldLearningGoal]),
|
||||
ByLanguageChallange: collapseAnalyticsLabelCounts(
|
||||
ByEducationLevel: collapseAnalyticsField(education, domain.ProfileFieldEducationLevel, fieldOptions),
|
||||
ByOccupation: collapseAnalyticsField(occupations, domain.ProfileFieldOccupation, fieldOptions),
|
||||
ByLearningGoal: collapseAnalyticsField(learningGoals, domain.ProfileFieldLearningGoal, fieldOptions),
|
||||
ByLanguageChallange: collapseAnalyticsField(
|
||||
languageChallanges,
|
||||
fieldOptions[domain.ProfileFieldLanguageChallange],
|
||||
domain.ProfileFieldLanguageChallange,
|
||||
fieldOptions,
|
||||
),
|
||||
ByKnowledgeLevel: collapseUnknownToOther(knowledge),
|
||||
ByCountry: collapseAnalyticsLabelCounts(countries, fieldOptions[domain.ProfileFieldCountry]),
|
||||
ByRegion: collapseAnalyticsLabelCounts(regions, fieldOptions[domain.ProfileFieldEthiopiaRegions]),
|
||||
ByKnowledgeLevel: collapseUnknownToDefault(knowledge, analyticsKnowledgeLevelDefault),
|
||||
ByCountry: collapseAnalyticsField(countries, domain.ProfileFieldCountry, fieldOptions),
|
||||
ByRegion: collapseAnalyticsField(regions, domain.ProfileFieldEthiopiaRegions, fieldOptions),
|
||||
RegistrationsLast30Days: timePoints,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user