analytics: customers totals, open counts, normalize education/marriage
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -607,6 +607,19 @@ const guarantorsSQL = `
|
||||
ELSE 'none'
|
||||
END`
|
||||
|
||||
// Older rows spell some education values differently from the form's options
|
||||
// (higher/secondary vs high/school); fold them so each level is one group.
|
||||
const educationSQL = `
|
||||
CASE lower(trim(coalesce(education, '')))
|
||||
WHEN '' THEN 'unknown'
|
||||
WHEN 'higher' THEN 'high'
|
||||
WHEN 'secondary' THEN 'school'
|
||||
ELSE lower(trim(education))
|
||||
END`
|
||||
|
||||
// Form options are upper case (MARRIED, SINGLE, ...); older rows are lower case.
|
||||
const marriageSQL = `coalesce(nullif(upper(trim(marriage_status)), ''), 'unknown')`
|
||||
|
||||
func (r *repository) Customers(ctx context.Context, f analytics.Filter) (analytics.CustomersDTO, error) {
|
||||
out := analytics.CustomersDTO{
|
||||
AgeGroups: []analytics.ApprovalCountDTO{},
|
||||
@@ -623,10 +636,10 @@ func (r *repository) Customers(ctx context.Context, f analytics.Filter) (analyti
|
||||
return out, err
|
||||
}
|
||||
sortByOrder(out.AgeGroups, ageGroupOrder)
|
||||
if out.Education, err = r.loanApprovalBy(ctx, f, `coalesce(nullif(trim(education), ''), 'unknown')`); err != nil {
|
||||
if out.Education, err = r.loanApprovalBy(ctx, f, educationSQL); err != nil {
|
||||
return out, err
|
||||
}
|
||||
if out.MarriageStatus, err = r.loanApprovalBy(ctx, f, `coalesce(nullif(trim(marriage_status), ''), 'unknown')`); err != nil {
|
||||
if out.MarriageStatus, err = r.loanApprovalBy(ctx, f, marriageSQL); err != nil {
|
||||
return out, err
|
||||
}
|
||||
if out.Guarantors, err = r.loanApprovalBy(ctx, f, guarantorsSQL); err != nil {
|
||||
@@ -634,18 +647,24 @@ func (r *repository) Customers(ctx context.Context, f analytics.Filter) (analyti
|
||||
}
|
||||
sortByOrder(out.Guarantors, []string{"none", "one", "two"})
|
||||
|
||||
// Every order falls in exactly one age group, so its totals are the overall ones.
|
||||
for _, g := range out.AgeGroups {
|
||||
out.Total += g.Count
|
||||
out.Approved += g.Approved
|
||||
out.Rejected += g.Rejected
|
||||
out.Open += g.Open
|
||||
}
|
||||
out.ApprovalRate = ratePtr(out.Approved, out.Approved+out.Rejected)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// loanApprovalBy groups loan orders in range by keyExpr with approved and
|
||||
// rejected counts per group.
|
||||
// loanApprovalBy groups loan orders in range by keyExpr with approved,
|
||||
// rejected and open counts per group.
|
||||
func (r *repository) loanApprovalBy(ctx context.Context, f analytics.Filter, keyExpr string) ([]analytics.ApprovalCountDTO, error) {
|
||||
var args sqlArgs
|
||||
query := fmt.Sprintf(`
|
||||
SELECT k, COUNT(*), COUNT(*) FILTER (WHERE bucket = 'approved'), COUNT(*) FILTER (WHERE bucket = 'rejected')
|
||||
SELECT k, COUNT(*), COUNT(*) FILTER (WHERE bucket = 'approved'), COUNT(*) FILTER (WHERE bucket = 'rejected'),
|
||||
COUNT(*) FILTER (WHERE bucket = 'open')
|
||||
FROM (
|
||||
SELECT %s AS k, %s AS bucket
|
||||
FROM loan_orders
|
||||
@@ -664,7 +683,7 @@ func (r *repository) loanApprovalBy(ctx context.Context, f analytics.Filter, key
|
||||
var total int64
|
||||
for rows.Next() {
|
||||
var it analytics.ApprovalCountDTO
|
||||
if err := rows.Scan(&it.Key, &it.Count, &it.Approved, &it.Rejected); err != nil {
|
||||
if err := rows.Scan(&it.Key, &it.Count, &it.Approved, &it.Rejected, &it.Open); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
it.ApprovalRate = ratePtr(it.Approved, it.Approved+it.Rejected)
|
||||
|
||||
@@ -141,18 +141,23 @@ type ProductsDTO struct {
|
||||
// ---- /customers ----
|
||||
|
||||
// ApprovalCountDTO is a CountDTO plus how many of those loan orders were
|
||||
// approved / rejected, so the UI can show approval rate per group.
|
||||
// approved / rejected / still open, so the UI can show approval rate per group.
|
||||
type ApprovalCountDTO struct {
|
||||
Key string `json:"key"`
|
||||
Count int64 `json:"count"`
|
||||
Percent float64 `json:"percent"`
|
||||
Approved int64 `json:"approved"`
|
||||
Rejected int64 `json:"rejected"`
|
||||
Open int64 `json:"open"`
|
||||
ApprovalRate *float64 `json:"approvalRate"`
|
||||
}
|
||||
|
||||
type CustomersDTO struct {
|
||||
Total int64 `json:"total"`
|
||||
Approved int64 `json:"approved"`
|
||||
Rejected int64 `json:"rejected"`
|
||||
Open int64 `json:"open"`
|
||||
ApprovalRate *float64 `json:"approvalRate"`
|
||||
AgeGroups []ApprovalCountDTO `json:"ageGroups"`
|
||||
Education []ApprovalCountDTO `json:"education"`
|
||||
MarriageStatus []ApprovalCountDTO `json:"marriageStatus"`
|
||||
|
||||
Reference in New Issue
Block a user