diff options
| author | Ali Golzar <57574919+aliglzr@users.noreply.github.com> | 2025-08-28 02:10:50 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-28 02:10:50 +0300 |
| commit | 3087c1b123f426b7c1306ab634fb84e7943e4217 (patch) | |
| tree | ce6f33e025bb16f73797a119a41590f03c0b8c9e /web/service | |
| parent | 21983971971b14377b36c8db92c8603f723f955d (diff) | |
Add all-time traffic for inbounds and clients (#3387)
* 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
Diffstat (limited to 'web/service')
| -rw-r--r-- | web/service/inbound.go | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/web/service/inbound.go b/web/service/inbound.go index 4ef5fce3..78abef73 100644 --- a/web/service/inbound.go +++ b/web/service/inbound.go @@ -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 |
