mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2025-12-18 05:04:40 +03:00
Add all-time traffic for inbounds and clients (#3387)
Some checks failed
Release 3X-UI / build (386) (push) Has been cancelled
Release 3X-UI / build (amd64) (push) Has been cancelled
Release 3X-UI / build (arm64) (push) Has been cancelled
Release 3X-UI / build (armv5) (push) Has been cancelled
Release 3X-UI / build (armv6) (push) Has been cancelled
Release 3X-UI / build (armv7) (push) Has been cancelled
Release 3X-UI / build (s390x) (push) Has been cancelled
Some checks failed
Release 3X-UI / build (386) (push) Has been cancelled
Release 3X-UI / build (amd64) (push) Has been cancelled
Release 3X-UI / build (arm64) (push) Has been cancelled
Release 3X-UI / build (armv5) (push) Has been cancelled
Release 3X-UI / build (armv6) (push) Has been cancelled
Release 3X-UI / build (armv7) (push) Has been cancelled
Release 3X-UI / build (s390x) (push) Has been cancelled
* feat(db): add allTime field to Inbound and ClientTraffic models * feat(inbound): increment all_time for inbounds and clients on traffic updates calculate correct all_time traffic on migrate command * feat(ui): show all-time traffic column for inbounds and its clients * i18n: add pages.inbounds.allTimeTraffic label across locales * Add All Time Traffic Usage in inbounds page top banner
This commit is contained in:
@@ -915,8 +915,9 @@ func (s *InboundService) addInboundTraffic(tx *gorm.DB, traffics []*xray.Traffic
|
||||
if traffic.IsInbound {
|
||||
err = tx.Model(&model.Inbound{}).Where("tag = ?", traffic.Tag).
|
||||
Updates(map[string]any{
|
||||
"up": gorm.Expr("up + ?", traffic.Up),
|
||||
"down": gorm.Expr("down + ?", traffic.Down),
|
||||
"up": gorm.Expr("up + ?", traffic.Up),
|
||||
"down": gorm.Expr("down + ?", traffic.Down),
|
||||
"all_time": gorm.Expr("COALESCE(all_time, 0) + ?", traffic.Up+traffic.Down),
|
||||
}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -962,6 +963,7 @@ func (s *InboundService) addClientTraffic(tx *gorm.DB, traffics []*xray.ClientTr
|
||||
if dbClientTraffics[dbTraffic_index].Email == traffics[traffic_index].Email {
|
||||
dbClientTraffics[dbTraffic_index].Up += traffics[traffic_index].Up
|
||||
dbClientTraffics[dbTraffic_index].Down += traffics[traffic_index].Down
|
||||
dbClientTraffics[dbTraffic_index].AllTime += (traffics[traffic_index].Up + traffics[traffic_index].Down)
|
||||
|
||||
// Add user in onlineUsers array on traffic
|
||||
if traffics[traffic_index].Up+traffics[traffic_index].Down > 0 {
|
||||
@@ -2035,6 +2037,26 @@ func (s *InboundService) MigrationRequirements() {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
// Calculate and backfill all_time from up+down for inbounds and clients
|
||||
err = tx.Exec(`
|
||||
UPDATE inbounds
|
||||
SET all_time = IFNULL(up, 0) + IFNULL(down, 0)
|
||||
WHERE IFNULL(all_time, 0) = 0 AND (IFNULL(up, 0) + IFNULL(down, 0)) > 0
|
||||
`).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = tx.Exec(`
|
||||
UPDATE client_traffics
|
||||
SET all_time = IFNULL(up, 0) + IFNULL(down, 0)
|
||||
WHERE IFNULL(all_time, 0) = 0 AND (IFNULL(up, 0) + IFNULL(down, 0)) > 0
|
||||
`).Error
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Fix inbounds based problems
|
||||
var inbounds []*model.Inbound
|
||||
|
||||
Reference in New Issue
Block a user