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"
|
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.
|
// analyticsProfileFieldKeys are user-profile breakdowns validated against field_options.
|
||||||
var analyticsProfileFieldKeys = []string{
|
var analyticsProfileFieldKeys = []string{
|
||||||
domain.ProfileFieldAgeGroup,
|
domain.ProfileFieldAgeGroup,
|
||||||
|
|
@ -44,13 +57,35 @@ func normalizeAnalyticsOptionKey(raw string) string {
|
||||||
return 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.
|
// resolveAnalyticsFieldLabel maps a stored user value to a static field_options code.
|
||||||
// Empty / "unknown" / values not in the option list become OTHER.
|
// Empty / "unknown" / OTHER / values not in the option list become the field default
|
||||||
// When options is empty, only empty/unknown are remapped (other values kept as-is).
|
// (never OTHER — analytics should not surface an Other bucket).
|
||||||
func resolveAnalyticsFieldLabel(raw string, options []domain.ProfileFieldOption) string {
|
func resolveAnalyticsFieldLabel(raw string, options []domain.ProfileFieldOption, defaultCode string) string {
|
||||||
|
fallback := strings.TrimSpace(defaultCode)
|
||||||
|
if fallback == "" {
|
||||||
|
fallback = analyticsOtherLabel
|
||||||
|
}
|
||||||
|
|
||||||
trimmed := strings.TrimSpace(raw)
|
trimmed := strings.TrimSpace(raw)
|
||||||
if trimmed == "" || strings.EqualFold(trimmed, "unknown") {
|
if isAnalyticsCatchAllLabel(trimmed) {
|
||||||
return analyticsOtherLabel
|
return fallback
|
||||||
}
|
}
|
||||||
if len(options) == 0 {
|
if len(options) == 0 {
|
||||||
return trimmed
|
return trimmed
|
||||||
|
|
@ -58,23 +93,38 @@ func resolveAnalyticsFieldLabel(raw string, options []domain.ProfileFieldOption)
|
||||||
|
|
||||||
key := normalizeAnalyticsOptionKey(trimmed)
|
key := normalizeAnalyticsOptionKey(trimmed)
|
||||||
for _, opt := range options {
|
for _, opt := range options {
|
||||||
if strings.EqualFold(opt.Code, trimmed) || normalizeAnalyticsOptionKey(opt.Code) == key {
|
code := strings.TrimSpace(opt.Code)
|
||||||
return 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) {
|
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 {
|
if len(rows) == 0 {
|
||||||
return rows
|
return rows
|
||||||
}
|
}
|
||||||
sums := make(map[string]int64, len(rows))
|
sums := make(map[string]int64, len(rows))
|
||||||
for _, row := range 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
|
sums[label] += row.Count
|
||||||
}
|
}
|
||||||
out := make([]domain.AnalyticsLabelCount, 0, len(sums))
|
out := make([]domain.AnalyticsLabelCount, 0, len(sums))
|
||||||
|
|
@ -90,9 +140,22 @@ func collapseAnalyticsLabelCounts(rows []domain.AnalyticsLabelCount, options []d
|
||||||
return out
|
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 {
|
func collapseUnknownToOther(rows []domain.AnalyticsLabelCount) []domain.AnalyticsLabelCount {
|
||||||
return collapseAnalyticsLabelCounts(rows, nil)
|
return collapseUnknownToDefault(rows, analyticsKnowledgeLevelDefault)
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
||||||
|
|
@ -12,28 +12,29 @@ func TestResolveAnalyticsFieldLabel(t *testing.T) {
|
||||||
{Code: "OTHER", Label: "Other"},
|
{Code: "OTHER", Label: "Other"},
|
||||||
{Code: "ADDIS_ABABA", Label: "Addis Ababa"},
|
{Code: "ADDIS_ABABA", Label: "Addis Ababa"},
|
||||||
}
|
}
|
||||||
|
defaultCode := "ET"
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
raw string
|
raw string
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{"", "OTHER"},
|
{"", "ET"},
|
||||||
{"unknown", "OTHER"},
|
{"unknown", "ET"},
|
||||||
{"Unknown", "OTHER"},
|
{"Unknown", "ET"},
|
||||||
|
{"OTHER", "ET"},
|
||||||
{"ET", "ET"},
|
{"ET", "ET"},
|
||||||
{"et", "ET"},
|
{"et", "ET"},
|
||||||
{"Ethiopia", "ET"},
|
{"Ethiopia", "ET"},
|
||||||
{"EVERYDAY_CONVERSATION", "EVERYDAY_CONVERSATION"},
|
{"EVERYDAY_CONVERSATION", "EVERYDAY_CONVERSATION"},
|
||||||
{"Everyday conversation", "EVERYDAY_CONVERSATION"},
|
{"Everyday conversation", "EVERYDAY_CONVERSATION"},
|
||||||
{"Learn to Speak English", "OTHER"},
|
{"Learn to Speak English", "ET"},
|
||||||
{"Nurse", "OTHER"},
|
{"Nurse", "ET"},
|
||||||
{"addis abeba", "OTHER"},
|
{"addis abeba", "ET"},
|
||||||
{"Addis Ababa", "ADDIS_ABABA"},
|
{"Addis Ababa", "ADDIS_ABABA"},
|
||||||
{"OTHER", "OTHER"},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
got := resolveAnalyticsFieldLabel(tc.raw, options)
|
got := resolveAnalyticsFieldLabel(tc.raw, options, defaultCode)
|
||||||
if got != tc.want {
|
if got != tc.want {
|
||||||
t.Fatalf("resolveAnalyticsFieldLabel(%q) = %q, want %q", tc.raw, 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},
|
{Label: "OTHER", Count: 4},
|
||||||
}
|
}
|
||||||
|
|
||||||
got := collapseAnalyticsLabelCounts(rows, options)
|
got := collapseAnalyticsLabelCounts(rows, options, "PRONUNCIATION")
|
||||||
want := map[string]int64{
|
want := map[string]int64{
|
||||||
"ET": 12,
|
"ET": 12,
|
||||||
"PRONUNCIATION": 3,
|
"PRONUNCIATION": 13, // 3 Pronunciation + 5 unknown + 1 Grammar + 4 OTHER
|
||||||
"OTHER": 10, // 5 unknown + 1 Grammar + 4 OTHER
|
|
||||||
}
|
}
|
||||||
if len(got) != len(want) {
|
if len(got) != len(want) {
|
||||||
t.Fatalf("got %d rows, want %d: %+v", len(got), len(want), got)
|
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 {
|
if want[row.Label] != row.Count {
|
||||||
t.Fatalf("label %q count = %d, want %d", row.Label, row.Count, want[row.Label])
|
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{
|
rows := []domain.AnalyticsLabelCount{
|
||||||
{Label: "BEGINNER", Count: 3},
|
{Label: "BEGINNER", Count: 3},
|
||||||
{Label: "unknown", Count: 2},
|
{Label: "unknown", Count: 2},
|
||||||
|
{Label: "OTHER", Count: 1},
|
||||||
}
|
}
|
||||||
got := collapseUnknownToOther(rows)
|
got := collapseUnknownToDefault(rows, "BEGINNER")
|
||||||
want := map[string]int64{"BEGINNER": 3, "OTHER": 2}
|
want := map[string]int64{"BEGINNER": 6}
|
||||||
if len(got) != len(want) {
|
if len(got) != len(want) {
|
||||||
t.Fatalf("got %+v", got)
|
t.Fatalf("got %+v", got)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -398,18 +398,19 @@ func mapUsersSection(
|
||||||
NewMonth: summary.NewThisMonth,
|
NewMonth: summary.NewThisMonth,
|
||||||
ByRole: roles,
|
ByRole: roles,
|
||||||
ByStatus: statuses,
|
ByStatus: statuses,
|
||||||
ByAgeGroup: collapseAnalyticsLabelCounts(ages, fieldOptions[domain.ProfileFieldAgeGroup]),
|
ByAgeGroup: collapseAnalyticsField(ages, domain.ProfileFieldAgeGroup, fieldOptions),
|
||||||
ByGender: collapseGenderAnalytics(genders),
|
ByGender: collapseGenderAnalytics(genders),
|
||||||
ByEducationLevel: collapseAnalyticsLabelCounts(education, fieldOptions[domain.ProfileFieldEducationLevel]),
|
ByEducationLevel: collapseAnalyticsField(education, domain.ProfileFieldEducationLevel, fieldOptions),
|
||||||
ByOccupation: collapseAnalyticsLabelCounts(occupations, fieldOptions[domain.ProfileFieldOccupation]),
|
ByOccupation: collapseAnalyticsField(occupations, domain.ProfileFieldOccupation, fieldOptions),
|
||||||
ByLearningGoal: collapseAnalyticsLabelCounts(learningGoals, fieldOptions[domain.ProfileFieldLearningGoal]),
|
ByLearningGoal: collapseAnalyticsField(learningGoals, domain.ProfileFieldLearningGoal, fieldOptions),
|
||||||
ByLanguageChallange: collapseAnalyticsLabelCounts(
|
ByLanguageChallange: collapseAnalyticsField(
|
||||||
languageChallanges,
|
languageChallanges,
|
||||||
fieldOptions[domain.ProfileFieldLanguageChallange],
|
domain.ProfileFieldLanguageChallange,
|
||||||
|
fieldOptions,
|
||||||
),
|
),
|
||||||
ByKnowledgeLevel: collapseUnknownToOther(knowledge),
|
ByKnowledgeLevel: collapseUnknownToDefault(knowledge, analyticsKnowledgeLevelDefault),
|
||||||
ByCountry: collapseAnalyticsLabelCounts(countries, fieldOptions[domain.ProfileFieldCountry]),
|
ByCountry: collapseAnalyticsField(countries, domain.ProfileFieldCountry, fieldOptions),
|
||||||
ByRegion: collapseAnalyticsLabelCounts(regions, fieldOptions[domain.ProfileFieldEthiopiaRegions]),
|
ByRegion: collapseAnalyticsField(regions, domain.ProfileFieldEthiopiaRegions, fieldOptions),
|
||||||
RegistrationsLast30Days: timePoints,
|
RegistrationsLast30Days: timePoints,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user