Formatting throughput stats properly

This commit is contained in:
Sebastian Stenzel
2019-08-28 11:18:21 +02:00
parent c9d897edd1
commit f4ec9c277f
2 changed files with 95 additions and 2 deletions

View File

@@ -0,0 +1,92 @@
package org.cryptomator.ui.controls;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.StringBinding;
import javafx.beans.property.LongProperty;
import javafx.beans.property.SimpleLongProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.control.Label;
public class ThrougputLabel extends Label {
private static final long kibsThreshold = 1l << 7; // 0.128 kiB/s
private static final long mibsThreshold = 1l << 19; // 0.512 MiB/s
private final StringProperty idleFormat = new SimpleStringProperty("-");
private final StringProperty kibsFormat = new SimpleStringProperty("%.3f");
private final StringProperty mibsFormat = new SimpleStringProperty("%.3f");
private final LongProperty bytesPerSecond = new SimpleLongProperty();
public ThrougputLabel() {
textProperty().bind(createStringBinding());
}
protected StringBinding createStringBinding() {
return Bindings.createStringBinding(this::updateText, kibsFormat, mibsFormat, bytesPerSecond);
}
private String updateText() {
long bps = bytesPerSecond.get();
if (bps > mibsThreshold) {
double mibs = ((double) bytesPerSecond.get()) / 1024.0 / 1024.0;
return String.format(mibsFormat.get(), mibs);
} else if (bps > kibsThreshold) {
double kibs = ((double) bytesPerSecond.get()) / 1024.0;
return String.format(kibsFormat.get(), kibs);
} else {
return String.format(idleFormat.get(), bps);
}
}
/* Observables */
public StringProperty idleFormatProperty() {
return idleFormat;
}
public String getIdleFormat() {
return idleFormat.get();
}
public void setIdleFormat(String idleFormat) {
this.idleFormat.set(idleFormat);
}
public StringProperty kibsFormatProperty() {
return kibsFormat;
}
public String getKibsFormat() {
return kibsFormat.get();
}
public void setKibsFormat(String kibsFormat) {
this.kibsFormat.set(kibsFormat);
}
public StringProperty mibsFormatProperty() {
return mibsFormat;
}
public String getMibsFormat() {
return mibsFormat.get();
}
public void setMibsFormat(String mibsFormat) {
this.mibsFormat.set(mibsFormat);
}
public LongProperty bytesPerSecondProperty() {
return bytesPerSecond;
}
public long getBytesPerSecond() {
return bytesPerSecond.get();
}
public void setBytesPerSecond(long bytesPerSecond) {
this.bytesPerSecond.set(bytesPerSecond);
}
}

View File

@@ -10,6 +10,7 @@
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import org.cryptomator.ui.controls.FontAwesome5IconView?>
<?import org.cryptomator.ui.controls.ThrougputLabel?>
<VBox xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
fx:controller="org.cryptomator.ui.mainwindow.VaultDetailController"
@@ -71,12 +72,12 @@
<graphic>
<HBox alignment="CENTER" spacing="24">
<VBox styleClass="button-group-labels" HBox.hgrow="ALWAYS">
<Label text="${controller.vault.stats.bytesPerSecondRead}"/>
<Label styleClass="button-group-heading" text="%main.vaultDetail.bytesPerSecondRead"/>
<ThrougputLabel idleFormat="%main.vaultDetail.throughput.idle" kibsFormat="%main.vaultDetail.throughput.kbps" mibsFormat="%main.vaultDetail.throughput.mbps" bytesPerSecond="${controller.vault.stats.bytesPerSecondRead}"/>
</VBox>
<VBox styleClass="button-group-labels" HBox.hgrow="ALWAYS">
<Label text="${controller.vault.stats.bytesPerSecondWritten}"/>
<Label styleClass="button-group-heading" text="%main.vaultDetail.bytesPerSecondWritten"/>
<ThrougputLabel idleFormat="%main.vaultDetail.throughput.idle" kibsFormat="%main.vaultDetail.throughput.kbps" mibsFormat="%main.vaultDetail.throughput.mbps" bytesPerSecond="${controller.vault.stats.bytesPerSecondWritten}"/>
</VBox>
<Region HBox.hgrow="ALWAYS"/>
<Label styleClass="button-group-action" text="%main.vaultDetail.showChart" minWidth="-Infinity"/>