Skip to content

Commit 6b032a7

Browse files
committed
v1.7.11
1 parent 9663c6d commit 6b032a7

File tree

1 file changed

+76
-80
lines changed

1 file changed

+76
-80
lines changed

snail-javafx/src/main/java/com/acgist/snail/gui/javafx/window/statistics/StatisticsController.java

Lines changed: 76 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import javafx.event.EventHandler;
3838
import javafx.fxml.FXML;
3939
import javafx.fxml.Initializable;
40+
import javafx.scene.Node;
4041
import javafx.scene.chart.CategoryAxis;
4142
import javafx.scene.chart.NumberAxis;
4243
import javafx.scene.chart.PieChart;
@@ -258,7 +259,7 @@ public void statistics() {
258259
* <p>释放资源</p>
259260
*/
260261
public void release() {
261-
this.statisticsBox.getChildren().clear();
262+
this.statisticsBoxClear();
262263
}
263264

264265
/**
@@ -268,7 +269,7 @@ private void buildSelectInfoHashs() {
268269
final var defaultValue = this.selectInfoHashs.getValue();
269270
final ObservableList<SelectInfoHash> obs = FXCollections.observableArrayList();
270271
TorrentContext.getInstance().allTorrentSession().stream()
271-
.filter(session -> session.useable()) // 准备完成
272+
.filter(TorrentSession::useable) // 准备完成
272273
.forEach(session -> obs.add(new SelectInfoHash(session.infoHashHex(), session.name())));
273274
this.selectInfoHashs.setItems(obs);
274275
if(defaultValue == null) {
@@ -329,9 +330,9 @@ private void buildSelectSystemStatistics() {
329330
this.buildTextFlow("虚拟机名称:", System.getProperty("java.vm.name"))
330331
);
331332
systemInfo.getStyleClass().add(ITheme.CLASS_SYSTEM_INFO);
332-
final var nodes = this.statisticsBox.getChildren();
333-
nodes.clear();
334-
nodes.add(systemInfo);
333+
// 添加节点
334+
final var statisticsBoxNode = this.statisticsBoxClear();
335+
statisticsBoxNode.add(systemInfo);
335336
}
336337

337338
/**
@@ -350,17 +351,11 @@ private void buildSelectNodeStatistics() {
350351
new PieChart.Data("可用", available),
351352
new PieChart.Data("未知", unuse)
352353
);
353-
final PieChart pieChart = new PieChart(pieChartData);
354-
pieChart.setTitle(String.format("总量:%d", total));
355-
pieChartData.forEach(data -> {
356-
Tooltip.install(data.getNode(), Tooltips.newTooltip(String.format("%s:%.0f", data.getName(), data.getPieValue())));
357-
});
358-
pieChart.setPrefWidth(CHART_WIDTH);
359-
pieChart.setPrefHeight(CHART_HEIGHT);
360-
final var nodes = this.statisticsBox.getChildren();
361-
nodes.clear();
362-
nodes.add(pieChart);
363-
LOGGER.debug("节点统计,总量:{}、验证:{}、可用:{}、未知:{}", total, verify, available, unuse);
354+
final String title = String.format("总量:%d", total);
355+
final PieChart pieChart = this.buildPieChart(title, pieChartData);
356+
// 添加节点
357+
final var statisticsBoxNode = this.statisticsBoxClear();
358+
statisticsBoxNode.add(pieChart);
364359
}
365360

366361
/**
@@ -377,17 +372,11 @@ private void buildSelectTrackerStatistics() {
377372
new PieChart.Data("禁用", disable),
378373
new PieChart.Data("可用", available)
379374
);
380-
final PieChart pieChart = new PieChart(pieChartData);
381-
pieChart.setTitle(String.format("总量:%d", total));
382-
pieChartData.forEach(data -> {
383-
Tooltip.install(data.getNode(), Tooltips.newTooltip(String.format("%s:%.0f", data.getName(), data.getPieValue())));
384-
});
385-
pieChart.setPrefWidth(CHART_WIDTH);
386-
pieChart.setPrefHeight(CHART_HEIGHT);
387-
final var nodes= this.statisticsBox.getChildren();
388-
nodes.clear();
389-
nodes.add(pieChart);
390-
LOGGER.debug("Tracker统计,总量:{}、禁用:{}、可用:{}", total, disable, available);
375+
final String title = String.format("总量:%d", total);
376+
final PieChart pieChart = this.buildPieChart(title, pieChartData);
377+
// 添加节点
378+
final var statisticsBoxNode= this.statisticsBoxClear();
379+
statisticsBoxNode.add(pieChart);
391380
}
392381

393382
/**
@@ -452,32 +441,17 @@ private void buildSelectSourceStatistics() {
452441
new PieChart.Data("Connect", connectCount.get()),
453442
new PieChart.Data("Holepunch", holepunchCount.get())
454443
);
455-
final PieChart pieChart = new PieChart(pieChartData);
456-
pieChart.setTitle(
457-
String.format(
444+
final String title = String.format(
458445
"总量:%d 可用:%d 下载:%d 上传:%d",
459446
peers.size(),
460447
availableCount.get(),
461448
downloadCount.get(),
462449
uploadCount.get()
463-
)
464-
);
465-
pieChartData.forEach(data -> {
466-
Tooltip.install(data.getNode(), Tooltips.newTooltip(String.format("%s:%.0f", data.getName(), data.getPieValue())));
467-
});
468-
pieChart.setPrefWidth(CHART_WIDTH);
469-
pieChart.setPrefHeight(CHART_HEIGHT);
470-
final var nodes = this.statisticsBox.getChildren();
471-
nodes.clear();
472-
nodes.add(pieChart);
473-
LOGGER.debug(
474-
"Peer统计,总量:{}、可用:{}、下载:{}、上传:{}",
475-
peers.size(), availableCount.get(), downloadCount.get(), uploadCount.get()
476-
);
477-
LOGGER.debug(
478-
"Peer来源,DHT:{}、PEX:{}、LSD:{}、Tracker:{}、Connect:{}、Holepunch:{}",
479-
dhtCount.get(), pexCount.get(), lsdCount.get(), trackerCount.get(), connectCount.get(), holepunchCount.get()
480-
);
450+
);
451+
final PieChart pieChart = this.buildPieChart(title, pieChartData);
452+
// 添加节点
453+
final var statisticsBoxNode = this.statisticsBoxClear();
454+
statisticsBoxNode.add(pieChart);
481455
}
482456

483457
/**
@@ -532,9 +506,9 @@ private void buildSelectConnectStatistics() {
532506
yAxis.setLabel("流量(MB)");
533507
// 流量图表
534508
final StackedBarChart<String, Number> stackedBarChart = new StackedBarChart<>(xAxis, yAxis);
509+
stackedBarChart.setTitle("连接统计");
535510
stackedBarChart.setPrefWidth(CHART_WIDTH);
536511
stackedBarChart.setPrefHeight(CHART_HEIGHT);
537-
stackedBarChart.setTitle("连接统计");
538512
// 上传流量
539513
final XYChart.Series<String, Number> uploadSeries = new XYChart.Series<>();
540514
uploadSeries.setName("上传");
@@ -547,15 +521,11 @@ private void buildSelectConnectStatistics() {
547521
stackedBarChart.getData().add(uploadSeries);
548522
stackedBarChart.getData().add(downloadSeries);
549523
// 设置提示消息
550-
uploadPeer.forEach(data -> {
551-
Tooltip.install(data.getNode(), Tooltips.newTooltip(String.format("IP:%s 上传:%.2fMB", data.getXValue(), data.getYValue())));
552-
});
553-
downloadPeer.forEach(data -> {
554-
Tooltip.install(data.getNode(), Tooltips.newTooltip(String.format("IP:%s 下载:%.2fMB", data.getXValue(), data.getYValue())));
555-
});
556-
final var nodes = this.statisticsBox.getChildren();
557-
nodes.clear();
558-
nodes.add(stackedBarChart);
524+
uploadPeer.forEach(data -> Tooltip.install(data.getNode(), Tooltips.newTooltip(String.format("IP:%s 上传:%.2fMB", data.getXValue(), data.getYValue()))));
525+
downloadPeer.forEach(data -> Tooltip.install(data.getNode(), Tooltips.newTooltip(String.format("IP:%s 下载:%.2fMB", data.getXValue(), data.getYValue()))));
526+
// 添加节点
527+
final var statisticsBoxNode = this.statisticsBoxClear();
528+
statisticsBoxNode.add(stackedBarChart);
559529
}
560530

561531
/**
@@ -570,16 +540,14 @@ private void buildSelectTrafficStatistics() {
570540
final var torrentSession = TorrentContext.getInstance().torrentSession(infoHashHex);
571541
final int length = peers.size();
572542
PeerSession peer;
573-
long uploadSize = 0L;
574-
long downloadSize = 0L;
575543
final BitSet uploadPeers = new BitSet();
576544
final BitSet downloadPeers = new BitSet();
577545
final BitSet exchangePeers = new BitSet();
578546
final BitSet indifferencePeers = new BitSet();
579547
for (int index = 0; index < length; index++) {
580548
peer = peers.get(index);
581-
uploadSize = peer.uploadSize();
582-
downloadSize = peer.downloadSize();
549+
final long uploadSize = peer.uploadSize();
550+
final long downloadSize = peer.downloadSize();
583551
if(uploadSize != 0L && downloadSize != 0L) {
584552
exchangePeers.set(index);
585553
} else if(uploadSize > 0L) {
@@ -599,20 +567,20 @@ private void buildSelectTrafficStatistics() {
599567
.build()
600568
.draw();
601569
// 流量统计
602-
final String traffic = String.format(
570+
final String message = String.format(
603571
"累计上传:%s 累计下载:%s",
604572
FileUtils.formatSize(torrentSession.statistics().uploadSize()),
605573
FileUtils.formatSize(torrentSession.statistics().downloadSize())
606574
);
607-
final HBox trafficHBox = this.buildStatisticsInfo(traffic);
575+
final HBox trafficHBox = this.buildStatisticsInfo(message);
608576
// 颜色描述
609577
final String[] tabs = new String[] { "交战", "上传", "下载", "无情" };
610578
final HBox painterHBox = this.buildPainterInfo(tabs, colors);
611-
final var nodes = this.statisticsBox.getChildren();
612-
nodes.clear();
613-
nodes.add(trafficHBox);
614-
nodes.add(painter.canvas());
615-
nodes.add(painterHBox);
579+
// 添加节点
580+
final var statisticsBoxNode = this.statisticsBoxClear();
581+
statisticsBoxNode.add(trafficHBox);
582+
statisticsBoxNode.add(painter.canvas());
583+
statisticsBoxNode.add(painterHBox);
616584
}
617585

618586
/**
@@ -632,9 +600,12 @@ private void buildSelectPieceStatistics() {
632600
return;
633601
}
634602
final int pieceSize = torrent.getInfo().pieceSize();
635-
final BitSet pieces = torrentSession.pieces(); // 已经下载Pieces
636-
final BitSet selectPieces = torrentSession.selectPieces(); // 选择下载Pieces
637-
final BitSet mousePieces = new BitSet(); // 鼠标可选Pieces
603+
// 已经下载Pieces
604+
final BitSet pieces = torrentSession.pieces();
605+
// 选择下载Pieces
606+
final BitSet selectPieces = torrentSession.selectPieces();
607+
// 鼠标可选Pieces
608+
final BitSet mousePieces = new BitSet();
638609
mousePieces.or(selectPieces);
639610
mousePieces.andNot(pieces);
640611
final Color[] colors = new Color[] { ITheme.COLOR_GREEN, ITheme.COLOR_YELLOW };
@@ -648,16 +619,15 @@ private void buildSelectPieceStatistics() {
648619
.draw();
649620
// 健康度
650621
final HBox healthHBox = this.buildStatisticsInfo("健康度:" + torrentSession.health() + "%");
651-
final var nodes = this.statisticsBox.getChildren();
652622
// 颜色描述
653623
final String[] tabs = new String[] { "已下载", "未下载", "不下载" };
654624
final Color[] tabColors = new Color[] { ITheme.COLOR_GREEN, ITheme.COLOR_YELLOW, ITheme.COLOR_GRAY };
655625
final HBox painterHBox = this.buildPainterInfo(tabs, tabColors);
656-
nodes.clear();
657626
// 添加节点
658-
nodes.add(healthHBox);
659-
nodes.add(painter.canvas());
660-
nodes.add(painterHBox);
627+
final var statisticsBoxNode = this.statisticsBoxClear();
628+
statisticsBoxNode.add(healthHBox);
629+
statisticsBoxNode.add(painter.canvas());
630+
statisticsBoxNode.add(painterHBox);
661631
}
662632

663633
/**
@@ -727,12 +697,38 @@ private HBox buildPainterInfo(String [] tabs, Color [] colors) {
727697
return hBox;
728698
}
729699

700+
/**
701+
* <p>创建饼状图</p>
702+
*
703+
* @param title 标题
704+
* @param pieChartData 饼状图数据
705+
*
706+
* @return 饼状图
707+
*/
708+
private PieChart buildPieChart(String title, ObservableList<PieChart.Data> pieChartData) {
709+
final PieChart pieChart = new PieChart(pieChartData);
710+
pieChart.setTitle(title);
711+
pieChart.setPrefWidth(CHART_WIDTH);
712+
pieChart.setPrefHeight(CHART_HEIGHT);
713+
pieChartData.forEach(data -> Tooltip.install(data.getNode(), Tooltips.newTooltip(String.format("%s:%.0f", data.getName(), data.getPieValue()))));
714+
return pieChart;
715+
}
716+
717+
/**
718+
* <p>获取统计节点</p>
719+
*
720+
* @return 统计节点
721+
*/
722+
private ObservableList<Node> statisticsBoxClear() {
723+
final var nodes = this.statisticsBox.getChildren();
724+
nodes.clear();
725+
return nodes;
726+
}
727+
730728
/**
731729
* <p>选择BT任务事件</p>
732730
*/
733-
private EventHandler<ActionEvent> selectInfoHashsEvent = event -> {
734-
this.buildSelectStatistics();
735-
};
731+
private EventHandler<ActionEvent> selectInfoHashsEvent = event -> this.buildSelectStatistics();
736732

737733
/**
738734
* <p>下载任务</p>

0 commit comments

Comments
 (0)