analytics: customers totals, open counts, normalize education/marriage
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
29867561d1
commit
965b00dba5
2 files changed
+31
-7
No files matched your search
@@ -607,6 +607,19 @@ const guarantorsSQL = `
|
|||||||
ELSE 'none'
|
ELSE 'none'
|
||||||
END`
|
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) {
|
func (r *repository) Customers(ctx context.Context, f analytics.Filter) (analytics.CustomersDTO, error) {
|
||||||
out := analytics.CustomersDTO{
|
out := analytics.CustomersDTO{
|
||||||
AgeGroups: []analytics.ApprovalCountDTO{},
|
AgeGroups: []analytics.ApprovalCountDTO{},
|
||||||
@@ -623,10 +636,10 @@ func (r *repository) Customers(ctx context.Context, f analytics.Filter) (analyti
|
|||||||
return out, err
|
return out, err
|
||||||
}
|
}
|
||||||
sortByOrder(out.AgeGroups, ageGroupOrder)
|
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
|
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
|
return out, err
|
||||||
}
|
}
|
||||||
if out.Guarantors, err = r.loanApprovalBy(ctx, f, guarantorsSQL); err != nil {
|
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"})
|
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 {
|
for _, g := range out.AgeGroups {
|
||||||
out.Total += g.Count
|
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
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// loanApprovalBy groups loan orders in range by keyExpr with approved and
|
// loanApprovalBy groups loan orders in range by keyExpr with approved,
|
||||||
// rejected counts per group.
|
// rejected and open counts per group.
|
||||||
func (r *repository) loanApprovalBy(ctx context.Context, f analytics.Filter, keyExpr string) ([]analytics.ApprovalCountDTO, error) {
|
func (r *repository) loanApprovalBy(ctx context.Context, f analytics.Filter, keyExpr string) ([]analytics.ApprovalCountDTO, error) {
|
||||||
var args sqlArgs
|
var args sqlArgs
|
||||||
query := fmt.Sprintf(`
|
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 (
|
FROM (
|
||||||
SELECT %s AS k, %s AS bucket
|
SELECT %s AS k, %s AS bucket
|
||||||
FROM loan_orders
|
FROM loan_orders
|
||||||
@@ -664,7 +683,7 @@ func (r *repository) loanApprovalBy(ctx context.Context, f analytics.Filter, key
|
|||||||
var total int64
|
var total int64
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var it analytics.ApprovalCountDTO
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
it.ApprovalRate = ratePtr(it.Approved, it.Approved+it.Rejected)
|
it.ApprovalRate = ratePtr(it.Approved, it.Approved+it.Rejected)
|
||||||
|
|||||||
@@ -141,18 +141,23 @@ type ProductsDTO struct {
|
|||||||
// ---- /customers ----
|
// ---- /customers ----
|
||||||
|
|
||||||
// ApprovalCountDTO is a CountDTO plus how many of those loan orders were
|
// 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 {
|
type ApprovalCountDTO struct {
|
||||||
Key string `json:"key"`
|
Key string `json:"key"`
|
||||||
Count int64 `json:"count"`
|
Count int64 `json:"count"`
|
||||||
Percent float64 `json:"percent"`
|
Percent float64 `json:"percent"`
|
||||||
Approved int64 `json:"approved"`
|
Approved int64 `json:"approved"`
|
||||||
Rejected int64 `json:"rejected"`
|
Rejected int64 `json:"rejected"`
|
||||||
|
Open int64 `json:"open"`
|
||||||
ApprovalRate *float64 `json:"approvalRate"`
|
ApprovalRate *float64 `json:"approvalRate"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CustomersDTO struct {
|
type CustomersDTO struct {
|
||||||
Total int64 `json:"total"`
|
Total int64 `json:"total"`
|
||||||
|
Approved int64 `json:"approved"`
|
||||||
|
Rejected int64 `json:"rejected"`
|
||||||
|
Open int64 `json:"open"`
|
||||||
|
ApprovalRate *float64 `json:"approvalRate"`
|
||||||
AgeGroups []ApprovalCountDTO `json:"ageGroups"`
|
AgeGroups []ApprovalCountDTO `json:"ageGroups"`
|
||||||
Education []ApprovalCountDTO `json:"education"`
|
Education []ApprovalCountDTO `json:"education"`
|
||||||
MarriageStatus []ApprovalCountDTO `json:"marriageStatus"`
|
MarriageStatus []ApprovalCountDTO `json:"marriageStatus"`
|
||||||
|
|||||||
Reference in new issue
Block a user