Skip to content

Commit

Permalink
Fix an issue with long file names in Windows on svg files.
Browse files Browse the repository at this point in the history
Add batching for spectra ratio project loading.
Fix NPE when loading user ratios for some events.
Fix partial screenshot writes on large plots.
  • Loading branch information
justinbarno committed Feb 15, 2024
1 parent 5a4d7e9 commit 63a3585
Show file tree
Hide file tree
Showing 18 changed files with 199 additions and 184 deletions.
2 changes: 1 addition & 1 deletion calibration-gui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>gov.llnl.gnem.apps.coda.calibration</groupId>
<artifactId>coda-calibration</artifactId>
<version>1.0.21</version>
<version>1.0.21.1</version>
</parent>

<artifactId>calibration-gui</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,36 +162,42 @@ public void loadFiles(List<File> inputFiles, Runnable completionCallback, Progre
fileFailedProgress.setTotal(0l);
bus.post(processingFailedProgressEvent);

final int batchSize = 50;
for (File file : files) {
List<Result<SpectraRatioPairDetailsMetadata>> results = ratioLoader.convertFile(file);
try {
List<SpectraRatioPairDetailsMetadata> successfulResults = results.parallelStream()
.filter(Result::isSuccess)
.map(result -> result.getResultPayload().get())
.collect(Collectors.toList());

List<String> loadFailures = new ArrayList<>();
client.loadRatioMetadata(idCounter.getAndIncrement(), successfulResults).doOnNext(ret -> loadFailures.add(ret)).retry(3).blockLast(Duration.ofHours(1l));

if (loadFailures.size() > 0) {
fileFailedProgress.setTotal(fileFailedProgress.getTotal() + loadFailures.size());
fileFailedProgress.setCurrent(fileFailedProgress.getCurrent() + loadFailures.size());
bus.post(processingFailedProgressEvent);
loadFailures.forEach(
r -> bus.post(new PassFailEvent(LOCAL_FAIL_EVENT, "", new Result<>(false, Collections.singletonList(new LightweightIllegalStateException(r)), null))));
int i = 0;
fileProcessingProgress.setTotal(fileProcessingProgress.getTotal() + results.size());

while (i < results.size()) {
try {
List<Result<SpectraRatioPairDetailsMetadata>> batch = results.subList(i, Math.min(i + batchSize, results.size()));
i = i + batchSize;
List<SpectraRatioPairDetailsMetadata> successfulResults = batch.parallelStream()
.filter(Result::isSuccess)
.map(result -> result.getResultPayload().get())
.collect(Collectors.toList());

List<String> loadFailures = new ArrayList<>();
client.loadRatioMetadata(idCounter.getAndIncrement(), successfulResults).doOnNext(ret -> loadFailures.add(ret)).retry(3).blockLast(Duration.ofHours(1l));

if (loadFailures.size() > 0) {
fileFailedProgress.setTotal(fileFailedProgress.getTotal() + loadFailures.size());
fileFailedProgress.setCurrent(fileFailedProgress.getCurrent() + loadFailures.size());
bus.post(processingFailedProgressEvent);
loadFailures.forEach(
r -> bus.post(new PassFailEvent(LOCAL_FAIL_EVENT, "", new Result<>(false, Collections.singletonList(new LightweightIllegalStateException(r)), null))));
}

fileProcessingProgress.setCurrent(fileProcessingProgress.getCurrent() + successfulResults.size() - loadFailures.size());
bus.post(processingProgressEvent);
} catch (RuntimeException ex) {
log.trace(ex.getMessage(), ex);
}

fileProcessingProgress.setTotal(fileProcessingProgress.getTotal() + successfulResults.size());
fileProcessingProgress.setCurrent(fileProcessingProgress.getCurrent() + successfulResults.size() - loadFailures.size());
bus.post(processingProgressEvent);
} catch (RuntimeException ex) {
log.trace(ex.getMessage(), ex);
}
}
} catch (RuntimeException e) {
log.error(e.getMessage(), e);
}

}
} catch (IllegalStateException e) {
log.error("Unable to instantiate loading display {}", e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ public class RatioMeasurementSpectraPlotManager {
@FXML
private SplitPane mainSplitPane;

@FXML
private StackPane rootPane;

@FXML
private StackPane borderPane;

Expand Down Expand Up @@ -1019,12 +1022,7 @@ public SpectraRatioPairDetails getCurrentFirstRatio() {
}

public String getPlotIdentifier() {
StringBuilder sb = new StringBuilder();
ratioMeasurementReport.getStationsForEventPair(getEventPair()).forEach(station -> {
sb.append(station.getStationName());
sb.append("_");
});
return String.format("large_event_%s_small_event_%s_stations_%s", getEventPair().getY().getEventId(), getEventPair().getX().getEventId(), sb.toString());
return String.format("large_event_%s_small_event_%s", getEventPair().getY().getEventId(), getEventPair().getX().getEventId());
}

// Gets the epicentral distance EModel WGS84 distance between both events
Expand Down Expand Up @@ -1250,7 +1248,7 @@ protected void updateSpectraPlot(EventPair eventPair) {

public void exportScreenshots(final File folder) {
String timestamp = SnapshotUtils.getTimestampWithLeadingSeparator();
SnapshotUtils.writePng(folder, new Pair<>(ALL_PLOTS_PREFIX, mainSplitPane), timestamp);
SnapshotUtils.writePng(folder, new Pair<>(ALL_PLOTS_PREFIX, rootPane), timestamp);
SnapshotUtils.writePng(folder, new Pair<>(SPECTRA_RATIO_PREFIX, spectraRatioPlotNode), timestamp);
SnapshotUtils.writePng(folder, new Pair<>(JOINT_MOMENT_PREFIX, jointMomentPlotNode), timestamp);
SnapshotUtils.writePng(folder, new Pair<>(JOINT_STRESS_PREFIX, jointStressPlotNode), timestamp);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,86 +1,96 @@
/*
* Copyright (c) 2020, Lawrence Livermore National Security, LLC. Produced at the Lawrence Livermore National Laboratory
* CODE-743439.
* All rights reserved.
* This file is part of CCT. For details, see https://github.com/LLNL/coda-calibration-tool.
*
* Licensed under the Apache License, Version 2.0 (the “Licensee”); you may not use this file except in compliance with the License. You may obtain a copy of the License at:
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the license.
*
* This work was performed under the auspices of the U.S. Department of Energy
* by Lawrence Livermore National Laboratory under Contract DE-AC52-07NA27344.
*/
package gov.llnl.gnem.apps.coda.common.gui.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import gov.llnl.gnem.apps.coda.common.model.domain.Pair;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Node;
import javafx.scene.SnapshotParameters;
import javafx.scene.image.Image;
import javafx.scene.transform.Transform;

public class SnapshotUtils {
private static final Logger log = LoggerFactory.getLogger(SnapshotUtils.class);

public static Image snapshot(final Node node) {
SnapshotParameters snapshotParams = new SnapshotParameters();
snapshotParams.setTransform(Transform.scale(8.0, 8.0));
return node.snapshot(snapshotParams, null);
}

public static void writePng(BufferedImage image, String filename) {
try {
ImageIO.write(image, "png", new File(filename));
log.trace("Wrote image to: {}", filename);
} catch (IOException ex) {
log.warn(ex.getMessage(), ex);
} catch (NullPointerException ex) {
log.warn("Null pointer writing image {} to file {} : {}", image, filename, ex.getMessage(), ex);
}
}

public static void writePng(final Node node, String filename) {
Image snapshot = snapshot(node);
CompletableFuture.runAsync(() -> writePng(SwingFXUtils.fromFXImage(snapshot, null), filename));
}

public static void writePng(File folder, Pair<String, Node> nameAndNode) {
writePng(folder, Collections.singletonList(nameAndNode), null);
}

public static void writePng(File folder, Pair<String, Node> nameAndNode, String id) {
writePng(folder, Collections.singletonList(nameAndNode), id);
}

public static void writePng(File folder, List<Pair<String, Node>> namesAndNodes, String id) {
String timestamp;
if (id != null) {
timestamp = id;
} else {
timestamp = getTimestampWithLeadingSeparator();
}
if (folder != null && namesAndNodes != null && !namesAndNodes.isEmpty()) {
namesAndNodes.stream().forEach(nameAndNode -> writePng(nameAndNode.getY(), folder.getAbsolutePath() + File.separator + nameAndNode.getX() + timestamp + ".png"));
}
}

public static String getTimestampWithLeadingSeparator() {
return "_" + Instant.now().toEpochMilli();
}

}
/*
* Copyright (c) 2020, Lawrence Livermore National Security, LLC. Produced at the Lawrence Livermore National Laboratory
* CODE-743439.
* All rights reserved.
* This file is part of CCT. For details, see https://github.com/LLNL/coda-calibration-tool.
*
* Licensed under the Apache License, Version 2.0 (the “Licensee”); you may not use this file except in compliance with the License. You may obtain a copy of the License at:
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the license.
*
* This work was performed under the auspices of the U.S. Department of Energy
* by Lawrence Livermore National Laboratory under Contract DE-AC52-07NA27344.
*/
package gov.llnl.gnem.apps.coda.common.gui.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import gov.llnl.gnem.apps.coda.common.model.domain.Pair;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.Node;
import javafx.scene.SnapshotParameters;
import javafx.scene.image.Image;
import javafx.scene.transform.Transform;

public class SnapshotUtils {
private static final Logger log = LoggerFactory.getLogger(SnapshotUtils.class);

public static Image snapshot(final Node node) {
SnapshotParameters snapshotParams = new SnapshotParameters();
snapshotParams.setTransform(Transform.scale(2.0, 2.0));
return node.snapshot(snapshotParams, null);
}

public static void writePng(BufferedImage image, String filename) {
try {
ImageIO.write(image, "png", new File(filename));
log.trace("Wrote image to: {}", filename);
} catch (IOException ex) {
log.warn(ex.getMessage(), ex);
} catch (NullPointerException ex) {
log.warn("Null pointer writing image {} to file {} : {}", image, filename, ex.getMessage(), ex);
}
}

public static CompletableFuture<Void> writePng(final Node node, String filename) {
Image snapshot = snapshot(node);
return CompletableFuture.runAsync(() -> writePng(SwingFXUtils.fromFXImage(snapshot, null), filename));
}

public static void writePng(File folder, Pair<String, Node> nameAndNode) {
writePng(folder, Collections.singletonList(nameAndNode), null);
}

public static void writePng(File folder, Pair<String, Node> nameAndNode, String id) {
writePng(folder, Collections.singletonList(nameAndNode), id);
}

public static void writePng(File folder, List<Pair<String, Node>> namesAndNodes, String id) {
String timestamp;
if (id != null) {
timestamp = id;
} else {
timestamp = getTimestampWithLeadingSeparator();
}
if (folder != null && namesAndNodes != null && !namesAndNodes.isEmpty()) {
List<CompletableFuture<Void>> futures = namesAndNodes.stream()
.map(nameAndNode -> writePng(nameAndNode.getY(), folder.getAbsolutePath() + File.separator + nameAndNode.getX() + timestamp + ".png"))
.collect(Collectors.toList());
try {
//Block for a minute to make sure the PNGs get written to disk in total before we move on
CompletableFuture.allOf(futures.toArray(new CompletableFuture<?>[0])).get(1l, TimeUnit.MINUTES);
} catch (Exception ex) {
log.debug(ex.getLocalizedMessage(), ex);
}
}
}

public static String getTimestampWithLeadingSeparator() {
return "_" + Instant.now().toEpochMilli();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>

<StackPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" stylesheets="@CommonGui.css" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1">
<StackPane fx:id="rootPane" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" stylesheets="@CommonGui.css" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1">
<children>
<VBox>
<children>
Expand Down
2 changes: 1 addition & 1 deletion calibration-service/calibration-application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>gov.llnl.gnem.apps.coda.calibration</groupId>
<artifactId>calibration-service</artifactId>
<version>1.0.21</version>
<version>1.0.21.1</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion calibration-service/calibration-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>gov.llnl.gnem.apps.coda.calibration</groupId>
<artifactId>calibration-service</artifactId>
<version>1.0.21</version>
<version>1.0.21.1</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
Expand All @@ -36,7 +37,7 @@
import gov.llnl.gnem.apps.coda.common.model.domain.Waveform;

@Entity
@Table(name = "Spectra_Ratio_Pair_Details")
@Table(name = "Spectra_Ratio_Pair_Details", indexes = { @Index(name = "denomWaveform.id_index", columnList = "id"), @Index(name = "numerWaveform.id_index", columnList = "id") })
public class SpectraRatioPairDetails {

@Id
Expand Down
2 changes: 1 addition & 1 deletion calibration-service/calibration-repository/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>gov.llnl.gnem.apps.coda.calibration</groupId>
<artifactId>calibration-service</artifactId>
<version>1.0.21</version>
<version>1.0.21.1</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion calibration-service/calibration-service-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>gov.llnl.gnem.apps.coda.calibration</groupId>
<artifactId>calibration-service</artifactId>
<version>1.0.21</version>
<version>1.0.21.1</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
2 changes: 1 addition & 1 deletion calibration-service/calibration-service-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>gov.llnl.gnem.apps.coda.calibration</groupId>
<artifactId>calibration-service</artifactId>
<version>1.0.21</version>
<version>1.0.21.1</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
Loading

0 comments on commit 63a3585

Please sign in to comment.