Skip to content

Commit 6683b6a

Browse files
committed
Update.
1 parent c4bc78c commit 6683b6a

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

model/server.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ type Server struct {
4444
PrevTransferOutSnapshot int64 `gorm:"-" json:"-"` // 上次数据点时的出站使用量
4545

4646
// 累计流量数据
47-
CumulativeNetInTransfer uint64 `gorm:"default:0" json:"cumulative_net_in_transfer"` // 累计入站使用量
48-
CumulativeNetOutTransfer uint64 `gorm:"default:0" json:"cumulative_net_out_transfer"` // 累计出站使用量
47+
CumulativeNetInTransfer uint64 `gorm:"default:0" json:"cumulative_net_in_transfer"` // 累计入站使用量
48+
CumulativeNetOutTransfer uint64 `gorm:"default:0" json:"cumulative_net_out_transfer"` // 累计出站使用量
49+
LastFlowSaveTime time.Time `gorm:"-" json:"-"` // 最后一次保存流量数据的时间
4950
}
5051

5152
func (s *Server) CopyFromRunningServer(old *Server) {
@@ -61,6 +62,7 @@ func (s *Server) CopyFromRunningServer(old *Server) {
6162
s.IsOnline = old.IsOnline
6263
s.CumulativeNetInTransfer = old.CumulativeNetInTransfer
6364
s.CumulativeNetOutTransfer = old.CumulativeNetOutTransfer
65+
s.LastFlowSaveTime = old.LastFlowSaveTime
6466
}
6567

6668
func (s *Server) AfterFind(tx *gorm.DB) error {

resource/static/main.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,13 +1180,11 @@ if (window.statusCards) {
11801180
// 获取流量显示
11811181
window.statusCards.getTrafficDisplay = function(serverId) {
11821182
const trafficData = this.getServerTrafficData(serverId);
1183-
if (!trafficData || !trafficData.usedBytes || !trafficData.maxBytes) {
1183+
if (!trafficData) {
11841184
return '0%';
11851185
}
1186-
const percent = (trafficData.maxBytes > 0)
1187-
? (trafficData.usedBytes / trafficData.maxBytes) * 100
1188-
: 0;
1189-
return percent.toFixed(2) + '%';
1186+
// 直接使用后端提供的百分比值,避免重复计算导致的不一致
1187+
return trafficData.percent.toFixed(2) + '%';
11901188
};
11911189

11921190
// 获取流量提示

resource/template/theme-default/network.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@
189189
// 优化初始化序列,减少延迟
190190
this.$nextTick(() => {
191191
// 先渲染图表
192-
this.renderChart();
192+
this.renderChart();
193193

194194
// 然后加载数据,避免阻塞UI
195195
setTimeout(() => {
196-
this.parseMonitorInfo(monitorInfo);
196+
this.parseMonitorInfo(monitorInfo);
197197
}, 50);
198198
});
199199
},
@@ -387,19 +387,19 @@
387387

388388
// 使用requestAnimationFrame优化渲染
389389
requestAnimationFrame(() => {
390-
this.myChart.resize({
391-
width: 'auto',
392-
height: height
393-
});
394-
this.myChart.clear();
390+
this.myChart.resize({
391+
width: 'auto',
392+
height: height
393+
});
394+
this.myChart.clear();
395395
this.myChart.setOption(this.option, true);
396396
});
397397
},
398398
isWindowsPlatform(str) {
399399
return str.includes('Windows')
400400
},
401401
renderChart() {
402-
const chartTheme = $('html').attr('nz-theme') == "dark" ? "dark" : "";
402+
const chartTheme = $('html').attr('nz-theme') == "dark" ? "dark" : "";
403403

404404
// 避免重复初始化
405405
if (this.myChart && !this.myChart.isDisposed()) {
@@ -432,7 +432,7 @@
432432
// 使用requestAnimationFrame进一步优化
433433
requestAnimationFrame(() => {
434434
if (this.myChart && !this.myChart.isDisposed()) {
435-
this.myChart.resize();
435+
this.myChart.resize();
436436
}
437437
});
438438
}, 200);
@@ -446,7 +446,7 @@
446446
}
447447
// 清理图表实例
448448
if (this.myChart && !this.myChart.isDisposed()) {
449-
this.myChart.dispose();
449+
this.myChart.dispose();
450450
}
451451
this.myChart = null;
452452
},

service/rpc/server.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ func (s *ServerHandler) ReportSystemState(c context.Context, r *pb.State) (*pb.R
184184
originalNetOutTransfer,
185185
singleton.ServerList[clientID].CumulativeNetInTransfer,
186186
singleton.ServerList[clientID].CumulativeNetOutTransfer)
187+
188+
// 将累计流量写入数据库,确保重启后数据不丢失
189+
if err := singleton.DB.Model(singleton.ServerList[clientID]).Updates(map[string]interface{}{
190+
"cumulative_net_in_transfer": singleton.ServerList[clientID].CumulativeNetInTransfer,
191+
"cumulative_net_out_transfer": singleton.ServerList[clientID].CumulativeNetOutTransfer,
192+
}).Error; err != nil {
193+
log.Printf("更新服务器 %s 累计流量到数据库失败: %v", singleton.ServerList[clientID].Name, err)
194+
}
187195
} else {
188196
// 正常更新,使用增量计算
189197
state.NetInTransfer = originalNetInTransfer + singleton.ServerList[clientID].CumulativeNetInTransfer
@@ -194,6 +202,18 @@ func (s *ServerHandler) ReportSystemState(c context.Context, r *pb.State) (*pb.R
194202
originalNetOutTransfer,
195203
singleton.ServerList[clientID].CumulativeNetInTransfer,
196204
singleton.ServerList[clientID].CumulativeNetOutTransfer)
205+
206+
// 定期更新累计流量到数据库(按3分钟间隔)
207+
if time.Since(singleton.ServerList[clientID].LastFlowSaveTime).Minutes() > 3 {
208+
if err := singleton.DB.Model(singleton.ServerList[clientID]).Updates(map[string]interface{}{
209+
"cumulative_net_in_transfer": singleton.ServerList[clientID].CumulativeNetInTransfer,
210+
"cumulative_net_out_transfer": singleton.ServerList[clientID].CumulativeNetOutTransfer,
211+
}).Error; err != nil {
212+
log.Printf("更新服务器 %s 累计流量到数据库失败: %v", singleton.ServerList[clientID].Name, err)
213+
}
214+
// 更新最后保存时间
215+
singleton.ServerList[clientID].LastFlowSaveTime = time.Now()
216+
}
197217
}
198218

199219
// 保存当前状态

0 commit comments

Comments
 (0)