Files
GuwandykA 965b00dba5
Some checks failed
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled
analytics: customers totals, open counts, normalize education/marriage
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 11:06:18 +05:00

218 lines
7.0 KiB
Go

package analytics
import (
"encoding/json"
"time"
)
// Filter narrows every analytics query to [From, To) and, for branch-scope
// callers, to their branches.
type Filter struct {
BranchIds []int64
// Types are the order types the caller may see; queries over "all
// orders" union only these tables.
Types []string
From time.Time
To time.Time // exclusive
}
// Order types: one per customer-facing order table.
const (
OrderLoan = "loan"
OrderLoanLetter = "loan_paid_off_letter"
OrderCard = "card"
OrderCardPin = "card_pin"
OrderCardRekwizit = "card_rekwizit"
OrderVisaMaster = "visa_master"
)
type CountDTO struct {
Key string `json:"key"`
Count int64 `json:"count"`
Percent float64 `json:"percent"`
}
// ---- /finance ----
// PaymentTotalsDTO sums successful payouts only (gateway_status = success).
// BankFeeTmt is the bank's own fee; GBÜS and TVEB fees are reported apart
// since they go to those parties.
type PaymentTotalsDTO struct {
Count int64 `json:"count"`
AmountTmt float64 `json:"amountTmt"`
BankFeeTmt float64 `json:"bankFeeTmt"`
GbusFeeTmt float64 `json:"gbusFeeTmt"`
TvebFeeTmt float64 `json:"tvebFeeTmt"`
TotalTmt float64 `json:"totalTmt"`
AvgTotalTmt float64 `json:"avgTotalTmt"`
}
type FinanceMonthDTO struct {
Month string `json:"month"` // YYYY-MM
Count int64 `json:"count"`
TotalTmt float64 `json:"totalTmt"`
BankFeeTmt float64 `json:"bankFeeTmt"`
}
// LoanRequestedDTO sums loan_orders.loan_amount, a free-text column; rows
// whose amount does not parse as a number are counted in Unparsed.
type LoanRequestedDTO struct {
Count int64 `json:"count"`
TotalTmt float64 `json:"totalTmt"`
AvgTmt float64 `json:"avgTmt"`
Unparsed int64 `json:"unparsed"`
}
type FinanceDTO struct {
Payments PaymentTotalsDTO `json:"payments"`
Gateway []CountDTO `json:"gateway"`
GatewaySuccessRate *float64 `json:"gatewaySuccessRate"`
Monthly []FinanceMonthDTO `json:"monthly"`
LoanRequested LoanRequestedDTO `json:"loanRequested"`
}
// ---- /backlog ----
// BacklogDTO is one order type's open queue, aged by created_at, plus the
// average time finished orders took (updated_at - created_at, so any later
// edit makes it an upper bound rather than an exact figure).
type BacklogDTO struct {
Type string `json:"type"`
Open int64 `json:"open"`
Under1Day int64 `json:"under1Day"`
From1To3Days int64 `json:"from1To3Days"`
Over3Days int64 `json:"over3Days"`
OldestOpenAt *string `json:"oldestOpenAt"`
OldestOpenDays *float64 `json:"oldestOpenDays"`
Finished int64 `json:"finished"`
AvgProcessHours *float64 `json:"avgProcessHours"`
}
// VisaMasterTimingDTO is exact payout timing from visa_master_payment_logs:
// first logged step to payment_finalized, per order.
type VisaMasterTimingDTO struct {
Orders int64 `json:"orders"`
AvgMinutes *float64 `json:"avgMinutes"`
P90Minutes *float64 `json:"p90Minutes"`
MaxMinutes *float64 `json:"maxMinutes"`
}
type BacklogResponseDTO struct {
Items []BacklogDTO `json:"items"`
VisaMasterTiming VisaMasterTimingDTO `json:"visaMasterTiming"`
}
// ---- /branches ----
type BranchStatDTO struct {
BranchId int64 `json:"branchId"`
Name json.RawMessage `json:"name"`
Region string `json:"region"`
Orders map[string]int64 `json:"orders"`
Total int64 `json:"total"`
Open int64 `json:"open"`
Approved int64 `json:"approved"`
Rejected int64 `json:"rejected"`
ApprovalRate *float64 `json:"approvalRate"`
AvgProcessHours *float64 `json:"avgProcessHours"`
}
type BranchesDTO struct {
Branches []BranchStatDTO `json:"branches"`
Regions []CountDTO `json:"regions"`
}
// ---- /products ----
type NamedCountDTO struct {
Id int64 `json:"id"`
Name json.RawMessage `json:"name"`
Count int64 `json:"count"`
Percent float64 `json:"percent"`
}
type ProductsDTO struct {
LoanSources []CountDTO `json:"loanSources"`
LoanTypes []NamedCountDTO `json:"loanTypes"`
CardTypes []NamedCountDTO `json:"cardTypes"`
CardOrderPay []CountDTO `json:"cardOrderPay"` // paid / unpaid
}
// ---- /customers ----
// ApprovalCountDTO is a CountDTO plus how many of those loan orders were
// 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"`
Guarantors []ApprovalCountDTO `json:"guarantors"` // none / one / two
}
// ---- /system ----
// RegistrationFunnelDTO: a verification row is deleted once registration
// completes, so Completed counts new customer accounts and the rows still
// present are unfinished. Password resets reuse the same table, so
// Abandoned/WaitingCode slightly overcount registrations.
type RegistrationFunnelDTO struct {
Started int64 `json:"started"`
Completed int64 `json:"completed"`
Abandoned int64 `json:"abandoned"` // code expired, never finished
WaitingCode int64 `json:"waitingCode"` // code still valid
WrongCodeTries int64 `json:"wrongCodeTries"`
ConversionPct *float64 `json:"conversionPct"`
}
type OperatorActivityDTO struct {
UserId int64 `json:"userId"`
Name string `json:"name"`
Actions int64 `json:"actions"`
Errors int64 `json:"errors"`
LastSeen *string `json:"lastSeen"`
}
type ModuleErrorDTO struct {
Module string `json:"module"`
Requests int64 `json:"requests"`
Errors int64 `json:"errors"`
ErrorRate float64 `json:"errorRate"`
}
// HeatmapCellDTO: Weekday is Monday=1 .. Sunday=7, Hour is 0..23, both in
// Asia/Ashgabat time.
type HeatmapCellDTO struct {
Weekday int `json:"weekday"`
Hour int `json:"hour"`
Count int64 `json:"count"`
}
type CardBalanceStatsDTO struct {
Statuses []CountDTO `json:"statuses"`
FailureRate *float64 `json:"failureRate"`
ExpiringIn30d int64 `json:"expiringIn30d"`
}
type SystemDTO struct {
Registration *RegistrationFunnelDTO `json:"registration"`
Operators []OperatorActivityDTO `json:"operators"`
ModuleErrors []ModuleErrorDTO `json:"moduleErrors"`
PeakHours []HeatmapCellDTO `json:"peakHours"`
CardBalances *CardBalanceStatsDTO `json:"cardBalances"`
}