@@ -76,26 +76,7 @@ func (x *XrayHandler) GetUsersStats(ctx context.Context, reset bool) (*common.St
7676 return nil , err
7777 }
7878
79- stats := & common.StatResponse {}
80- for _ , stat := range resp .GetStat () {
81- data := stat .GetName ()
82- value := stat .GetValue ()
83-
84- // Extract the type from the name (e.g., "traffic")
85- parts := strings .Split (data , ">>>" )
86- name := parts [1 ]
87- link := parts [2 ]
88- statType := parts [3 ]
89-
90- stats .Stats = append (stats .Stats , & common.Stat {
91- Name : name ,
92- Type : statType ,
93- Link : link ,
94- Value : value ,
95- })
96- }
97-
98- return stats , nil
79+ return buildStatResponse (resp .GetStat ()), nil
9980}
10081
10182func (x * XrayHandler ) GetInboundsStats (ctx context.Context , reset bool ) (* common.StatResponse , error ) {
@@ -104,26 +85,7 @@ func (x *XrayHandler) GetInboundsStats(ctx context.Context, reset bool) (*common
10485 return nil , err
10586 }
10687
107- stats := & common.StatResponse {}
108- for _ , stat := range resp .GetStat () {
109- data := stat .GetName ()
110- value := stat .GetValue ()
111-
112- // Extract the type from the name (e.g., "traffic")
113- parts := strings .Split (data , ">>>" )
114- name := parts [1 ]
115- link := parts [2 ]
116- statType := parts [3 ]
117-
118- stats .Stats = append (stats .Stats , & common.Stat {
119- Name : name ,
120- Type : statType ,
121- Link : link ,
122- Value : value ,
123- })
124- }
125-
126- return stats , nil
88+ return buildStatResponse (resp .GetStat ()), nil
12789}
12890
12991func (x * XrayHandler ) GetOutboundsStats (ctx context.Context , reset bool ) (* common.StatResponse , error ) {
@@ -132,25 +94,7 @@ func (x *XrayHandler) GetOutboundsStats(ctx context.Context, reset bool) (*commo
13294 return nil , err
13395 }
13496
135- stats := & common.StatResponse {}
136- for _ , stat := range resp .GetStat () {
137- data := stat .GetName ()
138- value := stat .GetValue ()
139-
140- parts := strings .Split (data , ">>>" )
141- name := parts [1 ]
142- link := parts [2 ]
143- statType := parts [3 ]
144-
145- stats .Stats = append (stats .Stats , & common.Stat {
146- Name : name ,
147- Type : statType ,
148- Link : link ,
149- Value : value ,
150- })
151- }
152-
153- return stats , nil
97+ return buildStatResponse (resp .GetStat ()), nil
15498}
15599
156100func (x * XrayHandler ) GetUserStats (ctx context.Context , email string , reset bool ) (* common.StatResponse , error ) {
@@ -162,25 +106,7 @@ func (x *XrayHandler) GetUserStats(ctx context.Context, email string, reset bool
162106 return nil , err
163107 }
164108
165- stats := & common.StatResponse {}
166- for _ , stat := range resp .GetStat () {
167- data := stat .GetName ()
168- value := stat .GetValue ()
169-
170- parts := strings .Split (data , ">>>" )
171- name := parts [1 ]
172- statType := parts [2 ]
173- link := parts [3 ]
174-
175- stats .Stats = append (stats .Stats , & common.Stat {
176- Name : name ,
177- Type : statType ,
178- Link : link ,
179- Value : value ,
180- })
181- }
182-
183- return stats , nil
109+ return buildStatResponse (resp .GetStat ()), nil
184110}
185111
186112func (x * XrayHandler ) GetInboundStats (ctx context.Context , tag string , reset bool ) (* common.StatResponse , error ) {
@@ -192,25 +118,7 @@ func (x *XrayHandler) GetInboundStats(ctx context.Context, tag string, reset boo
192118 return nil , err
193119 }
194120
195- stats := & common.StatResponse {}
196- for _ , stat := range resp .GetStat () {
197- data := stat .GetName ()
198- value := stat .GetValue ()
199-
200- parts := strings .Split (data , ">>>" )
201- name := parts [1 ]
202- statType := parts [2 ]
203- link := parts [3 ]
204-
205- stats .Stats = append (stats .Stats , & common.Stat {
206- Name : name ,
207- Type : statType ,
208- Link : link ,
209- Value : value ,
210- })
211- }
212-
213- return stats , nil
121+ return buildStatResponse (resp .GetStat ()), nil
214122}
215123
216124func (x * XrayHandler ) GetOutboundStats (ctx context.Context , tag string , reset bool ) (* common.StatResponse , error ) {
@@ -222,23 +130,36 @@ func (x *XrayHandler) GetOutboundStats(ctx context.Context, tag string, reset bo
222130 return nil , err
223131 }
224132
225- stats := & common.StatResponse {}
226- for _ , stat := range resp .GetStat () {
227- data := stat .GetName ()
228- value := stat .GetValue ()
133+ return buildStatResponse (resp .GetStat ()), nil
134+ }
229135
230- parts := strings .Split (data , ">>>" )
231- name := parts [1 ]
232- statType := parts [2 ]
233- link := parts [3 ]
136+ func buildStatResponse (queryStats []* command.Stat ) * common.StatResponse {
137+ stats := & common.StatResponse {}
138+ for _ , stat := range queryStats {
139+ name , link , statType , ok := parseStatName (stat .GetName ())
140+ if ! ok {
141+ continue
142+ }
234143
235144 stats .Stats = append (stats .Stats , & common.Stat {
236145 Name : name ,
237146 Type : statType ,
238147 Link : link ,
239- Value : value ,
148+ Value : stat . GetValue () ,
240149 })
241150 }
242151
243- return stats , nil
152+ return stats
153+ }
154+
155+ func parseStatName (raw string ) (name , link , statType string , ok bool ) {
156+ parts := strings .Split (raw , ">>>" )
157+ if len (parts ) < 4 {
158+ return "" , "" , "" , false
159+ }
160+ if parts [1 ] == "" || parts [2 ] == "" || parts [3 ] == "" {
161+ return "" , "" , "" , false
162+ }
163+
164+ return parts [1 ], parts [2 ], parts [3 ], true
244165}
0 commit comments