@@ -21,17 +21,14 @@ type ServerController struct {
2121 serverService service.ServerService
2222 settingService service.SettingService
2323
24- lastStatus * service.Status
25- lastGetStatusTime time.Time
24+ lastStatus * service.Status
2625
2726 lastVersions []string
28- lastGetVersionsTime time. Time
27+ lastGetVersionsTime int64 // unix seconds
2928}
3029
3130func NewServerController (g * gin.RouterGroup ) * ServerController {
32- a := & ServerController {
33- lastGetStatusTime : time .Now (),
34- }
31+ a := & ServerController {}
3532 a .initRouter (g )
3633 a .startTask ()
3734 return a
@@ -40,7 +37,7 @@ func NewServerController(g *gin.RouterGroup) *ServerController {
4037func (a * ServerController ) initRouter (g * gin.RouterGroup ) {
4138
4239 g .GET ("/status" , a .status )
43- g .GET ("/cpuHistory" , a .getCpuHistory )
40+ g .GET ("/cpuHistory/:bucket " , a .getCpuHistoryBucket )
4441 g .GET ("/getXrayVersion" , a .getXrayVersion )
4542 g .GET ("/getConfigJson" , a .getConfigJson )
4643 g .GET ("/getDb" , a .getDb )
@@ -79,35 +76,34 @@ func (a *ServerController) startTask() {
7976 })
8077}
8178
82- func (a * ServerController ) status (c * gin.Context ) {
83- a .lastGetStatusTime = time .Now ()
84-
85- jsonObj (c , a .lastStatus , nil )
86- }
79+ func (a * ServerController ) status (c * gin.Context ) { jsonObj (c , a .lastStatus , nil ) }
8780
88- // getCpuHistory returns recent CPU utilization points.
89- // Query param q=minutes (int). Bounds: 1..360 (6 hours). Defaults to 60.
90- func (a * ServerController ) getCpuHistory (c * gin.Context ) {
91- minsStr := c .Query ("q" )
92- mins := 60
93- if minsStr != "" {
94- if v , err := strconv .Atoi (minsStr ); err == nil {
95- mins = v
96- }
81+ func (a * ServerController ) getCpuHistoryBucket (c * gin.Context ) {
82+ bucketStr := c .Param ("bucket" )
83+ bucket , err := strconv .Atoi (bucketStr )
84+ if err != nil || bucket <= 0 {
85+ jsonMsg (c , "invalid bucket" , fmt .Errorf ("bad bucket" ))
86+ return
9787 }
98- if mins < 1 {
99- mins = 1
88+ allowed := map [int ]bool {
89+ 2 : true , // Real-time view
90+ 30 : true , // 30s intervals
91+ 60 : true , // 1m intervals
92+ 120 : true , // 2m intervals
93+ 180 : true , // 3m intervals
94+ 300 : true , // 5m intervals
10095 }
101- if mins > 360 {
102- mins = 360
96+ if ! allowed [bucket ] {
97+ jsonMsg (c , "invalid bucket" , fmt .Errorf ("unsupported bucket" ))
98+ return
10399 }
104- res := a .serverService .GetCpuHistory ( mins )
105- jsonObj (c , res , nil )
100+ points := a .serverService .AggregateCpuHistory ( bucket , 60 )
101+ jsonObj (c , points , nil )
106102}
107103
108104func (a * ServerController ) getXrayVersion (c * gin.Context ) {
109- now := time .Now ()
110- if now . Sub ( a .lastGetVersionsTime ) <= time . Minute {
105+ now := time .Now (). Unix ()
106+ if now - a .lastGetVersionsTime <= 60 { // 1 minute cache
111107 jsonObj (c , a .lastVersions , nil )
112108 return
113109 }
@@ -119,7 +115,7 @@ func (a *ServerController) getXrayVersion(c *gin.Context) {
119115 }
120116
121117 a .lastVersions = versions
122- a .lastGetVersionsTime = time . Now ()
118+ a .lastGetVersionsTime = now
123119
124120 jsonObj (c , versions , nil )
125121}
@@ -137,7 +133,6 @@ func (a *ServerController) updateGeofile(c *gin.Context) {
137133}
138134
139135func (a * ServerController ) stopXrayService (c * gin.Context ) {
140- a .lastGetStatusTime = time .Now ()
141136 err := a .serverService .StopXrayService ()
142137 if err != nil {
143138 jsonMsg (c , I18nWeb (c , "pages.xray.stopError" ), err )
@@ -253,9 +248,7 @@ func (a *ServerController) importDB(c *gin.Context) {
253248 defer file .Close ()
254249 // Always restart Xray before return
255250 defer a .serverService .RestartXrayService ()
256- defer func () {
257- a .lastGetStatusTime = time .Now ()
258- }()
251+ // lastGetStatusTime removed; no longer needed
259252 // Import it
260253 err = a .serverService .ImportDB (file )
261254 if err != nil {
0 commit comments