perf(clients): make the clients page scale to large panels

The clients page was slow on panels with many clients for two independent
reasons: the server rebuilt the whole picture on every request, and the
browser rebuilt the whole table on every poll.

Server side, ListPaged loaded every client row, every client_inbounds link
and every client_traffics row into Go memory, then filtered, sorted and
paginated in a loop -- on a request the page repeats every five seconds.
Every predicate now runs in SQL and only the requested page's ids are
hydrated, so the cost tracks the page size rather than the client count.
Measured on SQLite with a realistic status mix: the default view at 100k
clients goes from 1,072ms to 64ms. Behaviour is preserved deliberately in
the subtle places -- the cross-panel global-traffic overlay is folded into
the same used-bytes expression the predicates and sort use, LIKE wildcards
are escaped so a search for "a_b" stays literal, and the two different
tiebreak rules the in-memory comparator had are reproduced per sort key.

The summary's per-bucket email lists are capped at 200 with exact counters
beside them. They only back hover popovers, but shipping every match made
the response grow with the panel: at 100k clients it carried ~42k emails,
and the page revalidated all of them through a strict Zod parse every five
seconds. The popover now shows a "+N" chip for the remainder.

Browser side, the page fired three sequential list requests per load and
threw the first two away: the query went out before the persisted sort was
applied, and again before the configured page size was known -- 0 meaning
"one long page" is indistinguishable from "not loaded yet". The page size
is now derived rather than mirrored through an effect, and the previous
visit's value is remembered so the single request goes out at mount instead
of queueing behind /setting/defaultSettings.

Then the per-poll work. Reading isFetching made it a tracked property, so
the refetch interval notified twice per cycle and re-rendered the page even
when structural sharing left the data identical. Xray reports a traffic row
per client whether or not it moved bytes, so the speed map was mostly zeros
and was replaced wholesale every push; zero rows are now dropped and an
unchanged result returns the previous object, which lets React bail out
instead of re-rendering. The five Tooltip-wrapped buttons and the inbound
chips per row do not depend on traffic at all and are now memoised, keyed on
the email because a push replaces the row object of every client whose
counters moved. antd's hashed:false drops 3,311 :where(.css-<hash>) wrappers
and 29% of the generated stylesheet, and a pinned cssVar key stops each of
the eleven page-level ConfigProviders minting its own token scope.

Two callers that only need the mutations, GroupsPage and ClientBulkAddModal,
no longer start the list query -- the groups page had been polling the full
paged list every five seconds for data it never renders.
This commit is contained in:
Sanaei
2026-07-30 02:49:32 +02:00
parent 1e2d6f6081
commit f52c3c4837
18 changed files with 1927 additions and 528 deletions
+27 -9
View File
@@ -29,6 +29,31 @@ type XrayTrafficJob struct {
// refetch for the rest.
const clientStatsSnapshotMaxClients = 5000
// splitMovedClientTraffics keeps the rows that actually moved bytes this poll,
// alongside the active-email list and set derived from the same pass.
//
// Xray reports a row for every known email whether or not it transferred
// anything, so on a large panel nearly every delta is zero. The database writes
// and the external-API inform consume the full slice before this point; the
// WebSocket frame only feeds the dashboard's live speed column, where an absent
// row and a zero row render identically. Broadcasting just the movers keeps that
// frame from growing with the client count — at 5k clients it was carrying about
// a megabyte of zeros every five seconds.
func splitMovedClientTraffics(clientTraffics []*xray.ClientTraffic) ([]*xray.ClientTraffic, []string, map[string]bool) {
moved := make([]*xray.ClientTraffic, 0, len(clientTraffics))
emails := make([]string, 0, len(clientTraffics))
active := make(map[string]bool, len(clientTraffics))
for _, ct := range clientTraffics {
if ct == nil || ct.Up+ct.Down <= 0 {
continue
}
moved = append(moved, ct)
emails = append(emails, ct.Email)
active[ct.Email] = true
}
return moved, emails, active
}
const externalInformTimeout = 3 * time.Second
var externalInformClient = &fasthttp.Client{
@@ -88,14 +113,7 @@ func (j *XrayTrafficJob) Run() {
// than the shared last_online column, which remote-node syncs also bump
// and would otherwise make a client active only on a remote node appear
// online on local inbounds.
activeEmails := make([]string, 0, len(clientTraffics))
deltaActive := make(map[string]bool, len(clientTraffics))
for _, ct := range clientTraffics {
if ct != nil && ct.Up+ct.Down > 0 {
activeEmails = append(activeEmails, ct.Email)
deltaActive[ct.Email] = true
}
}
movedTraffics, activeEmails, deltaActive := splitMovedClientTraffics(clientTraffics)
// When the core supports the online-stats API, union in connection-based
// onlines. Neither signal alone covers everything: an idle-but-connected
// client moves no bytes between polls (the delta heuristic's blind spot),
@@ -179,7 +197,7 @@ func (j *XrayTrafficJob) Run() {
}
websocket.BroadcastTraffic(map[string]any{
"traffics": traffics,
"clientTraffics": clientTraffics,
"clientTraffics": movedTraffics,
"onlineClients": onlineClients,
"onlineByGuid": j.inboundService.GetOnlineClientsByGuid(),
"activeInbounds": j.inboundService.GetActiveInboundsByGuid(),
@@ -0,0 +1,59 @@
package job
import (
"slices"
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
)
func TestSplitMovedClientTraffics(t *testing.T) {
rows := []*xray.ClientTraffic{
{Email: "idle@x", Up: 0, Down: 0},
{Email: "up@x", Up: 1024, Down: 0},
{Email: "down@x", Up: 0, Down: 2048},
nil,
{Email: "both@x", Up: 512, Down: 512},
{Email: "alsoidle@x", Up: 0, Down: 0},
}
moved, emails, active := splitMovedClientTraffics(rows)
t.Run("only the rows that moved bytes are broadcast", func(t *testing.T) {
got := make([]string, 0, len(moved))
for _, ct := range moved {
got = append(got, ct.Email)
}
want := []string{"up@x", "down@x", "both@x"}
if !slices.Equal(got, want) {
t.Fatalf("moved = %v, want %v", got, want)
}
})
t.Run("the active list and set agree with the broadcast rows", func(t *testing.T) {
want := []string{"up@x", "down@x", "both@x"}
if !slices.Equal(emails, want) {
t.Fatalf("activeEmails = %v, want %v", emails, want)
}
if len(active) != len(want) {
t.Fatalf("deltaActive has %d entries, want %d", len(active), len(want))
}
for _, e := range want {
if !active[e] {
t.Fatalf("deltaActive missing %q", e)
}
}
if active["idle@x"] {
t.Fatal("an idle client must not count as active")
}
})
t.Run("an all-idle poll broadcasts nothing", func(t *testing.T) {
moved, emails, active := splitMovedClientTraffics([]*xray.ClientTraffic{
{Email: "a@x"}, {Email: "b@x"},
})
if len(moved) != 0 || len(emails) != 0 || len(active) != 0 {
t.Fatalf("expected an empty split, got %d/%d/%d", len(moved), len(emails), len(active))
}
})
}
+486 -399
View File
@@ -1,13 +1,16 @@
package service
import (
"slices"
"sort"
"strconv"
"strings"
"time"
"github.com/mhsanaei/3x-ui/v3/internal/database"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
"gorm.io/gorm"
)
// ClientSlim is the row-shape used by the clients page. It drops fields the
@@ -74,33 +77,262 @@ type ClientPageResponse struct {
// ClientsSummary collects per-bucket counts plus the matching email lists so
// the clients page can render the dashboard stat cards and their hover
// popovers without shipping the full client array.
// popovers without shipping the full client array. The counters are exact;
// the lists stop at clientSummaryEmailCap entries and only back the popovers.
type ClientsSummary struct {
Total int `json:"total"`
Active int `json:"active"`
Online []string `json:"online"`
Depleted []string `json:"depleted"`
Expiring []string `json:"expiring"`
Deactive []string `json:"deactive"`
Total int `json:"total"`
Active int `json:"active"`
OnlineCount int `json:"onlineCount"`
DepletedCount int `json:"depletedCount"`
ExpiringCount int `json:"expiringCount"`
DeactiveCount int `json:"deactiveCount"`
Online []string `json:"online"`
Depleted []string `json:"depleted"`
Expiring []string `json:"expiring"`
Deactive []string `json:"deactive"`
}
const (
clientPageDefaultSize = 25
clientPageMaxSize = 200
// clientSummaryEmailCap bounds each bucket's email list. Shipping every
// matching email made the response — and the Zod validation the page runs
// over it — grow with the client count on a request that repeats every 5s,
// and left the hover popover rendering thousands of rows.
clientSummaryEmailCap = 200
// sqlNeverSentinel sorts "never expires" / "unlimited quota" clients last,
// matching the sentinel the in-memory comparator used.
sqlNeverSentinel = "4611686018427387903"
// sqlClientEnabled tolerates a NULL enable column, which GORM scans as
// false: without the COALESCE such a row would match neither the enabled
// nor the disabled branch of any predicate.
sqlClientEnabled = "COALESCE(c.enable, FALSE)"
)
// ListPaged loads every client (with traffic + attachments) into memory,
// applies the requested filter / search / protocol predicates, sorts, and
// returns the requested page along with total and filtered counts. The DB
// query itself is unchanged from List(); the win is that the response
// only carries 25-ish slim rows over the wire instead of all 2000 full
// records, which on real panels was the dominant cost.
func (s *ClientService) ListPaged(inboundSvc *InboundService, settingSvc *SettingService, params ClientPageParams) (*ClientPageResponse, error) {
all, err := s.List()
if err != nil {
return nil, err
const clientSearchCond = `(LOWER(c.email) LIKE ? ESCAPE '\'
OR LOWER(COALESCE(c.sub_id, '')) LIKE ? ESCAPE '\'
OR LOWER(COALESCE(c.comment, '')) LIKE ? ESCAPE '\'
OR LOWER(COALESCE(c.uuid, '')) LIKE ? ESCAPE '\'
OR LOWER(COALESCE(c.password, '')) LIKE ? ESCAPE '\'
OR LOWER(COALESCE(c.auth, '')) LIKE ? ESCAPE '\'
OR (COALESCE(c.tg_id, 0) <> 0 AND CAST(c.tg_id AS TEXT) LIKE ? ESCAPE '\'))`
// clientQuery builds the statements behind the clients page: a clients row
// joined to its traffic counters, plus the expressions every bucket predicate
// shares. Filtering, sorting, paging and the summary all run in the database.
// Loading every client (with attachments and traffic) into Go and doing it in
// memory cost ~200ms per request at 20k clients on a page that polls every
// 5 seconds, which is what made the table feel stuck on large panels.
type clientQuery struct {
db *gorm.DB
joins []clientQueryJoin
usedExpr string
nowMs int64
expireDiffMs int64
trafficDiffBytes int64
}
type clientQueryJoin struct {
sql string
args []any
}
func newClientQuery(db *gorm.DB, nowMs, expireDiffMs, trafficDiffBytes int64) clientQuery {
q := clientQuery{
db: db,
nowMs: nowMs,
expireDiffMs: expireDiffMs,
trafficDiffBytes: trafficDiffBytes,
joins: []clientQueryJoin{{sql: "LEFT JOIN client_traffics ct ON ct.email = c.email"}},
usedExpr: "(COALESCE(ct.up, 0) + COALESCE(ct.down, 0))",
}
total := len(all)
freshSince := globalTrafficFreshSince()
var probe int64
err := db.Model(&model.ClientGlobalTraffic{}).
Where("updated_at >= ?", freshSince).
Limit(1).Count(&probe).Error
if err != nil || probe == 0 {
return q
}
// A master still pushes cross-panel usage here, so the predicates have to
// see the same raised counters overlayGlobalTraffic applies on read.
q.joins = append(q.joins, clientQueryJoin{
sql: "LEFT JOIN (SELECT email, MAX(up) AS up, MAX(down) AS down FROM client_global_traffics" +
" WHERE updated_at >= ? GROUP BY email) g ON g.email = c.email",
args: []any{freshSince},
})
q.usedExpr = "(CASE WHEN COALESCE(g.up, 0) > COALESCE(ct.up, 0) THEN COALESCE(g.up, 0) ELSE COALESCE(ct.up, 0) END" +
" + CASE WHEN COALESCE(g.down, 0) > COALESCE(ct.down, 0) THEN COALESCE(g.down, 0) ELSE COALESCE(ct.down, 0) END)"
return q
}
func (q clientQuery) from() *gorm.DB {
tx := q.db.Table("clients AS c")
for _, j := range q.joins {
tx = tx.Joins(j.sql, j.args...)
}
return tx
}
func (q clientQuery) depletedExpr() string {
return "((c.total_gb > 0 AND " + q.usedExpr + " >= c.total_gb)" +
" OR (c.expiry_time > 0 AND c.expiry_time <= " + sqlInt(q.nowMs) + "))"
}
func (q clientQuery) nearDepletionExpr() string {
return "((c.expiry_time > 0 AND c.expiry_time - " + sqlInt(q.nowMs) + " < " + sqlInt(q.expireDiffMs) + ")" +
" OR (c.total_gb > 0 AND c.total_gb - " + q.usedExpr + " < " + sqlInt(q.trafficDiffBytes) + "))"
}
func (q clientQuery) expiringExpr() string {
return "(" + sqlClientEnabled + " AND NOT " + q.depletedExpr() + " AND " + q.nearDepletionExpr() + ")"
}
func (q clientQuery) activeExpr() string {
return "(" + sqlClientEnabled + " AND NOT " + q.depletedExpr() + " AND NOT " + q.nearDepletionExpr() + ")"
}
// summaryDeactiveExpr is narrower than the "deactive" bucket filter: a disabled
// client that also ran out counts once, under depleted, so the stat cards add
// up to the client total.
func (q clientQuery) summaryDeactiveExpr() string {
return "(NOT " + sqlClientEnabled + " AND NOT " + q.depletedExpr() + ")"
}
// applyParams narrows tx by every predicate the clients page sends. Matching is
// OR within a field and AND across fields, mirroring the query-param contract.
// The second return says whether anything narrowed the set, so an unfiltered
// request can reuse the total count instead of scanning for it again.
func (q clientQuery) applyParams(tx *gorm.DB, params ClientPageParams, onlines []string) (*gorm.DB, bool) {
narrowed := false
where := func(cond string, args ...any) {
narrowed = true
tx = tx.Where(cond, args...)
}
if needle := strings.ToLower(strings.TrimSpace(params.Search)); needle != "" {
pattern := "%" + escapeLikeLiteral(needle) + "%"
where(clientSearchCond, pattern, pattern, pattern, pattern, pattern, pattern, pattern)
}
if protocols := parseCSVStrings(params.Protocol); len(protocols) > 0 {
where("EXISTS (SELECT 1 FROM client_inbounds ci JOIN inbounds ib ON ib.id = ci.inbound_id"+
" WHERE ci.client_id = c.id AND LOWER(ib.protocol) IN ?)", protocols)
}
if inboundIds := parseCSVInts(params.Inbound); len(inboundIds) > 0 {
where("EXISTS (SELECT 1 FROM client_inbounds ci WHERE ci.client_id = c.id AND ci.inbound_id IN ?)", inboundIds)
}
if buckets := parseCSVStrings(params.Filter); len(buckets) > 0 {
cond, args := q.bucketCond(buckets, onlines)
where(cond, args...)
}
if params.ExpiryFrom > 0 || params.ExpiryTo > 0 {
// 0 means "never expires" and a negative value is the delayed-start
// sentinel; both sit outside any bounded range.
where("c.expiry_time > 0")
if params.ExpiryFrom > 0 {
where("c.expiry_time >= ?", params.ExpiryFrom)
}
if params.ExpiryTo > 0 {
where("c.expiry_time <= ?", params.ExpiryTo)
}
}
if params.UsageFrom > 0 {
where(q.usedExpr+" >= ?", params.UsageFrom)
}
if params.UsageTo > 0 {
where(q.usedExpr+" <= ?", params.UsageTo)
}
switch strings.ToLower(strings.TrimSpace(params.AutoRenew)) {
case "on":
where("COALESCE(c.reset, 0) > 0")
case "off":
where("COALESCE(c.reset, 0) <= 0")
}
switch strings.ToLower(strings.TrimSpace(params.HasTgID)) {
case "yes":
where("COALESCE(c.tg_id, 0) <> 0")
case "no":
where("COALESCE(c.tg_id, 0) = 0")
}
switch strings.ToLower(strings.TrimSpace(params.HasComment)) {
case "yes":
where("TRIM(COALESCE(c.comment, '')) <> ''")
case "no":
where("TRIM(COALESCE(c.comment, '')) = ''")
}
if groups := parseCSVStrings(params.Group); len(groups) > 0 {
where("LOWER(TRIM(COALESCE(c.group_name, ''))) IN ?", groups)
}
return tx, narrowed
}
func (q clientQuery) bucketCond(buckets, onlines []string) (string, []any) {
conds := make([]string, 0, len(buckets))
args := make([]any, 0, len(buckets))
for _, b := range buckets {
switch b {
case "active":
conds = append(conds, "("+sqlClientEnabled+" AND NOT "+q.depletedExpr()+")")
case "deactive":
conds = append(conds, "(NOT "+sqlClientEnabled+")")
case "depleted":
conds = append(conds, q.depletedExpr())
case "expiring":
conds = append(conds, q.expiringExpr())
case "online":
cond, inArgs := emailInCond("c.email", onlines)
conds = append(conds, "("+sqlClientEnabled+" AND "+cond+")")
args = append(args, inArgs...)
default:
// An unrecognised bucket name matched every client before the
// predicates moved into SQL; keep that so a stale saved filter
// cannot silently empty the table.
conds = append(conds, "(1 = 1)")
}
}
return "(" + strings.Join(conds, " OR ") + ")", args
}
func (q clientQuery) applyOrder(tx *gorm.DB, sortKey, order string) *gorm.DB {
dir := " ASC"
if order == "descend" {
dir = " DESC"
}
// createdAt / updatedAt / lastOnline broke ties on the client id inside the
// comparator, so reversing the sort reversed the tiebreak with it. The
// other keys leaned on a stable sort over an id-ordered slice instead.
tieDir := " ASC"
var expr string
switch sortKey {
case "enable":
expr = sqlClientEnabled
case "email":
expr = "LOWER(c.email)"
case "inboundIds":
expr = "(SELECT COUNT(*) FROM client_inbounds ci WHERE ci.client_id = c.id)"
case "traffic":
expr = q.usedExpr
case "remaining":
expr = "CASE WHEN c.total_gb > 0 THEN c.total_gb - " + q.usedExpr + " ELSE " + sqlNeverSentinel + " END"
case "expiryTime":
expr = "CASE WHEN c.expiry_time > 0 THEN c.expiry_time ELSE " + sqlNeverSentinel + " END"
case "createdAt":
expr, tieDir = "c.created_at", dir
case "updatedAt":
expr, tieDir = "c.updated_at", dir
case "lastOnline":
expr, tieDir = "COALESCE(ct.last_online, 0)", dir
default:
return tx.Order("c.id ASC")
}
return tx.Order(expr + dir + ", c.id" + tieDir)
}
// ListPaged returns one page of clients together with the counts the clients
// page header needs. Every predicate runs in SQL, so the cost tracks the page
// size rather than the number of clients on the panel.
func (s *ClientService) ListPaged(inboundSvc *InboundService, settingSvc *SettingService, params ClientPageParams) (*ClientPageResponse, error) {
db := database.GetDB()
pageSize := params.PageSize
if pageSize <= 0 {
@@ -114,27 +346,6 @@ func (s *ClientService) ListPaged(inboundSvc *InboundService, settingSvc *Settin
page = 1
}
protocols := parseCSVStrings(params.Protocol)
inboundIDs := parseCSVInts(params.Inbound)
buckets := parseCSVStrings(params.Filter)
var protocolByInbound map[int]string
if len(protocols) > 0 {
inbounds, err := inboundSvc.GetAllInbounds()
if err == nil {
protocolByInbound = make(map[int]string, len(inbounds))
for _, ib := range inbounds {
protocolByInbound[ib.Id] = string(ib.Protocol)
}
}
}
onlines := inboundSvc.GetOnlineClients()
onlineSet := make(map[string]struct{}, len(onlines))
for _, e := range onlines {
onlineSet[e] = struct{}{}
}
var expireDiffMs, trafficDiffBytes int64
if settingSvc != nil {
if v, err := settingSvc.GetExpireDiff(); err == nil {
@@ -145,77 +356,44 @@ func (s *ClientService) ListPaged(inboundSvc *InboundService, settingSvc *Settin
}
}
nowMs := time.Now().UnixMilli()
summary := buildClientsSummary(all, onlineSet, nowMs, expireDiffMs, trafficDiffBytes)
onlines := inboundSvc.GetOnlineClients()
q := newClientQuery(db, time.Now().UnixMilli(), expireDiffMs, trafficDiffBytes)
needle := strings.ToLower(strings.TrimSpace(params.Search))
filtered := make([]ClientWithAttachments, 0, len(all))
for _, c := range all {
if needle != "" && !clientMatchesSearch(c, needle) {
continue
}
if len(protocols) > 0 && !clientMatchesAnyProtocol(c, protocols, protocolByInbound) {
continue
}
if len(inboundIDs) > 0 && !clientMatchesAnyInbound(c, inboundIDs) {
continue
}
if len(buckets) > 0 && !clientMatchesAnyBucket(c, buckets, onlineSet, nowMs, expireDiffMs, trafficDiffBytes) {
continue
}
if !clientMatchesExpiryRange(c, params.ExpiryFrom, params.ExpiryTo) {
continue
}
if !clientMatchesUsageRange(c, params.UsageFrom, params.UsageTo) {
continue
}
if !clientMatchesAutoRenew(c, params.AutoRenew) {
continue
}
if !clientMatchesHasTgID(c, params.HasTgID) {
continue
}
if !clientMatchesHasComment(c, params.HasComment) {
continue
}
if !clientMatchesAnyGroup(c, params.Group) {
continue
}
filtered = append(filtered, c)
var total int64
if err := db.Model(&model.ClientRecord{}).Count(&total).Error; err != nil {
return nil, err
}
sortClients(filtered, params.Sort, params.Order)
filteredCount := len(filtered)
start := (page - 1) * pageSize
end := start + pageSize
if start > filteredCount {
start = filteredCount
}
if end > filteredCount {
end = filteredCount
}
pageRows := filtered[start:end]
items := make([]ClientSlim, 0, len(pageRows))
for _, c := range pageRows {
items = append(items, toClientSlim(c))
summary, err := q.summary(onlines, int(total))
if err != nil {
return nil, err
}
groupRows, gErr := s.ListGroups()
if gErr != nil {
return nil, gErr
filtered := total
if scoped, narrowed := q.applyParams(q.from(), params, onlines); narrowed {
if err := scoped.Count(&filtered).Error; err != nil {
return nil, err
}
}
groups := make([]string, 0, len(groupRows))
for _, g := range groupRows {
groups = append(groups, g.Name)
items := []ClientSlim{}
offset := (page - 1) * pageSize
if int64(offset) < filtered {
items, err = q.pageRows(params, onlines, offset, pageSize)
if err != nil {
return nil, err
}
}
groups, err := s.listGroupNames()
if err != nil {
return nil, err
}
return &ClientPageResponse{
Items: items,
Total: total,
Filtered: filteredCount,
Total: int(total),
Filtered: int(filtered),
Page: page,
PageSize: pageSize,
Summary: summary,
@@ -223,77 +401,229 @@ func (s *ClientService) ListPaged(inboundSvc *InboundService, settingSvc *Settin
}, nil
}
func buildClientsSummary(all []ClientWithAttachments, onlineSet map[string]struct{}, nowMs, expireDiffMs, trafficDiffBytes int64) ClientsSummary {
// pageRows resolves the requested page to client ids, then loads the records,
// attachments and traffic for those ids only. A page never exceeds
// clientPageMaxSize rows, which stays under sqlInChunk, so the follow-up IN
// lists need no chunking.
func (q clientQuery) pageRows(params ClientPageParams, onlines []string, offset, limit int) ([]ClientSlim, error) {
tx, _ := q.applyParams(q.from(), params, onlines)
var ids []int
if err := q.applyOrder(tx, params.Sort, params.Order).
Offset(offset).Limit(limit).
Pluck("c.id", &ids).Error; err != nil {
return nil, err
}
if len(ids) == 0 {
return []ClientSlim{}, nil
}
var records []model.ClientRecord
if err := q.db.Where("id IN ?", ids).Find(&records).Error; err != nil {
return nil, err
}
byId := make(map[int]*model.ClientRecord, len(records))
emails := make([]string, 0, len(records))
for i := range records {
byId[records[i].Id] = &records[i]
if records[i].Email != "" {
emails = append(emails, records[i].Email)
}
}
var links []model.ClientInbound
if err := q.db.Where("client_id IN ?", ids).Order("inbound_id ASC").Find(&links).Error; err != nil {
return nil, err
}
attachments := make(map[int][]int, len(ids))
for _, l := range links {
attachments[l.ClientId] = append(attachments[l.ClientId], l.InboundId)
}
trafficByEmail := make(map[string]*xray.ClientTraffic, len(emails))
if len(emails) > 0 {
var stats []xray.ClientTraffic
if err := q.db.Where("email IN ?", emails).Find(&stats).Error; err != nil {
return nil, err
}
overlayGlobalTrafficValues(q.db, stats)
for i := range stats {
trafficByEmail[stats[i].Email] = &stats[i]
}
}
items := make([]ClientSlim, 0, len(ids))
for _, id := range ids {
rec := byId[id]
if rec == nil {
continue
}
items = append(items, ClientSlim{
Email: rec.Email,
SubID: rec.SubID,
Enable: rec.Enable,
TotalGB: rec.TotalGB,
ExpiryTime: rec.ExpiryTime,
LimitIP: rec.LimitIP,
Reset: rec.Reset,
Group: rec.Group,
Comment: rec.Comment,
InboundIds: attachments[rec.Id],
Traffic: trafficByEmail[rec.Email],
CreatedAt: rec.CreatedAt,
UpdatedAt: rec.UpdatedAt,
})
}
return items, nil
}
func (q clientQuery) summary(onlines []string, total int) (ClientsSummary, error) {
s := ClientsSummary{
Total: len(all),
Total: total,
Online: []string{},
Depleted: []string{},
Expiring: []string{},
Deactive: []string{},
}
for _, c := range all {
used := int64(0)
if c.Traffic != nil {
used = c.Traffic.Up + c.Traffic.Down
var counts struct {
Active int64
Depleted int64
Expiring int64
Deactive int64
}
// SUM over an empty table yields NULL, which not every driver scans into an
// int; COALESCE keeps a panel with no clients from erroring out.
if err := q.from().Select(
"COALESCE(SUM(CASE WHEN " + q.activeExpr() + " THEN 1 ELSE 0 END), 0) AS active," +
" COALESCE(SUM(CASE WHEN " + q.depletedExpr() + " THEN 1 ELSE 0 END), 0) AS depleted," +
" COALESCE(SUM(CASE WHEN " + q.expiringExpr() + " THEN 1 ELSE 0 END), 0) AS expiring," +
" COALESCE(SUM(CASE WHEN " + q.summaryDeactiveExpr() + " THEN 1 ELSE 0 END), 0) AS deactive",
).Scan(&counts).Error; err != nil {
return s, err
}
s.Active = int(counts.Active)
s.DepletedCount = int(counts.Depleted)
s.ExpiringCount = int(counts.Expiring)
s.DeactiveCount = int(counts.Deactive)
buckets := []struct {
cond string
count int
out *[]string
}{
{q.depletedExpr(), s.DepletedCount, &s.Depleted},
{q.expiringExpr(), s.ExpiringCount, &s.Expiring},
{q.summaryDeactiveExpr(), s.DeactiveCount, &s.Deactive},
}
for _, b := range buckets {
// The counter already says the bucket is empty, so skip the scan that
// would look for emails it cannot find.
if b.count == 0 {
continue
}
exhausted := c.TotalGB > 0 && used >= c.TotalGB
expired := c.ExpiryTime > 0 && c.ExpiryTime <= nowMs
if c.Enable {
if _, ok := onlineSet[c.Email]; ok {
s.Online = append(s.Online, c.Email)
var emails []string
if err := q.from().Where(b.cond).
Order("c.id ASC").Limit(clientSummaryEmailCap).
Pluck("c.email", &emails).Error; err != nil {
return s, err
}
if len(emails) > 0 {
*b.out = emails
}
}
online, onlineCount, err := q.onlineEmails(onlines)
if err != nil {
return s, err
}
s.Online = online
s.OnlineCount = onlineCount
return s, nil
}
// onlineEmails intersects the emails xray reports as connected with the enabled
// clients this panel stores. The online set lives in memory and is bounded by
// live connections, so it drives the query rather than a scan of every client.
func (q clientQuery) onlineEmails(onlines []string) ([]string, int, error) {
matched := []string{}
count := 0
for _, batch := range chunkStrings(onlines, sqlInChunk) {
var page []string
if err := q.db.Model(&model.ClientRecord{}).
Where("COALESCE(enable, FALSE) = TRUE AND email IN ?", batch).
Order("id ASC").
Pluck("email", &page).Error; err != nil {
return nil, 0, err
}
count += len(page)
if room := clientSummaryEmailCap - len(matched); room > 0 {
matched = append(matched, page[:min(room, len(page))]...)
}
}
return matched, count, nil
}
// listGroupNames returns the group names the clients page offers as filters:
// the stored groups plus any name a client still carries. ListGroups also sums
// per-client traffic per group, which this page never reads and which costs a
// full join over client_traffics on every poll.
func (s *ClientService) listGroupNames() ([]string, error) {
db := database.GetDB()
var stored []string
if err := db.Model(&model.ClientGroup{}).Pluck("name", &stored).Error; err != nil {
return nil, err
}
var used []string
if err := db.Model(&model.ClientRecord{}).
Where("group_name <> ''").
Distinct().
Pluck("group_name", &used).Error; err != nil {
return nil, err
}
seen := make(map[string]struct{}, len(stored)+len(used))
out := make([]string, 0, len(stored)+len(used))
for _, list := range [][]string{stored, used} {
for _, name := range list {
if name == "" {
continue
}
}
if exhausted || expired {
s.Depleted = append(s.Depleted, c.Email)
continue
}
if !c.Enable {
s.Deactive = append(s.Deactive, c.Email)
continue
}
nearExpiry := c.ExpiryTime > 0 && c.ExpiryTime-nowMs < expireDiffMs
nearLimit := c.TotalGB > 0 && c.TotalGB-used < trafficDiffBytes
if nearExpiry || nearLimit {
s.Expiring = append(s.Expiring, c.Email)
} else {
s.Active++
if _, dup := seen[name]; dup {
continue
}
seen[name] = struct{}{}
out = append(out, name)
}
}
return s
sort.Slice(out, func(i, j int) bool {
return strings.ToLower(out[i]) < strings.ToLower(out[j])
})
return out, nil
}
func toClientSlim(c ClientWithAttachments) ClientSlim {
return ClientSlim{
Email: c.Email,
SubID: c.SubID,
Enable: c.Enable,
TotalGB: c.TotalGB,
ExpiryTime: c.ExpiryTime,
LimitIP: c.LimitIP,
Reset: c.Reset,
Group: c.Group,
Comment: c.Comment,
InboundIds: c.InboundIds,
Traffic: c.Traffic,
CreatedAt: c.CreatedAt,
UpdatedAt: c.UpdatedAt,
}
func sqlInt(v int64) string {
return strconv.FormatInt(v, 10)
}
func clientMatchesSearch(c ClientWithAttachments, needle string) bool {
if needle == "" {
return true
// escapeLikeLiteral neutralises LIKE wildcards so searching for "a_b" keeps
// matching literally, the way strings.Contains did.
func escapeLikeLiteral(s string) string {
return strings.NewReplacer(`\`, `\\`, `%`, `\%`, `_`, `\_`).Replace(s)
}
// emailInCond renders an IN over a possibly large email set, split so no single
// IN list outgrows the drivers' bind-parameter ceiling.
func emailInCond(column string, emails []string) (string, []any) {
if len(emails) == 0 {
return "1 = 0", nil
}
candidates := [...]string{c.Email, c.SubID, c.Comment, c.UUID, c.Password, c.Auth}
for _, v := range candidates {
if v != "" && strings.Contains(strings.ToLower(v), needle) {
return true
}
chunks := chunkStrings(emails, sqlInChunk)
parts := make([]string, 0, len(chunks))
args := make([]any, 0, len(chunks))
for _, chunk := range chunks {
parts = append(parts, column+" IN ?")
args = append(args, chunk)
}
if c.TgID != 0 && strings.Contains(strconv.FormatInt(c.TgID, 10), needle) {
return true
}
return false
return "(" + strings.Join(parts, " OR ") + ")", args
}
// parseCSVStrings splits a comma-separated list, trims/lower-cases each item,
@@ -339,246 +669,3 @@ func parseCSVInts(raw string) []int {
}
return out
}
func clientMatchesAnyProtocol(c ClientWithAttachments, protocols []string, byInbound map[int]string) bool {
for _, id := range c.InboundIds {
p := byInbound[id]
if p == "" {
continue
}
if slices.Contains(protocols, strings.ToLower(p)) {
return true
}
}
return false
}
func clientMatchesAnyInbound(c ClientWithAttachments, inboundIds []int) bool {
for _, id := range c.InboundIds {
if slices.Contains(inboundIds, id) {
return true
}
}
return false
}
func clientMatchesAnyBucket(c ClientWithAttachments, buckets []string, onlineSet map[string]struct{}, nowMs, expireDiffMs, trafficDiffBytes int64) bool {
for _, b := range buckets {
if clientMatchesBucket(c, b, onlineSet, nowMs, expireDiffMs, trafficDiffBytes) {
return true
}
}
return false
}
func clientMatchesExpiryRange(c ClientWithAttachments, fromMs, toMs int64) bool {
if fromMs <= 0 && toMs <= 0 {
return true
}
// expiryTime of 0 means "never expires"; treat it as outside any bounded
// range so users filtering by date see only clients with concrete expiries.
if c.ExpiryTime == 0 {
return false
}
// Negative expiry is the "delayed start" sentinel; same treatment as never.
if c.ExpiryTime < 0 {
return false
}
if fromMs > 0 && c.ExpiryTime < fromMs {
return false
}
if toMs > 0 && c.ExpiryTime > toMs {
return false
}
return true
}
func clientMatchesUsageRange(c ClientWithAttachments, fromBytes, toBytes int64) bool {
if fromBytes <= 0 && toBytes <= 0 {
return true
}
used := int64(0)
if c.Traffic != nil {
used = c.Traffic.Up + c.Traffic.Down
}
if fromBytes > 0 && used < fromBytes {
return false
}
if toBytes > 0 && used > toBytes {
return false
}
return true
}
func clientMatchesAutoRenew(c ClientWithAttachments, mode string) bool {
switch strings.ToLower(strings.TrimSpace(mode)) {
case "on":
return c.Reset > 0
case "off":
return c.Reset <= 0
}
return true
}
func clientMatchesHasTgID(c ClientWithAttachments, mode string) bool {
switch strings.ToLower(strings.TrimSpace(mode)) {
case "yes":
return c.TgID != 0
case "no":
return c.TgID == 0
}
return true
}
func clientMatchesHasComment(c ClientWithAttachments, mode string) bool {
switch strings.ToLower(strings.TrimSpace(mode)) {
case "yes":
return strings.TrimSpace(c.Comment) != ""
case "no":
return strings.TrimSpace(c.Comment) == ""
}
return true
}
func clientMatchesAnyGroup(c ClientWithAttachments, csv string) bool {
groups := parseCSVStrings(csv)
if len(groups) == 0 {
return true
}
current := strings.TrimSpace(c.Group)
for _, g := range groups {
if g == "" {
if current == "" {
return true
}
continue
}
if strings.EqualFold(g, current) {
return true
}
}
return false
}
func clientMatchesBucket(c ClientWithAttachments, bucket string, onlineSet map[string]struct{}, nowMs, expireDiffMs, trafficDiffBytes int64) bool {
if bucket == "" {
return true
}
used := int64(0)
if c.Traffic != nil {
used = c.Traffic.Up + c.Traffic.Down
}
exhausted := c.TotalGB > 0 && used >= c.TotalGB
expired := c.ExpiryTime > 0 && c.ExpiryTime <= nowMs
switch bucket {
case "online":
if onlineSet == nil {
return false
}
_, ok := onlineSet[c.Email]
return ok && c.Enable
case "depleted":
return exhausted || expired
case "deactive":
return !c.Enable
case "active":
return c.Enable && !exhausted && !expired
case "expiring":
if !c.Enable || exhausted || expired {
return false
}
nearExpiry := c.ExpiryTime > 0 && c.ExpiryTime-nowMs < expireDiffMs
nearLimit := c.TotalGB > 0 && c.TotalGB-used < trafficDiffBytes
return nearExpiry || nearLimit
}
return true
}
func sortClients(rows []ClientWithAttachments, sortKey, order string) {
if sortKey == "" {
return
}
desc := order == "descend"
less := func(i, j int) bool {
a, b := rows[i], rows[j]
switch sortKey {
case "enable":
if a.Enable == b.Enable {
return false
}
return !a.Enable && b.Enable
case "email":
return strings.ToLower(a.Email) < strings.ToLower(b.Email)
case "inboundIds":
return len(a.InboundIds) < len(b.InboundIds)
case "traffic":
ua := int64(0)
if a.Traffic != nil {
ua = a.Traffic.Up + a.Traffic.Down
}
ub := int64(0)
if b.Traffic != nil {
ub = b.Traffic.Up + b.Traffic.Down
}
return ua < ub
case "remaining":
ra := int64(1<<62 - 1)
if a.TotalGB > 0 {
used := int64(0)
if a.Traffic != nil {
used = a.Traffic.Up + a.Traffic.Down
}
ra = a.TotalGB - used
}
rb := int64(1<<62 - 1)
if b.TotalGB > 0 {
used := int64(0)
if b.Traffic != nil {
used = b.Traffic.Up + b.Traffic.Down
}
rb = b.TotalGB - used
}
return ra < rb
case "expiryTime":
ea := int64(1<<62 - 1)
if a.ExpiryTime > 0 {
ea = a.ExpiryTime
}
eb := int64(1<<62 - 1)
if b.ExpiryTime > 0 {
eb = b.ExpiryTime
}
return ea < eb
case "createdAt":
if a.CreatedAt == b.CreatedAt {
return a.Id < b.Id
}
return a.CreatedAt < b.CreatedAt
case "updatedAt":
if a.UpdatedAt == b.UpdatedAt {
return a.Id < b.Id
}
return a.UpdatedAt < b.UpdatedAt
case "lastOnline":
la := int64(0)
if a.Traffic != nil {
la = a.Traffic.LastOnline
}
lb := int64(0)
if b.Traffic != nil {
lb = b.Traffic.LastOnline
}
if la == lb {
return a.Id < b.Id
}
return la < lb
}
return false
}
sort.SliceStable(rows, func(i, j int) bool {
if desc {
return less(j, i)
}
return less(i, j)
})
}
+624
View File
@@ -0,0 +1,624 @@
package service
import (
"slices"
"strconv"
"testing"
"time"
"github.com/mhsanaei/3x-ui/v3/internal/database"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
)
const (
pagingDay = int64(86400000)
pagingGB = int64(1) << 30
)
type pagingSeed struct {
email string
enable bool
totalGB int64
expiryTime int64
used int64
subID string
uuid string
password string
auth string
comment string
group string
tgID int64
reset int
lastOnline int64
inbounds []int
}
// seedPagingClients writes one vless and one trojan inbound plus a fixed client
// set covering every bucket, sort key and search field ListPaged supports.
// Returns "now" so the expectations can be phrased relative to it.
func seedPagingClients(t *testing.T) (int64, []pagingSeed) {
t.Helper()
db := database.GetDB()
now := time.Now().UnixMilli()
vless := &model.Inbound{UserId: 1, Tag: "in-vless", Enable: true, Port: 40001, Protocol: model.VLESS, Settings: `{"clients":[]}`}
trojan := &model.Inbound{UserId: 1, Tag: "in-trojan", Enable: true, Port: 40002, Protocol: model.Trojan, Settings: `{"clients":[]}`}
for _, ib := range []*model.Inbound{vless, trojan} {
if err := db.Create(ib).Error; err != nil {
t.Fatalf("create inbound %s: %v", ib.Tag, err)
}
}
seeds := []pagingSeed{
{email: "alpha@x", enable: true, totalGB: 0, expiryTime: 0, used: 5 * pagingGB, subID: "sub-alpha", uuid: "uuid-alpha", inbounds: []int{vless.Id}, lastOnline: now - 10*pagingDay},
{email: "bravo@x", enable: true, totalGB: 10 * pagingGB, expiryTime: now + 30*pagingDay, used: pagingGB, password: "pw-bravo", inbounds: []int{vless.Id, trojan.Id}, lastOnline: now - pagingDay},
{email: "charlie@x", enable: true, totalGB: 10 * pagingGB, expiryTime: now + 30*pagingDay, used: 10 * pagingGB, auth: "auth-charlie", inbounds: []int{trojan.Id}},
{email: "delta@x", enable: true, totalGB: 10 * pagingGB, expiryTime: now - pagingDay, used: pagingGB, inbounds: []int{vless.Id}},
{email: "echo@x", enable: false, totalGB: 10 * pagingGB, expiryTime: now + 30*pagingDay, inbounds: []int{vless.Id}},
{email: "foxtrot@x", enable: false, totalGB: 10 * pagingGB, expiryTime: now - pagingDay, inbounds: []int{trojan.Id}},
{email: "golf@x", enable: true, totalGB: 10 * pagingGB, expiryTime: now + 2*pagingDay, inbounds: []int{vless.Id}},
{email: "hotel@x", enable: true, totalGB: 10 * pagingGB, expiryTime: now + 30*pagingDay, used: 10*pagingGB - pagingGB/2, inbounds: []int{vless.Id}},
{email: "india@x", enable: true, totalGB: 0, expiryTime: -5 * pagingDay, inbounds: []int{vless.Id}},
{email: "juliet@x", enable: true, comment: " vip customer ", group: "VIP", tgID: 555, reset: 7, inbounds: []int{vless.Id}},
{email: "kilo_1@x", enable: true, group: "vip", inbounds: nil},
{email: "kilo1@x", enable: true, inbounds: []int{trojan.Id}},
}
for i, s := range seeds {
rec := model.ClientRecord{
Email: s.email,
SubID: s.subID,
UUID: s.uuid,
Password: s.password,
Auth: s.auth,
Comment: s.comment,
Group: s.group,
TgID: s.tgID,
Reset: s.reset,
Enable: s.enable,
TotalGB: s.totalGB,
ExpiryTime: s.expiryTime,
CreatedAt: now - int64(len(seeds)-i)*pagingDay,
UpdatedAt: now - int64(i)*pagingDay,
}
if err := db.Create(&rec).Error; err != nil {
t.Fatalf("create client %s: %v", s.email, err)
}
if !s.enable {
// clients.enable carries a `default:true` tag, so GORM leaves the
// zero value out of the INSERT and the column comes back true.
// Restate updated_at so the autoUpdateTime hook cannot reshuffle
// the sort fixtures.
if err := db.Model(&model.ClientRecord{}).Where("id = ?", rec.Id).
Updates(map[string]any{"enable": false, "updated_at": rec.UpdatedAt}).Error; err != nil {
t.Fatalf("disable %s: %v", s.email, err)
}
}
traffic := xray.ClientTraffic{
Email: s.email,
Enable: s.enable,
Up: s.used / 2,
Down: s.used - s.used/2,
Total: s.totalGB,
ExpiryTime: s.expiryTime,
LastOnline: s.lastOnline,
}
if err := db.Create(&traffic).Error; err != nil {
t.Fatalf("create traffic %s: %v", s.email, err)
}
for _, id := range s.inbounds {
if err := db.Create(&model.ClientInbound{ClientId: rec.Id, InboundId: id}).Error; err != nil {
t.Fatalf("attach %s to %d: %v", s.email, id, err)
}
}
}
return now, seeds
}
func pagedEmails(items []ClientSlim) []string {
out := make([]string, 0, len(items))
for _, it := range items {
out = append(out, it.Email)
}
return out
}
func setupPagingServices(t *testing.T) (*ClientService, *InboundService, *SettingService) {
t.Helper()
setupBulkDB(t)
settingSvc := &SettingService{}
if err := settingSvc.setInt("expireDiff", 3); err != nil {
t.Fatalf("set expireDiff: %v", err)
}
if err := settingSvc.setInt("trafficDiff", 1); err != nil {
t.Fatalf("set trafficDiff: %v", err)
}
return &ClientService{}, &InboundService{}, settingSvc
}
func TestListPagedFilters(t *testing.T) {
svc, inboundSvc, settingSvc := setupPagingServices(t)
now, _ := seedPagingClients(t)
tests := []struct {
name string
params ClientPageParams
want []string
}{
{
name: "no filter returns every client in id order",
params: ClientPageParams{PageSize: 50},
want: []string{"alpha@x", "bravo@x", "charlie@x", "delta@x", "echo@x", "foxtrot@x", "golf@x", "hotel@x", "india@x", "juliet@x", "kilo_1@x", "kilo1@x"},
},
{
name: "depleted bucket covers quota and expiry",
params: ClientPageParams{PageSize: 50, Filter: "depleted"},
want: []string{"charlie@x", "delta@x", "foxtrot@x"},
},
{
name: "deactive bucket is every disabled client",
params: ClientPageParams{PageSize: 50, Filter: "deactive"},
want: []string{"echo@x", "foxtrot@x"},
},
{
name: "expiring bucket covers near expiry and near quota",
params: ClientPageParams{PageSize: 50, Filter: "expiring"},
want: []string{"golf@x", "hotel@x"},
},
{
name: "active bucket keeps enabled clients that still have room",
params: ClientPageParams{PageSize: 50, Filter: "active"},
want: []string{"alpha@x", "bravo@x", "golf@x", "hotel@x", "india@x", "juliet@x", "kilo_1@x", "kilo1@x"},
},
{
name: "buckets are ORed",
params: ClientPageParams{PageSize: 50, Filter: "depleted,expiring"},
want: []string{"charlie@x", "delta@x", "foxtrot@x", "golf@x", "hotel@x"},
},
{
name: "unknown bucket keeps matching everything",
params: ClientPageParams{PageSize: 50, Filter: "nonsense"},
want: []string{"alpha@x", "bravo@x", "charlie@x", "delta@x", "echo@x", "foxtrot@x", "golf@x", "hotel@x", "india@x", "juliet@x", "kilo_1@x", "kilo1@x"},
},
{
name: "protocol filter follows the attachments",
params: ClientPageParams{PageSize: 50, Protocol: "trojan"},
want: []string{"bravo@x", "charlie@x", "foxtrot@x", "kilo1@x"},
},
{
name: "inbound filter follows the attachments",
params: ClientPageParams{PageSize: 50, Inbound: "2"},
want: []string{"bravo@x", "charlie@x", "foxtrot@x", "kilo1@x"},
},
{
name: "search matches the email",
params: ClientPageParams{PageSize: 50, Search: "KILO"},
want: []string{"kilo_1@x", "kilo1@x"},
},
{
name: "search treats LIKE wildcards literally",
params: ClientPageParams{PageSize: 50, Search: "kilo_1"},
want: []string{"kilo_1@x"},
},
{
name: "search matches the subId",
params: ClientPageParams{PageSize: 50, Search: "sub-alpha"},
want: []string{"alpha@x"},
},
{
name: "search matches the uuid",
params: ClientPageParams{PageSize: 50, Search: "uuid-alpha"},
want: []string{"alpha@x"},
},
{
name: "search matches the password",
params: ClientPageParams{PageSize: 50, Search: "pw-bravo"},
want: []string{"bravo@x"},
},
{
name: "search matches the auth",
params: ClientPageParams{PageSize: 50, Search: "auth-charlie"},
want: []string{"charlie@x"},
},
{
name: "search matches the comment",
params: ClientPageParams{PageSize: 50, Search: "vip customer"},
want: []string{"juliet@x"},
},
{
name: "search matches the telegram id",
params: ClientPageParams{PageSize: 50, Search: "555"},
want: []string{"juliet@x"},
},
{
name: "group filter is case insensitive",
params: ClientPageParams{PageSize: 50, Group: "vip"},
want: []string{"juliet@x", "kilo_1@x"},
},
{
name: "hasComment yes",
params: ClientPageParams{PageSize: 50, HasComment: "yes"},
want: []string{"juliet@x"},
},
{
name: "hasTgId yes",
params: ClientPageParams{PageSize: 50, HasTgID: "yes"},
want: []string{"juliet@x"},
},
{
name: "autoRenew on",
params: ClientPageParams{PageSize: 50, AutoRenew: "on"},
want: []string{"juliet@x"},
},
{
name: "usage range is inclusive on both bounds",
params: ClientPageParams{PageSize: 50, UsageFrom: pagingGB, UsageTo: 5 * pagingGB},
want: []string{"alpha@x", "bravo@x", "delta@x"},
},
{
name: "expiry range excludes never and delayed start",
params: ClientPageParams{PageSize: 50, ExpiryFrom: now, ExpiryTo: now + 10*pagingDay},
want: []string{"golf@x"},
},
{
name: "filters combine with AND",
params: ClientPageParams{PageSize: 50, Filter: "depleted", Protocol: "trojan"},
want: []string{"charlie@x", "foxtrot@x"},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
resp, err := svc.ListPaged(inboundSvc, settingSvc, tc.params)
if err != nil {
t.Fatalf("ListPaged: %v", err)
}
got := pagedEmails(resp.Items)
if !slices.Equal(got, tc.want) {
t.Fatalf("emails = %v, want %v", got, tc.want)
}
if resp.Filtered != len(tc.want) {
t.Fatalf("filtered = %d, want %d", resp.Filtered, len(tc.want))
}
if resp.Total != 12 {
t.Fatalf("total = %d, want 12", resp.Total)
}
})
}
}
func TestListPagedSorting(t *testing.T) {
svc, inboundSvc, settingSvc := setupPagingServices(t)
seedPagingClients(t)
tests := []struct {
name string
sort string
order string
want []string
}{
{
name: "no sort key keeps insertion order",
want: []string{"alpha@x", "bravo@x", "charlie@x"},
},
{
name: "email ascending", sort: "email", order: "ascend",
want: []string{"alpha@x", "bravo@x", "charlie@x"},
},
{
name: "email descending", sort: "email", order: "descend",
want: []string{"kilo_1@x", "kilo1@x", "juliet@x"},
},
{
name: "traffic descending", sort: "traffic", order: "descend",
want: []string{"charlie@x", "hotel@x", "alpha@x"},
},
{
name: "remaining descending puts unlimited quotas first", sort: "remaining", order: "descend",
want: []string{"alpha@x", "india@x", "juliet@x"},
},
{
name: "expiry ascending starts with the expired", sort: "expiryTime", order: "ascend",
want: []string{"delta@x", "foxtrot@x", "golf@x"},
},
{
name: "createdAt ascending follows insertion", sort: "createdAt", order: "ascend",
want: []string{"alpha@x", "bravo@x", "charlie@x"},
},
{
name: "updatedAt descending starts with the newest", sort: "updatedAt", order: "descend",
want: []string{"alpha@x", "bravo@x", "charlie@x"},
},
{
name: "lastOnline descending breaks ties on the id, reversed too", sort: "lastOnline", order: "descend",
want: []string{"bravo@x", "alpha@x", "kilo1@x"},
},
{
name: "enable ascending puts disabled first", sort: "enable", order: "ascend",
want: []string{"echo@x", "foxtrot@x", "alpha@x"},
},
{
name: "inboundIds descending puts the widest attachment first", sort: "inboundIds", order: "descend",
want: []string{"bravo@x", "alpha@x", "charlie@x"},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
resp, err := svc.ListPaged(inboundSvc, settingSvc, ClientPageParams{PageSize: 3, Sort: tc.sort, Order: tc.order})
if err != nil {
t.Fatalf("ListPaged: %v", err)
}
got := pagedEmails(resp.Items)
if !slices.Equal(got, tc.want) {
t.Fatalf("emails = %v, want %v", got, tc.want)
}
})
}
}
func TestListPagedPagination(t *testing.T) {
svc, inboundSvc, settingSvc := setupPagingServices(t)
seedPagingClients(t)
t.Run("second page continues where the first stopped", func(t *testing.T) {
resp, err := svc.ListPaged(inboundSvc, settingSvc, ClientPageParams{Page: 2, PageSize: 5, Sort: "email", Order: "ascend"})
if err != nil {
t.Fatalf("ListPaged: %v", err)
}
want := []string{"foxtrot@x", "golf@x", "hotel@x", "india@x", "juliet@x"}
if got := pagedEmails(resp.Items); !slices.Equal(got, want) {
t.Fatalf("emails = %v, want %v", got, want)
}
if resp.Page != 2 || resp.PageSize != 5 {
t.Fatalf("page/pageSize = %d/%d, want 2/5", resp.Page, resp.PageSize)
}
})
t.Run("page past the end is empty but keeps the counts", func(t *testing.T) {
resp, err := svc.ListPaged(inboundSvc, settingSvc, ClientPageParams{Page: 9, PageSize: 5})
if err != nil {
t.Fatalf("ListPaged: %v", err)
}
if len(resp.Items) != 0 {
t.Fatalf("items = %v, want none", pagedEmails(resp.Items))
}
if resp.Filtered != 12 || resp.Total != 12 {
t.Fatalf("filtered/total = %d/%d, want 12/12", resp.Filtered, resp.Total)
}
})
t.Run("page size is clamped to the maximum", func(t *testing.T) {
resp, err := svc.ListPaged(inboundSvc, settingSvc, ClientPageParams{Page: 1, PageSize: 5000})
if err != nil {
t.Fatalf("ListPaged: %v", err)
}
if resp.PageSize != clientPageMaxSize {
t.Fatalf("pageSize = %d, want %d", resp.PageSize, clientPageMaxSize)
}
})
}
func TestListPagedRowContents(t *testing.T) {
svc, inboundSvc, settingSvc := setupPagingServices(t)
seedPagingClients(t)
resp, err := svc.ListPaged(inboundSvc, settingSvc, ClientPageParams{PageSize: 50})
if err != nil {
t.Fatalf("ListPaged: %v", err)
}
byEmail := make(map[string]ClientSlim, len(resp.Items))
for _, it := range resp.Items {
byEmail[it.Email] = it
}
t.Run("attachments are reported in inbound order", func(t *testing.T) {
got := byEmail["bravo@x"].InboundIds
if !slices.Equal(got, []int{1, 2}) {
t.Fatalf("inboundIds = %v, want [1 2]", got)
}
})
t.Run("an unattached client reports no inbounds", func(t *testing.T) {
if got := byEmail["kilo_1@x"].InboundIds; len(got) != 0 {
t.Fatalf("inboundIds = %v, want none", got)
}
})
t.Run("traffic counters ride along with the row", func(t *testing.T) {
got := byEmail["hotel@x"].Traffic
if got == nil {
t.Fatal("traffic = nil, want the seeded counters")
}
if want := 10*pagingGB - pagingGB/2; got.Up+got.Down != want {
t.Fatalf("used = %d, want %d", got.Up+got.Down, want)
}
})
t.Run("groups list every name in use", func(t *testing.T) {
if !slices.Equal(resp.Groups, []string{"vip", "VIP"}) && !slices.Equal(resp.Groups, []string{"VIP", "vip"}) {
t.Fatalf("groups = %v, want VIP and vip", resp.Groups)
}
})
}
func TestListPagedSummary(t *testing.T) {
svc, inboundSvc, settingSvc := setupPagingServices(t)
seedPagingClients(t)
resp, err := svc.ListPaged(inboundSvc, settingSvc, ClientPageParams{PageSize: 5, Filter: "depleted"})
if err != nil {
t.Fatalf("ListPaged: %v", err)
}
s := resp.Summary
t.Run("counts stay whole-panel while the page is filtered", func(t *testing.T) {
if s.Total != 12 {
t.Fatalf("total = %d, want 12", s.Total)
}
if s.DepletedCount != 3 {
t.Fatalf("depletedCount = %d, want 3", s.DepletedCount)
}
if s.ExpiringCount != 2 {
t.Fatalf("expiringCount = %d, want 2", s.ExpiringCount)
}
if s.DeactiveCount != 1 {
t.Fatalf("deactiveCount = %d, want 1", s.DeactiveCount)
}
if s.Active != 6 {
t.Fatalf("active = %d, want 6", s.Active)
}
})
t.Run("every client lands in exactly one counter", func(t *testing.T) {
if sum := s.Active + s.DepletedCount + s.ExpiringCount + s.DeactiveCount; sum != s.Total {
t.Fatalf("buckets sum to %d, want %d", sum, s.Total)
}
})
t.Run("bucket lists carry the matching emails", func(t *testing.T) {
if want := []string{"charlie@x", "delta@x", "foxtrot@x"}; !slices.Equal(s.Depleted, want) {
t.Fatalf("depleted = %v, want %v", s.Depleted, want)
}
if want := []string{"golf@x", "hotel@x"}; !slices.Equal(s.Expiring, want) {
t.Fatalf("expiring = %v, want %v", s.Expiring, want)
}
if want := []string{"echo@x"}; !slices.Equal(s.Deactive, want) {
t.Fatalf("deactive = %v, want %v", s.Deactive, want)
}
})
}
func TestListPagedSummaryEmailListsAreCapped(t *testing.T) {
svc, inboundSvc, settingSvc := setupPagingServices(t)
db := database.GetDB()
const n = clientSummaryEmailCap + 25
past := time.Now().UnixMilli() - pagingDay
for i := range n {
rec := model.ClientRecord{Email: "bulk-" + strconv.Itoa(i) + "@x", Enable: true, TotalGB: pagingGB, ExpiryTime: past}
if err := db.Create(&rec).Error; err != nil {
t.Fatalf("create client %d: %v", i, err)
}
}
resp, err := svc.ListPaged(inboundSvc, settingSvc, ClientPageParams{PageSize: 25})
if err != nil {
t.Fatalf("ListPaged: %v", err)
}
if resp.Summary.DepletedCount != n {
t.Fatalf("depletedCount = %d, want %d", resp.Summary.DepletedCount, n)
}
if len(resp.Summary.Depleted) != clientSummaryEmailCap {
t.Fatalf("depleted list = %d entries, want %d", len(resp.Summary.Depleted), clientSummaryEmailCap)
}
}
func TestListPagedGlobalTrafficOverlay(t *testing.T) {
svc, inboundSvc, settingSvc := setupPagingServices(t)
seedPagingClients(t)
// bravo has used 1GB of its 10GB locally; a master reporting 10GB of
// cross-panel usage has to move it into the depleted bucket.
if err := inboundSvc.AcceptGlobalTraffic("master-guid", []*xray.ClientTraffic{
{Email: "bravo@x", Up: 4 * pagingGB, Down: 6 * pagingGB},
}); err != nil {
t.Fatalf("AcceptGlobalTraffic: %v", err)
}
resp, err := svc.ListPaged(inboundSvc, settingSvc, ClientPageParams{PageSize: 50, Filter: "depleted"})
if err != nil {
t.Fatalf("ListPaged: %v", err)
}
want := []string{"bravo@x", "charlie@x", "delta@x", "foxtrot@x"}
if got := pagedEmails(resp.Items); !slices.Equal(got, want) {
t.Fatalf("depleted = %v, want %v", got, want)
}
if resp.Summary.DepletedCount != 4 {
t.Fatalf("depletedCount = %d, want 4", resp.Summary.DepletedCount)
}
for _, it := range resp.Items {
if it.Email != "bravo@x" {
continue
}
if it.Traffic == nil || it.Traffic.Up+it.Traffic.Down != 10*pagingGB {
t.Fatalf("bravo traffic = %+v, want the overlaid 10GB", it.Traffic)
}
}
}
func TestClientQueryOnlineEmails(t *testing.T) {
_, _, _ = setupPagingServices(t)
seedPagingClients(t)
q := newClientQuery(database.GetDB(), time.Now().UnixMilli(), 0, 0)
emails, count, err := q.onlineEmails([]string{"alpha@x", "echo@x", "ghost@x", "kilo1@x"})
if err != nil {
t.Fatalf("onlineEmails: %v", err)
}
if want := []string{"alpha@x", "kilo1@x"}; !slices.Equal(emails, want) {
t.Fatalf("online = %v, want %v (disabled and unknown emails drop out)", emails, want)
}
if count != 2 {
t.Fatalf("count = %d, want 2", count)
}
}
func TestEmailInCondChunksLargeSets(t *testing.T) {
emails := make([]string, sqlInChunk+1)
for i := range emails {
emails[i] = "e" + strconv.Itoa(i)
}
cond, args := emailInCond("c.email", emails)
if want := "(c.email IN ? OR c.email IN ?)"; cond != want {
t.Fatalf("cond = %q, want %q", cond, want)
}
if len(args) != 2 {
t.Fatalf("args = %d chunks, want 2", len(args))
}
if first, ok := args[0].([]string); !ok || len(first) != sqlInChunk {
t.Fatalf("first chunk = %v, want %d entries", args[0], sqlInChunk)
}
emptyCond, emptyArgs := emailInCond("c.email", nil)
if emptyCond != "1 = 0" || emptyArgs != nil {
t.Fatalf("empty set = %q/%v, want an always-false predicate", emptyCond, emptyArgs)
}
}
func TestEscapeLikeLiteral(t *testing.T) {
tests := []struct {
in string
want string
}{
{"plain", "plain"},
{"a_b", `a\_b`},
{"50%", `50\%`},
{`back\slash`, `back\\slash`},
}
for _, tc := range tests {
if got := escapeLikeLiteral(tc.in); got != tc.want {
t.Fatalf("escapeLikeLiteral(%q) = %q, want %q", tc.in, got, tc.want)
}
}
}
func TestListPagedEmptyPanel(t *testing.T) {
svc, inboundSvc, settingSvc := setupPagingServices(t)
resp, err := svc.ListPaged(inboundSvc, settingSvc, ClientPageParams{})
if err != nil {
t.Fatalf("ListPaged on a panel with no clients: %v", err)
}
if len(resp.Items) != 0 || resp.Total != 0 || resp.Filtered != 0 {
t.Fatalf("items/total/filtered = %d/%d/%d, want 0/0/0", len(resp.Items), resp.Total, resp.Filtered)
}
if resp.Summary.Active != 0 || resp.Summary.DepletedCount != 0 {
t.Fatalf("summary = %+v, want zeroed counters", resp.Summary)
}
if resp.Groups == nil {
t.Fatal("groups = nil, want an empty list so the filter drawer renders")
}
}