Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log summary of mapped objects and duration #17

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/main/java/ch/geowerkstatt/lk2dxf/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
import org.apache.logging.log4j.core.layout.PatternLayout;

import java.io.File;
import java.time.Duration;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

public final class Main {
Expand All @@ -46,6 +51,7 @@ private Main() { }
* Application entry point.
*/
public static void main(String[] args) {
Instant start = Instant.now();
Options cliOptions = createCliOptions();
CommandLine commandLine = parseCommandLine(cliOptions, args);

Expand All @@ -65,6 +71,11 @@ public static void main(String[] args) {
if (!processFiles(options.get())) {
System.exit(1);
}

Instant end = Instant.now();
Duration duration = Duration.between(start, end);
String formattedDuration = String.format("%02dh:%02dm:%02ds.%03dms", duration.toHours(), duration.toMinutesPart(), duration.toSecondsPart(), duration.toMillisPart());
LOGGER.info("Processing took {}", formattedDuration);
}
}
}
Expand All @@ -76,6 +87,8 @@ public static void main(String[] args) {
*/
private static boolean processFiles(LK2DxfOptions options) {
Optional<Geometry> perimeter = options.parsePerimeter();
AtomicInteger objectCounter = new AtomicInteger();
Map<String, AtomicInteger> layerCounters = new HashMap<>();

ObjectMapper objectMapper;
try {
Expand All @@ -93,7 +106,12 @@ private static boolean processFiles(LK2DxfOptions options) {
objects = objects.filter(o -> perimeter.get().intersects(o.geometry()));
}

objects.forEach(o -> o.writeToDxf(dxfWriter));
objects.forEach(o -> {
o.writeToDxf(dxfWriter);

objectCounter.incrementAndGet();
incrementLayerCounter(layerCounters, o.layerMapping().layer());
});
} catch (Exception e) {
LOGGER.error("Failed to process file: {}", xtfFile, e);
return false;
Expand All @@ -104,9 +122,24 @@ private static boolean processFiles(LK2DxfOptions options) {
return false;
}

LOGGER.info("The output DXF file contains {} mapped objects", objectCounter.get());
layerCounters.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey())
.forEach(entry -> LOGGER.info("Layer {}: {} objects", entry.getKey(), entry.getValue().get()));

return true;
}

private static void incrementLayerCounter(Map<String, AtomicInteger> layerCounters, String layer) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PP: Diese Funktion könnte mit computeIfAbsent umgesetzt werden.

AtomicInteger counter = layerCounters.get(layer);
if (counter == null) {
layerCounters.put(layer, new AtomicInteger(1));
} else {
counter.incrementAndGet();
}
}

private static void configureLogging(LK2DxfOptions lk2DxfOptions) {
Level logLevel = lk2DxfOptions.trace() ? Level.TRACE : Level.INFO;
Configurator.setRootLevel(logLevel);
Expand Down