Skip to content

Commit

Permalink
add more user friendly reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
wow-such-code committed Mar 11, 2024
1 parent ac10137 commit 16cc00b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 26 deletions.
46 changes: 42 additions & 4 deletions src/main/java/life/qbic/io/commandline/SampleHierarchyCommand.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package life.qbic.io.commandline;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import life.qbic.App;
import life.qbic.model.Configuration;
import life.qbic.model.SampleTypeConnection;
import life.qbic.model.download.Authentication;
import life.qbic.model.download.FileSystemWriter;
import life.qbic.model.download.ModelReporter;
import life.qbic.model.download.OpenbisConnector;
import picocli.CommandLine.Command;
import picocli.CommandLine.Mixin;
Expand All @@ -18,20 +28,48 @@ public class SampleHierarchyCommand implements Runnable {

@Option(arity = "1", paramLabel = "<space>", description = "optional openBIS spaces to filter samples", names = {"-s", "--space"})
private String space;
@Option(arity = "1", paramLabel = "<output file path>", description = "optional output path", names = {"-o", "--out"})
private String outpath;
@Mixin
AuthenticationOptions auth = new AuthenticationOptions();

@Override
public void run() {
List<String> spaces = new ArrayList<>();
List<String> summary = new ArrayList<>();
List<String> spaces = new ArrayList<>();
if(space!=null) {
summary.add("Querying samples in space: "+space+"...\n");
spaces.add(space);
} else {
summary.add("Querying samples in all available spaces...\n");
}
Authentication authentication = App.loginToOpenBIS(auth.getPassword(), auth.user, auth.as_url);
OpenbisConnector openbis = new OpenbisConnector(auth.as_url, authentication.getSessionToken(), "/Users/afriedrich/Downloads/");
Map<SampleTypeConnection, Integer> hierarchy = openbis.queryFullSampleHierarchy(spaces);
OpenbisConnector openbis = new OpenbisConnector(auth.as_url, authentication.getSessionToken());
Map<SampleTypeConnection, Integer> hierarchy = openbis.queryFullSampleHierarchy(spaces);

hierarchy.entrySet().stream()
.sorted(Entry.comparingByValue())
.forEach(entry -> System.err.println(entry.getKey()+" ("+entry.getValue()+")"));
.forEach(entry -> summary.add(entry.getKey()+" ("+entry.getValue()+")"));

for(String s : summary) {
System.out.println(s);
}
Path outputPath = Paths.get(Configuration.LOG_PATH.toString(),
"summary_model_"+getTimeStamp()+".txt");
if(outpath!=null) {
outputPath = Paths.get(outpath);
}
ModelReporter modelReporter = new FileSystemWriter(outputPath);
try {
modelReporter.reportSummary(summary);
} catch (IOException e) {
throw new RuntimeException("Could not write summary file.");
}
}

private String getTimeStamp() {
final String PATTERN_FORMAT = "YYYY-MM-dd_HHmmss";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(PATTERN_FORMAT);
return LocalDateTime.ofInstant(Instant.now(), ZoneOffset.UTC).format(formatter).toString();
}
}
30 changes: 22 additions & 8 deletions src/main/java/life/qbic/model/download/FileSystemWriter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package life.qbic.model.download;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;

Expand All @@ -11,29 +14,40 @@
*
* @author: Sven Fillinger, Andreas Friedrich
*/
class FileSystemWriter implements ModelReporter {
public class FileSystemWriter implements ModelReporter {

/**
* File that stores the summary report content for valid checksums.
*/
final private File matchingSummaryFile;
final private File summaryFile;


/**
* FileSystemWriter constructor with the paths for the summary files. *
* FileSystemWriter constructor with the paths for the summary files.
*
* @param matchingSummaryFile The path where to write the summary
* @param summaryFile The path where to write the summary
*/
FileSystemWriter(Path matchingSummaryFile) {
this.matchingSummaryFile = new File(matchingSummaryFile.toString());
public FileSystemWriter(Path summaryFile) {
this.summaryFile = new File(summaryFile.toString());
}

/**
* {@inheritDoc}
*/
@Override
public void reportSummary(List<String> summary) {
//TODO
public void reportSummary(List<String> summary) throws IOException {
if (!summaryFile.exists()) {
summaryFile.createNewFile();
//file exists or could not be created
if (!summaryFile.exists()) {
throw new IOException("The file " + summaryFile.getAbsoluteFile() + " could not be created.");
}
}
BufferedWriter writer = new BufferedWriter(new FileWriter(summaryFile, true));
for(String line : summary) {
writer.append(line+"\n");
}
writer.close();
}

}
5 changes: 3 additions & 2 deletions src/main/java/life/qbic/model/download/ModelReporter.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package life.qbic.model.download;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.util.List;

interface ModelReporter {
public interface ModelReporter {

void reportSummary(List<String> summary);
void reportSummary(List<String> summary) throws IOException;
}
13 changes: 1 addition & 12 deletions src/main/java/life/qbic/model/download/OpenbisConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@
import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.fetchoptions.SpaceFetchOptions;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.search.SpaceSearchCriteria;
import ch.systemsx.cisd.common.spring.HttpInvokerUtils;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import life.qbic.App;
import life.qbic.model.Configuration;
import life.qbic.model.SampleTypeConnection;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -27,11 +23,6 @@ public class OpenbisConnector {
private static final Logger LOG = LogManager.getLogger(OpenbisConnector.class);
private final IApplicationServerApi applicationServer;
private final String sessionToken;
private final String outputPath;

private final ModelReporter modelReporter = new FileSystemWriter(
Paths.get(Configuration.LOG_PATH.toString(), "summary_model.txt"));

/**
* Constructor for a QBiCDataDownloader instance
*
Expand All @@ -40,10 +31,8 @@ public class OpenbisConnector {
*/
public OpenbisConnector(
String AppServerUri,
String sessionToken,
String outputPath) {
String sessionToken) {
this.sessionToken = sessionToken;
this.outputPath = outputPath;

if (!AppServerUri.isEmpty()) {
applicationServer =
Expand Down

0 comments on commit 16cc00b

Please sign in to comment.